initial commit

This commit is contained in:
Robbe De Greef 2022-12-06 18:01:23 +01:00
commit 50b6ff29c6
11 changed files with 1089 additions and 0 deletions

26
pong_1.py Normal file
View File

@ -0,0 +1,26 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (255, 255, 255)
SCREEN_SIZE = (500, 500) # Screen size is 500 x 500
# Start pygame
pygame.init()
window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Hello world")
# The main loop will run until the QUIT event is triggered
# this event will be triggered by closing the window
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Fill the screen with the background color and update the screen
window.fill(BACKGROUND_COLOR)
pygame.display.flip()

154
pong_10.py Normal file
View File

@ -0,0 +1,154 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (0, 0, 0) # Black background
SCREEN_SIZE = (800, 500) # Screen size is 500 x 500
PADDLE_COLOR = (255, 255, 255) # Paddle color is white
PADDLE_SIZE = (10, 100) # Paddle size is 10 x 100
BALL_COLOR = (255, 255, 255) # Ball color is white
BALL_RADIUS = 10 # Ball radius is 10 pixels
PLAYER_START_X_OFFSET = 50 # The amount of pixels the player is away from the wall on the X axis
PLAYER_START_Y_OFFSET = 200 # The amount of pixels the player is away from the wall on the Y axis
PLAYER_MOVE_SPEED = 400 # The amount the player moves when a key is pressed
FRAMERATE = 60 # The target framerate of the game
BALL_START_OFFSET = (250, 250) # The location of the ball when the game first starts
BALL_INITIAL_DIRECTION = [1, -1.5] # The initial direction of the ball, notice that this is not a tuple
BALL_SPEED = 100 # The speed of the ball
class RenderAble:
def render(self, window):
pass
class Ball(RenderAble):
def __init__(self, x, y, direction, radius=BALL_RADIUS, speed=BALL_SPEED):
self.x = x
self.y = y
self.direction = direction
self.radius = radius
self.speed = speed
def render(self, window):
pygame.draw.circle(window, BALL_COLOR, (self.x, self.y), self.radius)
def bounce_x(self):
self.direction[0] = -self.direction[0]
def bounce_y(self):
self.direction[1] = -self.direction[1]
def update(self, dt):
new_x = self.x + (self.direction[0] * self.speed * dt)
new_y = self.y + (self.direction[1] * self.speed * dt)
# Check for a bounce with the top of the screen, we need to test the top of the ball
if new_y - self.radius < 0 :
new_y = self.radius
self.bounce_y()
# Check for a bounce with the bottom of the screen, we need to test the bottom of the ball
elif new_y + self.radius >= SCREEN_SIZE[1]:
new_y = SCREEN_SIZE[1] - self.radius
self.bounce_y()
self.x = new_x
self.y = new_y
class Player(RenderAble):
def __init__(self, x, y, width=PADDLE_SIZE[0], height=PADDLE_SIZE[1]):
self.x = x
self.y = y
self.width = width
self.height = height
def render(self, window):
pygame.draw.rect(window, PADDLE_COLOR, (self.x, self.y, self.width, self.height))
# Will move the player on the Y axis but only if it is certain
# that the player will not move out of the bounds of the screen
def move(self, amount):
if self.y + amount >= 0 and self.y + self.height + amount < SCREEN_SIZE[1]:
self.y += amount
# This class will represent our game
class Game:
# This function will be called when the class is first created
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Super awesome pong")
self.initialize_gameobjects()
def initialize_gameobjects(self):
self.player1 = Player(PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.player2 = Player(SCREEN_SIZE[0] - PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.ball = Ball(BALL_START_OFFSET[0], BALL_START_OFFSET[1], BALL_INITIAL_DIRECTION)
self.gameobjects = [self.player1, self.player2, self.ball]
# Called when the game needs to be reset
def game_reset(self):
self.initialize_gameobjects()
# Calculates the collisions of the paddles with the ball
def check_collision(self):
if self.ball.x - self.ball.radius <= self.player1.x + self.player1.width:
# Ball is behind the left paddle
if self.player1.y <= self.ball.y + self.ball.radius and self.player1.y + self.player1.height >= self.ball.y - self.ball.radius:
# The paddle has catched the ball, bounce back
self.ball.bounce_x()
else:
self.game_reset()
if self.ball.x + self.ball.radius >= self.player2.x:
# Ball is behind the right paddle
if self.player2.y <= self.ball.y + self.ball.radius and self.player2.y + self.player2.height >= self.ball.y - self.ball.radius:
# The paddle has catched the ball, bounce back
self.ball.bounce_x()
else:
self.game_reset()
# This function will render the contents of the screen
def render(self):
self.window.fill(BACKGROUND_COLOR)
for renderable in self.gameobjects:
renderable.render(self.window)
pygame.display.flip()
def start(self):
# The main loop will run until the QUIT event is triggered
# this event will be triggered by closing the window
clock = pygame.time.Clock()
while True:
# Check for user input
# Get a map of all currently pressed keys
keys = pygame.key.get_pressed()
# Change the movement of the player based on which key is pressed
dt = clock.get_time() / 1000
movement = PLAYER_MOVE_SPEED * dt
if keys[pygame.K_d]: self.player1.move(movement)
elif keys[pygame.K_f]: self.player1.move(-movement)
if keys[pygame.K_j]: self.player2.move(movement)
elif keys[pygame.K_k]: self.player2.move(-movement)
# Loop over all current events and check if the quit event was fired
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.ball.update(dt)
self.check_collision()
self.render()
clock.tick(FRAMERATE)
# Create the game object and start the game
game = Game()
game.start()

31
pong_2.py Normal file
View File

@ -0,0 +1,31 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (255, 255, 255)
SCREEN_SIZE = (500, 500) # Screen size is 500 x 500
# This class will represent our game
class Game:
# This function will be called when the class is first created
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Hello world")
# This function will render the contents of the screen
def start(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Fill the screen with the background color and update the screen
self.window.fill(BACKGROUND_COLOR)
pygame.display.flip()
# Create the game object and start the game
game = Game()
game.start()

40
pong_3.py Normal file
View File

@ -0,0 +1,40 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (0, 0, 0) # Black background
SCREEN_SIZE = (500, 500) # Screen size is 500 x 500
PADDLE_COLOR = (255, 255, 255) # Paddle color is white
# This class will represent our game
class Game:
# This function will be called when the class is first created
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Hello world")
# This function will render the contents of the screen
def render(self):
self.window.fill(BACKGROUND_COLOR)
# Draw a single paddle
pygame.draw.rect(self.window, PADDLE_COLOR, (50, 50, 10, 100))
pygame.display.flip()
def start(self):
# The main loop will run until the QUIT event is triggered
# this event will be triggered by closing the window
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.render()
# Create the game object and start the game
game = Game()
game.start()

55
pong_4.py Normal file
View File

@ -0,0 +1,55 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (0, 0, 0) # Black background
SCREEN_SIZE = (500, 500) # Screen size is 500 x 500
PADDLE_COLOR = (255, 255, 255) # Paddle color is white
PADDLE_SIZE = (10, 100) # Paddle size is 10 x 100
BALL_COLOR = (255, 255, 255) # Ball color is white
BALL_RADIUS = 10 # Ball radius is 10 pixels
# This class will represent our game
class Game:
# This function will be called when the class is first created
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Hello world")
# Draw one paddle at a given location
def draw_paddle(self, location):
pygame.draw.rect(self.window, PADDLE_COLOR, (location[0], location[1], PADDLE_SIZE[0], PADDLE_SIZE[1]))
# Draw the ball at a given location
def draw_ball(self, location):
pygame.draw.circle(self.window, BALL_COLOR, location, BALL_RADIUS)
# This function will render the contents of the screen
def render(self):
self.window.fill(BACKGROUND_COLOR)
# Draw the two players
self.draw_paddle((50, 200))
self.draw_paddle((450, 200))
# Draw the ball
self.draw_ball((250, 250))
pygame.display.flip()
def start(self):
# The main loop will run until the QUIT event is triggered
# this event will be triggered by closing the window
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.render()
# Create the game object and start the game
game = Game()
game.start()

65
pong_5.py Normal file
View File

@ -0,0 +1,65 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (0, 0, 0) # Black background
SCREEN_SIZE = (500, 500) # Screen size is 500 x 500
PADDLE_COLOR = (255, 255, 255) # Paddle color is white
PADDLE_SIZE = (10, 100) # Paddle size is 10 x 100
BALL_COLOR = (255, 255, 255) # Ball color is white
BALL_RADIUS = 10 # Ball radius is 10 pixels
PLAYER_START_X_OFFSET = 50 # The amount of pixels the player is away from the wall on the X axis
PLAYER_START_Y_OFFSET = 200 # The amount of pixels the player is away from the wall on the Y axis
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
# This class will represent our game
class Game:
# This function will be called when the class is first created
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Hello world")
self.player1 = Player(PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.player2 = Player(SCREEN_SIZE[0] - PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
# Draw one paddle at a given location
def draw_paddle(self, location):
pygame.draw.rect(self.window, PADDLE_COLOR, (location[0], location[1], PADDLE_SIZE[0], PADDLE_SIZE[1]))
# Draw the ball at a given location
def draw_ball(self, location):
pygame.draw.circle(self.window, BALL_COLOR, location, BALL_RADIUS)
# This function will render the contents of the screen
def render(self):
self.window.fill(BACKGROUND_COLOR)
# Draw the two players
self.draw_paddle((self.player1.x, self.player1.y))
self.draw_paddle((self.player2.x, self.player2.y))
# Draw the ball
self.draw_ball((250, 250))
pygame.display.flip()
def start(self):
# The main loop will run until the QUIT event is triggered
# this event will be triggered by closing the window
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.render()
# Create the game object and start the game
game = Game()
game.start()

89
pong_6.py Normal file
View File

@ -0,0 +1,89 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (0, 0, 0) # Black background
SCREEN_SIZE = (500, 500) # Screen size is 500 x 500
PADDLE_COLOR = (255, 255, 255) # Paddle color is white
PADDLE_SIZE = (10, 100) # Paddle size is 10 x 100
BALL_COLOR = (255, 255, 255) # Ball color is white
BALL_RADIUS = 10 # Ball radius is 10 pixels
PLAYER_START_X_OFFSET = 50 # The amount of pixels the player is away from the wall on the X axis
PLAYER_START_Y_OFFSET = 200 # The amount of pixels the player is away from the wall on the Y axis
PLAYER_MOVE_SPEED = 400 # The amount the player moves when a key is pressed
FRAMERATE = 60 # The target framerate of the game
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
# Will move the player on the Y axis but only if it is certain
# that the player will not move out of the bounds of the screen
def move(self, amount):
if self.y + amount >= 0 and self.y + PADDLE_SIZE[1] + amount < SCREEN_SIZE[1]:
self.y += amount
# This class will represent our game
class Game:
# This function will be called when the class is first created
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Hello world")
self.player1 = Player(PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.player2 = Player(SCREEN_SIZE[0] - PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
# Draw one paddle at a given location
def draw_paddle(self, location):
pygame.draw.rect(self.window, PADDLE_COLOR, (location[0], location[1], PADDLE_SIZE[0], PADDLE_SIZE[1]))
# Draw the ball at a given location
def draw_ball(self, location):
pygame.draw.circle(self.window, BALL_COLOR, location, BALL_RADIUS)
# This function will render the contents of the screen
def render(self):
self.window.fill(BACKGROUND_COLOR)
# Draw the two players
self.draw_paddle((self.player1.x, self.player1.y))
self.draw_paddle((self.player2.x, self.player2.y))
# Draw the ball
self.draw_ball((250, 250))
pygame.display.flip()
def start(self):
# The main loop will run until the QUIT event is triggered
# this event will be triggered by closing the window
clock = pygame.time.Clock()
while True:
# Check for user input
# Get a map of all currently pressed keys
keys = pygame.key.get_pressed()
# Change the movement of the player based on which key is pressed
movement = PLAYER_MOVE_SPEED * (clock.get_time() / 1000)
if keys[pygame.K_d]: self.player1.move(movement)
elif keys[pygame.K_f]: self.player1.move(-movement)
if keys[pygame.K_j]: self.player2.move(movement)
elif keys[pygame.K_k]: self.player2.move(-movement)
# Loop over all current events and check if the quit event was fired
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# snip ...
self.render()
clock.tick(FRAMERATE)
# Create the game object and start the game
game = Game()
game.start()

120
pong_7.py Normal file
View File

@ -0,0 +1,120 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (0, 0, 0) # Black background
SCREEN_SIZE = (500, 500) # Screen size is 500 x 500
PADDLE_COLOR = (255, 255, 255) # Paddle color is white
PADDLE_SIZE = (10, 100) # Paddle size is 10 x 100
BALL_COLOR = (255, 255, 255) # Ball color is white
BALL_RADIUS = 10 # Ball radius is 10 pixels
PLAYER_START_X_OFFSET = 50 # The amount of pixels the player is away from the wall on the X axis
PLAYER_START_Y_OFFSET = 200 # The amount of pixels the player is away from the wall on the Y axis
PLAYER_MOVE_SPEED = 400 # The amount the player moves when a key is pressed
FRAMERATE = 60 # The target framerate of the game
BALL_START_OFFSET = (250, 250) # The location of the ball when the game first starts
BALL_INITIAL_DIRECTION = [1, -1.5] # The initial direction of the ball, notice that this is not a tuple
BALL_SPEED = 100 # The speed of the ball
class Ball:
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.direction = direction
def bounce_y(self):
self.direction[1] = -self.direction[1]
def update(self, dt):
new_x = self.x + (self.direction[0] * BALL_SPEED * dt)
new_y = self.y + (self.direction[1] * BALL_SPEED * dt)
# Check for a bounce with the top of the screen, we need to test the top of the ball
if new_y - BALL_RADIUS < 0 :
new_y = BALL_RADIUS
self.bounce_y()
# Check for a bounce with the bottom of the screen, we need to test the bottom of the ball
elif new_y + BALL_RADIUS >= SCREEN_SIZE[1]:
new_y = SCREEN_SIZE[1] - BALL_RADIUS
self.bounce_y()
self.x = new_x
self.y = new_y
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
# Will move the player on the Y axis but only if it is certain
# that the player will not move out of the bounds of the screen
def move(self, amount):
if self.y + amount >= 0 and self.y + PADDLE_SIZE[1] + amount < SCREEN_SIZE[1]:
self.y += amount
# This class will represent our game
class Game:
# This function will be called when the class is first created
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Hello world")
self.player1 = Player(PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.player2 = Player(SCREEN_SIZE[0] - PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.ball = Ball(BALL_START_OFFSET[0], BALL_START_OFFSET[1], BALL_INITIAL_DIRECTION)
# Draw one paddle at a given location
def draw_paddle(self, location):
pygame.draw.rect(self.window, PADDLE_COLOR, (location[0], location[1], PADDLE_SIZE[0], PADDLE_SIZE[1]))
# Draw the ball at a given location
def draw_ball(self, location):
pygame.draw.circle(self.window, BALL_COLOR, location, BALL_RADIUS)
# This function will render the contents of the screen
def render(self):
self.window.fill(BACKGROUND_COLOR)
# Draw the two players
self.draw_paddle((self.player1.x, self.player1.y))
self.draw_paddle((self.player2.x, self.player2.y))
# Draw the ball
self.draw_ball((self.ball.x, self.ball.y))
pygame.display.flip()
def start(self):
# The main loop will run until the QUIT event is triggered
# this event will be triggered by closing the window
clock = pygame.time.Clock()
while True:
# Check for user input
# Get a map of all currently pressed keys
keys = pygame.key.get_pressed()
# Change the movement of the player based on which key is pressed
dt = clock.get_time() / 1000
movement = PLAYER_MOVE_SPEED * dt
if keys[pygame.K_d]: self.player1.move(movement)
elif keys[pygame.K_f]: self.player1.move(-movement)
if keys[pygame.K_j]: self.player2.move(movement)
elif keys[pygame.K_k]: self.player2.move(-movement)
# Loop over all current events and check if the quit event was fired
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.ball.update(dt)
self.render()
clock.tick(FRAMERATE)
# Create the game object and start the game
game = Game()
game.start()

150
pong_8.py Normal file
View File

@ -0,0 +1,150 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (0, 0, 0) # Black background
SCREEN_SIZE = (800, 500) # Screen size is 500 x 500
PADDLE_COLOR = (255, 255, 255) # Paddle color is white
PADDLE_SIZE = (10, 100) # Paddle size is 10 x 100
BALL_COLOR = (255, 255, 255) # Ball color is white
BALL_RADIUS = 10 # Ball radius is 10 pixels
PLAYER_START_X_OFFSET = 50 # The amount of pixels the player is away from the wall on the X axis
PLAYER_START_Y_OFFSET = 200 # The amount of pixels the player is away from the wall on the Y axis
PLAYER_MOVE_SPEED = 400 # The amount the player moves when a key is pressed
FRAMERATE = 60 # The target framerate of the game
BALL_START_OFFSET = (250, 250) # The location of the ball when the game first starts
BALL_INITIAL_DIRECTION = [1, -1.5] # The initial direction of the ball, notice that this is not a tuple
BALL_SPEED = 100 # The speed of the ball
class Ball:
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.direction = direction
def bounce_x(self):
self.direction[0] = -self.direction[0]
def bounce_y(self):
self.direction[1] = -self.direction[1]
def update(self, dt):
new_x = self.x + (self.direction[0] * BALL_SPEED * dt)
new_y = self.y + (self.direction[1] * BALL_SPEED * dt)
# Check for a bounce with the top of the screen, we need to test the top of the ball
if new_y - BALL_RADIUS < 0 :
new_y = BALL_RADIUS
self.bounce_y()
# Check for a bounce with the bottom of the screen, we need to test the bottom of the ball
elif new_y + BALL_RADIUS >= SCREEN_SIZE[1]:
new_y = SCREEN_SIZE[1] - BALL_RADIUS
self.bounce_y()
self.x = new_x
self.y = new_y
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
# Will move the player on the Y axis but only if it is certain
# that the player will not move out of the bounds of the screen
def move(self, amount):
if self.y + amount >= 0 and self.y + PADDLE_SIZE[1] + amount < SCREEN_SIZE[1]:
self.y += amount
# This class will represent our game
class Game:
# This function will be called when the class is first created
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Super awesome pong")
self.initialize_gameobjects()
def initialize_gameobjects(self):
self.player1 = Player(PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.player2 = Player(SCREEN_SIZE[0] - PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.ball = Ball(BALL_START_OFFSET[0], BALL_START_OFFSET[1], BALL_INITIAL_DIRECTION)
# Draw one paddle at a given location
def draw_paddle(self, location):
pygame.draw.rect(self.window, PADDLE_COLOR, (location[0], location[1], PADDLE_SIZE[0], PADDLE_SIZE[1]))
# Draw the ball at a given location
def draw_ball(self, location):
pygame.draw.circle(self.window, BALL_COLOR, location, BALL_RADIUS)
# Called when the game needs to be reset
def game_reset(self):
self.initialize_gameobjects()
# Calculates the collisions of the paddles with the ball
def check_collision(self):
if self.ball.x - BALL_RADIUS <= self.player1.x + PADDLE_SIZE[0]:
# Ball is behind the left paddle
if self.player1.y <= self.ball.y + BALL_RADIUS and self.player1.y + PADDLE_SIZE[1] >= self.ball.y - BALL_RADIUS:
# The paddle has catched the ball, bounce back
self.ball.bounce_x()
else:
self.game_reset()
if self.ball.x + BALL_RADIUS >= self.player2.x:
# Ball is behind the right paddle
if self.player2.y <= self.ball.y + BALL_RADIUS and self.player2.y + PADDLE_SIZE[1] >= self.ball.y - BALL_RADIUS:
# The paddle has catched the ball, bounce back
self.ball.bounce_x()
else:
self.game_reset()
# This function will render the contents of the screen
def render(self):
self.window.fill(BACKGROUND_COLOR)
# Draw the two players
self.draw_paddle((self.player1.x, self.player1.y))
self.draw_paddle((self.player2.x, self.player2.y))
# Draw the ball
self.draw_ball((self.ball.x, self.ball.y))
pygame.display.flip()
def start(self):
# The main loop will run until the QUIT event is triggered
# this event will be triggered by closing the window
clock = pygame.time.Clock()
while True:
# Check for user input
# Get a map of all currently pressed keys
keys = pygame.key.get_pressed()
# Change the movement of the player based on which key is pressed
dt = clock.get_time() / 1000
movement = PLAYER_MOVE_SPEED * dt
if keys[pygame.K_d]: self.player1.move(movement)
elif keys[pygame.K_f]: self.player1.move(-movement)
if keys[pygame.K_j]: self.player2.move(movement)
elif keys[pygame.K_k]: self.player2.move(-movement)
# Loop over all current events and check if the quit event was fired
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.ball.update(dt)
self.check_collision()
self.render()
clock.tick(FRAMERATE)
# Create the game object and start the game
game = Game()
game.start()

152
pong_9.py Normal file
View File

@ -0,0 +1,152 @@
import pygame
import sys
# Some constants that we use in the program
BACKGROUND_COLOR = (0, 0, 0) # Black background
SCREEN_SIZE = (800, 500) # Screen size is 500 x 500
PADDLE_COLOR = (255, 255, 255) # Paddle color is white
PADDLE_SIZE = (10, 100) # Paddle size is 10 x 100
BALL_COLOR = (255, 255, 255) # Ball color is white
BALL_RADIUS = 10 # Ball radius is 10 pixels
PLAYER_START_X_OFFSET = 50 # The amount of pixels the player is away from the wall on the X axis
PLAYER_START_Y_OFFSET = 200 # The amount of pixels the player is away from the wall on the Y axis
PLAYER_MOVE_SPEED = 400 # The amount the player moves when a key is pressed
FRAMERATE = 60 # The target framerate of the game
BALL_START_OFFSET = (250, 250) # The location of the ball when the game first starts
BALL_INITIAL_DIRECTION = [1, -1.5] # The initial direction of the ball, notice that this is not a tuple
BALL_SPEED = 100 # The speed of the ball
class Ball:
def __init__(self, x, y, direction, radius=BALL_RADIUS, speed=BALL_SPEED):
self.x = x
self.y = y
self.direction = direction
self.radius = radius
self.speed = speed
def render(self, window):
pygame.draw.circle(window, BALL_COLOR, (self.x, self.y), self.radius)
def bounce_x(self):
self.direction[0] = -self.direction[0]
def bounce_y(self):
self.direction[1] = -self.direction[1]
def update(self, dt):
new_x = self.x + (self.direction[0] * self.speed * dt)
new_y = self.y + (self.direction[1] * self.speed * dt)
# Check for a bounce with the top of the screen, we need to test the top of the ball
if new_y - self.radius < 0 :
new_y = self.radius
self.bounce_y()
# Check for a bounce with the bottom of the screen, we need to test the bottom of the ball
elif new_y + self.radius >= SCREEN_SIZE[1]:
new_y = SCREEN_SIZE[1] - self.radius
self.bounce_y()
self.x = new_x
self.y = new_y
class Player:
def __init__(self, x, y, width=PADDLE_SIZE[0], height=PADDLE_SIZE[1]):
self.x = x
self.y = y
self.width = width
self.height = height
def render(self, window):
pygame.draw.rect(window, PADDLE_COLOR, (self.x, self.y, self.width, self.height))
# Will move the player on the Y axis but only if it is certain
# that the player will not move out of the bounds of the screen
def move(self, amount):
if self.y + amount >= 0 and self.y + self.height + amount < SCREEN_SIZE[1]:
self.y += amount
# This class will represent our game
class Game:
# This function will be called when the class is first created
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Super awesome pong")
self.initialize_gameobjects()
def initialize_gameobjects(self):
self.player1 = Player(PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.player2 = Player(SCREEN_SIZE[0] - PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
self.ball = Ball(BALL_START_OFFSET[0], BALL_START_OFFSET[1], BALL_INITIAL_DIRECTION)
# Called when the game needs to be reset
def game_reset(self):
self.initialize_gameobjects()
# Calculates the collisions of the paddles with the ball
def check_collision(self):
if self.ball.x - self.ball.radius <= self.player1.x + self.player1.width:
# Ball is behind the left paddle
if self.player1.y <= self.ball.y + self.ball.radius and self.player1.y + self.player1.height >= self.ball.y - self.ball.radius:
# The paddle has catched the ball, bounce back
self.ball.bounce_x()
else:
self.game_reset()
if self.ball.x + self.ball.radius >= self.player2.x:
# Ball is behind the right paddle
if self.player2.y <= self.ball.y + self.ball.radius and self.player2.y + self.player2.height >= self.ball.y - self.ball.radius:
# The paddle has catched the ball, bounce back
self.ball.bounce_x()
else:
self.game_reset()
# This function will render the contents of the screen
def render(self):
self.window.fill(BACKGROUND_COLOR)
# Draw the two players
self.player1.render(self.window)
self.player2.render(self.window)
# Draw the ball
self.ball.render(self.window)
pygame.display.flip()
def start(self):
# The main loop will run until the QUIT event is triggered
# this event will be triggered by closing the window
clock = pygame.time.Clock()
while True:
# Check for user input
# Get a map of all currently pressed keys
keys = pygame.key.get_pressed()
# Change the movement of the player based on which key is pressed
dt = clock.get_time() / 1000
movement = PLAYER_MOVE_SPEED * dt
if keys[pygame.K_d]: self.player1.move(movement)
elif keys[pygame.K_f]: self.player1.move(-movement)
if keys[pygame.K_j]: self.player2.move(movement)
elif keys[pygame.K_k]: self.player2.move(-movement)
# Loop over all current events and check if the quit event was fired
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.ball.update(dt)
self.check_collision()
self.render()
clock.tick(FRAMERATE)
# Create the game object and start the game
game = Game()
game.start()

207
syntax.py Normal file
View File

@ -0,0 +1,207 @@
# Lines starting with a '#' are seen as comments, you can
# write whatever you want after them, python won't care and
# just discard it as white space
# Variables
# They are the building blocks of programming and maths,
# luckily for us the difference between python variables and
# math variables is minimal so you already know what they are.
# Variables have a name and a value. The name can be whatever
# you want it to be, it is not limited to one character.
i_am_a_variable = 5
# Here we define a variable with the name "i_am_a_variable" and
# value 5
# Python looks a lot like english and math mixed.
variable_1 = 3
variable_2 = 5
variable_3 = variable_1 + variable_2
# The print function can be used to output the value of something
# to the screen, you will learn what functions are later
print(variable_3) # 8
# Types
# Every variable has a type, in the previous example that type
# was "integer" (shortened to "int") because we were using whole
# numbers
# If we were to assign a string of text to a variable like so
stringy_string = "hello"
# Now the type of the variable "stringy_string" is a string
# (shortened to "str")
# We can request the type of something with the type function
print(type(stringy_string)) # <class 'str'>
# For floating point values it is a float
floaty_float = 3.14
print(type(floaty_float)) # <class 'float'>
some_boolean_value = False
print(type(some_boolean_value)) # <class 'bool'>
# Lists
# Lists are essentially yet another type.
# Their value is simply a list of values
listy_list = [1, 2, 3]
# Here we create a variable with name listy_list of type list
# that contains the values 1, 2, 3
# You can manipulate these lists to your liking
# For example you can insert a value like so
listy_list.append(4)
print(listy_list) # [1, 2, 3, 4]
# Or change a certain value at a given index in the list
# note that indexes start at 0.
listy_list[2] = 5
print(listy_list) # [1, 2, 5, 4]
# Tuples are very similar in that they can store a list
# of values. The big difference is that they cannot be
# manipulated
t = (1, 2, 3)
# t.append(4) would give an error
# t[2] = 5 would give an error
# Functions
def foo(x, y):
return x + y
z = foo(1, 2)
print(z) # 3
# variable_3 is now equal to 8
# As you can see the foo function is called in the same way
# we use "print". This is because print is a function too!
# Functions most of the time have a return value, this means
# that we can even chain our print and our foo function
# inside each other. This way we don't need the "z" variable.
print(foo(1, 3)) # 3
# Conditionals
# When you want to do something only IF a certain
# predicate is true, you can do that with the "if"
# keyword, surprising right
bar = 8
if bar == 8:
print("bar is equal to 8")
else:
print("bar is equal to something other than 8")
# Other comparators are
bar != 8 # bar not equal to 8
bar < 8 # bar smaller than 8
bar > 8 # bar larger than 8
bar <= 8 # bar smaller or equal to 8
bar >= 8 # bar larger or equal to 8
# and / or / not operations
# You can even chain these comparators
baz = 1
bar != 8 and baz < 4
bar < 1 or baz > 1
not bar < 1 # same as bar >= 1
# Loops
# Loops use the same conditionals as we just used above
# There are two types of loops. For loops and while loops.
# For loops are used to loop for each element of something
# (eg. a list).
# While loops are used to loop while a conditional is true
x = 0
while x < 4:
print(x)
x = x + 1
# This loop will loop 4 types and print the value of x each
# time. This means the output will look like:
# 0
# 1
# 2
# 3
sentence = ["hello", "world", "it", "is", "cold", "outside"]
for word in sentence:
print(word)
# hello
# world
# it
# is
# cold
# outside
# Classes
# Classes can be seen as a bundle of functions and variables.
# They can contain both! They can be used to structure your code.
# A class can be defined with the class keyword
class MyCoolClass:
pass # This "pass" keyword just means do nothing, ignore
# As you can see classes have a name too
# As I said they can contain functions and code as so
class CoolClass:
cool_var = 3
# This self parameter has to be added to all members functions
# of a class (unless they are static, but you can ignore that for now)
# This self reference makes it possible to change values inside the
# instance of the class that called this function.
def change_cool_var(self, new_cool_var):
self.cool_var = new_cool_var
# Now to use these classes you need to create an instance of this
# class. This is easy, just act as if it is a function call
my_class = CoolClass()
# Now the variable my_class contains an instance of CoolClass
# Now you can use the functions in this class
print(my_class.cool_var) # 3
my_class.change_cool_var(5)
print(my_class.cool_var) # 5