-
Notifications
You must be signed in to change notification settings - Fork 39
/
Brython_Game.py
328 lines (279 loc) · 9.77 KB
/
Brython_Game.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
if __name__ == '__main__':
import sys, os, webbrowser
url = "file:///" + os.getcwd() + "/index.html"
if webbrowser.open_new(url):
webbrowser.open_new(url)
else:
print("You need to execute file index.html")
os.system("Pause")
sys.exit()
from browser import document as doc
from browser import alert, html
from browser.timer import request_animation_frame as raf
import random
import time
class Game:
canvas = None
context = None
class SET:
WINDOW_SIZE = (800, 570)
FPS = 60
class Rect:
def __init__(self, pos=(0,0), size=(10,10)):
(self._x, self._y) = pos
(self._w, self._h) = size
def __getattr__(self, name):
if name == 'top' or name == 'y':
return self._y
elif name == 'left' or name == 'x':
return self._x
elif name == 'bottom':
return self._y + self._h
elif name == 'right':
return self._x + self._w
elif name == 'size':
return self._w, self._h
elif name == 'width':
return self._w
elif name == 'height':
return self._h
else:
raise AttributeError(name)
def __setattr__(self, name, value):
if name == 'top' or name == 'y':
self._y = int(value)
elif name == 'left' or name == 'x':
self._x = int(value)
elif name == 'bottom':
self._y = int(value) - self._h
elif name == 'right':
self._x = int(value) - self._w
elif name == 'size':
if int(value[0]) > 0 and int(value[1]) > 0:
self._w, self._h = int(value)
elif name == 'width':
if int(value) > 0:
self._w = int(value)
elif name == 'height':
if int(value) > 0:
self._h = int(value)
else:
super().__setattr__(name, value)
def get_collide(self, other_rect):
return self._rect_get_collide(other_rect)
def draw(self):
Game.context.fillRect(self.x, self.y, self.width, self.height)
def _rect_get_collide(a, b):
return a.x + a.width > b.x and b.x + b.width > a.x and \
a.y + a.height > b.y and b.y + b.height > a.y
class Level:
def __init__(self):
self.groupPlatform = list()
self.get_map()
self.allPlatform = {
"Platform": self.groupPlatform,
}
def draw(self):
for e in self.groupPlatform:
e.draw()
def get_map(self):
self.groupPlatform.append(Rect((349,450),(100,10)))
self.groupPlatform.append(Rect((349,250),(100,10)))
self.groupPlatform.append(Rect((49,400),(200,10)))
self.groupPlatform.append(Rect((549,400),(200,10)))
self.groupPlatform.append(Rect((549,150),(200,10)))
self.groupPlatform.append(Rect((49,150),(200,10)))
self.groupPlatform.append(Rect((349,50),(100,10)))
class Puce:
def __init__(self, level, pos=(10,10), size=(10,10)):
self.rect = Rect(pos,size)
self.change_x, self.change_y = 0, 0
self.level = level
self.compteur = random.uniform(0, 2)
self.chrono = time.time()
self.toggle_walk = True
def update(self):
if self.toggle_walk: self.go_left()
elif not self.toggle_walk: self.go_right()
self.calc_gravity()
self.rect.x += self.change_x
for platform in self.level.allPlatform["Platform"]:
if self.rect.get_collide(platform):
if self.change_x > 0:
self.rect.right = platform.left
elif self.change_x < 0:
self.rect.left = platform.right
self.toggle_walk = not self.toggle_walk
self.change_y = -2
self.rect.y += self.change_y
for platform in self.level.allPlatform["Platform"]:
if self.rect.get_collide(platform):
if self.change_y > 0:
self.rect.bottom = platform.top
elif self.change_y < 0:
self.rect.top = platform.bottom
self.change_y = 0
# Teste si le player touche les bords du niveau
if self.rect.right > SET.WINDOW_SIZE[0]:
self.rect.right = SET.WINDOW_SIZE[0]
self.toggle_walk = not self.toggle_walk
if self.rect.left < 0:
self.toggle_walk = not self.toggle_walk
self.rect.left = 0
if time.time() - self.chrono >= self.compteur:
self.jump(random.randint(0,15))
self.compteur = random.uniform(0,2)
self.chrono = time.time()
def calc_gravity(self):
if self.change_y == 0:
self.change_y = 1
else:
self.change_y += .35
if self.rect.bottom >= SET.WINDOW_SIZE[1] and self.change_y >= 0:
self.change_y = 0
self.rect.bottom = SET.WINDOW_SIZE[1]
def jump(self, power=10):
_jump = False
self.rect.y += 2
for platform in self.level.allPlatform["Platform"]:
if self.rect.get_collide(platform):
_jump = True
self.rect.y -= 2
if _jump or self.rect.bottom >= SET.WINDOW_SIZE[1]:
self.change_y = -power
def go_left(self):
self.change_x = -9
def go_right(self):
self.change_x = 9
def stop(self):
self.change_x = 0
def draw(self):
self.rect.draw()
class Player:
def __init__(self, level, pos=(10,10), size=(50,50)):
self.rect = Rect(pos,size)
self.change_x, self.change_y = 0, 0
self.level = level
def update(self):
self.calc_gravity()
self.rect.x += self.change_x
for platform in self.level.allPlatform["Platform"]:
if self.rect.get_collide(platform):
if self.change_x > 0:
self.rect.right = platform.left
elif self.change_x < 0:
self.rect.left = platform.right
self.change_y = -2
self.rect.y += self.change_y
for platform in self.level.allPlatform["Platform"]:
if self.rect.get_collide(platform):
if self.change_y > 0:
self.rect.bottom = platform.top
elif self.change_y < 0:
self.rect.top = platform.bottom
self.change_y = 0
# If player touch the border
if self.rect.right > SET.WINDOW_SIZE[0]:
self.rect.right = SET.WINDOW_SIZE[0]
if self.rect.left < 0:
self.rect.left = 0
for mob in Mob.Mob["Puce"]:
if self.rect.get_collide(mob.rect):
Mob.Mob["Puce"].remove(mob)
Mob._score()
def calc_gravity(self):
if self.change_y == 0:
self.change_y = 1
else:
self.change_y += .35
if self.rect.bottom >= SET.WINDOW_SIZE[1] and self.change_y >= 0:
self.change_y = 0
self.rect.bottom = SET.WINDOW_SIZE[1]
def player_event(self, e):
if e.type == 'keydown':
if e.keyCode == 37 or e.keyCode == 81:
self.go_left()
if e.keyCode == 39 or e.keyCode == 68:
self.go_right()
if e.keyCode == 38 or e.keyCode == 90:
self.jump()
if e.type == 'keyup':
if (e.keyCode == 37 or e.keyCode == 81) and self.change_x < 0:
self.stop()
if (e.keyCode == 39 or e.keyCode == 68) and self.change_x > 0:
self.stop()
if e.keyCode == 80:
Mob.Mob["Puce"].append(Puce(self.level, (random.randint(0,SET.WINDOW_SIZE[0]),random.randint(0,SET.WINDOW_SIZE[1]))))
def jump(self):
_jump = False
self.rect.y += 2
for platform in self.level.allPlatform["Platform"]:
if self.rect.get_collide(platform):
_jump = True
self.rect.y -= 2
if _jump or self.rect.bottom >= SET.WINDOW_SIZE[1]:
self.change_y = -10
def go_left(self):
self.change_x = -6
def go_right(self):
self.change_x = 6
def stop(self):
self.change_x = 0
def draw(self):
self.rect.draw()
class Mob:
Mob = {'Puce': list()}
score = 0
def update(name):
for e in Mob.Mob[name]:
e.update()
def draw(name):
for e in Mob.Mob[name]:
e.draw()
def _score():
Mob.score += 1
s = "Score : %s" % Mob.score
doc["score"].text = s
def main(i):
def animate(i):
global id
id = raf(animate)
draw()
def draw():
Game.context.clearRect(0, 0, Game.canvas.width, Game.canvas.height)
lvl.draw()
Mob.update('Puce')
player.update()
Mob.draw('Puce')
player.draw()
global p
p.style = {'display': "none"}
Game.canvas = html.CANVAS(width=SET.WINDOW_SIZE[0], height=SET.WINDOW_SIZE[1])
mL = (doc.body.clientWidth - SET.WINDOW_SIZE[0]) / 2
Game.canvas.style = {"border": "1px solid black", 'marginLeft': "{}px".format(mL)}
Game.context = Game.canvas.getContext("2d")
if not Game.canvas:
alert("Can't access to the canvas")
Game.context = Game.canvas.getContext('2d')
if not Game.context:
alert("Can't access to the canvas")
lvl = Level()
player = Player(lvl)
doc.body.addEventListener("keydown", player.player_event)
doc.body.addEventListener("keyup", player.player_event)
animate(0)
doc.body.append(Game.canvas)
p = html.P(id="anim")
b = html.BUTTON('START')
b.style = {
'width': "100%",
'height': '100%',
'fontFamily': 'sans-serif',
'fontSize': '60px',
'cursor': 'pointer'
}
p <= b
doc.body <= p
score = html.P(id="score")
doc.body <= score
doc["anim"].bind('click', main)