Skip to content

Commit 28f8d87

Browse files
committed
div., game speed bar, current points
1 parent 621cc5a commit 28f8d87

3 files changed

Lines changed: 86 additions & 22 deletions

File tree

joystick.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def joystickAvailable(self):
2222
else:
2323
return False
2424

25-
def haveAction(self):
25+
def getAction(self):
26+
action = ""
2627
if self.myJoystick != None:
2728
try:
2829
xAx = 0
@@ -44,27 +45,29 @@ def haveAction(self):
4445
self.doMove = 1
4546

4647
if self.myJoystick.get_button(0) and self.joyButtonDown == False: # speed up
47-
return "speedUp"
48+
action = "speedUp"
4849
elif self.myJoystick.get_button(1) and self.joyButtonDown == False: # speed down
49-
return "speedDown"
50+
action = "speedDown"
5051
elif (self.myJoystick.get_button(9) and self.joyButtonDown == False)\
5152
or (self.myJoystick.get_button(3) and self.joyButtonDown == False): # (re)start
52-
return "restart"
53-
else:
54-
# make sure NO button is down for reset
55-
someJoyButtonDown = False
56-
for i in range(0, self.myJoystick.get_numbuttons()):
57-
if (self.myJoystick.get_button(i)):
58-
someJoyButtonDown = True
59-
self.joyButtonDown = someJoyButtonDown
53+
action = "restart"
54+
elif (self.myJoystick.get_button(8) and self.joyButtonDown == False):
55+
action = 'quit'
56+
#else:
57+
# make sure NO button is down for reset
58+
someJoyButtonDown = False
59+
for i in range(0, self.myJoystick.get_numbuttons()):
60+
if (self.myJoystick.get_button(i)):
61+
someJoyButtonDown = True
62+
self.joyButtonDown = someJoyButtonDown
6063

6164
if self.doMove != -1:
62-
return "move"
65+
action = "move"
6366
except Exception as ex:
6467
print('JOYSTICK ERROR, DEACTIVATING:' + str(ex))
6568
self.myJoystick = None
6669

67-
return ""
70+
return action
6871

6972
def getMoveAction(self):
7073
tmp = self.doMove

popUp.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pygame
2+
import time
3+
class popUp(object):
4+
def __init__(self, screen):
5+
self._screen = screen
6+
self.fnt = pygame.font.SysFont("MS Comic Sans" , 30)
7+
self.color = (245, 101, 44) # orange ;)
8+
self.popUps = []
9+
self.defaultDuration = 3
10+
11+
12+
def singlePopUp(self, txt, duration=0):
13+
if duration == 0:
14+
duration = self.defaultDuration
15+
self.popUps = []
16+
self.popUps.append({"txt":txt, "start":time.time(), "duration":duration})
17+
18+
def addPopUp(self, txt, duration=0):
19+
if duration == 0:
20+
duration = self.defaultDuration
21+
self.popUps.append({"txt":txt, "start":time.time(), "duration":duration})
22+
23+
def drawPopUps(self):
24+
if len(self.popUps) == 0:
25+
return False
26+
27+
fullText = ""
28+
for i in range(len(self.popUps)):
29+
if time.time() > self.popUps[i]['start'] + self.popUps[i]['duration']:
30+
self.popUps.pop(i)
31+
break
32+
33+
for popUp in self.popUps:
34+
fullText += popUp['txt'] + " "
35+
36+
# TODO: multilines!
37+
# http://stackoverflow.com/questions/2770886/pygames-message-multiple-lines
38+
if fullText != "":
39+
xPos = (self._screen.get_width() / 2)
40+
self._screen.blit(self.fnt.render(fullText, 1, self.color), ( xPos - (self.fnt.size(fullText)[0] / 2), 5))
41+
42+
return True

snake.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pygame
88
from ui import ui
99
from joystick import joystick
10+
from popUp import popUp
1011
from pygame.sprite import Sprite
1112

