-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrain.py
134 lines (88 loc) · 3.45 KB
/
rain.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from scene import *
import sound
import random
import math
import time
A = Action
scl = 0.8
bomb_number = 30
def compute_time(time_ini, time_now):
return time_now - time_ini
def random_pos(size):
return (random.randrange(20, size[0]), random.randrange(size[1], 2 * size[1]))
def alive(player_lives, view):
if player_lives <= 0:
return False
else:
return True
class MyScene (Scene):
def setup(self):
self.background_color = '#727fff'
self.start_time = time.time()
#self.player = SpriteNode('emj:Boy', position = (200, 200), parent = self, scale = 0.7)
self.player = SpriteNode('emj:Aubergine', position = (200, 200), parent = self, scale = scl)
self.player_lives = 1
self.player_lives_Node = LabelNode(f"{self.player_lives} lives", position = self.size / 1.05, parent = self, color = 'yellow')
self.time_Node = LabelNode(f"{0} seconds", position = (0, self.size[1] / 1.1), parent = self, color = "white")
self.drops = [SpriteNode('emj:Bomb', (random.randrange(20, self.size[0]), random.randrange(self.size[1], 2 * self.size[1])), scale = scl, parent = self) for i in range(bomb_number)]
self.load_view_bool = True
def drops_in_screen(self):
for drop in self.drops:
if drop.position.y < - 50:
del self.drops[self.drops.index(drop)]
self.drops.append(SpriteNode('emj:Bomb', (random.randrange(20, self.size[0]), random.randrange(self.size[1] + 100, 2 * self.size[1])), scale = scl, parent = self))
def did_change_size(self):
pass
def draw(self):
self.time_now = time.time()
self.time = compute_time(self.start_time, self.time_now)
self.remove_all_Label()
self.player_lives_Node = LabelNode(f"{self.player_lives} lives", position = self.size / 1.05, parent = self, color = 'yellow')
self.time_Node = LabelNode(f"{int(self.time)} seconds", position = (50, self.size[1] / 1.05), parent = self, color = "black")
self.simulate()
self.drops_in_screen()
if not alive(self.player_lives, self.view):
self.view.close()
print('You lost !')
print(f'You stayed alive {self.time} seconds')
if 0 < math.fabs(self.time - 15.0) % 15 < 0.02:
self.add_live()
def touch_began(self, touch):
(x, y) = touch.location
move = A.move_to(x, y, 0.1)
self.player.run_action(move)
def load_view(self):
ui.load_view('UI').present('sheet')
def remove_all_Label(self):
self.player_lives_Node.remove_from_parent()
self.time_Node.remove_from_parent()
def add_live(self):
self.live = SpriteNode('emj:Heart', position = random_pos(self.size), parent = self)
def touch_moved(self, touch):
self.touch_began(touch)
def add_all_child(self):
self.add_child(self.player)
for drop in self.drops:
self.add_child(drop)
def simulate(self):
if self.time > 5:
move = A.move_by(0, -300)
else:
move = A.move_by(0, 0)
for drop in self.drops:
drop.run_action(move)
if self.player.frame.intersects(drop.frame):
self.player_lives -= 1
drop.remove_from_parent()
del self.drops[self.drops.index(drop)]
self.drops.append(SpriteNode('emj:Bomb', (random.randrange(20, self.size[0]), random.randrange(self.size[1], 2 * self.size[1])), scale = scl, parent = self))
try:
self.live.run_action(move)
if self.player.frame.intersects(self.live.frame):
self.player_lives += 1
self.live.remove_from_parent()
del self.live
except:
pass
if __name__ == '__main__':
run(MyScene(), show_fps=False)