-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsnake.py
executable file
·237 lines (206 loc) · 5.49 KB
/
snake.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 python
# -*- coding: utf-8 -*-
#
# Version: 0.2
# Author: digit (markus)
# Git: king-wee-wee
#
import acabsl
from random import randint
import time
from threading import Thread
#import pygame
#from pygame.locals import *
import serial
width = acabsl.WALLSIZEX
height = acabsl.WALLSIZEY
wait = 0.33
class posOutOfMapError(Exception):
def __init__(self):
Exception.__init__(self)
class posInTailError(Exception):
def __init__(self):
Exception.__init__(self)
class Direction:
def __init__(self,x,y):
self.direction = (x,y)
def getNewPos(self,pos):
return self.checkPos([pos[0]+self.direction[0],pos[1]+self.direction[1]])
def alarm(self):
raise posOutOfMapError
def checkPos(self,pos):
return (pos if pos[0]>=0 and pos[0]<width and pos[1]>=0 and pos[1]<height else self.alarm())
south = Direction(0,-1)
north = Direction(0,1)
east = Direction(1,0)
west = Direction(-1,0)
directions = [south,west,north,east]
tail = (122,122,122)
head = (0,0,255)
foodposition = []
foodcolor = (255,0,0)
class Player(Thread):
def __init__(self,x=width/2,y=height/2-1):
Thread.__init__(self)
self.position = [x,y]
self.tail = []
self.deltail = []
self.running = False
self.direction = south
self.error = False
def run(self):
redrawAll(self)
seefood = 0
while 1:
seefood += 1
drawMap(self,seefood%(2))
time.sleep(wait)
try:
self.goForward()
except posInTailError,posOutOfMapError:
self.error = True
break
except Exception:
self.error = True
break
#<unused>
def changeRight(self):
self.changeBoth(+1)
def changeLeft(self):
self.changeBoth(-1)
def changeBoth(self,direction):
self.direction = (0 if self.direction >= len(directions)-1 else (len(directions)-1 if self.direction <= 0 else self.direction+direction))
#</unused>
def changeDirection(self,direction):
if direction.getNewPos(self.position) not in self.tail:
self.direction = direction
def goForward(self):
global wait
self.tail = [self.position]+self.tail
if self.position != foodposition:
self.deltail = self.tail[-1]
del self.tail[-1]
else:
newFoodPosition(self)
redrawAll(player)
wait /= 1.1
acabsl.send(self.position[0],self.position[1],*tail,t=wait)
self.position = self.direction.getNewPos(self.position)
for i in self.tail:
if self.position == i:
raise posInTailError
"""if self.position != foodposition:
self.deltail = self.tail[-1]
del self.tail[-1]
else:
newFoodPosition(self)
redrawAll(player)
wait /= 1.1"""
def redrawAll(player,seefood=True):
acabsl.update()
for i in xrange(width):
for j in xrange(height):
acabsl.send(i,j,0,0,0)
acabsl.update()
for i in player.tail:
acabsl.send(i[0],i[1],*tail)
acabsl.send(player.position[0],player.position[1],*head)
if seefood:
acabsl.send(foodposition[0],foodposition[1],*foodcolor,t=wait)
acabsl.update()
def drawMap(player,seefood):
acabsl.send(player.position[0],player.position[1],*head)
if player.deltail:
acabsl.send(player.deltail[0],player.deltail[1],0,0,0)
if seefood:
acabsl.send(foodposition[0],foodposition[1],*foodcolor,t=wait)
else:
acabsl.send(foodposition[0],foodposition[1],0,0,0,wait)
acabsl.update()
def newFoodPosition(player):
global foodposition
while not foodposition or foodposition in player.position or foodposition in player.tail:
foodposition = [randint(0,width-1),randint(0,height-1)]
def pygameInputHandler(player):
go = False
pygame.init()
screen = pygame.display.set_mode((10,10))
pygame.display.set_caption("Pygame Caption")
pygame.mouse.set_visible(0)
while not go:
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key == K_DOWN:
go = True
player.start()
while 1:
if player.error:
print "Game Over"
print "Tail length: %d" % len(player.tail)
break
c = ""
#temporary
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key == K_LEFT:
c = "l"
elif event.key == K_RIGHT:
c = "r"
elif event.key == K_UP:
c = "u"
elif event.key == K_DOWN:
c = "d"
if c:
if c == "u":
player.changeDirection(north)
elif c == "r":
player.changeDirection(east)
elif c == "l":
player.changeDirection(west)
elif c == "d":
player.changeDirection(south)
else:
pass
time.sleep(wait/10)
def rad1oInputHandler(player):
UP = 1
DOWN = 2
LEFT = 4
RIGHT = 8
ENTER = 16
direction_mapping = {UP:north, DOWN:south, LEFT:west, RIGHT:east}
go = False
ser = serial.Serial('/dev/rad1o', 115200,timeout=1)
while True:
try:
event, id1, id2 = [ord(x) for x in ser.read(3)]
except ValueError:
continue
if not go:
if event == ENTER:
go = True
player.start()
continue
if player.error:
print "Game Over"
print "Tail length: %d" % len(player.tail)
break
#temporary
if event in direction_mapping:
player.changeDirection(direction_mapping[event])
time.sleep(wait/10)
ser.close()
def setupGame():
global wait
player = Player()
newFoodPosition(player)
redrawAll(player)
wait = 0.33
return player
while True:
try:
player = setupGame()
rad1oInputHandler(player)
#pygameInputHandler(player)
except KeyboardInterrupt:
break