Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
class FoodManager:
|
4 |
+
def __init__(self):
|
5 |
+
self.amount = 5
|
6 |
+
|
7 |
+
class BodyPart:
|
8 |
+
position = [-1, -1]
|
9 |
+
|
10 |
+
class Head:
|
11 |
+
_position = 0
|
12 |
+
_velocity = [1, 1]
|
13 |
+
|
14 |
+
class Tail:
|
15 |
+
_position = 3
|
16 |
+
_velocity = [0, 0]
|
17 |
+
|
18 |
+
def check_collision():
|
19 |
+
global head, tail, body, food_manager
|
20 |
+
collision = False
|
21 |
+
tail_x, body_items, food_size = len(tail._position)-1, len(body), body_item_width() + body_margin()
|
22 |
+
if abs(head._position) < food_size / 2:
|
23 |
+
body_items += 1
|
24 |
+
for _ in range(body_items+1):
|
25 |
+
if min(_ == head._position - 1, len(head._position) - 1) > max(body._position, food._position):
|
26 |
+
collision = True
|
27 |
+
break
|
28 |
+
if food_manager.amount > 0:
|
29 |
+
for _ in range(4):
|
30 |
+
if abs(_ == head._position % food_size or _ == tail._position % food_size) <= food_size / 4:
|
31 |
+
collision = True
|
32 |
+
break
|
33 |
+
else:
|
34 |
+
collision |= abs(head._position) >= food_size / 2
|
35 |
+
return collision
|
36 |
+
|
37 |
+
def set_score_label_text():
|
38 |
+
label_str = "Score: {}".format(game.points)
|
39 |
+
global score_label
|
40 |
+
score_label.setText(label_str)
|
41 |
+
|
42 |
+
def main():
|
43 |
+
game.setWindowSize((len(header)+min(game_field_width(), body_height())+178, len(footer)))
|
44 |
+
global header, footer
|
45 |
+
header.pushLabels(["Settings", "FPS"])
|
46 |
+
footer.pushButtonLabels(["New Game", "Quit Game"])
|
47 |
+
score_label.setText("Score: 0")
|
48 |
+
player.addKeyboardControlledActor(main_actor)
|
49 |
+
play_loop = game.repeat(game.timePerFrame/60, 1, update)
|
50 |
+
|
51 |
+
def update(deltaTime):
|
52 |
+
global head, tail, food_positions, food_manager
|
53 |
+
while deltaTime > 0:
|
54 |
+
current_state = check_collision()
|
55 |
+
if food_manager.amount and all(not c[-1] for c in food_
|