-
Notifications
You must be signed in to change notification settings - Fork 1
/
matopeli.py
162 lines (142 loc) · 3.47 KB
/
matopeli.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
import msvcrt
import time
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
def win_get_direction(timeout):
start = time.time()
key = None
while True:
time.sleep(0.01)
if msvcrt.kbhit():
if msvcrt.getch() == b'\xe0':
key = {
b'H': UP,
b'P': DOWN,
b'K': LEFT,
b'M': RIGHT,
}[msvcrt.getch()]
if time.time() - start > timeout:
break
return key
def direction_to_text(direction):
return {
0: "up",
1: "down",
2: "left",
3: "right",
}[direction]
def legal_direction(previous, next):
if previous == next:
return False
else:
return {
UP: DOWN,
DOWN: UP,
LEFT: RIGHT,
RIGHT: LEFT,
}[previous] != next
def move(pos, direction, n):
if direction == UP:
pos[1] -= n
elif direction == DOWN:
pos[1] += n
elif direction == LEFT:
pos[0] -= n
elif direction == RIGHT:
pos[0] += n
class Game:
def __init__(self, size=(100, 100)):
self.size_x = size[0]
self.size_y = size[1]
self.mato = None
self.food = None
def __repr__(self):
grid = [[None for _ in range(self.size_x + 1)] for _ in range(self.size_y + 1)]
grid[0] = ["-" for _ in range(self.size_x + 2)]
grid[-1] = ["-" for _ in range(self.size_x + 2)]
for row in range(self.size_y):
grid[row + 1][0] = '|'
grid[row + 1][-1] = '|'
for part in self.mato.parts:
pos = list(part.start)
grid[pos[1] + 1][pos[0] + 1] = "■"
for _ in range(part.length):
move(pos, part.direction, 1)
grid[pos[1] + 1][pos[0] + 1] = "■"
return '\n'.join(''.join(s or " " for s in r) for r in grid)
class Part:
def __init__(self, start, direction, length):
self.start = start
self.direction = direction
self.length = length
def move(self, head=False, tail=False):
if head:
self.length += 1
elif tail:
self.length -= 1
move(self.start, self.direction, 1)
def get_head(self):
out = list(self.start)
move(out, self.direction, self.length)
return out
def covers(self, position):
direction = -1 if self.direction in [LEFT, UP] else 1
pos = list(self.start)
if pos == position:
return True
for _ in range(self.length):
move(pos, self.direction, direction)
if pos == position:
return True
def __repr__(self):
start = self.start
direction = direction_to_text(self.direction)
length = self.length
return f"Part<start={start} direction={direction} length={length}>"
class Mato:
def __init__(self, parts=None):
self.parts = parts or []
self.next_direction = None
def move(self):
if self.next_direction is not None:
self.add_part()
self.parts[-1].move(head=True)
self.parts[0].move(tail=True)
if self.parts[0].length < 1:
del self.parts[0]
def add_part(self):
legal = legal_direction(self.parts[-1].direction, self.next_direction)
if not legal:
direction = direction_to_text(self.next_direction)
print(f"illegal direction: {direction}")
else:
start = list(self.parts[-1].get_head())
direction = self.next_direction
length = 0
self.parts.append(Part(start, direction, length))
self.next_direction = None
def covers(self, position):
for part in self.parts[:-1]:
if part.covers(position):
return True
def __repr__(self):
return '\n'.join(str(p) for p in self.parts)
def main():
game = Game(size=(30, 30))
mato = Mato()
game.mato = mato
mato.parts = [
Part([0, 0], RIGHT, 7),
]
while True:
mato.next_direction = win_get_direction(0.1)
mato.move()
print(mato)
print(game)
if mato.covers(mato.parts[-1].get_head()):
print("hävisit pelin")
break
if __name__ == "__main__":
main()