A Game pong using SDL2

news/2025/2/13 21:19:59/文章来源:https://www.cnblogs.com/comp9721/p/18714437

Homework: Pong
Goal
Reimplement the classic arcade game ‘pong’ using SDL2. More information on the game ‘pong’ can be found
Core Requirements
The following core requirements should be implemented (the weight of each requirement is provided for your convenience):
• The game should be played in an 1024x768 window. Changing the size of the window should be disabled (10%).
• A well-defined game loop that coordinates game timing and event management (20%).
• A single player version should be implemented where the user controls a single paddle (15%), and the computer the other paddle (15%). The paddle can be moved by keyboard or mouse
(one method of control is sufficient). The paddle of both the player and the AI should move at a constant speed.
• A ball should bounce of the top and bottom edge of the playfield and the direction of the ball should change to the reflected direction (15%). To add an element of uncertainty, a small
random perturbation could be added/subtracted from the reflected direction (5%).
• A player scores when the ball hits the edge of the competitors’ side (5%).
• The game starts with both players having zero points, and ends when one player reaches 11 points. A message should be shown to indicate whether the player won or lost. The player should have the option to restart the game or quit (10%).
• The current score should be shown in the window (5%).

Bonus Features
In addition to the above core requirements, additional bonus points can be obtained by implementing one or more of the bonus features listed below.
• Additional support for correct resizing and maximization of the window (10%). As well as pausing of the game when the window is minimized (5%).
• Add sounds when hitting a wall, pad, or when the ball is leaving the screen (15%).

• Make the pad moveslower when just starting to move, and accelerate the longer the pad is moving in the same direction up to some maximum speed (10%).
• Take the speed and direction of the pad motion in account when computing the reflected direction (15%).
• Add obstacles (10%).
• User selectable difficulty by setting the game speed (i.e., more difficult == faster game) (10%).
• Add a title screen with selectable options (e.g., which pad to play with, difficulty level, etc...) (15%).
• Add randomly placed power-ups that can be enabled to hitting it with a ball (e.g., hold ballon pad, safety net behind pad, double size pad, etc..) (10%).
• Something else creative (?%)

