-
Notifications
You must be signed in to change notification settings - Fork 0
/
isb.py
93 lines (66 loc) · 1.87 KB
/
isb.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
rect_width = 100
rect_height = 50
radius = 40
rect_color = random_color()
rect_x = dim[0] // 2
rect_y = dim[1] // 2
rect_x_speed = 0.5
rect_y_speed = 0.5
# draw the circle on the screen
def draw_rect():
pygame.draw.rect(screen, rect_color, (rect_x, rect_y,
rect_width, rect_height), radius)
# update the screen
r_c = random_color()
def update_screen():
screen.fill(r_c)
draw_rect()
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
rect_x += rect_x_speed
rect_y += rect_y_speed
if rect_x > dim[0] - radius:
rect_x_speed = rect_x_speed * -1
rect_color = random_color()
if rect_x < 1 - radius:
rect_x_speed = rect_x_speed * -1
rect_color = random_color()
if rect_y > dim[1] - radius:
rect_y_speed = rect_y_speed * -1
rect_color = random_color()
if rect_y < 0 - radius:
rect_y_speed = rect_y_speed * -1
rect_color = random_color()
if rect_y > dim[0]:
rect_y_speed = rect_y_speed * 1
rect_color = random_color()
if rect_x > dim[0]:
rect_x_speed = rect_x_speed * -1
rect_color = random_color()
if rect_x < 0:
rect_x_speed = rect_x_speed * -1
rect_color = random_color()
if rect_y < 1:
rect_y_speed = rect_y_speed * -1
rect_color = random_color()
update_screen()