-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (81 loc) · 2.23 KB
/
main.py
File metadata and controls
100 lines (81 loc) · 2.23 KB
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
from random import randint
import time
import sys
from bonus import Bonus
from field import Field
# from kbhit import KBHit
from kbthread import keys
from snake import Snake
from Cursor import hide_cursor, show_cursor
WIDTH = 60
HEIGHT = 20
directions_codes = {
'left': (-1, 0),
'right': (1, 0),
'up': (0, -1),
'down': (0, 1),
's': (0, 1),
'w': (0, -1),
'a': (-1, 0),
'd': (1, 0),
}
if len(sys.argv) > 1 and sys.argv[1] == 'dev':
developer_mode = True
else:
developer_mode = False
def create_snake():
return Snake(randint(0, WIDTH // 2), randint(0, HEIGHT - 2), field)
def create_apple():
apple = randint(0, WIDTH - 2), randint(0, HEIGHT - 2)
while apple in fruits or apple in snake:
apple = randint(0, WIDTH - 2), randint(0, HEIGHT - 2)
return Bonus('o', *apple)
def game_over():
# TODO
pass
def update_screen():
global time_delta
for i in range(len(fruits)):
apple = fruits[i]
if snake.check_bonus(apple):
fruits[i] = create_apple()
if not developer_mode:
time_delta *= 0.9
break
snake.move()
if snake.check_lose():
return False
print(f'\033[{HEIGHT + 3}A' + field.to_text(snake, fruits))
print(f'Your score is {len(snake)}.')
return True
field = Field(WIDTH, HEIGHT)
fruits = []
snake = create_snake()
fruits.append(create_apple())
print(field.to_text(snake, fruits) + '\n')
time_delta = 0.001 if developer_mode else 0.1
p = time.time()
hide_cursor()
try:
while True:
c = time.time()
if c - p < time_delta:
pause = time_delta - (c - p)
time.sleep(pause if snake.direction[1] == 0 else pause * 2)
p = time.time()
if keys:
key = keys.pop().lower()
if key == '\033':
break
elif key in directions_codes:
snake.direction = directions_codes[key]
#print(tuple(keys))
elif developer_mode:
snake.direction = (0, 0)
if not update_screen():
break
elif not developer_mode:
if not update_screen():
break
finally: # except KeyboardInterrupt
show_cursor()