Bounce: a Game Made From Python

by Ann Zhao in Design > Animation

171 Views, 1 Favorites, 0 Comments

Bounce: a Game Made From Python

I made a game called Bounce! In this game, you control a paddle while a ball bounces around the screen. Whenever the ball bounces off the paddle, colorful effects explode from it, making the game more exciting and fun to play. Your goal is to keep the ball from touching the ground. If it does, the game is over!

I programmed this game as one of my first coding projects. It was a fun way to practice programming while creating something that people can actually play. I hope you enjoy playing Bounce as much as I enjoyed making it!

Supplies

Python, my code, and Command Prompt

Setting It Up

First, open up Command Prompt on your computer. Then type in pip install pygame and press Enter. This will install Pygame, which you will need to run the code I will give you in the next steps.

The Code

If you haven’t already, download Python onto your computer. Try to download the latest version available. Once Python is installed, open it. In the top-left corner, click File, then select New File. Copy and paste my code below into the new file. When you are finished, save the file by pressing Ctrl + S.

import pygame

import sys

import random


pygame.init()


# Screen

WIDTH, HEIGHT = 600, 800

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("Bounce Paddle Game")


# Colors

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

BLUE = (50, 100, 255)

RED = (255, 60, 60)


clock = pygame.time.Clock()


# Paddle

paddle_width = 120

paddle_height = 15

paddle_x = WIDTH // 2 - paddle_width // 2

paddle_y = HEIGHT - 60

paddle_speed = 8


# Ball

ball_radius = 10

ball_x = WIDTH // 2

ball_y = HEIGHT // 2

ball_dx = random.choice([-4, 4])

ball_dy = -5


# Score

score = 0

font = pygame.font.SysFont(None, 40)


# Particles

particles = []


def create_particles(x, y):

for _ in range(20):

particle = {

"x": x,

"y": y,

"dx": random.uniform(-3, 3),

"dy": random.uniform(-5, -1),

"life": random.randint(20, 40),

"color": (

random.randint(50, 255),

random.randint(50, 255),

random.randint(50, 255)

)

}

particles.append(particle)


def update_particles():

for p in particles[:]:

p["x"] += p["dx"]

p["y"] += p["dy"]

p["dy"] += 0.2

p["life"] -= 1

if p["life"] <= 0:

particles.remove(p)


def draw_particles():

for p in particles:

pygame.draw.circle(screen, p["color"], (int(p["x"]), int(p["y"])), 4)


def reset_game():

global ball_x, ball_y, ball_dx, ball_dy, score

ball_x = WIDTH // 2

ball_y = HEIGHT // 2

ball_dx = random.choice([-4, 4])

ball_dy = -5

score = 0


def draw():

screen.fill(WHITE)


# Paddle (blue)

pygame.draw.rect(screen, BLUE, (paddle_x, paddle_y, paddle_width, paddle_height))


# Ball (red)

pygame.draw.circle(screen, RED, (int(ball_x), int(ball_y)), ball_radius)


# Particles

draw_particles()


# Score (black text)

score_text = font.render(f"Score: {score}", True, BLACK)

screen.blit(score_text, (20, 20))


pygame.display.flip()


def game_over_screen():

while True:

screen.fill(WHITE)

text = font.render("Game Over - Press R to Restart", True, BLACK)

screen.blit(text, (80, HEIGHT // 2))

pygame.display.flip()


for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_r:

return


running = True

while running:

clock.tick(60)


for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False


keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and paddle_x > 0:

paddle_x -= paddle_speed

if keys[pygame.K_RIGHT] and paddle_x < WIDTH - paddle_width:

paddle_x += paddle_speed


# Ball movement

ball_x += ball_dx

ball_y += ball_dy


# Wall collision

if ball_x <= ball_radius or ball_x >= WIDTH - ball_radius:

ball_dx *= -1

if ball_y <= ball_radius:

ball_dy *= -1


# Paddle collision

if (paddle_y <= ball_y + ball_radius <= paddle_y + paddle_height and

paddle_x <= ball_x <= paddle_x + paddle_width):


ball_dy *= -1

score += 1

ball_dx += random.choice([-1, 0, 1])


create_particles(ball_x, paddle_y)


# Game over

if ball_y > HEIGHT:

game_over_screen()

reset_game()


update_particles()

draw()


pygame.quit()

Done

You are done! Now you can start playing Bounce. If the ball touches the floor, press Shift + R to reset the game and play again. Try to keep the ball bouncing for as long as you can and see how high of a score you can get. Have fun!!!! 🎮