Practical
This homework should be solved using the SDL2 library (we use SDL version 2; we will not use the legacy 1.x SDL version, or the recent SDL3). Precompiled libraries exists for a wide variety of operating systems and C++ compilers. SDL2 should be preinstalled on the lab machines. This homework also relies on
CMake (http://www.cmake.org) to provide cross platform Makefile support (CMake supports both
Linux and Windows). Linux or OSx requires minimal additional packages to be installed (Ccompiler,
CMake, and SDL2). Many distributions include precompiled packages (note: when installing SDL2 also
include the ‘development’ version if available). For windows I recommend using MSYS2 with MinGW64 as C++ compiler. Warning, windows setups area bit more complicated, so start early. Please refer to the README.txt file of the skeleton code for detailed setup instructions for Windows-based machines.
A skeleton-code that provides a basic SDL2-based ‘Hello World’ program. You will need to request access to the repository (open the URL in your web-browser, login, and select the three dotson the right next to the project title). Once
you have you should ‘fork’ the project as private in your own code.wm.edu code repository (button on the right next to the title). Next, goto Manage->Members and invite me (ppeers) as “maintainer” (the default is “guest” which is not sufficient), with an expiration date of 06/01/2025. Next, create a token (or setup an ssh key and use git:// instead of https:// below): Settings->Access Tokens (set your role as Owner, and enable all options). You can now clone the project with :
git clone https://code.wm.edu//CSCI437-Pong.git
(replace with your WM username) to your local computer, where you can make any
alterations. Do not forget to commit your changes (git commit -am ‘Description of change’) and push your commits to the server (git push).
It is highly recommended to start from this skeleton code; you must use the CMake configuration. To
compile the skeleton code (see the included README.TXT), create a subdirectory ‘Debug’,and from
within this ‘Debug’ directory run'cmake ..' to generate the Makefile. SDL2 might not be found if it is not

installed in a standard location. You can specify to cmake where to search for SDL2 via the “-DSDL2_PATH=” option:
cmake -DSDL2_PATH=/home/me/nonstandardlocation/SDL2 ..
Don’tforget the ‘dot dot’ at the end. If you want to use a different compiler than the default (on linux or OSX) or in case there is no default compiler (Windows), you will need to specify to cmake for which compiler to generate Makefiles for. For example, if you want to use mingw64 as the compiler, then:
cmake -G “MinGW Makefiles” ..
Please run ‘cmake’ without arguments to get the full list of compile targets. Note, if you want to change your compile target, it is best to fully remove the代 写A Game pong using SDL2 ‘Debug’ directory, and recreate it.
Normally, you do not need to rerun cmake, unless in the situation described below. The CMake files in this skeleton have been setup to simplify adding files without the need to alter the CMake files:
• If you want to add a new executable, just add the corresponding ‘ .cpp’ file in the 'bin'
subdirectory, and rerun 'cmake ..' to update the Makefiles. This cpp file should contain a ‘ main’ function.
• If you want to add a source file, just add the corresponding ‘ .cpp’ file in the 'src'subdirectory, and rerun 'cmake ..' to update the Makefile. These source files should not contain a ‘ main’
function.
• If you want to add a header file, just add the corresponding ‘ .h’ file to the 'include' subdirectory. You do not need to update the Makefile by rerunning cmake.
• Adding an external library requires more work – you will need to add a FindPackage entry in the CMakeLists.txt file. Please refer to how SDL is added in the included CMakeLists file as a template.
In case you want to remove all compiled files, just remove the ‘Debug’ directory. Based on the directory name,cmake will generate a makefile to compile your code in either Debug or Release mode. In Debug mode, debug information is included in your executable, and certain checks are included. Release mode, runs faster, but it is advised to on use this when you know your code works. To compile in release mode, just create a directory ‘Release’,and run ‘cmake ..’ from within this directory.

Submission Details
Fork your own version of the homework code skeleton as soon as possible. Granting access requires me to take action, and thus is not instantaneous. Do not change the repo name I will clone your project on or after the deadline, and use the cloned version to grade your project. Make sure you have your final version committed by the deadline
and you have added me as “ maintainer” to your repo (preferably as soon as you fork the repo)! The delay between the deadline and granting me access (as “ maintainer”) will be counted as a late

submission. In addition, by the deadline you need to add a PDF report (max 1 page) in the root of the forked repository, named ‘report.pdf’ (git add report.pdf) that contains the following information:
o Time required to finish the core requirements this homework
o Time spend on the bonus features (if any).
o A list of any external libraries (besides STL or Boost) that you used and why.
o A list of core features that do not work correctly.
o A list of implemented bonus features, indicating whether they work or not.

Remarks
• The estimated time required for the basic requirements is 16 hours, excluding time to master C++.
• This homework accounts for 2/9th of the total for this course. Additional bonus credits (max 1/9th) can be obtained by implementing the bonus features.
• The grading for this homework will be as follows:
o 5% whether or not your code compiles (without excessive changes to the compile process).
o 50% on correctness and playability. Does the ball move correctly? Can the pad be
moved correctly? Are scores being tracked? Does the game end? The percentages
listed in the ‘core requirements’ and ‘bonus features’ pertain to this component of the grade.
o 25% on code quality. See below.
o 15% on the separation in the code of logic versus view(s).
o 5% on the report.
• This is not a group project; this homework should be solved alone.
• You are not allowed to use existing SDL pong solutions found online - your code will be
compared to available online solutions, and if a (partial) match is found, you will receive 0 credits.
• You are allowed to use external libraries, but you will need to justify their use in your report. In general, external libraries should be used sparingly. If in doubt, contact the lecturer.
• Late policy: score -= ceil(hours_late / 24) * 10. Negative scores are possible. An extension needs to be requested at least 48 hours before the deadline. Each extension request needs to clearly justify why the extension is needed.

Code Quality Criteria
• Clean readable code. Feel free to pick any bracketing style you prefer, but be consistent. Indentation help is making the code readable. Use a consistent indentation style.
• Sufficient comments.
• Your code should be Object Oriented. There should not be many functions or constants not contained in a class. Each class should be meaningful; a near-empty class or a data container is

only needed in exceptional circumstances. Similarly, avoid putting (almost) all code in a single big class. If you class exceeds 250 lines of code, you should carefully consider if your design is good.
• Your class should enforce access via access specifiers (public and private). Your code should expose the very minimum of its internals.
• Only when a user is allowed to alter class data members without limits or side-effects, only then should a user from outside the object have direct access to data members; use getter and setter methods otherwise.
• Methods should be placed in the proper class. A method should only alter the state of the
object it belongs to (it can call modifiers on another class). An object should never be
responsible to keep the state of another object consistent. For example, a ‘Game’ object should never have to worry about whether any coordinate of a ‘ Ball’ object’s position is positive. This is an example that the position of the ‘ Ball’ should be private, and the set method in ‘ Ball’ should check the condition (i.e., position remains positive).
• Each class should have its own header file and source file. There should not be anything else in the header/source file.
• Each header file should have proper #ifndef guards (see CPP overview lecture). No #pragma
• Pass method parameters by reference. Only when passing basic data types (Boolean, int, char, float, double, long, pointers) may you pass them by value. Avoid passing pointers as a
parameter unless the variable was originally declared a pointer.
• Correct use of ‘const’ on both parameters being passed to methods, as well as the methods
themselves (see C++ overview lecture). In general, if a method does not alter the object, then it should be declared const (e.g., void getValue(void) const). Furthermore, if a variable is passed by reference and its content will not be altered by the method, then pass as const (e.g., void
addHealth(constint& increment)).
• Data that is always passed as whole, likely needs to have its own container class/type. For example: void setColor(const Color& col) instead of void setColor(int r, intg, int b);
• Do not use SDL data types to pass data to non-SDL methods. For example, don’t use SDL_Point to represent 2D points. Write your own point class. The reason for this is that you want to
have a strict separation between view and logic (SDL should be fully contained in the view). You can add methods in your data containers to cast to SDL types.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/883404.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

边坡智能监测识别摄像头

边坡智能监测识别摄像头具备24小时不间断的视频监控能力,可以随时捕捉到边坡的动态变化,并记录所有视频数据。通过深度学习模型,该设备可以自动识别不同类型的异常现象,包括土体位移、裂缝扩展等,大幅提升检测准确率。一旦发现异常情况,系统会立即向相关人员发送警报信息…

AI滴漏监测识别摄像机

AI滴漏监测识别摄像机具备24小时不间断的视频监控能力,可以随时查看现场情况,并记录所有视频数据。一旦检测到液体泄漏,系统会立即向管理人员发送警报信息,以便迅速采取措施进行处理。AI滴漏监测识别摄像机设计坚固,可以在各种复杂环境中稳定工作,包括极端温度、高湿度等…

Fastjson反序列化漏洞原理与漏洞复现

根据这位大佬文章学习 https://blog.csdn.net/Bossfrank/article/details/130100893 Fastjson反序列化 一:json是啥 json是一种格式json全称是JavaScript object notation。即JavaScript对象标记法,使用键值对进行信息的存储。点击查看代码 {"name":"BossFran…

串串全家桶

目前有:马拉车Manacher 用途: 该算法可在 \(O(n)\) 的时间复杂度下求出以每一个点(和缝,即回文串长为偶数的情况)为中心的最长回文串长度。 做法: 前置:定义朴素算法为,对于每一个点为中心的情况,暴力枚举是否可以扩展两边,直到不能扩展,此时得到结果。 代码如下,其…

C#字符串拼接的6种方式及其性能分析对比

前言 在C#编程中字符串拼接是一种常见且基础的操作,广泛应用于各种场景,如动态生成SQL查询、构建日志信息、格式化用户显示内容等。然而,不同的字符串拼接方式在性能和内存使用上可能存在显著差异。今天咱们一起来看看在C#中字符串拼接的常见6种方式及其使用BenchmarkDotNet…

寒假集训专题五:搜索

ESAY1:自然数的拆分 P2404 自然数的拆分问题 题目描述 任何一个大于 \(1\) 的自然数 \(n\),总可以拆分成若干个小于 \(n\) 的自然数之和。现在给你一个自然数 \(n\),要求你求出 \(n\) 的拆分成一些数字的和。每个拆分后的序列中的数字从小到大排序。然后你需要输出这些序列,…

多模态 AI 怎么玩?这里有 18 个脑洞

在 RTE 开发者社区,我们会和大家一起探索全球最前沿的 Real-Time AI 技术,和最有想法的新兴场景。Google 近期举办了一场名为「MultiModal Hackathon」的限时编程活动,聚焦于 多模态与 Gemini 2.0 的最新能力。活动汇聚了 200 多位开发者,共同探索多模态 AI、实时 AI、生成…

Svelte 最新中文文档翻译(7)—— snippet 与 @render

前言 Svelte,一个非常“有趣”、用起来“很爽”的前端框架。从 Svelte 诞生之初,就备受开发者的喜爱,根据统计,从 2019 年到 2024 年,连续 6 年一直是开发者最感兴趣的前端框架 No.1:Svelte 以其独特的编译时优化机制著称,具有轻量级、高性能、易上手等特性,非常适合构…

基环树 DP:学习笔记

总述 定义 基环树,是一个 \(N\) 个点和 \(N\) 条边的连通图,特征是图中有且仅有一个环。特别的,如果不连通且每个连通块的点数和边数都相等,那么这就是一个基环树森林。 基环树 DP,顾名思义,就是在一个基环树上 DP,或是 DP 的结构类似基环树。相对于正常的树型 DP,一般…

数字孪生如何让GIS场景视效瞬间高大上?带你了解鲸孪生中的GIS系统

GIS与数字孪生的结合非常紧密,而山海鲸可视化作为一个数字孪生平台,也将GIS系统整合在了鲸孪生功能之中。 GIS中包含了大量的数据,例如遥感数据、地形数据、倾斜摄影数据等,能够为数字孪生系统提供非常好的补充。同时,传统的GIS系统整体视觉效果相对较差,与其他模型和数据…

2 分支 多个思路

利用分支,你就可以在同一个代码基础上同时处理多个完全没有关联、相互独立的工作。考虑以下场景。 假设你正在改一个 Bug-A,此时已经产生了大量的代码修改,并且离修复完成还有很长一段时间(起码得明天)。此时,有一个着急但简单的 Bug-B 需要你立即完成,并在一个小时内同…

记录一种DAG计数方法与一个配套技巧

记录一种DAG计数方法与一个配套技巧 定义 \(f_S\) 表示集合 \(S\) 中的点构成的合法 DAG 子图的方案数。假设找到 DAG 中一个入度为 \(0\) 的节点 \(x\),那么很明显 \(f_S=\sum_{x}f_{S\setminus \{x\}}\),这明显要算重因为 \(S\setminus \{x\}\) 中也有入度为 \(0\) 的点。 …