def joystick_thread(): """Manage the joysticks with PyGame""" global PLAYERS global bulletSpawnOk pygame.joystick.init() # Wait until we have a joystick # TODO: Doesn't account for unplugged. while not pygame.joystick.get_count(): print "No Joystick detected!" time.sleep(5) # This loop will avoid trying to initialize any disconnected controllers (such as wii-mote) initjoy = -1 for iX in range(0, pygame.joystick.get_count()): js = pygame.joystick.Joystick(iX) js.init() if js.get_numaxes() > 0: initjoy = js print "Joystick #%i %s axis=%s btn=%s" % ( iX, js.get_name(), js.get_numaxes(), js.get_numbuttons()) break p1 = Player(initjoy, rgb=COLOR_PINK) PLAYERS.append(p1) numButtons = p1.js.get_numbuttons() # XXX NO! bulletLastFired = datetime.now() # Controller wrapper (Xbox, PS3, etc.) controller = setup_controls(p1.js) # Bullet color increment colors = [COLOR_GREEN, COLOR_RED, COLOR_BLUE, COLOR_YELLOW] ship = STATE.ship ci = 0 while True: e = pygame.event.get() for p in PLAYERS: lVert, lHori, rVert, rHori = (0, 0, 0, 0) lVert = controller.getLeftVert() lHori = controller.getLeftHori() rVert = controller.getRightVert() rHori = controller.getRightHori() if abs(rVert) > 0.2: y = ship.y y += -1 * int(rVert * SIMPLE_TRANSLATION_SPD) if MIN_Y < y < MAX_Y: ship.y = y if abs(rHori) > 0.2: x = ship.x x += -1 * int(rHori * SIMPLE_TRANSLATION_SPD) if MIN_X < x < MAX_X: ship.x = x # Player rotation t = math.atan2(lVert, lHori) ship.theta = t # Health Bar Location STATE.healthbar.x = ship.x STATE.healthbar.y = ship.y - 2500 # Both triggers shoot tOff = [0.0, -1.0] trigger = True if controller.getLeftTrigger() in tOff and \ controller.getRightTrigger() in tOff: trigger = False if bulletSpawnOk and trigger: td = timedelta(milliseconds=150) # TODO: Cache this. if datetime.now() > bulletLastFired + td: ang = ship.theta + math.pi ci = (ci + 1) % len(colors) color = colors[ci] b = Bullet(ship.x, ship.y, rgb=color, shotAngle=ang) DRAW.objects.append(b) STATE.bullets.append(b) bulletLastFired = datetime.now() ch = pew.play() time.sleep(0.02) # Keep this thread from hogging CPU
def joystick_thread(): """Manage the joysticks with PyGame""" global PLAYERS global bulletSpawnOk pygame.joystick.init() # Wait until we have a joystick # TODO: Doesn't account for unplugged. while not pygame.joystick.get_count(): print "No Joystick detected!" time.sleep(5) p1 = Player(pygame.joystick.Joystick(0), rgb = COLOR_PINK) PLAYERS.append(p1) numButtons = p1.js.get_numbuttons() # XXX NO! bulletLastFired = datetime.now() # Controller wrapper (Xbox, PS3, etc.) controller = setup_controls(p1.js) # Bullet color increment colors = [COLOR_GREEN, COLOR_RED, COLOR_BLUE, COLOR_YELLOW] ship = STATE.ship ci = 0 while True: e = pygame.event.get() for p in PLAYERS: lVert, lHori, rVert, rHori = (0, 0, 0, 0) lVert = controller.getLeftVert() lHori = controller.getLeftHori() rVert = controller.getRightVert() rHori = controller.getRightHori() if abs(rVert) > 0.2: y = ship.y y += -1 * int(rVert * SIMPLE_TRANSLATION_SPD) if MIN_Y < y < MAX_Y: ship.y = y if abs(rHori) > 0.2: x = ship.x x += -1 * int(rHori * SIMPLE_TRANSLATION_SPD) if MIN_X < x < MAX_X: ship.x = x # Player rotation t = math.atan2(lVert, lHori) ship.theta = t # Health Bar Location STATE.healthbar.x = ship.x STATE.healthbar.y = ship.y - 2500 # Both triggers shoot tOff = [0.0, -1.0] trigger = True if controller.getLeftTrigger() in tOff and \ controller.getRightTrigger() in tOff: trigger = False if bulletSpawnOk and trigger: td = timedelta(milliseconds=150) # TODO: Cache this. if datetime.now() > bulletLastFired + td: ang = ship.theta + math.pi ci = (ci+1) % len(colors) color = colors[ci] b = Bullet(ship.x, ship.y, rgb=color, shotAngle=ang) DRAW.objects.append(b) STATE.bullets.append(b) bulletLastFired = datetime.now() time.sleep(0.02) # Keep this thread from hogging CPU
def joystick_thread(): """Manage the joysticks with PyGame""" global PLAYERS global player global bulletSpawnOk pygame.joystick.init() pygame.display.init() # Wait until we have a joystick # TODO: Doesn't account for unplugged. while not pygame.joystick.get_count(): print "No Joystick detected!" time.sleep(5) p1 = Player(pygame.joystick.Joystick(0), rgb = COLOR_PINK) PLAYERS.append(p1) player = p1 numButtons = p1.js.get_numbuttons() # XXX NO! bulletLastFired = datetime.now() # Controller wrapper (Xbox, PS3, etc.) controller = setup_controls(p1.js) print controller # Bullet color increment colors = [COLOR_GREEN, COLOR_RED, COLOR_BLUE, COLOR_YELLOW] ci = 0 while True: ship = STATE.ship e = pygame.event.get() if e: print e for p in PLAYERS: lVert, lHori, rVert, rHori = (0, 0, 0, 0) lVert = controller.getLeftVert() lHori = controller.getLeftHori() rVert = controller.getRightVert() rHori = controller.getRightHori() """ if abs(rVert) > 0.2: y = ship.y y += -1 * rVert * SIMPLE_TRANSLATION_SPD if y > MAX_Y: y = MIN_Y if y < MIN_Y: y = MAX_Y if MIN_Y <= y <= MAX_Y: ship.y = y if abs(rHori) > 0.2: x = ship.x x += 1 * rHori * SIMPLE_TRANSLATION_SPD if x > MAX_X: x = MIN_X if x < MIN_X: x = MAX_X if MIN_X <= x <= MAX_X: ship.x = x """ if controller.getThrustButton() not in [0.0, -1.0]: accel = 0.0005 accelX = accel * math.cos(ship.theta) accelY = accel * math.sin(ship.theta) ship.xVel -= accelX ship.yVel -= accelY MAX_V = 0.04 if ship.xVel > MAX_V: ship.xVel = MAX_V if ship.xVel < -MAX_V: ship.xVel = -MAX_V if ship.yVel > MAX_V: ship.yVel = MAX_V if ship.yVel < -MAX_V: ship.yVel = -MAX_V ship.vel = math.sqrt(ship.yVel * ship.yVel + ship.xVel * ship.xVel) vel_theta = math.atan2(ship.yVel, ship.xVel) elif abs(ship.vel) > 0.0: if ship.vel > 0.0: ship.vel -= 0.0001 if ship.vel < 0.0: ship.vel = 0.0 if ship.vel < 0.0: ship.vel += 0.0001 if ship.vel > 0.0: ship.vel = 0.0 ship.xVel = ship.vel * math.cos(vel_theta) ship.yVel = ship.vel * math.sin(vel_theta) print "accel_theta, vel, xvel, yvel = ", ship.accel_theta, ship.vel, ship.xVel, ship.yVel # Player rotation #t = math.atan2(lVert, -lHori) ship.theta -= lHori * 0.1 # Health Bar Location #STATE.healthbar.x = ship.x #STATE.healthbar.y = ship.y + 0.1 # Both triggers shoot tOff = [0.0, -1.0] trigger = True #print controller.getLeftTrigger() if controller.getLeftTrigger() in tOff and \ controller.getRightTrigger() in tOff: trigger = False if bulletSpawnOk and trigger: td = timedelta(milliseconds=150) # TODO: Cache this. if datetime.now() > bulletLastFired + td: ang = ship.theta + math.pi ci = (ci+1) % len(colors) color = colors[ci] b = Bullet(ship.x, ship.y, rgb=color, shotAngle=ang) DRAW.objects.append(b) STATE.bullets.append(b) bulletLastFired = datetime.now() if STATE.gameOver and controller.getStartButton() not in [0.0, -1.0]: STATE.startNewGame = True time.sleep(0.02) # Keep this thread from hogging CPU