-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
339 lines (276 loc) · 8.73 KB
/
Game.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import os
import sys
import random
import pygame
import conf
from Ui import Ui
from Joystick import Joystick
from PopUp import PopUp
from TouchScreen import TouchScreen
from Box import Box
class game(object):
def __init__(self):
self.screen = None
self.playerBox = None
self.ui = None
self.iUi = None
self.popUp = None
self.touchScreen = None
# Game parameters
self.SCREEN_WIDTH = conf.SCREEN_WIDTH
self.SCREEN_HEIGHT = conf.SCREEN_HEIGHT
self.DRAW_RECT = None # must be set after pygame initialisation
self.BG_COLOR = conf.BG_COLOR
self.BLOCKSIZE = conf.BLOCKSIZE
self.SNACKS = conf.SNACKS
self.REST_WITH = self.SCREEN_WIDTH % self.BLOCKSIZE
self.REST_HEIGHT = self.SCREEN_HEIGHT % self.BLOCKSIZE
self.gameSpeed = 250
self.gameSpeedFactors = range(0, 400, 25)
self.gameSpeedFactor = 0
self.joystickInteract = None # must be set after pygame initialisation
self.elements = []
self.haveToAdd = []
def move(self, direction):
if self.playerBox is not None:
curDir = self.playerBox.getDirection()
# DONT move backwards!
if (curDir == 1 and direction == 3) or (curDir == 3 and direction == 1):
pass
elif (curDir == 2 and direction == 4) or (curDir == 4 and direction == 2):
pass
else:
self.playerBox.setDirection(direction)
def getLastElement(self):
tmp = self.playerBox
found = False
while found is False:
if tmp.back is None:
found = True
else:
tmp = tmp.back
return tmp
def getBodyLen(self):
tmp = self.playerBox
cnt = 0
while tmp is not None:
tmp = tmp.back
cnt += 1
return cnt
def fieldContainsBox(self, x, y):
for elem in self.elements:
if elem.x == x and elem.y == y:
return True
return False
def addSnack(self, elements):
snackCount = 0
coords = []
for elem in elements:
coords.append([elem.x, elem.y])
if elem.bType == 'snack':
snackCount += 1
#print 'snackCount', snackCount
if snackCount < self.SNACKS:
xy = [
random.randint(1, (self.SCREEN_WIDTH / self.BLOCKSIZE)-1),
random.randint(1, (self.SCREEN_HEIGHT / self.BLOCKSIZE)-1)
]
if not xy in coords:
snack = Box(self.screen, self.BLOCKSIZE, xy[0], xy[1])
snack.bType = 'snack'
snack.color = conf.SNACK_COLOR
snack.direction = 0
elements.append(snack)
def eatSnack(self, elements):
for elem in elements:
if elem.bType == 'snack' and elem.x == self.playerBox.x and elem.y == self.playerBox.y:
elements.remove(elem)
return self.playerBox.x, self.playerBox.y
return None
def headDied(self, elements):
dead = False
# collision with myself or stuff
for elem in elements:
if elem.bType != 'head' and elem.bType != 'snack' and elem.bType:
if self.playerBox.x == elem.x and self.playerBox.y == elem.y:
dead = True
# border collision
if self.playerBox.x < 0 or self.playerBox.y < 0 \
or self.playerBox.x > (self.SCREEN_WIDTH / self.BLOCKSIZE)-1 \
or self.playerBox.y > (self.SCREEN_HEIGHT / self.BLOCKSIZE)-1:
dead = True
if dead:
highscores = '0'
scores = self.getBodyLen()
if os.path.isfile("highscore.txt") is True:
f = open("highscore.txt", 'r')
highscores = f.read()
f.close()
if scores > int(highscores):
highscores = str(scores)
f = open("highscore.txt", 'w')
f.write(highscores)
f.close()
self.iUi.addSimpleMenu("gameOver",
[
"GAME OVER", "POINTS: " + str(scores), "HIGHSCORE "+highscores,
"Button / Keyboard: 2 = Speed Up",
"Button / Keyboard: 1 = Speed Down",
"R = restart / respawn",
"Q = Quit / Exit"
],
color=conf.FONT_COLOR
)
self.iUi.draw("gameOver")
return dead
def resetGame(self):
self.screen.fill((0, 0, 0))
self.playerBox = Box(self.screen, self.BLOCKSIZE, 3, 3)
self.playerBox.bType = 'head'
self.elements = []
self.elements.append(self.playerBox)
self.haveToAdd = [[3, 3], [3, 3], [3, 3]]
def gameSpeedUp(self):
self.gameSpeedFactor += 1
if self.gameSpeedFactor >= len(self.gameSpeedFactors):
self.gameSpeedFactor -= 1
self.speedChanged()
def gameSpeedDown(self):
self.gameSpeedFactor -= 1
if self.gameSpeedFactor < 0:
self.gameSpeedFactor = 0
self.speedChanged()
def speedChanged(self):
oneBased = self.gameSpeedFactor + 1
txt = "|" * oneBased
txt = "[" + txt.ljust(len(self.gameSpeedFactors)) + "]"
self.popUp.singlePopUp(txt)
def run_game(self):
pygame.init()
self.DRAW_RECT = pygame.Rect(0, 0, self.SCREEN_WIDTH - self.REST_WITH, self.SCREEN_HEIGHT - self.REST_HEIGHT)
# do fancy window stuff
pygame.display.set_caption("pySnake")
pygame.mouse.set_visible(False)
if not conf.FULLSCREEN:
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (conf.WINDOW_POSITION_X, conf.WINDOW_POSITION_Y)
if not conf.WINDOW_BORDER:
self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT), pygame.NOFRAME, 32)
if self.screen is None:
self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT), 0, 32)
if conf.FULLSCREEN:
pygame.display.toggle_fullscreen()
clock = pygame.time.Clock()
redrawCount = 0
# init the menu, add stuff later
self.iUi = Ui(self.screen)
self.popUp = PopUp(self.screen)
self.popUp.color = conf.FONT_COLOR
self.touchScreen = TouchScreen(self.SCREEN_WIDTH, self.SCREEN_HEIGHT)
pygame.joystick.init()
self.joystickInteract = Joystick()
keymap = {
pygame.K_UP: 1,
pygame.K_RIGHT: 2,
pygame.K_DOWN: 3,
pygame.K_LEFT: 4
}
self.playerBox = None
self.elements = []
self.haveToAdd = []
# The main game loop
gameOver = False
doMove = -1
while True:
if self.playerBox is None:
self.resetGame()
# Limit frame speed to 50 FPS
time_passed = clock.tick(50)
redrawCount += time_passed
if self.joystickInteract.joystickAvailable():
self.joystickInteract.processAxis()
joyAction = self.joystickInteract.getAction()
if joyAction == "move":
doMove = self.joystickInteract.getMoveAction()
elif joyAction == "speedUp":
self.gameSpeedUp()
elif joyAction == "speedDown":
self.gameSpeedDown()
elif joyAction == "restart":
self.resetGame()
gameOver = False
elif joyAction == 'quit':
self.exit_game()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.exit_game()
elif event.type == pygame.KEYDOWN:
if event.key in keymap:
doMove = keymap[event.key]
elif event.key == pygame.K_2: # speed up game
self.gameSpeedUp()
elif event.key == pygame.K_1: # slow down up game
self.gameSpeedDown()
elif event.key == pygame.K_r: # restart game
self.resetGame()
gameOver = False
elif event.key == pygame.K_q:
self.exit_game()
else:
print("event.key:", event.key)
else:
pass
#print event
if conf.TOUCH_SCREEN:
mouseAction = self.touchScreen.getEventBoxes()
if mouseAction and mouseAction > 0:
if gameOver:
self.resetGame()
gameOver = False
else:
doMove = mouseAction
if gameOver is False and redrawCount >= (self.gameSpeed - self.gameSpeedFactors[self.gameSpeedFactor]):
# ONLY move, when the timer elapses!
# otherwise you could change the direction multiple times before the scenery changes and updates
# strange shit goes on!
if doMove != -1:
self.move(doMove)
doMove = -1
redrawCount = 0
self.screen.fill(self.BG_COLOR, self.DRAW_RECT)
# move the elements
for elem in reversed(self.elements):
elem.update()
# add elements BEFORE blit is called and they change direction!
# WEIRD stuff would happen otherwise!!!1!!!!!!!!
if len(self.haveToAdd) > 0:
for i in range(len(self.haveToAdd)):
coords = self.haveToAdd[i]
if self.fieldContainsBox(coords[0], coords[1]) is False:
lastElem = self.getLastElement()
lastElem.back = Box(self.screen, self.BLOCKSIZE, coords[0], coords[1])
lastElem.back.setDirection(lastElem.getDirection())
self.elements.append(self.getLastElement())
self.haveToAdd.pop(i)
break
else:
# if there is NOTHING to add to the Snake, add a new snack, if needed
# preventing from spawning a snack inside the "new" tail of the snake and shit
self.addSnack(self.elements)
# update elements
for elem in reversed(self.elements):
elem.blit()
# draw touchscreen
# TODO: draw touch areas
# draw popups
self.popUp.drawPopUps()
# if a snack has been eaten, add it to the to add list
snackEaten = self.eatSnack(self.elements)
if snackEaten is not None:
self.haveToAdd.append(snackEaten)
self.popUp.singlePopUp(str(self.getBodyLen()))
# collision!
if self.headDied(self.elements):
gameOver = True
pygame.display.flip()
def exit_game(self):
sys.exit()