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 init(): "init the joystick" global joystick global lastaxisvalue global lasthatvalue try: num = pygame.joystick.get_count() if num > 0: joystick = pygame.joystick.Joystick(0) joystick.init() lastaxisvalue = [None] * joystick.get_numaxes() lasthatvalue = [[None, None]] * joystick.get_numhats() except pygame.error: joystick = None
def __init__(self): pygame.joystick.init() pygame.joystick.get_init() self.count = pygame.joystick.get_count() if self.count > 0: self.joystick = pygame.joystick.Joystick(0) self.joystick.init() print '%d joystick(s) connected: %s' % (self.count, joystick.get_name()) self.num_axes = joystick.get_numaxes() self.num_buttons = joystick.get_numbuttons() self.num_hats = joystick.get_numhats() print 'Joystick has %d axes, %d buttons and %d hats.' % (num_axes, num_buttons, num_hats) pygame.event.pump() self.old_axis = [] for num in range(self.num_axes): self.old_axis.append(0.0)
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}))
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_count = pygame.joystick.get_count() print("Number of joysticks: ", joystick_count) 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