-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
93 lines (78 loc) · 2.73 KB
/
game.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
import random
import curses
class SnakeGame:
def __init__(self, width, height):
self.width = width
self.height = height
self.snake = [(width // 2, height // 2)]
self.direction = curses.KEY_RIGHT
self.food = self.generate_food()
def generate_food(self):
while True:
food = (random.randint(0, self.width - 1), random.randint(0, self.height - 1))
if food not in self.snake:
return food
def update(self):
try:
key = self.window.getkey()
except:
key = None
if key is not None:
if key.lower() == "w" and self.direction != curses.KEY_DOWN:
self.direction = curses.KEY_UP
elif key.lower() == "s" and self.direction != curses.KEY_UP:
self.direction = curses.KEY_DOWN
elif key.lower() == "a" and self.direction != curses.KEY_RIGHT:
self.direction = curses.KEY_LEFT
elif key.lower() == "d" and self.direction != curses.KEY_LEFT:
self.direction = curses.KEY_RIGHT
head = self.snake[0]
if self.direction == curses.KEY_UP:
new_head = (head[0], head[1] - 1)
elif self.direction == curses.KEY_DOWN:
new_head = (head[0], head[1] + 1)
elif self.direction == curses.KEY_LEFT:
new_head = (head[0] - 1, head[1])
else:
new_head = (head[0] + 1, head[1])
self.snake.insert(0, new_head)
if new_head == self.food:
self.food = self.generate_food()
else:
self.snake.pop()
def display(self):
self.window.clear()
self.window.border()
for y in range(self.height):
for x in range(self.width):
if (x, y) in self.snake:
self.window.addch(y + 1, x + 1, "*")
elif (x, y) == self.food:
self.window.addch(y + 1, x + 1, "#")
self.window.refresh()
def run(self):
try:
curses.initscr()
curses.curs_set(0)
self.window = curses.newwin(self.height + 2, self.width + 2, 0, 0)
self.window.timeout(100)
while True:
self.display()
self.update()
if self.is_collision():
break
except (curses.error, KeyboardInterrupt):
pass
finally:
curses.endwin()
def is_collision(self):
head = self.snake[0]
if (
head[0] < 0
or head[0] >= self.width
or head[1] < 0
or head[1] >= self.height
or head in self.snake[1:]
):
return True
return False