Bounce: a Game Made From Python
by Ann Zhao in Design > Animation
113 Views, 1 Favorites, 0 Comments
Bounce: a Game Made From Python
I made the game of bounce. This game is where you have a paddle, and there is a ball bouncing around. Except whenever the ball bounces, colors will explode off of the paddle. You don't want the ball to touch the ground. I programmed this game as one of my first projects of coding. I hope you will enjoy!
Supplies
Python, my code, and Command Prompt
Setting It Up
Open up command prompt. Type in, "pip install pygame." This will soon help as I give you my code.
The Code
Download Python if you haven't already. Try to get the latest version. Open up Python, in the left corner, press file and then press new file. Copy my code below and save it using 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. If the ball touches the floor, press shift+r to reset and play again. Have Fun!!!!