-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.py
More file actions
141 lines (112 loc) · 4.82 KB
/
Snake.py
File metadata and controls
141 lines (112 loc) · 4.82 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
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
# Thanks sanchitgangwar @GitHub for the initial Interface of the Snake Game
# SNAKES GAME
# Use ARROW KEYS to play, SPACE BAR for pausing/resuming and Esc Key for exiting
import curses
from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
from random import randint
H = 20 # Final Constant Height
W = 80 # Final Constant Width
GAMESPEED = 5 # The initial GameSpeed
# Initialize Interface
curses.initscr()
win = curses.newwin(H, W, 0, 0)
win.keypad(1)
curses.noecho()
curses.curs_set(0)
win.border(0)
win.nodelay(1)
key = KEY_RIGHT # Initializing values
score = 0
init_x = randint(5, H - 5)
init_y = randint(5, W - 5)
food_x = randint(3, H - 3)
food_y = randint(3, W - 3)
snake = [[init_x, init_y],
[init_x, init_y - 1],
[init_x, init_y - 2]] # Initial snake co-ordinates
food = [food_x, food_y] # First food co-ordinates
win.addch(food[0], food[1], '#') # Prints the food
file1 = open("coordinates.txt", "wb")
count = 0
while key != 27: # While Esc key is not pressed
win.border(0)
win.addstr(0, 2, ' Score : ' + str(score) + ' ') # Printing 'Score' and
win.addstr(0, W / 2 - 2, ' SNAKE ') # 'SNAKE' strings
win.timeout(GAMESPEED) # Set Speed of the game to be constant
prevKey = key # Previous key pressed
event = win.getch()
key = key if event == -1 else event
# Prevent the situation if the food the directlly behind the snake,
# it might kill itself
if snake[0][0] - 1 == snake[1][0] and key == KEY_UP:
key = KEY_DOWN
elif snake[0][0] + 1 == snake[1][0] and key == KEY_DOWN:
key = KEY_UP
elif snake[0][1] - 1 == snake[1][1] and key == KEY_LEFT:
key = KEY_RIGHT
elif snake[0][1] + 1 == snake[1][1] and key == KEY_RIGHT:
key = KEY_LEFT
if key == ord(' '): # If SPACE BAR is pressed, wait for another
key = -1 # one (Pause/Resume)
while key != ord(' '):
key = win.getch()
key = prevKey
continue
if key not in [KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, 27]: # If an invalid key is pressed
key = prevKey
# Calculates the new coordinates of the head of the snake. NOTE: len(snake) increases.
# This is taken care of later at [1].
snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1),
snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])
# If snake crosses the boundaries, make it enter from the other side
if snake[0][0] == 0: snake[0][0] = H - 2
if snake[0][1] == 0: snake[0][1] = W - 2
if snake[0][0] == H - 1: snake[0][0] = 1
if snake[0][1] == W - 1: snake[0][1] = 1
if snake[0] == food: # When snake eats the food
food = []
score += 1
while food == []:
food = [randint(1, H - 2), randint(1, W - 2)] # Calculating next food's coordinates
if food in snake: food = []
win.addch(food[0], food[1], '#')
else:
last = snake.pop() # [1] If it does not eat the food, length decreases
win.addch(last[0], last[1], ' ')
win.addch(snake[0][0], snake[0][1], '*')
# The following four if/else statement is used to determine where is
# the food and where the snake should go.
# There is several bad thing about these lines of code:
# If the next food is on the same line with the snake, break
# The snake do not have its own selfawareness
# Other problems are still waiting for determination
if snake[0][0] == food[0] and snake[0][1] > food[1]:
key = KEY_LEFT
elif snake[0][0] == food[0] and snake[0][1] < food[1]:
key = KEY_RIGHT
elif snake[0][1] == food[1] and snake[0][0] > food[0]:
key = KEY_UP
elif snake[0][1] == food[1] and snake[0][0] < food[0]:
key = KEY_DOWN
# all_x_cor = [];
# all_y_cor = [];
# for i in range(1, len(snake) - 1):
# all_x_cor.append(snake[i][0])
# all_y_cor.append(snake[i][1])
# if snake[0][0] + 1 in all_x_cor:
# key = KEY_LEFT
# elif snake[0][0] - 1 in all_x_cor:
# key = KEY_RIGHT
# elif snake[0][1] + 1 in all_y_cor:
# key = KEY_UP
# elif snake[0][1] - 1 in all_y_cor:
# key = KEY_DOWN
count += 1 # Keeping tracking of the coordinates of the Game
file1.write('\ncoordinates ' + str(count) + str(snake[:]) + '\n')
# If snake runs over itself
# This line of code contains a lot of problems I think
if snake[0] in snake[1:]:
break
curses.endwin()
print("\nScore - " + str(score))
file1.close()