-
Notifications
You must be signed in to change notification settings - Fork 0
/
bouncing_shapes.py
92 lines (74 loc) · 1.78 KB
/
bouncing_shapes.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
import pygame
import random
pygame.init()
HEIGHT = 500
WIDTH = 500
TITLE = "BOOF's Cool Animation"
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption(TITLE)
clock = pygame.time.Clock()
#Initialize changing variables
x = 0
y = 0
dx = 2
dy = 3
going_left = False
going_up = False
bouncing = False
frame_ndx = 0
color_ndx = 0
shape_ndx = 0
height = 50
width = 50
colors = []
colors.append((255,0,0))
colors.append((255,165,0))
colors.append((255,255,0))
colors.append((0,255,0))
colors.append((0,255,255))
colors.append((0,165,255))
colors.append((0,0,255))
running = True
while(running):
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#Do variable changing here
frame_ndx = frame_ndx + 1
if frame_ndx % 10 == 0:
color_ndx = (color_ndx + 1) % len(colors)
if x > WIDTH-width:
going_left = True
bouncing = True
elif x < 0:
going_left = False
bouncing = True
if y > HEIGHT-height:
going_up = True
bouncing = True
elif y < 0:
going_up = False
bouncing = True
if going_left:
x = x - dx
else:
x = x + dx
if going_up:
y = y - dy
else:
y = y + dy
if bouncing:
shape_ndx = (shape_ndx + 1) % 3
bouncing = False
#Do screen clearing here
screen.fill((255,255,255))
#Do drawing here
if shape_ndx == 0:
pygame.draw.rect(screen,colors[color_ndx],(x,y,width,height))
elif shape_ndx == 1:
pygame.draw.circle(screen,colors[color_ndx],(x,y),int(width/2))
elif shape_ndx == 2:
pygame.draw.polygon(screen,colors[color_ndx],[(x,y+height),(x+int(width/2),y),(x+width,y+height)])
pygame.display.flip()
clock.tick(60)
pygame.quit()