-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplastic.py
More file actions
85 lines (84 loc) · 2.48 KB
/
Copy pathplastic.py
File metadata and controls
85 lines (84 loc) · 2.48 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import random
import pgzrun
WIDTH=600
HEIGHT=600
levels=5
start_speed=10
non_recycled_items=["bag", "battery", "chips", "bottle"]
game_over=False
game_complete=False
current_level=1
items=[]
animations=[]
def make_items(number_of_extra_items):
items_to_create=get_option_to_create(number_of_extra_items)
new_items=create_items(items_to_create)
layout_items(new_items)
animate_items(new_items)
return new_items
def get_option_to_create(number_of_extra_items):
items_to_create=["paper"]
for i in range(number_of_extra_items):
option=random.choice(non_recycled_items)
items_to_create.append(option)
return items_to_create
def create_items(items_to_create):
new_items=[]
for i in items_to_create:
item=Actor(i)
new_items.append(item)
return new_items
def draw():
global items, current_level,game_over,game_complete
screen.clear()
screen.blit("bground", (0,0)) # to display the image on the screen
if game_over:
screen.draw.text("GAME OVER", fontsize=20, center=(WIDTH/2,HEIGHT/2), color="blue")
elif game_complete:
screen.draw.text("GAME COMPLETED", fontsize=20, center=(WIDTH/2,HEIGHT/2), color="blue")
else:
for i in items:
i.draw()
def layout_items(items_to_layout):
gaps=len(items_to_layout)+1
gapsize=WIDTH/gaps
random.shuffle(items_to_layout)
for i,j in enumerate(items_to_layout):
new_x=(i+1)*gapsize
j.x=new_x
def animate_items(items_to_animate):
global animations
for i in items_to_animate:
duration=start_speed-current_level
i.anchor=("center", "bottom")
animation=animate(i,duration=duration,on_finished=handle_game_over,y=HEIGHT)
animations.append(animation)
def handle_game_over():
global game_over
game_over=True
def on_mouse_down(pos):
global items,current_level
for i in items:
if i.collidepoint(pos):
if "paper" in i.image:
handle_game_complete()
else:
handle_game_over()
def handle_game_complete():
global current_level,items,animations,game_complete
stop_animations(animations)
if current_level==levels:
game_complete=True
else:
current_level+=1
items=[]
animations=[]
def stop_animations(animations_to_stop):
for i in animations_to_stop:
if i.running:
i.stop()
def update():
global items
if len(items)==0:
items=make_items(current_level)
pgzrun.go()