-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController_Testing.py
More file actions
66 lines (52 loc) · 1.46 KB
/
Controller_Testing.py
File metadata and controls
66 lines (52 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pygame
from pygame.locals import *
# Initialize Pygame
pygame.init()
# Set up the display
WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Controller Test Game")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Game variables
x, y = WIDTH // 2, HEIGHT // 2
speed = 5
radius = 10
# Initialize the joystick
pygame.joystick.init()
if pygame.joystick.get_count() == 0:
print("No joystick detected!")
pygame.quit()
exit()
joystick = pygame.joystick.Joystick(0)
joystick.init()
print(f"Joystick Name: {joystick.get_name()}")
# Main game loop
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == JOYBUTTONDOWN:
print(f"Button {event.button} pressed!")
elif event.type == JOYBUTTONUP:
print(f"Button {event.button} released!")
# Get joystick input
axis_x = joystick.get_axis(0) # Left stick horizontal
axis_y = joystick.get_axis(1) # Left stick vertical
# Update position
x += int(axis_x * speed)
y += int(axis_y * speed)
# Boundaries check
x = max(radius, min(WIDTH - radius, x))
y = max(radius, min(HEIGHT - radius, y))
# Clear the screen
screen.fill(BLACK)
# Draw the "character"
pygame.draw.circle(screen, RED, (x, y), radius)
# Update the display
pygame.display.flip()
# Clean up
pygame.quit()