CODE CREATIVE
PONG
An intro to Game Design and Pygame
TASK #1
TASK:
What is Pong?

CHECK UP:
Watch the video below for an explanation.
TASK #2
TASK:
Create the screen.

HINT:
You will need to import pygame, create variables that will store the dimensions of the screen, initiate the pygame module using pygame’s init method, set a caption for the window using the set_caption method, and create a variable that will run the set_mode method.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #3
TASK:
Add the game loop.

HINT:
You will need to create an infinite while loop that runs in your main function.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #4
TASK:
Set a background color.

HINT:
You will need to first create a constant variable that will contain an RGB color. Then, you will set the background color of the screen by executing the fill method with the desired color as the argument.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #5
TASK:
Add a paddle sprite.

HINT:
You will need to start by creating two variables that will store the paddle’s height and width. Then you will need to create a surface using those values and the Surface method. Lastly, you will need blit the paddle using the blit method.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #6
TASK:
What are surfaces?

CHECK UP:
Watch the video below to check that you completed the task.


CHALLENGE EXERCISE #1
  1. Set the position of the paddle to the left side of the screen.
  2. Create a second paddle and place it in the correct position.




TASK #7
TASK:
Exit the screen with the red button.

HINT:
You will need to create a for loop using event’s get method as the iterating array. Then you will need to check if the array type equals QUIT, at which point you will exit the program.
for event in pygame.event.get():
If event.type == pygame.QUIT:
sys.exit()
You also need to add
“import sys”
at the top of your program.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #8
TASK:
Make the left paddle move up when the W key is pressed.

HINT:
You will need to create a set of boolean variables that will signify whether the player wants to move up or down. Then you will need to check the type of event and the key that was pressed and set the corresponding boolean variable. Lastly, after the for loop finishes, you will need to create a branch statement checking whether the variable is set to true.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #9
TASK:
Make the right paddle move down when the DOWN key is pressed.

HINT:
This task is essentially the opposite of Task #8, so just adjust the paddle y variable accordingly when the s key is pressed.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #9.5
TASK:
Review the process of receiving input from the user.

CHECK UP:
Watch the video below to check that you completed the task.


CHALLENGE EXERCISE #2
  1. Make the left paddle move down when the S key is pressed.
  2. Make the right paddle move when the UP and DOWN arrow keys are pressed.




TASK #10
TASK:
Remove the trailing sprites.

HINT:
You will need to move the invocation of the fill method from outside of the game loop to just after the paddle and ball are blitted.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #11
TASK:
Add an upper boundary for the left paddle.

HINT:
You need to add a branch statement to your algorithm that sets the vertical position variable to 0 if it is ever less than 0.

CHECK UP:
Watch the video below to check that you completed the task.


CHALLENGE EXERCISE #3
  1. Add a lower boundary for the left paddle.
  2. Add an upper and lower boundary for the right paddle.




TASK #12
TASK:
Create variables for the ball and blit it to the screen.

HINT:
You will need to create a variable for the surface and the ball’s x & y position. In this case, use a rect to contain the positional information of the ball.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #13
TASK:
What are rects?

HINT:
A rect is just an object that holds a bunch of information about a rectangle. You can find the
.top .bottom .left .right .centerx
and
.centery
of a rect object. For example,
my_rect.top()
will be the y coordinate of the top of
my_rect.
To create a rect you use the line
my_rect = pygame.rect(X_cord, Y_cord, height, width)


CHECK UP:
Watch the video below to check that you completed the task.
TASK #14
TASK:
Moving the ball.

HINT:
You will need to increment the ball’s rect on both the x and y using the ball_speed variable.

CHECK UP:
Watch the video below to check that you completed the task.


CHALLENGE EXERCISE #4
  1. Move the paddles using rects.




TASK #15
TASK:
Bounce the ball off the bottom wall.

HINT:
You will need to have a branch statement that checks if the ball’s y position is greater than the height of the screen. If so, the ball_speed’s second element needs to switch signs.

CHECK UP:
Watch the video below to check that you completed the task.


CHALLENGE EXERCISE #5
  1. Make the ball bounce off all four walls.




TASK #16
TASK:
Bounce the ball off the right paddle on the x-axis.

HINT:
You will need to add a branch statement that checks if the ball’s x position is greater than the right paddle’s x position. If so, the ball_speed’s first element needs to switch signs.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #17
TASK:
Bounce the ball off the right paddle on the y-axis.

HINT:
You will need to add a branch statement that checks if the ball’s y position is greater than the top of the right paddle or the ball’s y position is less than the bottom of the right paddle. If so, the ball_speed’s first element needs to switch signs.

CHECK UP:
Watch the video below to check that you completed the task.


CHALLENGE EXERCISE #6
  1. Bounce the ball off the left paddle on both the x and y-axis.




TASK #18
TASK:
The ball comes back.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #19
TASK:
Make the ball bounce off the right paddle only when inside a hitbox.

HINT:
You will need to check if the ball’s x position is within 5 pixels to the right or left of the left side of the paddle. If so, ball_speed’s first element needs to switch signs.

CHECK UP:
Watch the video below to check that you completed the task.


CHALLENGE EXERCISE #7
  1. Make the ball only bounce off the left paddle when inside a hitbox.
  2. Move the paddles using rects instead of the variables x and y.




Congratulations! You have completed Pong!