-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingpong.py
More file actions
66 lines (51 loc) · 1.44 KB
/
pingpong.py
File metadata and controls
66 lines (51 loc) · 1.44 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pygame
import random
pygame.init()
# setup the display
dim = (800, 600)
screen = pygame.display.set_mode(dim)
pygame.display.set_caption("Ping Pong")
# Create a function that returns a random color
def random_color():
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
return (red, green, blue)
# set up the circle
radius = 40
circle_color = random_color()
circle_x = dim[0] // 2
circle_y = dim[1] // 2
circle_x_speed = 0.5
circle_y_speed = 0.5
# draw the circle on the screen
def draw_circle():
pygame.draw.circle(screen, circle_color, (circle_x, circle_y), radius)
# update the screen
r_c = random_color()
def update_screen():
screen.fill(r_c)
draw_circle()
pygame.display.update()
update_screen()
# main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# move the circle
circle_x += circle_x_speed
circle_y += circle_y_speed
if circle_x > dim[0] - radius:
circle_x_speed = circle_x_speed * -1
circle_color = random_color()
if circle_x < 0:
circle_x_speed = circle_x_speed * -1
circle_color = random_color()
if circle_y > dim[1] - radius:
circle_y_speed = circle_y_speed * -1
circle_color = random_color()
if circle_y < 0:
circle_y_speed = circle_y_speed * -1
circle_color = random_color()
update_screen()