-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.py
168 lines (125 loc) · 4.32 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
#!/usr/bin/env python
from samplebase import SampleBase
import random
import time
import termios
import sys
import tty
#def _find_getch():
'''
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
'''
old_settings=None
def init_anykey():
global old_settings
old_settings = termios.tcgetattr(sys.stdin)
new_settings = termios.tcgetattr(sys.stdin)
new_settings[3] = new_settings[3] & ~(termios.ECHO | termios.ICANON) # lflags
new_settings[6][termios.VMIN] = 0 # cc
new_settings[6][termios.VTIME] = 0 # cc
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, new_settings)
#@atexit.register
def term_anykey():
global old_settings
if old_settings:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
def anykey():
ch_set = []
ch = os.read(sys.stdin.fileno(), 1)
while ch != None and len(ch) > 0:
ch_set.append( ord(ch[0]) )
ch = os.read(sys.stdin.fileno(), 1)
return ch_set;
#return _getch
class snake(object):
def __init__(self):
self.gameScreen = []
self.snakeX = 16
self.snakeY = 16
self.snakeXdelta = 1
self.snakeYdelta = 0
self.oldXdelta = self.snakeXdelta
self.oldYdelta = self.snakeYdelta
self.alive = True;
self.currentDotX = 5
self.currentDotY = 10
self.snakeBody = [[16,16]]
return
def cycle(self):
if(self.alive):
self.checkDotEat()
self.getUserInput()
self.snakeX = (self.snakeX + self.snakeXdelta)%32
self.snakeY = (self.snakeY + self.snakeYdelta)%32
self.checkUserDeath()
self.updateSnake()
self.updateScreen()
return
def updateScreen(self):
self.gameScreen = [[0 for x in range(32)] for y in range(32)]
#for each element of the snake body, draw a 1 on the matrix
for x in range(0, len(self.snakeBody)):
self.gameScreen[self.snakeBody[x][0]][self.snakeBody[x][1]] = 1
#draw the pellet
self.gameScreen[self.currentDotX][self.currentDotY] = 1
return
def updateSnake(self):
for x in range(0, len(self.snakeBody)-1):
self.snakeBody[x+1] = self.snakeBody[x]
self.snakeBody[0] = [self.snakeX, self.snakeY]
return
def getUserInput(self):
key = anykey()
if key == None:
print("nothing")
elif key == '^c':
self.alive = False
elif key == 'w':
print("got a w!")
return
def checkDotEat(self):
if((self.snakeX == self.currentDotX) & (self.snakeY == self.currentDotY)):
self.snakeBody.append([-1,-1])
self.currentDotX = (random.random() * 100) % 32
self.currentDotY = (random.random() * 100) % 32
return
def checkUserDeath(self):
for x in range(1, len(self.snakeBody)):
if ((self.snakeX == self.snakeBody[x][0]) | (self.snakeY == self.snakeBody[x][1])):
self.alive = False
return
def getGameScreen(self):
return self.gameScreen
class colorMatrix(SampleBase):
def __init__(self, *args, **kwargs):
self.snake = snake()
super(colorMatrix, self).__init__(*args, **kwargs)
def run(self):
#create virtual matrix
offset_canvas = self.matrix.CreateFrameCanvas()
tmp_matrix = snake.getGameScreen
#forever
while True:
self.snake.cycle()
tmp_matrix = self.snake.getGameScreen()
color = 0
for x in range(0, self.matrix.width):
for y in range(0, self.matrix.height):
#column, row, red, blue, green
color = tmp_matrix[x][y]
offset_canvas.SetPixel(x, y, 255*color, 0, 0)
offset_canvas = self.matrix.SwapOnVSync(offset_canvas)
# Main function
if __name__ == "__main__":
#Create colorMatrix
mat = colorMatrix()
if (not mat.process()):
print('broked')