Skip to content

Commit 0ffc252

Browse files
committed
joystick class added, false detection of joystick crashed the game
1 parent 6fdba80 commit 0ffc252

2 files changed

Lines changed: 85 additions & 46 deletions

File tree

joystick.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import pygame
2+
3+
class joystick(object):
4+
def __init__(self):
5+
self.joystick_names = []
6+
self.doMove = -1
7+
self.joyButtonDown = False
8+
self.myJoystick = None
9+
10+
# Enumerate joysticks
11+
for i in range(0, pygame.joystick.get_count()):
12+
self.joystick_names.append(pygame.joystick.Joystick(i).get_name())
13+
14+
# By default, load the first available joystick.
15+
if (len(self.joystick_names) > 0):
16+
self.myJoystick = pygame.joystick.Joystick(0)
17+
self.myJoystick.init()
18+
19+
def joystickAvailable(self):
20+
if self.myJoystick != None:
21+
return True
22+
else:
23+
return False
24+
25+
def haveAction(self):
26+
if self.myJoystick != None:
27+
try:
28+
xAx = 0
29+
yAx = 0
30+
# sometimes 2 axis, sometimes 6, wtf?!
31+
if self.myJoystick.get_numaxes() == 2:
32+
xAx = 0
33+
yAx = 1
34+
else:
35+
xAx = 3
36+
yAx = 4
37+
if self.myJoystick.get_axis(xAx) > 0:
38+
self.doMove = 2
39+
elif self.myJoystick.get_axis(xAx) < 0:
40+
self.doMove = 4
41+
elif self.myJoystick.get_axis(yAx) > 0:
42+
self.doMove = 3
43+
elif self.myJoystick.get_axis(yAx) < 0:
44+
self.doMove = 1
45+
46+
if self.myJoystick.get_button(0) and self.joyButtonDown == False: # speed up
47+
return "speedUp"
48+
elif self.myJoystick.get_button(1) and self.joyButtonDown == False: # speed down
49+
return "speedDown"
50+
elif (self.myJoystick.get_button(9) and self.joyButtonDown == False)\
51+
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
60+
61+
if self.doMove != -1:
62+
return "move"
63+
except Exception as ex:
64+
print('JOYSTICK ERROR, DEACTIVATING:' + str(ex))
65+
self.myJoystick = None
66+
67+
return ""
68+
69+
def getMoveAction(self):
70+
tmp = self.doMove
71+
if self.doMove != -1:
72+
self.doMove = -1
73+
return tmp

snake.py

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import random
77
import pygame
88
from ui import ui
9+
from joystick import joystick
910
from pygame.sprite import Sprite
1011

1112
#sys.stdout = os.devnull
@@ -197,17 +198,8 @@ def run_game(self):
197198
self.iUi = ui(self.screen)
198199

199200
pygame.joystick.init()
200-
self.myJoystick = None
201-
self.joystick_names = []
201+
self.joystickInteract = joystick()
202202

203-
# Enumerate joysticks
204-
for i in range(0, pygame.joystick.get_count()):
205-
self.joystick_names.append(pygame.joystick.Joystick(i).get_name())
206-
207-
# By default, load the first available joystick.
208-
if (len(self.joystick_names) > 0):
209-
self.myJoystick = pygame.joystick.Joystick(0)
210-
self.myJoystick.init()
211203

212204
keymap = {pygame.K_UP:1, pygame.K_RIGHT:2, pygame.K_DOWN:3, pygame.K_LEFT:4}
213205

@@ -221,7 +213,7 @@ def run_game(self):
221213
#
222214
gameOver = False
223215
doMove = -1
224-
joyButtonDown = False
216+
#joyButtonDown = False
225217
while True:
226218
if self.playerBox == None:
227219
self.resetGame()
@@ -231,44 +223,18 @@ def run_game(self):
231223
time_passed = clock.tick(50)
232224
redrawCount += time_passed
233225
worldChanged = False
234-
235-
if self.myJoystick != None:
236-
xAx = 0
237-
yAx = 0
238-
# sometimes 2 axis, sometimes 6, wtf?!
239-
if self.myJoystick.get_numaxes() == 2:
240-
xAx = 0
241-
yAx = 1
242-
else:
243-
xAx = 3
244-
yAx = 4
245-
if self.myJoystick.get_axis(xAx) > 0:
246-
doMove = 2
247-
elif self.myJoystick.get_axis(xAx) < 0:
248-
doMove = 4
249-
elif self.myJoystick.get_axis(yAx) > 0:
250-
doMove = 3
251-
elif self.myJoystick.get_axis(yAx) < 0:
252-
doMove = 1
253-
254-
if self.myJoystick.get_button(0) and joyButtonDown == False: # speed up
255-
joyButtonDown = True
226+
227+
if self.joystickInteract.joystickAvailable():
228+
if self.joystickInteract.haveAction() == "move":
229+
doMove = self.joystickInteract.getMoveAction()
230+
elif self.joystickInteract.haveAction() == "speedUp":
256231
self.gameSpeedUp()
257-
elif self.myJoystick.get_button(1) and joyButtonDown == False: # speed down
258-
joyButtonDown = True
232+
elif self.joystickInteract.haveAction() == "speedDown":
259233
self.gameSpeedDown()
260-
elif self.myJoystick.get_button(9) and joyButtonDown == False: # (re)start
261-
joyButtonDown = True
234+
elif self.joystickInteract.haveAction() == "restart":
262235
self.resetGame()
263-
gameOver = False
264-
else:
265-
# make shure NO button is down for reset
266-
someJoyButtonDown = False
267-
for i in range(0, self.myJoystick.get_numbuttons()):
268-
if (self.myJoystick.get_button(i)):
269-
someJoyButtonDown = True
270-
joyButtonDown = someJoyButtonDown
271-
236+
237+
272238
for event in pygame.event.get():
273239
if event.type == pygame.QUIT:
274240
self.exit_game()

0 commit comments

Comments
 (0)