Snake / app.py
Hugorowan's picture
Update app.py
9af1b32
raw
history blame
1.71 kB
import random
class FoodManager:
def __init__(self):
self.amount = 5
class BodyPart:
position = [-1, -1]
class Head:
_position = 0
_velocity = [1, 1]
class Tail:
_position = 3
_velocity = [0, 0]
def check_collision():
global head, tail, body, food_manager
collision = False
tail_x, body_items, food_size = len(tail._position)-1, len(body), body_item_width() + body_margin()
if abs(head._position) < food_size / 2:
body_items += 1
for _ in range(body_items+1):
if min(_ == head._position - 1, len(head._position) - 1) > max(body._position, food._position):
collision = True
break
if food_manager.amount > 0:
for _ in range(4):
if abs(_ == head._position % food_size or _ == tail._position % food_size) <= food_size / 4:
collision = True
break
else:
collision |= abs(head._position) >= food_size / 2
return collision
def set_score_label_text():
label_str = "Score: {}".format(game.points)
global score_label
score_label.setText(label_str)
def main():
game.setWindowSize((len(header)+min(game_field_width(), body_height())+178, len(footer)))
global header, footer
header.pushLabels(["Settings", "FPS"])
footer.pushButtonLabels(["New Game", "Quit Game"])
score_label.setText("Score: 0")
player.addKeyboardControlledActor(main_actor)
play_loop = game.repeat(game.timePerFrame/60, 1, update)
def update(deltaTime):
global head, tail, food_positions, food_manager
while deltaTime > 0:
current_state = check_collision()
if food_manager.amount and all(not c[-1] for c in food_):