Create a program that allows the user to play the classic game Pong
Variables - Constants
This program will require you to use constants, because we don't want to see any magic numbers in the code. It also has quite a few variables. Think carefully about what type you want to use for each one.
Throughout your program, make sure you do not hardcode it for set values. Take advantage of built-in variables like height and width when drawing positions of paddles, walls, and the puck.
This project is going to have a lot of methods. We want to divide up the problem. Remember that every program starts with two methods: setup() and draw().
In setup()
, we're only putting code to set up the program. This means things like screen size, initial values, setting up fonts, etc.
In draw()
we're really doing two things each frame. First, updating the logic and physics of the program. This means things like movement, bouncing, and changing values. Second, we render images on the screen using that data.
To keep this clear, we're going to call two methods from draw - update()
and render()
. Each of those methods will call other methods to do specific tasks. See the outline on the right for suggested names and organization.
Draw the walls and both paddles. Make the first paddle move.
Required Methods:
update()
render()
movePlayerPaddle()
drawWalls()
drawPaddles()
Make the puck move and the right paddle follow it.
Required Methods:
drawPuck()
movePuck()
moveComputerPaddle()
Make the puck bounce off the top and bottom borders
Required Methods:
bounceWalls()
Make the puck bounce off each paddle
Required Methods:
bounceLeftPaddle()
bounecRightPaddle()
Display the user and computer's score, reset the game each time a point is scored.
Required Methods:
checkBoundary()
displayScore()
Adding a little polish...
setup()
, replace your call to the size()
method with the fullScreen()
method.etup()
, add noStroke()
to make it look more like the classic Pongsetup()
, add noCursor()
to hide your cursor.Do one or more of the following.
keyPressed
, key
, and or keyCode
in processing. Google is your friend.Sample Organization
Below are the constants, variables, and methods your program will have at the end of step F. You don't need to put these all in right away. Follow the suggested outline starting with Part A.
Constants
PADDLE_WIDTH;
PADDLE_HEIGHT;
WALL_HEIGHT;
PUCK_SIZE;
SCORE_FONT_SIZE;
SCORE_OFFSET;
Variables
leftPaddleX
leftPaddleY
rightPaddleX
rightPaddleY
puckX
puckY
puckXSpeed
puckYSpeed
humanScore
cpuScore
Method Outline
setup()
draw() ---> calls update() & render()
update() ---> calls the methods below
movePlayerPaddle()
moveComputerPaddle()
movePuck()
bounceWalls()
bounceLeftPaddle()
bounceRightPaddle()
checkBoundary()
render() ---> calls methods below
drawPaddles()
drawWalls()
drawPuck()
displayScore()