def get_joystick(self): for _ in pygame.event.get(): pass if not self.joysticks: return 0, 0, 0, 0 joystick = self.joysticks[0] return joystick.get_axis(0), joystick.get_axis(1), \ joystick.get_button(0), joystick.get_button(1)
def checkJoystickButtons(): buttonStates.fire = joystick.get_button(1) xAxis = joystick.get_axis(0) yAxis = joystick.get_axis(1) buttonStates.left = xAxis < -0.01 buttonStates.right = xAxis > 0.01 buttonStates.up = yAxis < -0.01 buttonStates.down = yAxis > 0.01
def dualshock_handler(): joystick = Joystick(wii_joy_index) joystick.init() joy_x1 = joystick.get_axis(0) joy_y1 = joystick.get_axis(1) joy_x2 = joystick.get_axis(2) joy_y2 = joystick.get_axis(3) axes = [joy_x1, joy_y1, joy_x2, joy_y2] for axis in axes: print(axis)
def joystickcontrole(self): try: while self.ps3Connected: for event in pygame.event.get(): if event.type == QUIT: self.quit_game() if event.type == JOYBUTTONDOWN: for i in range(self.knoppen): # Haal de waarde van de knop op. knop = joystick.get_button(i) if knop == 1: print("Knop:", i, "ingedrukt!") if event.type == JOYAXISMOTION: if event.dict['axis'] == 1: for i in range(self.assen): # Haal de waarde van de as op. eenas = joystick.get_axis(i) if eenas != 0: if i == 0: self.asrichting = 'X' if i == 1: self.asrichting = 'Y' if i == 2: self.asrichting = 'X' if i == 3: self.asrichting = 'Y' print("AS", i, "waarde:", self.asrichting, eenas) except KeyboardInterrupt: pygame.quit()
def get_joysticks(joystick_number=None): # Get the state of all the joystick joystick_count = pygame.joystick.get_count() return_joystick_status = {} if joystick_number is not None and joystick_count >= joystick_number: joystick = pygame.joystick.Joystick(joystick_number) joystick.init() return_joystick_status[str(joystick_number)] = {} joystick_hats = joystick.get_numhats() for hat in range(joystick_hats): return_joystick_status[str(joystick_number)][ "hat " + str(hat)] = joystick.get_hat(hat) joystick_buttons = joystick.get_numbuttons() return_joystick_status[str( joystick_number)]["buttons"] = [0] * joystick_buttons for button in range(joystick_buttons): return_joystick_status[str(joystick_number)]["buttons"][ button] = joystick.get_button(button) joystick_axes = joystick.get_numaxes() for axis in range(joystick_axes): return_joystick_status[str(joystick_number)][ "axis " + str(axis)] = round(joystick.get_axis(axis), 3) elif joystick_number is None: for i in range(joystick_count): joystick = pygame.joystick.Joystick(i) joystick.init() return_joystick_status[str(i)] = {} joystick_hats = joystick.get_numhats() for hat in range(joystick_hats): return_joystick_status[str(i)][ "hat " + str(hat)] = joystick.get_hat(hat) joystick_buttons = joystick.get_numbuttons() return_joystick_status[str(i)]["buttons"] = [0] * joystick_buttons for button in range(joystick_buttons): return_joystick_status[str( i)]["buttons"][button] = joystick.get_button(button) joystick_axes = joystick.get_numaxes() for axis in range(joystick_axes): return_joystick_status[str(i)]["axis " + str(axis)] = round( joystick.get_axis(axis), 3) return return_joystick_status
def on_axis_motion(self, joystick, axis, value): # axis 1 = left stick Y # use the position of the left stick's Y axis as expression, but only if the stick is >90% from centre if axis == Axes.LY: vel = int(math.floor(((value + 1) / -2.0) * 127) + 127) if vel != self.expression and common.beyond_deadzone( 0.9, joystick.get_axis(Axes.LX), joystick.get_axis( Axes.LY)): print("velocity " + str(vel)) self.expression = vel self.outport.send( mido.Message('control_change', channel=3, control=11, value=self.expression)) elif axis == Axes.RT or axis == Axes.LT: self.on_button_change(joystick, 9999, value > -0.9)
def wiimote_handler(): joystick = pygame.joystick.Joystick(wii_joy_index) joystick.init() joy_x = joystick.get_axis(0) joy_y = joystick.get_axis(1) joy_buttons = list() for i in range(0, 8): joy_button = joystick.get_button(i) joy_buttons.append(joy_button) pub_direction((joy_x, joy_y)) pub_arm(joy_buttons[0] - joy_buttons[1]) pub_shovel(joy_buttons[2] - joy_buttons[3]) pitch = joy_buttons[4] - joy_buttons[5] yaw = joy_buttons[6] - joy_buttons[7] pub_camera(pitch, yaw)
def read_joystick(): global joystick axes = joystick.get_numaxes() response = [0] * axes for i in range(axes): response[i] = joystick.get_axis(i) return response
def jesses_handler(events, joystick): for event in events: if event.type == pygame.JOYAXISMOTION: left_motor_speed = joystick.get_axis(left_y_axis) right_motor_speed = joystick.get_axis(right_y_axis) if math.fabs(left_motor_speed) <= joystick_deadzone: left_motor_speed = 0 if math.fabs(right_motor_speed) <= joystick_deadzone: right_motor_speed = 0 left_motor_forward = left_motor_speed > 0 right_motor_forward = right_motor_speed > 0 left_motor_speed = math.fabs(left_motor_speed) * 100 right_motor_speed = math.fabs(right_motor_speed) * 100 s1.set_motor(left_motor_speed, p1) s2.set_motor(right_motor_speed, p2) m1.change_direction("reverse" if left_motor_forward else "forward") m2.change_direction( "reverse" if right_motor_forward else "forward") print("left_motor_speed: {}".format(left_motor_speed)) print("right_motor_speed: {}".format(right_motor_speed)) print("left_motor_direction: {}".format(left_motor_forward)) print("right_motor_direction: {}".format(right_motor_forward))
def gather_inputs(player, joystick): player.main_stick = [joystick.get_axis(0), joystick.get_axis(1)] player.c_stick = (joystick.get_axis(5), joystick.get_axis(4)) player.r_trigger = joystick.get_axis(3) player.l_trigger = joystick.get_axis(2) # keys = pygame.key.get_pressed() # buttons = joystick.get_buttons() # create deadzone if abs(player.main_stick[0]) < 0.22: player.main_stick[0] = 0 if abs(player.main_stick[1]) < 0.22: player.main_stick[1] = 0 # if joystick.event == pygame.JOYBUTTONDOWN: if joystick.get_button(0): # A player.akey = True print("controller pressed A") else: player.akey = False if joystick.get_button(1): # B player.specialkey = True else: player.specialkey = False if joystick.get_button(2) or joystick.get_button(3): # X and Y player.jumpkey = True else: player.jumpkey = False player.canJump = True if joystick.get_button(4): # Z player.grabkey = True else: player.grabkey = False if joystick.get_button(5) or joystick.get_button(6): # R and L player.blockkey = True else: player.blockkey = False player.canBlock = True if joystick.get_button(7): # START player.menukey = True else: player.menukey = False if joystick.get_button(8): # UP player.upkey = True else: player.upkey = False if joystick.get_button(9): # DOWN player.downkey = True else: player.downkey = False if joystick.get_button(10): # LEFT player.leftkey = True else: player.leftkey = False if joystick.get_button(11): # RIGHT player.rightkey = True else: player.rightkey = False
def postactive(): keystate = pygame.key.get_pressed() for key in range (len(keystate)): if keystate[key]: #I don't know how to get unicode pygame.event.post(pygame.event.Event(KEYDOWN, {'key': key, 'mod': pygame.key.get_mods()})) if joystick: for button in range(joystick.get_numbuttons()): if joystick.get_button(button): pygame.event.post(pygame.event.Event(JOYBUTTONDOWN, {'joy': joystick.get_id(), 'button': button})) for hat in range(joystick.get_numhats()): value = joystick.get_hat(hat) if 0 != value[0] or 0 != value[1]: pygame.event.post(pygame.event.Event(JOYHATMOTION, {'joy': joystick.get_id(), 'hat': hat, 'value': value})) for axis in range(joystick.get_numaxes()): lastaxisvalue[axis] = None value = joystick.get_axis(axis) pygame.event.post(pygame.event.Event(JOYAXISMOTION, {'joy': joystick.get_id(), 'axis': axis, 'value': value}))
name = sys.argv[1] registry = rotorc.RemoteRegistry( name ) options = registry.options() registry.registerMessageType( "JOYSTICK", joystickDefinition ) frequency = options.getDouble( name, "frequency", 10 ) joystickInfo = rotorc.Structure( "Joystick", None, registry ) pygame.init() pygame.joystick.init() joystick = pygame.joystick.Joystick( 0 ) joystick.init() time = rotorc.seconds() while True: delta = rotorc.seconds() time = delta pygame.event.pump() joystickInfo.axesCount = joystick.get_numaxes() for i in xrange( joystickInfo.axesCount ): joystickInfo.axes[i] = joystick.get_axis( i ) joystickInfo.buttonCount = joystick.get_numbuttons() for i in xrange( joystickInfo.buttonCount ): joystickInfo.buttons[i] = joystick.get_button( i ) registry.sendStructure( "JOYSTICK", joystickInfo ) rotorc.Thread.sleep( 0.1 )
def goon(): global q, q_gui if not dryrun: t = threading.Thread(target=gcodespitter) t.daemon = True t.start() st = 0 b0 = b1 = b2 = b3 = 0 correction = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] def print_(x): print >> sys.stderr, x calibd = skip_calib # False if calibaction == CALIB_LOAD: calibd = True with open(calibfile, "r") as f: correction = map( lambda j: map(float, j), map(lambda x: map(lambda z: z.strip(), x.split(",")), f.readlines()) ) pygame.joystick.init() # initialize joystick module pygame.joystick.get_init() # verify initialization (boolean) joystick_count = pygame.joystick.get_count() # get number of joysticks # it's for some case, important to get the joystick number ... if joystick_count == 1: joystick = pygame.joystick.Joystick(0) joystick.get_name() joystick.init() joystick.get_init() while True: # time.sleep(0.00001) if joystick_count == 1: # verify initialization (maybe cool to do some error trapping with this so game doesn't crash pygame.event.pump() # So Now i can get my joystick Axis events xax_ = joystick.get_axis(0) yax_ = joystick.get_axis(1) zax_ = joystick.get_axis(3) buttons = joystick.get_numbuttons() b0 = joystick.get_button(0) b1 = joystick.get_button(1) b2 = joystick.get_button(2) b3 = joystick.get_button(3) if st == 1: print >> sys.stderr, "put joystick to center and press B0", elif st == 3: print >> sys.stderr, "put joystick to left and press B0", elif st == 5: print >> sys.stderr, "put joystick to right and press B0", elif st == 7: print >> sys.stderr, "put joystick to down and press B0", elif st == 9: print >> sys.stderr, "put joystick to up and press B0", elif st == 11: print >> sys.stderr, "put joystick to bottom and press B0", elif st == 13: print >> sys.stderr, "put joystick to top and press B0", if not calibd: st = calib(st, b0, (xax_, yax_, zax_), correction) print >> sys.stderr, correction if st == 14: print >> sys.stderr, "CALIBRATED.", calibd = True if calibaction == CALIB_SAVE: with open(calibfile, "w+") as f: f.write("\n".join(map(lambda x: ",".join(map(str, x)), correction))) continue (xax, yax, zax) = correct(correction, (xax_, yax_, zax_)) # print >> sys.stderr, correction, # print >> sys.stderr, (xax, yax, zax) if not dryrun: if b1 != b2: if b1: # q.put('G1 Z10', block=False) put_nonblock(q, "G1 Z%f" % (zax)) if b2: # q.put('G1 Z-10', block=False) put_nonblock(q, "G1 Z%f" % (-zax)) # q.put('G1 Z-%f' % (zax), block=False) if b3: put_nonblock(q, "G1 X%f Y%f" % (xax, yax)) # q.put('G1 X%f Y%f' % (xax, yax), block=False) print "q.join" # q.join() if b0: put_nonblock(q_gui, "save")
import logging from hubsan import * import pygame import pygame.event import pygame.joystick pygame.init() pygame.joystick.init() joystick = pygame.joystick.Joystick(0) joystick.init() logging.basicConfig(level=logging.INFO) hubsan = Hubsan() hubsan.init() hubsan.bind() hubsan.safety() while True: pygame.event.get() throttle = max(0, -joystick.get_axis(4)) rudder = joystick.get_axis(0) elevator = joystick.get_axis(1) aileron = joystick.get_axis(3) hubsan.control(throttle, rudder, elevator, aileron)
def run_game(): global ch, dobollet, db, wea, t pygame.init() pygame.mixer.init() shoot = mixer.Sound("C:\windows\media\Ring02.wav") music = mixer.Sound("sounds/Windows Critical Stop.wav") image = pygame.image.load('bg.gif') turn = 0 ch = 10 fg = 1 pfg = 1 joysticks = [ pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count()) ] print(joysticks.pop(1)) for joystick in joysticks: joystick.init() i = 0 j = False sit = False sco = 0 ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption('Alien Invasion') ship = Ship(ai_settings, screen) bullets = Group() ships = Group() blocks = Group() aliens = Group() l = 0 r = 0 godown = 0 sb = Scoreboard(ai_settings, screen, 0, sco) alies = Group() buttons = Group() yu = False #rint(ship.y)iii alienbullets = Group() ert = 0 tr = [ 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2 ] sty = Group() uii = 0 do_liberint(pygame, Block, blocks, ai_settings, screen, 'liberint.txt', Alien, aliens) upup = 0 ship2 = Ship(ai_settings, screen) for alien in aliens.sprites(): alien.image = pygame.image.load('bomb.gif') poweraps = Group() aiens = Group() ant = Ant_men(ai_settings, screen, ship) ships.add(ship) ships.add(ship2) shoot.play(-1) time = 0 un = 0 while True: if r == 0: #ship.imageship.image = pygame.image.load('SCrightup.gif') ship.blitme() #pygame.display.flip() for alien in blocks.sprites(): alien.x -= 1 alien.rect.x = alien.x #pygame.display.flip() #pygame.display.flip() #pygame.display.flip() #ship.image = pygame.image.load('SCright.gif') ship.blitme() # if pygame.sprite.spritecollideany(ship,blocks): # for alien in poweraps.sprites(): # alien.x += 1 # alien.rect.x = alien.x # for alien in blocks.sprites(): # alien.x += 1 # alien.rect.x = alien.x if l == 0: # ship.image = pygame.image.load('SCleftup.gif') #pygame.display.flip() for alien in blocks.sprites(): alien.x += 1 alien.rect.x = alien.x #pygame.display.flip() #pygame.display.flip() #pygame.display.flip() #ship.blitme() # if pygame.sprite.spritecollideany(ship,blocks): # for alien in poweraps.sprites(): # alien.x += 1 # alien.rect.x = alien.x # for alien in blocks.sprites(): # alien.x += 1 # alien.rect.x = alien.x if r == 0: ant.x -= 1 ant.rect.x = ant.x # if pygame.sprite.spritecollideany(ship,blocks): # for alien in poweraps.sprites(): # alien.x += 1 # alien.rect.x = alien.x # for alien in blocks.sprites(): # alien.x += 1 # alien.rect.x = alien.x if l == 0: ant.x += 1 ant.rect.x = ant.x # if pygame.sprite.spritecollideany(ship,blocks): # for alien in poweraps.sprites(): # alien.x -= 1 # alien.rect.x = alien.x # for alien in blocks.sprites(): # alien.x -= 1 # alien.rect.x = alien.x for event in pygame.event.get(): #for event in pygame.event.get(): # # if event.type == pygame.KEYDOWN: # if event.key == pygame.K_1: # first = randint(0,220) # second = randint(0,220) # three = randint(0,220) # ai_settings.bg_color = (first,second,three) # if event.key == pygame.K_b: # sys.exit() # if event.key == pygame.K_RIGHT: # #ship.movin_right = True # cr = 0 # t = 2 # if event.key == pygame.K_LEFT: # #ship.movin_left = True # cl = 0 # t = 3 # if event.key == pygame.K_r: # #ship.movin_left = True # if len(bullets) <= bulets: # new_bullet = Bullet(ai_settings,screen,ship,aliens,bullets,0) # bullets.add(new_bullet) # if event.key == pygame.K_UP: # cu = 0 # t = 0 if event.type == pygame.MOUSEBUTTONDOWN: xy, yx = pygame.mouse.get_pos() blok = Block(ai_settings, screen, 'fkdf') blok.rect.x, blok.rect.y = xy, yx blok.x, blok.y = xy, yx blocks.add(blok) if event.type == pygame.JOYAXISMOTION: buttons = joystick.get_numaxes() for i in range(buttons): but = joystick.get_axis(0) if but < -.5: ship.x -= 1 ship.rect.x = ship.x t = 3 but = joystick.get_axis(0) if but > .5: ship.x += 1 ship.rect.x = ship.x t = 2 but = joystick.get_axis(1) if but < -.5: ship.y -= 1 ship.rect.y = ship.y if but > .5: ship.y += 1 ship.rect.y = ship.y # but = joystick.get_axis(1) # if but < -.5: # ship.y -= 1 # ship.rect.y = ship.y # if pygame.sprite.spritecollideany(ship,blocks): # ship.y += fuster # ship.rect.y = ship.y # t = 0 # but = joystick.get_axis(1) # if but > .5: # ship.y += 1 # ship.rect.y = ship.y # if pygame.sprite.spritecollideany(ship,blocks): # ship.y -= fuster # ship.rect.y = ship.y # t = 1 buttons = joystick.get_numhats() for i in range(buttons): but = joystick.get_hat(i) if but == (-1, 0): ship.rect.x -= 1 but = joystick.get_hat(i) if but == (1, 0): ship.rect.x += 1 but = joystick.get_hat(i) if but == (0, -1): ship.rect.y -= 1 but = joystick.get_hat(i) if but == (0, 1): ship.rect.y += 1 but = joystick.get_hat(i) print(but) #if but == (0,1): # j = False # pfg *=-1 # j = True # but = joystick.get_hat(i) if event.type == pygame.JOYBUTTONDOWN: buttons = joystick.get_numbuttons() for button in range(buttons): # # print(button) # #pass # # if button == (0,0): # # music.play() # # new_bullet = Bullet(ai_settings,screen,ship,aliens,bullets,alien,t) # # bullets.add(new_bullet) but = joystick.get_button(0) if but: j = False pfg *= -1 j = True but = joystick.get_button(2) if but: if upup == 0: upup = 1 break if upup == 1: upup = 0 break #print(but) # if but == 2: # j = False # pfg *=-1 # j = True # but = joystick.get_button(button) # if but == 1: # upup = 1 elif event.type == pygame.JOYBUTTONUP: #buttons = joystick.get_numbuttons() buttons = joystick.get_numbuttons() for button in range(buttons): # # print(button) # #pass # # if button == (0,0): # # music.play() # # new_bullet = Bullet(ai_settings,screen,ship,aliens,bullets,alien,t) # # bullets.add(new_bullet) but = joystick.get_button(2) if but: upup = 0 if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: cl2 = 0 cr2 = 1 cu2 = 1 cd2 = 1 t2 = 3 if event.key == pygame.K_RIGHT: cl2 = 1 cr2 = 0 cu2 = 1 cd2 = 1 t2 = 2 #but = joystick.get_hat(i) if event.key == pygame.K_DOWN: cl2 = 1 cr2 = 1 cu2 = 1 cd2 = 0 t2 = 1 #but = joystick.get_hat(i) if event.key == pygame.K_UP: cl2 = 1 cr2 = 1 cu2 = 0 cd2 = 1 t2 = 0 if event.key == pygame.K_v: new_bullet = Bullet(ai_settings, screen, ship2, aliens, bullets, alien, t2) bullets.add(new_bullet) # elif event.type == pygame.KEYUP: # if event.key == pygame.K_RIGHT: # ship.movin_right = False # cr = 1 # if event.key == pygame.K_LEFT: # ship.movin_left = False # cl = 1 # if event.key == pygame.K_UP: # cu = 1 # if event.key == pygame.K_DOWN: # cd = 1 # if event.key == pygame.K_2: # ship.image = pygame.image.load(random.choice(images)) # if event.key == pygame.K_DOWN: # ship.movin_down = False # #276 #276 ship.update(blocks, ship) bullets.update(bullets, aliens) collisions = pygame.sprite.groupcollide(ships, aliens, False, True) #print('you lose') #break ship.blitme() # if pygame.sprite.spritecollideany(ship,blocks): # for alien in blocks.sprites(): # alien.y += 1 # alien.rect.y = alien.y # ant.y += 1 # ant.rect.y = ant.y # godown = 0 # pygame.display.flip() for bullet in bullets.copy(): if bullet.rect.bottom <= 0 or bullet.rect.bottom >= 900 or bullet.rect.centerx <= 0 or bullet.rect.centerx >= 1000: bullets.remove(bullet) screen.fill(ai_settings.bg_color) # for bullet in alienbullets.copy(): # if bullet.rect.bottom >= 900: # bullets.remove(bul # screen.fill(ai_settings.bg_color) # for bullet in alienbullets.sprites(): # bullet.draw_bullet() # first = randint(0,200) # second = randint(0,200) # three = randint(0,200) # ai_settings.bullet_color = (first,second,three) # collisins = pygame.sprite.groupcollide(blocks,bullets,True,True) collisions = pygame.sprite.groupcollide(ships, poweraps, False, True) if len(aliens) <= 0: print('you win') break # for alien in aliens.sprites(): # if pygame.sprite.spritecollideany(alien,blocks): # alien.duraction *= -1 for bullet in bullets.sprites(): bullet.draw_bullet() collisions = pygame.sprite.groupcollide( bullets, aliens, True, True) if collisions: moneys += 1 collisions = pygame.sprite.groupcollide( bullets, blocks, True, False) first = randint(0, 200) second = randint(0, 200) three = randint(0, 200) ai_settings.bullet_color = (first, second, three) bullets.update(bullets, aliens) ship.blitme() #chekupdown_fleet_edges(ai_settings,aliens) #alien.blitme() bullets.draw(screen) sb.show_score() alienbullets.update(bullets, aliens) collisions = pygame.sprite.groupcollide(ships, aliens, False, True) aliens.draw(screen) #if un == 0: blocks.draw(screen) blocks.update() if pygame.sprite.spritecollideany(ant, ships): if pfg == -1: g = 1 bullets.empty() ch -= 1 if ch <= 0: ant.image = pygame.image.load('bowserflat.gif') un = 1 # print('you win') # break j = False pfg *= -1 j = True else: if un == 0: print('you lose') sys.exit() if pygame.sprite.spritecollideany(ant, bullets): ch -= 1 bullets.empty() if ch <= 0: print('you win') break bullets.update(0, 0) bullets.draw(screen) ship2.blitme() #if bezero == 34: #bezero = 0 #sb.show_score() #poweraps.draw(screen) #pygame.display.update() #screen.blit(image,(0,0)) pygame.display.flip()
joystick = pygame.joystick.Joystick(0) joystick.init() name = joystick.get_name() print(name) axes = joystick.get_numaxes() print("Nombre d'axes : ", axes) buttons = joystick.get_numbuttons() print("Nombre de boutons : ", buttons) hats = joystick.get_numhats() print("Nombres de HAT : ", hats) running = True while running: pygame.event.get() axis_0 = joystick.get_axis(0) axis_1 = joystick.get_axis(1) axis_2 = joystick.get_axis(2) axis_3 = joystick.get_axis(3) print(axis_0, axis_1, axis_2, axis_3) button_11 = joystick.get_button(11) if button_11 == 1: running = False pygame.quit()
def run(self): while True: self.window.blit( pygame.transform.flip(self.cam.get_image(), self.inversed.h, self.inversed.v), (0, 0)) self.window.blit(self.intizalize, (10, 10)) self.window.blit(self.inv_sys, (10, 40)) self.window.blit(self.position_X, (10, 70)) self.window.blit(self.position_Y, (10, 100)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() #evenement du clavier: if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: if self.online: self.intizalize = self.commande.initialize( 'Offline') self.online = False else: self.intizalize = self.commande.initialize( 'Online') self.online = True if self.online: if event.key == pygame.K_F4 or event.key == pygame.K_q: sys.exit() if event.key == pygame.K_i: if self.inversed.boolean: self.inversed.set_bool(False) self.inversed.h = 1 else: self.inversed.set_bool(True) self.inversed.h = 0 self.inv_sys = self.commande.initialize( 'Inversed : ' + str(self.inversed.boolean)) if event.key == pygame.K_UP: self.commande.forward() self.position_Y = self.commande.get_position_Y() if event.key == pygame.K_DOWN: self.commande.backward() self.position_Y = self.commande.get_position_Y() if event.key == pygame.K_RIGHT: self.commande.right() self.position_X = self.commande.get_position_X() if event.key == pygame.K_LEFT: self.commande.left() self.position_X = self.commande.get_position_X() if event.key == pygame.K_a: self.commande.grab(self.window, 'drop') if event.key == pygame.K_l: self.commande.drop(self.window, 'grab') if event.key == pygame.K_s: self.commande.screensaver(self.window) #evenement du joystick bouton: if event.type == pygame.JOYBUTTONDOWN: if event.button == 5: #fermeture de la pince self.commande.grab(self.window, 'grab') if event.button == 4: #Ouverture de la pince self.commande.drop(self.window, 'drop') else: print(event.button) #evenement du joystick axis: if event.type == pygame.JOYAXISMOTION: if (joystick.get_axis(0) > -32768 and joystick.get_axis(0) < 32768): if (joystick.get_axis(0) > -32768 and joystick.get_axis(0) < 0): self.commande.arm_rotation_left() if (joystick.get_axis(0) > 0 and joystick.get_axis(0) <= 32768): self.commande.arm_rotation_right() if (joystick.get_axis(1) > -32768 and joystick.get_axis(1) < 32768): if (joystick.get_axis(1) > -32768 and joystick.get_axis(1) < 0): self.commande.arm_rotation_forward() if (joystick.get_axis(1) > 0 and joystick.get_axis(1) <= 32768): self.commande.arm_rotation_backward() #evenement du joystick hat: if event.type == pygame.JOYHATMOTION: if (joystick.get_hat(0) == (0, 1)): #up self.commande.arm_rotation_forward() if (joystick.get_hat(0) == (0, -1)): #down self.commande.arm_rotation_backward() if (joystick.get_hat(0) == (1, 0)): #droite self.commande.arm_rotation_right() if (joystick.get_hat(0) == (-1, 0)): #gauche self.commande.arm_rotation_left() #initialisation du joystick et des bouton et axis: joystick_count = pygame.joystick.get_count() for i in range(joystick_count): joystick = pygame.joystick.Joystick(i) joystick.init() buttons = joystick.get_numbuttons() axes = joystick.get_numaxes()
import logging from hubsan import * import pygame import pygame.event import pygame.joystick pygame.init() pygame.joystick.init() joystick = pygame.joystick.Joystick(0) joystick.init() logging.basicConfig(level = logging.INFO) hubsan = Hubsan() hubsan.init() hubsan.bind() hubsan.safety() while True: pygame.event.get() throttle = max(0, -joystick.get_axis(4)) rudder = joystick.get_axis(0) elevator = joystick.get_axis(1) aileron = joystick.get_axis(3) hubsan.control(throttle, rudder, elevator, aileron)