-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
229 lines (184 loc) · 6.05 KB
/
main.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
import os
import sys
import time
from dataclasses import dataclass
from enum import Enum, auto
from random import randint
from typing import Optional
if os.name == "nt":
import msvcrt
else:
import select
import termios
import tty
RED = "\033[31m"
GREEN = "\033[32m"
RESET = "\033[0m"
class Direction(Enum):
UP = auto()
DOWN = auto()
LEFT = auto()
RIGHT = auto()
@dataclass
class Point:
x: int
y: int
def __add__(self, other: "Point") -> "Point":
return Point(self.x + other.x, self.y + other.y)
class Snake:
DIRECTION_VECTORS = {
Direction.UP: Point(-1, 0),
Direction.DOWN: Point(1, 0),
Direction.LEFT: Point(0, -1),
Direction.RIGHT: Point(0, 1),
}
def __init__(self, body, direction):
self.body = body
self.direction = direction
@property
def head(self) -> Point:
return self.body[-1]
@property
def tail(self) -> Point:
return self.body[0]
class Apple:
def __init__(self, position):
self.position = position
class Game:
CONTROLS = {
"w": Direction.UP,
"s": Direction.DOWN,
"a": Direction.LEFT,
"d": Direction.RIGHT,
}
def __init__(self, w: int, h: int):
self.height = h
self.width = w
self.canvas = [[" " for _ in range(self.width)] for _ in range(self.height)]
self.speed = 0.5
self.score = 0
self.game_over = False
self.snake = Snake(
[Point(x, y) for (x, y) in [(1, 3), (2, 3), (3, 3), (4, 3)]], Direction.DOWN
)
self.apple = Apple(Point(20, 20))
def update_score(self):
print("\033[1;4H" + f"Score: {self.score}")
def move(self, new_direction: Optional[Direction] = None):
if not new_direction:
new_direction = self.snake.direction
head = self.snake.head
new_head = head + self.snake.DIRECTION_VECTORS[new_direction]
self.validate_direction(new_head)
self.snake.body.append(new_head)
tail = self.snake.tail
print(f"\033[{tail.x+1};{tail.y+1}H", end="")
print(" ")
self.snake.body.pop(0)
self.snake.direction = new_direction
for i in self.snake.body[:-1]:
print(f"\033[{i.x + 1};{i.y + 1}H", end="")
print(f"{GREEN}0{RESET}")
print(f"\033[{new_head.x + 1};{new_head.y + 1}H", end="")
print(f"{GREEN}X{RESET}")
def get_movement(self, key: str):
opposite_directions = {
Direction.UP: Direction.DOWN,
Direction.DOWN: Direction.UP,
Direction.LEFT: Direction.RIGHT,
Direction.RIGHT: Direction.LEFT,
}
if (
key.lower() not in ["w", "a", "s", "d"]
or (
(new_direction := self.CONTROLS[key.lower()])
== opposite_directions[self.snake.direction]
)
or new_direction == self.snake.direction
):
return
self.move(new_direction)
def validate_direction(self, new_head: Point):
if (
new_head.x <= 0
or new_head.x >= self.height - 1
or new_head.y <= 0
or new_head.y >= self.width - 1
):
self.game_over = True
return
if any(b.x == new_head.x and b.y == new_head.y for b in self.snake.body[:-1]):
self.game_over = True
return
if new_head == self.apple.position:
self.spawn_apple()
self.snake.body.insert(
0, Point(self.snake.tail.x + 1, self.snake.tail.y + 1)
)
self.speed = max(0.05, self.speed * 0.9)
self.score += 1
self.update_score()
def spawn_apple(self):
x = randint(2, self.height - 3)
y = randint(2, self.height - 3)
self.apple.position = Point(x, y)
print(f"\033[{x+1};{y+1}H", end="")
print(f"{RED}*{RESET}")
def render(self):
snake_head = self.snake.head
apple = self.apple.position
for row in range(self.height):
for col in range(self.width):
if row == 0 or row == self.height - 1:
self.canvas[row][col] = "─"
elif col == 0 or col == self.width - 1:
self.canvas[row][col] = "│"
if (Point(row, col)) in self.snake.body:
self.canvas[row][col] = f"{GREEN}0{RESET}"
self.canvas[0][0] = "╭"
self.canvas[0][self.width - 1] = "╮"
self.canvas[self.height - 1][0] = "╰"
self.canvas[self.height - 1][self.width - 1] = "╯"
self.canvas[snake_head.x][snake_head.y] = f"{GREEN}X{RESET}"
self.canvas[apple.x][apple.y] = f"{RED}*{RESET}"
score_text = f"Score: {self.score}"
for i, char in enumerate(score_text):
self.canvas[0][i + 3] = char
for row in self.canvas:
print("".join(row), end="", flush=True)
def get_key():
if os.name == "nt":
if msvcrt.kbhit():
key = msvcrt.getch().decode("utf-8").lower()
return key
else:
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
dr, dw, de = select.select([sys.stdin], [], [], 0)
if dr:
key = sys.stdin.read(1).lower()
return key
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return None
if __name__ == "__main__":
print("\033[?25l", end="")
size = os.get_terminal_size()
game = Game(size.columns, size.lines)
game.render()
last_update = time.time()
try:
while not game.game_over:
if time.time() - last_update >= game.speed:
game.move()
last_update = time.time()
key = get_key()
if key and key == "q":
break
elif key in Game.CONTROLS:
game.get_movement(key)
print("Game Over!")
finally:
print("\033[?25h", end="")