| CompSci 190 Fall 2022 |
Programming Games FOCUS Section |
|
Pong (named after its distinctive sound) simulates table tennis is generally considered the first popular computer game. It was created in 1971 by Samuel Dabney in a makeshift workshop in his daughter’s bedroom with Nolan Bushnell and helped launch the first video game company, Atari. The first Pong console was installed in Andy Capp’s Tavern in Sunnyvale, California. It went on to be an incredible hit in stores and on home televisions, marking the beginning of the "arcade revolution". Pong is now part of the permanent collection at the Smithsonian Institution and part of the Video Game Hall of Fame. It even inspired mechanical toys. It had only two instructions: deposit quarter and avoid missing the ball for high score. |
|
Open button (or select File → Open), navigate to where you saved the folder, and select itIn the Project Explorer panel, click the arrow beside the lab_pong project to expand the folder and show the single Python file inside that represents Pong.
Right-click on the file PongGame.py in the Project Explorer and select Run PongGame to play the game (you are welcome to look at all of the code, hopefully most of it will make sense to you). When the game starts, all the elements will be setup correctly, but it will not be playable. The ball will move until it goes off the screen in the upper right corner. You can move the paddles (read the code to figure out which keys to use!). If you click the mouse in the game window, it will reset the ball back to the center.
It draws all the basic game pieces but, without meaningful interaction, it is not a game!
Here is a video going over the organization of the code
game_step() FunctionThe heart of any video game is the "game loop" because it manages the entire game logic from responding to input, to updating the state of the game items, to enforcing the game's rules, to checking if the game's goals are met, to potentially much more (such as accurate physics, AI, remote players' actions, etc.). It is called a loop because it is responsible for managing the game actions repeatedly until the user quits. Each frame, or step, is called on many times per second (typically between 60 up to 120 times), thus the statistic frames per second, or FPS. Game loops are typically managed by a separate Game Engine so new games can be written by focusing just on the new code specific its characters, rules, and goals without worrying about the all the different platforms technical details (there are many popular engines like Unity or Unreal (made in NC!)).
In this simple program, you will write this important step yourself in the game_step() function, which needs to solve the following problems:
dx and dy values)dy velocity valuedx velocity valuedx velocity valueYou can solve these problems in any order you want, but code them and test them one at a time to make sure you limit the number of issues you have to deal with at once. Note, you can set the ball's initial velocity (dx and dy values) to whatever values you want so it tests the problem you are trying to solve quickly.
Remember to think about how to solve each problem before coding it and then to experiment with your solution to make sure you are confident in your solution.
After you have a working game, change the reset_ball() function to launch the ball in a random direction by replacing the simple values (2, 2) used with calls to Python's random module. Note, your solution should ensure the ball moves reasonably towards one side or the other by:
Once you have the basic game features working, add more features to experiment and make your game more fun to play.
Feel free to be creative, but remember to keep your ideas at your skill level since there are still many things we have yet to learn, but here are some ideas to get you started:
dx and dy values) as you get closer to the outer edges of the paddle)Some ideas include Pong Soccer, Ping Pong, Pong on the iPhone using its notch or "dynamic island", the many new elements included in the Steam Project:Pong game, or your experience with other real-world games.
In the comment at the top of the Python file add: