Created
June 9, 2020 05:53
-
-
Save shinysu/56e2d41d24db5acf6199187a9496681a to your computer and use it in GitHub Desktop.
simple pong game using pygamezero
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pgzrun | |
from random import randint | |
WIDTH = 800 | |
HEIGHT = 600 | |
BLACK = (0, 0, 0) | |
paddle = {"x": 20, "y": HEIGHT-50, "length": 200, "width":25} | |
ball = {"x": 0, "y": 0, "move_x": 5, "move_y": 5} | |
BALL_RADIUS = 25 | |
WHITE = (255, 255, 255) | |
is_game_over = False | |
score = 0 | |
miss_count = 0 | |
missed = False | |
def draw(): | |
screen.clear() | |
screen.fill(BLACK) | |
screen.draw.filled_circle((ball["x"], ball["y"]), BALL_RADIUS, 'red') | |
screen.draw.filled_rect(Rect(paddle["x"],paddle["y"],paddle["length"],paddle["width"]),'white') | |
screen.draw.text("Score: "+str(score), (10, 10), fontsize=25, color=WHITE) | |
screen.draw.text("Miss: " + str(miss_count), (10, 30), fontsize=25, color=WHITE) | |
draw_game_end() | |
def update(): | |
if not missed and not is_game_over: | |
move_ball() | |
bounce() | |
detect_collision() | |
def move_ball(): | |
ball["x"] += ball["move_x"] | |
ball["y"] += ball["move_y"] | |
def on_mouse_move(pos): | |
if (pos[0] >= 0) and (pos[0] <= (WIDTH - paddle["length"])): | |
paddle["x"] = pos[0] | |
def on_mouse_down(): | |
global missed | |
if missed: | |
missed = False | |
position_ball() | |
def on_key_down(key): | |
global is_game_over | |
if is_game_over and key == keys.SPACE: | |
reset() | |
def bounce(): | |
if (ball["x"] > (WIDTH - BALL_RADIUS)) or (ball["x"] < BALL_RADIUS): | |
ball["move_x"] *= -1 | |
if ball["y"] < BALL_RADIUS: | |
ball["move_y"] *= -1 | |
def detect_collision(): | |
global score, miss_count, missed | |
if (ball["y"] + BALL_RADIUS) > (paddle["y"]): | |
if((ball["x"] + BALL_RADIUS) > paddle["x"]) and ((ball["x"]-BALL_RADIUS) <= (paddle["x"] + paddle["length"])): | |
ball["move_y"] *= -1 | |
score = score + 1 | |
else: | |
missed = True | |
miss_count = miss_count +1 | |
check_game_over() | |
def position_ball(): | |
ball["x"] = randint(BALL_RADIUS, WIDTH - BALL_RADIUS) | |
ball["y"] = BALL_RADIUS | |
def draw_game_end(): | |
global is_game_over | |
if is_game_over: | |
screen.draw.text("GAME OVER !!", (WIDTH/2, 10), fontsize=30, color=WHITE) | |
screen.draw.text("Press SPACE key to start a new game", (WIDTH / 2 - 50, 30), fontsize=25, color=WHITE) | |
elif missed: | |
screen.draw.text("Missed it! Click to continue", (WIDTH / 3, 30), fontsize=30, color=WHITE) | |
def check_game_over(): | |
global is_game_over, miss_count | |
if miss_count >= 5: | |
is_game_over = True | |
def reset(): | |
global score, miss_count, missed, is_game_over | |
score = 0 | |
miss_count = 0 | |
missed = False | |
is_game_over = False | |
position_ball() | |
position_ball() | |
pgzrun.go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment