Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created December 5, 2020 15:15
Show Gist options
  • Save shinysu/6273492ebd83e89c75f8848ddf0c1cae to your computer and use it in GitHub Desktop.
Save shinysu/6273492ebd83e89c75f8848ddf0c1cae to your computer and use it in GitHub Desktop.
Pong game using pgzero
"""
draw the ball
"""
import pgzrun
WIDTH = 1000
HEIGHT = 700
COLOUR = (0, 0, 250)
ball = Actor("ball")
def draw():
screen.fill(COLOUR)
ball.draw()
pgzrun.go()
"""
animate the ball
"""
import pgzrun
WIDTH = 1000
HEIGHT = 700
COLOUR = (0, 0, 250)
ball = Actor("ball")
ball_x_speed = 5
ball_y_speed = 5
def draw():
screen.fill(COLOUR)
ball.draw()
def update():
animate_ball()
def animate_ball():
ball.y += ball_y_speed
ball.x += ball_x_speed
pgzrun.go()
"""
check if the ball crosses boundary
"""
import pgzrun
WIDTH = 1000
HEIGHT = 700
COLOUR = (0, 0, 250)
ball = Actor("ball")
ball_x_speed = 5
ball_y_speed = 5
def draw():
screen.fill(COLOUR)
ball.draw()
def update():
animate_ball()
check_boundaries()
def animate_ball():
ball.y += ball_y_speed
ball.x += ball_x_speed
def check_boundaries():
global ball_x_speed, ball_y_speed
if ball.right > WIDTH or ball.left < 0:
ball_x_speed *= -1
if ball.top < 0 or ball.bottom > HEIGHT:
ball_y_speed *= -1
pgzrun.go()
"""
draw the paddle and animate it
"""
import pgzrun
WIDTH = 1000
HEIGHT = 700
COLOUR = (0, 0, 250)
ball = Actor("ball")
paddle = Actor("paddle")
ball_x_speed = 5
ball_y_speed = 5
def draw():
screen.fill(COLOUR)
ball.draw()
paddle.draw()
def update():
animate_ball()
check_boundaries()
def animate_ball():
ball.y += ball_y_speed
ball.x += ball_x_speed
def check_boundaries():
global ball_x_speed, ball_y_speed
if ball.right > WIDTH or ball.left < 0:
ball_x_speed *= -1
if ball.top < 0 or ball.bottom > HEIGHT:
ball_y_speed *= -1
def on_mouse_move(pos):
paddle.x = pos[0] # move paddle horizontally according to mouse position
if paddle.left < 0:
paddle.left = 0
elif paddle.right > WIDTH:
paddle.right = WIDTH
def position_objects():
paddle.bottom = HEIGHT
paddle.left = 0
position_objects() # to set the initial position of ball and paddle
pgzrun.go()
"""
check for collision of ball and paddle
check if ball misses paddle
"""
import pgzrun
WIDTH = 1000
HEIGHT = 700
COLOUR = (0, 0, 250)
ball = Actor("ball")
paddle = Actor("paddle")
ball_x_speed = 5
ball_y_speed = 5
missed = False
def draw():
screen.fill(COLOUR)
ball.draw()
paddle.draw()
def update():
if not missed:
animate_ball()
def animate_ball():
ball.y += ball_y_speed
ball.x += ball_x_speed
check_boundaries()
check_collision() # check if ball collides with paddle
check_paddle_miss() # check if ball misses paddle
def check_boundaries():
global ball_x_speed, ball_y_speed
if ball.right > WIDTH or ball.left < 0:
ball_x_speed *= -1
if ball.top < 0 or ball.bottom > HEIGHT:
ball_y_speed *= -1
def on_mouse_move(pos):
paddle.x = pos[0] # move paddle horizontally according to mouse position
if paddle.left < 0:
paddle.left = 0
elif paddle.right > WIDTH:
paddle.right = WIDTH
def position_objects():
paddle.bottom = HEIGHT
paddle.left = 0
def check_collision():
global ball_y_speed
if ball.colliderect(paddle): # check if ball collides with paddle
ball_y_speed *= -1
def check_paddle_miss():
global missed
if ball.bottom > paddle.top + ball_y_speed:
missed = True
position_objects() # to set the initial position of ball and paddle
pgzrun.go()
"""
include scores
"""
import pgzrun
WIDTH = 1000
HEIGHT = 700
COLOUR = (0, 0, 250)
ball = Actor("ball")
paddle = Actor("paddle")
ball_x_speed = 5
ball_y_speed = 5
missed = False
score = 0
def draw():
screen.fill(COLOUR)
ball.draw()
paddle.draw()
draw_score()
def update():
if not missed:
animate_ball()
def animate_ball():
ball.y += ball_y_speed
ball.x += ball_x_speed
check_boundaries()
check_collision() # check if ball collides with paddle
check_paddle_miss() # check if ball misses paddle
def check_boundaries():
global ball_x_speed, ball_y_speed
if ball.right > WIDTH or ball.left < 0:
ball_x_speed *= -1
if ball.top < 0 or ball.bottom > HEIGHT:
ball_y_speed *= -1
def on_mouse_move(pos):
paddle.x = pos[0] # move paddle horizontally according to mouse position
if paddle.left < 0:
paddle.left = 0
elif paddle.right > WIDTH:
paddle.right = WIDTH
def position_objects():
paddle.bottom = HEIGHT
paddle.left = 0
def check_collision():
global ball_y_speed, score
if ball.colliderect(paddle): # check if ball collides with paddle
ball_y_speed *= -1
score += 100
def check_paddle_miss():
global missed
if ball.bottom > paddle.top + 5:
missed = True
def draw_score():
screen.draw.text("Score: "+str(score), (10,10), fontsize=30, color="white")
if missed:
position = ((WIDTH // 2)-100, HEIGHT // 2)
screen.draw.text("OOPS!!! Missed it!!! ", position, fontsize=50, color="red")
position_objects() # to set the initial position of ball and paddle
pgzrun.go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment