0% found this document useful (0 votes)
69 views2 pages

Refer To Pongv1.Py Code Pygame

This document provides notes on the code for a Pong game created using the Pygame library. It explains key aspects of the Pygame library and outlines the overall structure of the Pong game code. It also describes plans to add objects like the ball, paddles controlled by the player and AI, using classes. The document focuses on initializing the game, setting up the display, handling events, and rendering frames in the main loop. It provides context needed to understand and expand upon the basic Pong game code.

Uploaded by

Nick Chee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views2 pages

Refer To Pongv1.Py Code Pygame

This document provides notes on the code for a Pong game created using the Pygame library. It explains key aspects of the Pygame library and outlines the overall structure of the Pong game code. It also describes plans to add objects like the ball, paddles controlled by the player and AI, using classes. The document focuses on initializing the game, setting up the display, handling events, and rendering frames in the main loop. It provides context needed to understand and expand upon the basic Pong game code.

Uploaded by

Nick Chee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Pong LAB Notes

Refer to PongV1.py code


pygame
-

library that allows for interaction of the program and gets an output
for graphics and events
from pygame, also importing a sub library called locals

from pygame.locals import* - means import everything from locals


locals sub library that makes defining easier (removes the need for a path ie.
pygame.locals.getkey etc.)
main function (def main( ) :)
-

brackets where the arguments are placed b/c we dont need input from outside of the
function
colon means the following lines are going to be a block of code
function block is indented under main
must call this function in the end

pygame.init() sets up basic interface


screen where the game is going to be drawn on
pygame.display.set_mode function (display is another module); set_mode takes the arguments
from the tuple (2 numbers: width and height), stored in screen for use
pygame.time.Clock() a class object from the time library that keeps track and limits the FPS of
the game, the function is initializing the object
while running: - dont need a operator and operand for comparison, since using such syntax
checks if object is true (which it is)

need to create events to allow program to shut down when a certain task or objective is
accomplished (prevents crash)
separate main while loop into 3 different phases in order to make sure everything is being
called in the proper order

for:
-

repitition control structure (loop) that goes through all contents of a list (elements)
assigns the elements of pygame.event.get() by sequence to the object (in this case, event)
and runs the code indented
o if statement embedded: pygame.quit deinitializes program and window, and sets
the object running to False, which stops the loop

pygame.event.get():

function, gets get() from the sub-library event


gets all the input (mouse-clicks, mouse-movement etc.)

pygame.display.flip() switches screens; used b/c screen.fill((0,0,0)) fills a second screen with
black, and the function flips screen to other
-

place pygame.quit() at the end of the loop so that the last frame is rendered before
shutting down the game

Next, need 3 objects: the ball (pong), human controlled paddle, and AI controlled paddle -> use
classes
def __init__(self):
-

function called to initialize (start up/create) object


must start with self (references itself) in order to act on itself
once we have pong object, we need center positions, and radius
creates a rectangle object for paddle using pygame.Rect() that has a left, right, and center
inserted as arguments(pygame.org/docs/ref/rect.html) -> although object is circle,
creating a rectangular collision object (works because the ball only makes contact with a
flat surface anyway)
color is needed using self.color, using hex values
direction for ball at the starting point using self.direction, uses list because we can
change individual values later on for different difficulties; allows for easy addition or
removing of values from a list
self.speed for the velocity of the ball (#CODE TASK: change speed as game progresses
to make it harder)
screensize[0]*0.5 gets the first element of screensize (screen width) and multiplies it by
half for placing object at the center of the screen -> allows to manipulate screensize
dimensions without needing to change center starting pt.
use self.hit_edge to add points to the scoreboard or whether or not we scored or not
o if statements used to run code when a certain condition is met

You might also like