-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnake.py
241 lines (165 loc) · 5.43 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
238
239
240
241
from random import randint
from sys import path
# NOTE: This weird import down here only looks this way because the module
# is located at the root . directory, while this file is at ./examples.
# Usually you would simply use `from eel import Eel`, with none of that
# Down there.
ATTEMPTS = 4
for i in range(ATTEMPTS):
try:
from eelengine import Eel, keyPressed, Canvas
from eelengine.figure import Rectangle, Line
from eelengine.shader import Shader
break
except ModuleNotFoundError:
path.insert(0, '../' * (i+1))
WIDTH, HEIGHT = 640, 480
SQ = 32
GAME_END = False
VSYNC = False # Setting this to false will likely cause screen flickering
class Snake:
dirs = {
b'W': ( 0, -1),
b'A': (-1, 0),
b'S': ( 0, 1),
b'D': ( 1, 0)
}
def __init__(self, x, y, size=3, grid=SQ, width=WIDTH, height=HEIGHT):
self.body = [
Rectangle(x + grid*i, y, width=grid, height=grid, fill=True)
for i in range(size)
]
self.dir = Snake.dirs[b'A']
self.max = (round(width/grid-1)*grid, round(height/grid-1)*grid)
self.grid = grid
self.olddir = self.dir
def grow(self):
self.body.append(None)
def step(self) -> bool:
# Returns wether the game ends or not
self.olddir = self.dir
nextpos = (
self.head.x + self.dir[0] * self.grid,
self.head.y + self.dir[1] * self.grid
)
for i, v in enumerate(nextpos):
if v < 0 or v > self.max[i]:
return True
for i in range(1, len(self.body)):
v = self.body[i]
if v and (nextpos == v.pos).all():
return True
for i in range(1, len(self.body))[::-1]:
v = self.body[i]
if v:
v.xy = self.body[i-1].pos
else:
v = self.body[i-1]
self.body[i] = Rectangle(
v.x, v.y, width=self.grid,
height=self.grid, fill=True
)
self.body[0].xy = nextpos
def input(self, inp):
pot = Snake.dirs[inp]
if not (pot[0] == -self.olddir[0] and pot[1] == -self.olddir[1]):
self.dir = pot
def drawTo(self, target):
for i in self.body:
if i:
# i.setColor(0, 200, 0)
i.setColor(200, 200, 200)
i.drawTo(target)
def getHead(self):
return self.body[0]
def getTail(self):
return self.body[-1]
def __len__(self):
return len(self.body)
def __iter__(self):
return iter(self.body)
head = property(getHead)
tail = property(getTail)
game = Eel(name="Snake", width=WIDTH, height=HEIGHT, vsync=VSYNC)
global player, apple, maxtimer, timer
player, apple, maxtimer, timer = None, None, None, None
# To be applied in this order
global shadernames, shaders, canvases, ccount
shadernames = (b'crt.frag', b'chroma.frag', b'vignette.frag')
# shadernames = (b'pass.frag',)
shaders = None
canvases = None
ccount = 0
def newApple():
global apple, player
done = False
while not done:
done = True
apple.xy = (randint(0, player.max[0]/SQ)*SQ, randint(0, player.max[1]/SQ)*SQ)
for segment in player:
if segment and (apple.pos == segment.pos).all():
done = False
@game.load
def startGame(eel):
global player, apple, maxtimer, timer, GAME_END, shaders, canvases, shadernames
player = Snake(round(WIDTH/2 / SQ)*SQ, round((HEIGHT - SQ)/2 / SQ)*SQ)
apple = Rectangle(0, 0, width=SQ, height=SQ, fill=True)
apple.setColor(200, 0, 0)
newApple()
maxtimer = 4
timer = maxtimer
GAME_END = False
if shaders is None:
shaders = []
for v in shadernames:
sh = Shader(b'pass.vert', v)
with sh:
sh.setUniform(b'canvasTexture', (0,))
sh.setUniform(b'resolution', eel.dimensions)
shaders.append(sh)
if canvases is None:
canvases = [Canvas(*eel.dimensions) for i in range(2)]
@game.draw
def gameLogic(eel):
global player, maxtimer, timer, GAME_END, canvases, shaders, lines, ccount
if VSYNC: timer -= 1 * (not GAME_END)
else:
fps = eel.fps or 60
timer -= (1 / fps * 60 * (not GAME_END))
if keyPressed(256): exit()
if keyPressed(b"R"):
startGame(eel)
GAME_END = False
if timer <= 0:
for k, v in Snake.dirs.items():
if keyPressed(k): player.input(k)
GAME_END = player.step()
timer = maxtimer
if (apple.pos == player.head.pos).all():
player.grow()
newApple()
for c in canvases: c.clear()
player.drawTo(canvases[0])
apple.drawTo(canvases[0])
for l in lines: l.drawTo(canvases[0])
for i, sh in enumerate(shaders):
with sh:
ccount = i&1
canvases[ccount].drawTo(canvases[1-ccount])
ccount = (i+1)&1
canvases[ccount].drawTo(eel)
global lines
lines = []
for y in range(0, HEIGHT, SQ):
l = Line(0, y, xp=WIDTH, yp=y)
l.setColor(50, 50, 50, 50)
lines.append(l)
for x in range(0, WIDTH, SQ):
l = Line(x, 0, xp=x, yp=HEIGHT)
l.setColor(50, 50, 50, 50)
lines.append(l)
# @game.draw
# def drawGrid(eel):
# global lines
# for l in lines: l.drawTo(eel)
game.run()