-
Notifications
You must be signed in to change notification settings - Fork 0
/
pambu.py
executable file
·291 lines (236 loc) · 9.24 KB
/
pambu.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
#!/usr/bin/env python3
import copy
import curses
import curses.ascii
from enum import Enum
import locale
import math
import sys
import signal
class Direction(Enum):
north, east, south, west = range(4)
def is_opp(self, other):
return ((self == Direction.north and other == Direction.south) or
(self == Direction.south and other == Direction.north) or
(self == Direction.east and other == Direction.west) or
(self == Direction.west and other == Direction.east))
class Point:
"""A point represented by a *y* and *x* coordinate"""
def __init__(self, y, x):
self.y = y
self.x = x
def __str__(self):
return str(self.__dict__)
def __eq__(self, other):
return (isinstance(other, self.__class__) and
self.__dict__ == other.__dict__)
def __ne__(self, other):
return not self.__eq__(other)
def distance_from(self, other):
dy = other.y - self.y
dx = other.x - self.x
return math.sqrt(dy**2 + dx**2)
def has_same_y(self, other):
return self.y == other.y
def has_same_x(self, other):
return self.x == other.x
def is_to_the_left_of(self, other):
return self.x <= other.x
def is_to_the_right_of(self, other):
return self.x >= other.x
def is_above(self, other):
return self.y <= other.y
def is_below(self, other):
return self.y >= other.y
def move(self, direction):
"""Move 1 unit in given direction"""
if direction == Direction.north:
self.y -= 1
elif direction == Direction.west:
self.x -= 1
elif direction == Direction.south:
self.y += 1
elif direction == Direction.east:
self.x += 1
class LineSegment:
"""A line segment represented by a head and tail point"""
def __init__(self, head, tail):
self.head = head
self.tail = tail
def __str__(self):
return str(self.__dict__)
def __eq__(self, other):
return (isinstance(other, self.__class__) and
self.__dict__ == other.__dict__)
def __ne__(self, other):
return not self.__eq__(other)
def length(self):
return self.head.distance_from(self.tail)
def is_vertical(self):
return self.head.has_same_x(self.tail)
def is_horizontal(self):
return self.head.has_same_y(self.tail)
def increment(self):
"""Increase the line segment length by 1 from the head"""
if self.is_horizontal():
if self.head.x < self.tail.x:
self.head.move(Direction.west)
else:
self.head.move(Direction.east)
elif self.is_vertical():
if self.head.y < self.tail.y:
self.head.move(Direction.north)
else:
self.head.move(Direction.south)
def decrement(self):
"""Decrease the line segment length by 1 from the tail"""
if self.is_horizontal():
if self.head.x < self.tail.x:
self.tail.move(Direction.west)
else:
self.tail.move(Direction.east)
elif self.is_vertical():
if self.head.y < self.tail.y:
self.tail.move(Direction.north)
else:
self.tail.move(Direction.south)
def draw(self, window):
"""Draw the line if it is horizontal or vertical"""
length = math.floor(self.length())
start_point = Point(min(self.head.y, self.tail.y),
min(self.head.x, self.tail.x))
if self.is_vertical():
window.vline(start_point.y, start_point.x, 0, length)
elif self.is_horizontal():
window.hline(start_point.y, start_point.x, 0, length)
def lies_on(self, point):
if self.is_horizontal():
return point.x <= max(self.head.x, self.tail.x) and point.x >= min(self.head.x, self.tail.x) and point.y == self.head.y
if self.is_vertical():
return point.y <= max(self.head.y, self.tail.y) and point.y >= min(self.head.y, self.tail.y) and point.x == self.head.x
def intersection_point(self, other):
if isinstance(other, self.__class__):
if self.head == other.head or self.head == other.tail:
return self.head
elif self.tail == other.head or self.tail == other.tail:
return self.tail
else:
return None
def join(self, other, window):
def join_char(hline, vline):
ch = None
if (ipoint.is_to_the_left_of(hline.head) and
ipoint.is_to_the_left_of(hline.tail)):
if (ipoint.is_above(vline.head) and
ipoint.is_above(vline.tail)):
ch = curses.ACS_ULCORNER
elif (ipoint.is_below(vline.head) and
ipoint.is_below(vline.tail)):
ch = curses.ACS_LLCORNER
elif (ipoint.is_to_the_right_of(hline.head) and
ipoint.is_to_the_right_of(hline.tail)):
if (ipoint.is_above(vline.head) and
ipoint.is_above(vline.tail)):
ch = curses.ACS_URCORNER
elif (ipoint.is_below(vline.head) and
ipoint.is_below(vline.tail)):
ch = curses.ACS_LRCORNER
return ch
if isinstance(other, self.__class__):
hline = None
vline = None
if self.is_vertical():
vline = self
elif self.is_horizontal():
hline = self
if other.is_vertical():
vline = other
elif other.is_horizontal():
hline = other
if hline is not None and vline is not None and hline != vline:
ipoint = hline.intersection_point(vline)
if ipoint is not None:
ch = join_char(hline, vline)
if ch is not None:
window.addch(ipoint.y, ipoint.x, ch)
class Snake:
def __init__(self, dimensions):
maxy, maxx = dimensions
self.points = [Point(math.floor(0.49 * maxy), math.floor(0.59 * maxx)),
Point(math.floor(0.49 * maxy), math.floor(0.40 * maxx))]
self.direction = Direction.east
def __str__(self):
return str(self.__dict__)
def __eq__(self, other):
return (isinstance(other, self.__class__) and
self.__dict__ == other.__dict__)
def __ne__(self, other):
return not self.__eq__(other)
def draw(self, window):
prev_seg = None
for prev_point, curr_point in zip(self.points[:-1], self.points[1:]):
curr_seg = LineSegment(prev_point, curr_point)
curr_seg.draw(window)
curr_seg.join(prev_seg, window)
prev_seg = curr_seg
def detect_collision(self):
head = self.points[0]
prev_seg = None
for prev_point, curr_point in zip(self.points[1:-1], self.points[2:]):
curr_seg = LineSegment(prev_point, curr_point)
if curr_seg.lies_on(head):
curses.endwin()
print("Collision Detected!")
sys.exit(0)
def move(self, window, direction=None):
"""Move 1 unit in given direction"""
first_seg = LineSegment(self.points[0], self.points[1])
last_seg = LineSegment(self.points[-2], self.points[-1])
if (direction is None or
direction == self.direction or
direction.is_opp(self.direction)):
first_seg.increment()
else:
new_head = copy.deepcopy(first_seg.head)
new_head.move(direction)
self.points.insert(0, new_head)
self.direction = direction
self.detect_collision()
last_seg.decrement()
if last_seg.length() == 0:
del self.points[-1]
def signal_handler(signal, frame):
curses.endwin()
print("Thanks for playing pambu!")
sys.exit(0)
def main():
signal.signal(signal.SIGINT, signal_handler)
locale.setlocale(locale.LC_ALL, "") # Use system's default encoding
stdscr = curses.initscr() # Initialize
curses.cbreak() # Enter cbreak mode
curses.noecho() # Don't echo any characters
curses.curs_set(0) # Make cursor invisible
stdscr.nodelay(1) # Make getch() non-blocking
stdscr.keypad(1) # Interpret escape sequences
snk = Snake(stdscr.getmaxyx()) # Initialize our Snake!!
ch = None
while ch != curses.ascii.ESC:
stdscr.clear()
direction = None
if ch == curses.KEY_UP:
direction = Direction.north
elif ch == curses.KEY_DOWN:
direction = Direction.south
elif ch == curses.KEY_LEFT:
direction = Direction.west
elif ch == curses.KEY_RIGHT:
direction = Direction.east
snk.move(stdscr, direction)
snk.draw(stdscr)
stdscr.refresh()
curses.napms(200)
ch = stdscr.getch()
curses.flushinp()
curses.endwin()
if __name__ == "__main__":
main()