-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdraw_xbox.py
More file actions
189 lines (158 loc) · 4.19 KB
/
draw_xbox.py
File metadata and controls
189 lines (158 loc) · 4.19 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import pygame
import turtle
import time
# ---------- PYGAME SETUP ----------
pygame.init()
pygame.joystick.init()
if pygame.joystick.get_count() == 0:
print("No controller detected")
quit()
js = pygame.joystick.Joystick(0)
js.init()
# ---------- TURTLE SETUP ----------
screen = turtle.Screen()
screen.title("Xbox Controller Turtle Draw")
screen.setup(800, 600)
pen = turtle.Turtle()
pen.speed(0)
pen.pendown()
# ---------- CURSOR SHAPES ----------
shapes = ["classic", "arrow", "turtle", "circle", "square", "triangle"]
shape_index = 0
pen.shape(shapes[shape_index])
# ---------- COLORS ----------
colors = ["black", "red", "blue", "green", "purple", "yellow"]
color_index = 0
pen.color(colors[color_index])
# ---------- PEN WIDTH ----------
pen_width = 3
MIN_WIDTH = 1
MAX_WIDTH = 20
pen.width(pen_width)
# ---------- SETTINGS ----------
DEADZONE = 0.15
SPEED = 6
fullscreen = False
pen_is_down = True
# ---------- LOCKS ----------
a_lock = b_lock = x_lock = y_lock = False
lb_lock = rb_lock = False
hat_lock = False
lt_lock = False
# ---------- TRIGGER EXIT ----------
rt_hold_start = None
RT_THRESHOLD = 0.5
# ---------- SHAPE FUNCTIONS ----------
def draw_square(size=80):
for _ in range(4):
pen.forward(size)
pen.right(90)
def draw_triangle(size=80):
for _ in range(3):
pen.forward(size)
pen.right(120)
def draw_hexagon(size=60):
for _ in range(6):
pen.forward(size)
pen.right(60)
# ---------- MAIN LOOP ----------
while True:
pygame.event.pump()
# ----- JOYSTICKS -----
lx = js.get_axis(0)
ly = js.get_axis(1)
rx = js.get_axis(2)
ry = js.get_axis(3)
if abs(lx) < DEADZONE: lx = 0
if abs(ly) < DEADZONE: ly = 0
if abs(rx) < DEADZONE: rx = 0
if abs(ry) < DEADZONE: ry = 0
dx = (lx + rx) * SPEED
dy = (ly + ry) * SPEED
pen.goto(pen.xcor() + dx, pen.ycor() - dy)
# ----- A: CHANGE COLOR -----
a = js.get_button(0)
if a and not a_lock:
color_index = (color_index + 1) % len(colors)
pen.color(colors[color_index])
a_lock = True
if not a:
a_lock = False
# ----- X: PEN UP / DOWN -----
x_btn = js.get_button(2)
if x_btn and not x_lock:
if pen_is_down:
pen.penup()
else:
pen.pendown()
pen_is_down = not pen_is_down
x_lock = True
if not x_btn:
x_lock = False
# ----- B: CLEAR -----
b = js.get_button(1)
if b and not b_lock:
pen.clear()
b_lock = True
if not b:
b_lock = False
# ----- Y: CURSOR SHAPE -----
y = js.get_button(3)
if y and not y_lock:
shape_index = (shape_index + 1) % len(shapes)
pen.shape(shapes[shape_index])
y_lock = True
if not y:
y_lock = False
# ----- D-PAD SHAPES -----
hat = js.get_hat(0)
if hat != (0, 0) and not hat_lock:
if hat == (0, 1):
pen.circle(50)
elif hat == (1, 0):
draw_square()
elif hat == (-1, 0):
draw_triangle()
elif hat == (0, -1):
draw_hexagon()
hat_lock = True
if hat == (0, 0):
hat_lock = False
# ----- LB / RB: PEN WIDTH -----
lb = js.get_button(4)
rb = js.get_button(5)
if lb and not lb_lock:
pen_width = max(MIN_WIDTH, pen_width - 1)
pen.width(pen_width)
lb_lock = True
if not lb:
lb_lock = False
if rb and not rb_lock:
pen_width = min(MAX_WIDTH, pen_width + 1)
pen.width(pen_width)
rb_lock = True
if not rb:
rb_lock = False
# ----- LT: FULLSCREEN TOGGLE -----
lt = js.get_axis(4)
if lt > 0.5 and not lt_lock:
fullscreen = not fullscreen
if fullscreen:
screen.setup(width=1.0, height=1.0)
else:
screen.setup(800, 600)
lt_lock = True
if lt <= 0.5:
lt_lock = False
# ----- RT: HOLD 3s TO EXIT -----
rt = js.get_axis(5)
if rt > RT_THRESHOLD:
if rt_hold_start is None:
rt_hold_start = time.time()
elif time.time() - rt_hold_start >= 3:
turtle.bye()
pygame.quit()
break
else:
rt_hold_start = None
time.sleep(0.01)