-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2pSnake.py
237 lines (187 loc) · 7.84 KB
/
2pSnake.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
#!/usr/bin/env
import curses
import random
import time
import copy
def draw(stdscr):
key = 0
timer = 0
winner = None
players = []
pellets = []
height, width = stdscr.getmaxyx()
width = width // 2
stdscr.clear()
stdscr.refresh()
curses.start_color()
green = 1
magenta = 2
red = 3
cyan = 4
yellow = 5
wintext = 6
black = 7
blue = 8
white = 9
curses.init_pair(green, curses.COLOR_GREEN, curses.COLOR_GREEN)
curses.init_pair(magenta, curses.COLOR_MAGENTA, curses. COLOR_MAGENTA)
curses.init_pair(red, curses.COLOR_RED, curses.COLOR_RED)
curses.init_pair(cyan, curses.COLOR_CYAN, curses.COLOR_CYAN)
curses.init_pair(yellow, curses.COLOR_YELLOW, curses.COLOR_YELLOW)
curses.init_pair(black, curses.COLOR_BLACK, curses.COLOR_BLACK)
curses.init_pair(blue, curses.COLOR_BLUE, curses.COLOR_BLUE)
curses.init_pair(white, curses.COLOR_WHITE, curses.COLOR_WHITE)
curses.init_pair(wintext, curses.COLOR_YELLOW, curses.COLOR_CYAN)
stdscr.nodelay(1)
class pellet:
def __init__(self):
self.coords = [random.randint(1, width-3), random.randint(1, height-2)]
def draw(self):
stdscr.attron(curses.color_pair(cyan))
stdscr.addstr(self.coords[1], self.coords[0] * 2, " ")
stdscr.attroff(curses.color_pair(cyan))
def regen(self):
self.coords = [random.randint(1, width-3), random.randint(1, height-2)]
class snake:
def __init__(self, name, control_string, color, seccolor, init_direction, init_coords, init_length):
self.name = name
self.control_string = control_string
self.color = color
self.sec_color = seccolor
self.direction = init_direction
self.head_coords = init_coords
self.length = init_length
self.color_counter = 0
self.snake_trail = []
def move(self):
if self.direction == "up":
self.head_coords[1] = self.head_coords[1] - 1
elif self.direction == "down":
self.head_coords[1] = self.head_coords[1] + 1
elif self.direction == "left":
self.head_coords[0] = self.head_coords[0] - 1
elif self.direction == "right":
self.head_coords[0] = self.head_coords[0] + 1
self.head_coords[0] = max(0, self.head_coords[0])
self.head_coords[0] = min(width-2, self.head_coords[0])
self.head_coords[1] = max(0, self.head_coords[1])
self.head_coords[1] = min(height-1, self.head_coords[1])
def update_tail(self):
self.snake_trail.append(copy.copy(self.head_coords))
if len(self.snake_trail) >= self.length:
del self.snake_trail[0]
def draw(self):
for coord in self.snake_trail[::-1]:
stdscr.attron(curses.color_pair(white))
stdscr.addstr(self.head_coords[1], self.head_coords[0]*2, " ")
stdscr.attroff(curses.color_pair(white))
if self.color_counter == 0:
stdscr.attron(curses.color_pair(self.sec_color))
stdscr.addstr(coord[1], coord[0]*2, " ")
stdscr.attroff(curses.color_pair(self.sec_color))
self.color_counter = 1
elif self.color_counter == 1:
stdscr.attron(curses.color_pair(self.color))
stdscr.addstr(coord[1], coord[0]*2, " ")
stdscr.attroff(curses.color_pair(self.color))
self.color_counter = 2
elif self.color_counter == 2:
stdscr.attron(curses.color_pair(self.color))
stdscr.addstr(coord[1], coord[0]*2, " ")
stdscr.attroff(curses.color_pair(self.color))
self.color_counter = 0
def check_for_collisions(self):
if self.head_coords[1] == 0 or self.head_coords[1] == height - 1:
return True
if self.head_coords[0] == 0 or self.head_coords[0] == width - 2:
return True
for tail in self.snake_trail:
if self.head_coords == tail:
return True
for player in players:
for tail in player.snake_trail[:len(player.snake_trail)-1]:
if self.head_coords == tail:
return True
for pellet in pellets:
if self.head_coords == pellet.coords:
self.length = self.length + 6
pellet.regen()
def key_check(self, key):
if self.control_string != "ARROW":
if key == ord(self.control_string[2]):
if self.direction != "up":
self.direction = "down"
elif key == ord(self.control_string[0]):
if self.direction != "down":
self.direction = "up"
elif key == ord(self.control_string[3]):
if self.direction != "left":
self.direction = "right"
elif key == ord(self.control_string[1]):
if self.direction != "right":
self.direction = "left"
else:
if key == curses.KEY_DOWN:
if self.direction != "up":
self.direction = "down"
elif key == curses.KEY_UP:
if self.direction != "down":
self.direction = "up"
elif key == curses.KEY_RIGHT:
if self.direction != "left":
self.direction = "right"
elif key == curses.KEY_LEFT:
if self.direction != "right":
self.direction = "left"
def auto(self):
self.update_tail()
self.move()
self.draw()
def draw_border():
stdscr.attron(curses.color_pair(red))
for y in range(0,height):
stdscr.addstr(y,0, " ")
stdscr.addstr(y, (width*2)-4, " ")
for x in range(2,width):
stdscr.addstr(0, (x * 2) - 4, " ")
stdscr.addstr(height-1, (x * 2) - 4 , " ")
stdscr.attroff(curses.color_pair(red))
players.append(snake("Green", "wasd", green, red, "right", [1,1], 10))
players.append(snake("Yellow", "ARROW", yellow, cyan, "left", [width-3, height-2], 10))
players.append(snake("Blue", "ijkl", blue, magenta, "down", [width-3, 1], 10))
pellets.append(pellet())
while key != ord('q'):
stdscr.clear()
if winner == None:
for player in range(0, len(players)):
players[player].key_check(key)
for player in range(0, len(players)):
players[player].auto()
for pelletz in pellets:
pelletz.draw()
if timer >= 5:
pellets.append(pellet())
timer = 0
draw_border()
to_delete = []
for player in range(0, len(players)):
if players[player].check_for_collisions() == True:
to_delete.append(player)
for delt in to_delete[::-1]:
del players[delt]
if len(players) == 1:
winner = players[0].name
else:
stdscr.clear()
win_message = str(winner + " won")
stdscr.attron(curses.color_pair(6))
stdscr.addstr(height//2, width - (len(win_message)/2), win_message, curses.A_BLINK)
stdscr.attroff(curses.color_pair(6))
stdscr.refresh()
key = stdscr.getch()
time.sleep(.05)
timer = timer + .05
def main():
curses.wrapper(draw)
if __name__ == "__main__":
main()