-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollerIF.py
More file actions
123 lines (108 loc) · 4.21 KB
/
controllerIF.py
File metadata and controls
123 lines (108 loc) · 4.21 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pygame
import time
import pyautogui # Import pyautogui for keyboard simulation
def print_controls_reference():
"""Display a reference guide for the controls"""
print("\n=== Interactive Fiction Controller Reference ===")
print("D-Pad:")
print(" ↑ (Up) -> north/n")
print(" ↓ (Down) -> south/s")
print(" ← (Left) -> west/w")
print(" → (Right) -> east/e")
print("\nButtons:")
print(" X (×) -> examine/x")
print(" O (○) -> inventory/i")
print(" □ (Square)-> open")
print(" △ (Triangle) -> close")
print("\nTriggers & Bumpers:")
print(" L1 -> help")
print(" L2 -> look")
print(" R1 -> wait")
print(" R2 -> get all")
print("\nOther:")
print(" Touchpad -> space")
print("==========================================\n")
def init_controller():
pygame.init()
pygame.joystick.init()
# Check if any controller is connected
if pygame.joystick.get_count() == 0:
print("No controller found!")
return None
# Initialize the first controller
controller = pygame.joystick.Joystick(0)
controller.init()
print(f"Connected to: {controller.get_name()}")
return controller
def main():
controller = init_controller()
if not controller:
return
print_controls_reference()
try:
while True:
pygame.event.pump() # Process events to get fresh values
# Get D-pad values as buttons
dpad_up = controller.get_button(11)
dpad_down = controller.get_button(12)
dpad_left = controller.get_button(13)
dpad_right = controller.get_button(14)
# Add triggers and bumpers
left_trigger = controller.get_axis(4) > 0.5 # L2 trigger
right_trigger = controller.get_axis(5) > 0.5 # R2 trigger
left_bumper = controller.get_button(9) # L1
right_bumper = controller.get_button(10) # R1
touchpad = controller.get_button(15) # Touchpad click - corrected button number
# Common button mappings
button_x = controller.get_button(0) # × button - EXAMINE
button_o = controller.get_button(1) # ○ button - INVENTORY
button_square = controller.get_button(2) # □ button - OPEN
button_triangle = controller.get_button(3) # △ button - CLOSE
# Check D-pad inputs and send corresponding key presses
if dpad_up:
pyautogui.typewrite('n\n')
time.sleep(0.5)
elif dpad_down:
pyautogui.typewrite('s\n')
time.sleep(0.5)
elif dpad_left:
pyautogui.typewrite('w\n')
time.sleep(0.5)
elif dpad_right:
pyautogui.typewrite('e\n')
time.sleep(0.5)
# Check trigger, bumper and button inputs
if left_trigger:
pyautogui.typewrite('look\n')
time.sleep(0.5)
elif right_trigger:
pyautogui.typewrite('get all\n')
time.sleep(0.5)
elif left_bumper:
pyautogui.typewrite('help\n')
time.sleep(0.5)
elif right_bumper:
pyautogui.typewrite('wait\n')
time.sleep(0.5)
elif touchpad:
pyautogui.press('space')
time.sleep(0.5)
elif button_x:
pyautogui.typewrite('x \n') # × button - examine
time.sleep(0.5)
elif button_o:
pyautogui.typewrite('i\n') # ○ button - inventory
time.sleep(0.5)
elif button_square:
pyautogui.typewrite('open \n') # □ button - open
time.sleep(0.5)
elif button_triangle:
pyautogui.typewrite('close \n') # △ button - close
time.sleep(0.5)
time.sleep(0.1) # Small delay to make output readable
except KeyboardInterrupt:
print("\nExiting...")
finally:
pygame.quit()
if __name__ == "__main__":
main()