1213
#sys.stdout = os.devnull
@@ -56,6 +57,7 @@ def __init__(self):
5657
self.screen = None
5758
self.playerBox = None
5859
self.ui = None
60+
self.popUp = None
5961

6062

6163
def move(self, direction):
@@ -164,11 +166,20 @@ def gameSpeedUp(self):
164166
self.gameSpeedFactor += 1
165167
if self.gameSpeedFactor >= len(self.gameSpeedFactors):
166168
self.gameSpeedFactor -= 1
169+
self.speedChanged()
167170

168171
def gameSpeedDown(self):
169172
self.gameSpeedFactor -= 1
170173
if self.gameSpeedFactor < 0:
171174
self.gameSpeedFactor = 0
175+
self.speedChanged()
176+
177+
def speedChanged(self):
178+
oneBased = self.gameSpeedFactor + 1
179+
txt = "|" * oneBased
180+
txt = "[" + txt.ljust(len(self.gameSpeedFactors)) + "]"
181+
182+
self.popUp.singlePopUp(txt)
172183

173184
def run_game(self):
174185
# Game parameters
@@ -196,6 +207,7 @@ def run_game(self):
196207

197208
# init the menu, add stuff later
198209
self.iUi = ui(self.screen)
210+
self.popUp = popUp(self.screen)
199211

200212
pygame.joystick.init()
201213
self.joystickInteract = joystick()
@@ -222,19 +234,22 @@ def run_game(self):
222234
worldChanged = False
223235

224236
if self.joystickInteract.joystickAvailable():
225-
if self.joystickInteract.haveAction() == "move":
237+
joyAction = self.joystickInteract.getAction()
238+
if joyAction == "move":
226239
doMove = self.joystickInteract.getMoveAction()
227-
elif self.joystickInteract.haveAction() == "speedUp":
240+
elif joyAction == "speedUp":
228241
self.gameSpeedUp()
229-
elif self.joystickInteract.haveAction() == "speedDown":
242+
elif joyAction == "speedDown":
230243
self.gameSpeedDown()
231-
elif self.joystickInteract.haveAction() == "restart":
244+
elif joyAction == "restart":
232245
self.resetGame()
246+
gameOver = False
247+
elif joyAction == 'quit':
248+
self.exit_game()
233249

234250
for event in pygame.event.get():
235251
if event.type == pygame.QUIT:
236252
self.exit_game()
237-
238253
elif event.type == pygame.KEYDOWN:
239254
if event.key in keymap:
240255
doMove = keymap[event.key]
@@ -270,7 +285,7 @@ def run_game(self):
270285
for elem in reversed(self.elements):
271286
elem.update()
272287

273-
# add elements BEVORE blit is called and they change direction!
288+
# add elements BEFORE blit is called and they change direction!
274289
# WEIRD stuff would happen otherwise!!!1!!!!!!!!
275290
if len(self.haveToAdd) > 0:
276291
for i in range(len(self.haveToAdd)):
@@ -283,23 +298,27 @@ def run_game(self):
283298
self.haveToAdd.pop(i)
284299
break
285300
else:
286-
# if theres NOTHING to add to the Snake, add a new snak, if needed
301+
# if there is NOTHING to add to the Snake, add a new snak, if needed
287302
# preventing from spawning a snack inside the "new" tail of the snake and shit
288303
self.addSnack(self.elements)
289304

290305
# update elements
291306
for elem in reversed(self.elements):
292307
elem.blit()
293308

294-
# if a snak has been eaten, add it to the to add list
309+
# draw pop ups
310+
self.popUp.drawPopUps()
311+
312+
# if a snack has been eaten, add it to the to add list
295313
snackEaten = self.eatSnack(self.elements)
296314
if snackEaten != None:
297315
self.haveToAdd.append(snackEaten)
316+
self.popUp.singlePopUp(str(self.getBodyLen()))
298317

299318
# collision!
300319
if self.headDied(self.elements) == True:
301320
gameOver = True
302-
321+
303322
pygame.display.flip()
304323

305324

0 commit comments

Comments
 (0)