66import random
77import pygame
88from ui import ui
9+ from joystick import joystick
910from 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