-
Notifications
You must be signed in to change notification settings - Fork 0
/
game008.py
50 lines (40 loc) · 1.14 KB
/
game008.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pygame
# Initialize Pygame
pygame.init()
# Set up game window
WIDTH = 800
HEIGHT = 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("DVD Screensaver")
# Set up game objects
BALL_RADIUS = 50
BALL_COLOR = (255, 255, 255)
ball = pygame.draw.circle(WIN, BALL_COLOR, (WIDTH//2, HEIGHT//2), BALL_RADIUS)
ball_speed_x = 5
ball_speed_y = 5
# Set up game loop
FPS = 60
clock = pygame.time.Clock()
running = True
while running:
# Handle user input
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update game state
ball.x += ball_speed_x
ball.y += ball_speed_y
# Check for collision with left or right wall
if ball.left <= 0 or ball.right >= WIDTH:
ball_speed_x *= -1
# Check for collision with top or bottom wall
if ball.top <= 0 or ball.bottom >= HEIGHT:
ball_speed_y *= -1
# Draw ball on screen
WIN.fill((0, 0, 0))
ball = pygame.draw.circle(WIN, BALL_COLOR, (ball.x, ball.y), BALL_RADIUS)
pygame.display.update()
# Set the frame rate
clock.tick(FPS)
# Quit Pygame when the game loop exits
pygame.quit()