File size: 1,705 Bytes
21229be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9af1b32
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
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_):