|
| 1 | +import sys |
| 2 | +import pygame |
| 3 | + |
| 4 | + |
| 5 | +def connect_joystick(index): |
| 6 | + if len(active_players) < len(players): |
| 7 | + joy = pygame.Joystick(index) |
| 8 | + index = players.index(None) |
| 9 | + # player data |
| 10 | + players[index] = { |
| 11 | + "joy": joy, |
| 12 | + "pos": [WIDTH * 0.25 + index * 64, 128], |
| 13 | + "surf_idx": index, |
| 14 | + "surf": colors[index], |
| 15 | + "joined": False, |
| 16 | + } |
| 17 | + active_players[joy.get_instance_id()] = index |
| 18 | + print(f"P{index + 1} Connected") |
| 19 | + |
| 20 | + |
| 21 | +def disconnect_joystick(instance_id: int): |
| 22 | + index = active_players[instance_id] |
| 23 | + players[index] = None |
| 24 | + del active_players[instance_id] |
| 25 | + print(f"P{index + 1} Disconnected") |
| 26 | + |
| 27 | + |
| 28 | +def control_player(player): # move player |
| 29 | + joy = player["joy"] |
| 30 | + if not player["joined"]: |
| 31 | + return |
| 32 | + player["pos"][0] += joy.get_axis(0) * 5 |
| 33 | + player["pos"][1] += joy.get_axis(1) * 5 |
| 34 | + |
| 35 | + |
| 36 | +def create_surf(size, color): |
| 37 | + surf = pygame.Surface(size) |
| 38 | + surf.fill(color) |
| 39 | + return surf |
| 40 | + |
| 41 | + |
| 42 | +pygame.init() |
| 43 | + |
| 44 | +WIDTH, HEIGHT = 500, 500 |
| 45 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 46 | +pygame.display.set_caption("Multiplayer Joystick example") |
| 47 | +clock = pygame.Clock() |
| 48 | +font_b = pygame.font.SysFont(None, 25) |
| 49 | +font_a = pygame.font.SysFont(None, 16) |
| 50 | + |
| 51 | +players = [None, None] # two players limit |
| 52 | +active_players = {} |
| 53 | + |
| 54 | +colors = [ |
| 55 | + create_surf((32, 32), (220, 180, 10)), |
| 56 | + create_surf((32, 32), (60, 230, 170)), |
| 57 | + create_surf((32, 32), (230, 20, 70)), |
| 58 | + create_surf((32, 32), (20, 170, 230)), |
| 59 | +] |
| 60 | +are_no_controllers_connected = True |
| 61 | + |
| 62 | +while True: |
| 63 | + for event in pygame.event.get(): |
| 64 | + if event.type == pygame.QUIT: |
| 65 | + pygame.quit() |
| 66 | + sys.exit() |
| 67 | + elif event.type == pygame.JOYDEVICEADDED: |
| 68 | + if len(active_players) < len(players): |
| 69 | + # connect controller |
| 70 | + connect_joystick(event.device_index) |
| 71 | + are_no_controllers_connected = False |
| 72 | + elif event.type == pygame.JOYDEVICEREMOVED: |
| 73 | + # disconnect controller |
| 74 | + if event.instance_id in active_players: |
| 75 | + disconnect_joystick(event.instance_id) |
| 76 | + # check if there is at leat one controller connected |
| 77 | + are_no_controllers_connected = True |
| 78 | + for player in players: |
| 79 | + if player: |
| 80 | + are_no_controllers_connected = False |
| 81 | + break |
| 82 | + elif event.type == pygame.JOYBUTTONDOWN: |
| 83 | + if event.instance_id in active_players: |
| 84 | + # join player |
| 85 | + if event.button == 0: |
| 86 | + index = active_players[event.instance_id] |
| 87 | + players[index]["joined"] = True |
| 88 | + print(f"P{index + 1} joined") |
| 89 | + # leave player |
| 90 | + if event.button == 1: |
| 91 | + index = active_players[event.instance_id] |
| 92 | + if players[index]["joined"]: |
| 93 | + players[index]["joined"] = False |
| 94 | + players[index]["pos"] = [WIDTH * 0.25 + index * 64, 128] |
| 95 | + print(f"P{index + 1} leave") |
| 96 | + elif event.type == pygame.JOYAXISMOTION: |
| 97 | + if event.instance_id in active_players: |
| 98 | + # change the color if player still hasn't joined |
| 99 | + if event.axis == 0: |
| 100 | + index = active_players[event.instance_id] |
| 101 | + player = players[index] |
| 102 | + if not player["joined"]: |
| 103 | + if event.value >= 1.0: |
| 104 | + player["surf_idx"] += 1 |
| 105 | + elif event.value <= -1.0: |
| 106 | + player["surf_idx"] -= 1 |
| 107 | + player["surf_idx"] = player["surf_idx"] % len(colors) |
| 108 | + player["surf"] = colors[player["surf_idx"]] |
| 109 | + |
| 110 | + screen.fill((30, 30, 30)) |
| 111 | + pygame.draw.line(screen, (230, 230, 230), (0, 96), (WIDTH, 96), 2) |
| 112 | + |
| 113 | + # update and draw players |
| 114 | + for player in players: |
| 115 | + if player: |
| 116 | + control_player(player) |
| 117 | + screen.blit(player["surf"], player["pos"]) |
| 118 | + |
| 119 | + # draw available colors |
| 120 | + for i, surf in enumerate(colors): |
| 121 | + screen.blit(surf, (WIDTH * 0.25 + i * 64, 32)) |
| 122 | + |
| 123 | + # show message for connecting a controller |
| 124 | + if are_no_controllers_connected: |
| 125 | + screen.blit( |
| 126 | + font_b.render( |
| 127 | + "Please connect a controller.", True, (230, 230, 230), None, 500 - 20 |
| 128 | + ), |
| 129 | + (WIDTH * 0.3, HEIGHT * 0.5), |
| 130 | + ) |
| 131 | + |
| 132 | + screen.blit( |
| 133 | + font_a.render( |
| 134 | + "A: join B: leave Joystick: move / change color", |
| 135 | + True, |
| 136 | + (230, 230, 230), |
| 137 | + None, |
| 138 | + WIDTH - 20, |
| 139 | + ), |
| 140 | + (10, HEIGHT - 20), |
| 141 | + ) |
| 142 | + |
| 143 | + clock.tick(60) |
| 144 | + pygame.display.update() |
0 commit comments