Skip to content

Instantly share code, notes, and snippets.

@aflaag
Last active March 3, 2021 21:41
Show Gist options
  • Save aflaag/a62107023d410cb1db657daf889403b0 to your computer and use it in GitHub Desktop.
Save aflaag/a62107023d410cb1db657daf889403b0 to your computer and use it in GitHub Desktop.
from inputs import get_gamepad
import pygame
import time
def scale(dimension, coordinate, gamepad_max):
scaled = coordinate * dimension // (gamepad_max * 2) + dimension // 2
return scaled if scaled > 0 else 0
def display_text(text, screen, pos, font, color):
screen.blit(font.render(text, True, color), pos)
def main():
Z_MAX = 10
WIDTH, HIGHT = 800, 800
BG_COLOR = (0, 0, 0)
CIRCLE_COLOR = (255, 0, 255)
X_AXIS_COLOR = (0, 255, 0)
Y_AXIS_COLOR = (255, 0, 0)
X_AXIS_WIDTH = 1
Y_AXIS_WIDTH = 1
TEXT_WIDTH = 36
GAMEPAD_MAX = 32767
RADIUS_SCALE_FACTOR = 3
x = 0
y = 0
z = 1
center = [x, y]
pygame.init()
screen = pygame.display.set_mode((WIDTH, HIGHT))
font = pygame.font.SysFont('calibri.ttf', TEXT_WIDTH)
running = True
while running:
events = get_gamepad()
for event in events:
if event.ev_type == "Absolute":
if event.code == "ABS_X":
x = event.state
elif event.code == "ABS_Y":
y = event.state
elif event.ev_type == "Key":
if event.code == "BTN_SOUTH" and event.state and z != Z_MAX:
z += event.state
elif event.code == "BTN_EAST" and event.state and z != 1:
z -= event.state
elif event.code == "BTN_WEST" and event.state:
running = False
else:
continue
screen.fill(BG_COLOR)
x_start_pos = (center[0], -GAMEPAD_MAX)
x_end_pos = (center[0], GAMEPAD_MAX)
y_start_pos = (GAMEPAD_MAX, center[1])
y_end_pos = (-GAMEPAD_MAX, center[1])
center[0] = scale(WIDTH, x, GAMEPAD_MAX)
center[1] = scale(HIGHT, y, -GAMEPAD_MAX)
radius = z * RADIUS_SCALE_FACTOR
pygame.draw.line(screen, X_AXIS_COLOR, x_start_pos, x_end_pos, X_AXIS_WIDTH)
pygame.draw.line(screen, Y_AXIS_COLOR, y_start_pos, y_end_pos, Y_AXIS_WIDTH)
pygame.draw.circle(screen, CIRCLE_COLOR, center, radius)
display_text(f"X: {center[0]}", screen, (20, 20), font, X_AXIS_COLOR)
display_text(f"Y: {center[1]}", screen, (20, 42), font, Y_AXIS_COLOR)
display_text(f"Z: {z}", screen, (20, 64), font, CIRCLE_COLOR)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment