-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKiethGetBetter.py
More file actions
243 lines (213 loc) · 6.89 KB
/
KiethGetBetter.py
File metadata and controls
243 lines (213 loc) · 6.89 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import pygame
import random
from time import sleep
# game speed
SPEED = 1
# Initialize Pygame
pygame.init()
# Define colors
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLACK = (2, 2, 2)
WHITE = (255, 255, 255)
# Define screen dimensions
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
# Create the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Joystick Game")
# known button values
# A = 0
# B = 1
# X = 2
# Y = 3
# LB = 4
# RB = 5
# 2square = 6
# select = 7
# LSB = 8
# RSB = 9
# Define joystick buttons
BUTTON_A = 0
BUTTON_B = 1
BUTTON_X = 2
BUTTON_Y = 3
BUTTON_LB = 4
BUTTON_RB = 5
BUTTON_SQ = 6
BUTTON_SELECT = 7
BUTTON_LSB = 8
BUTTON_RSB = 9
BUTTON_EJECT = BUTTON_RB
BUTTON_HIGH = BUTTON_Y
BUTTON_MID = BUTTON_X
BUTTON_LOW = BUTTON_A
BUTTON_INTAKE = BUTTON_LB
BUTTON_ARM_DOWN = BUTTON_LSB
BUTTON_ARM_INTAKE = BUTTON_SELECT
# Define joystick axes
AXIS_X = 0
AXIS_Y = 1
# Initialize joystick
joystick = None
if pygame.joystick.get_count() > 0:
joystick = pygame.joystick.Joystick(0)
joystick.init()
def display_text(text1):
# Create a font object
font = pygame.font.Font(None, 36)
# Render the text
text = font.render(text1, True, WHITE)
# Get the rect for the text surface
text_rect = text.get_rect()
# Center the text on the screen
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery
# Fill the screen with black
screen.fill(BLACK)
# Blit the text onto the surface
screen.blit(text, text_rect)
# Update the screen
pygame.display.update()
# Define game loop
def game_loop():
while True:
sleep(SPEED)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# reset screen
screen.fill(BLACK)
pygame.display.update()
# Check if joystick is connected
if joystick is None:
continue
# Get random action
action = random.randint(0, 6)
# good or bad
win = True
skip = False
# Handle action
if action == 0: # Press eject
#print("press A")
display_text("press: HIGH")
while not joystick.get_button(BUTTON_HIGH) and not skip:
for event in pygame.event.get():
print(event.type)
if event.type == 1540:
screen.fill(RED)
win = False
skip = True
if event.type == pygame.QUIT:
pygame.quit()
quit()
if win:
screen.fill(GREEN)
elif action == 1: # Press high
#print("press B")
display_text("press: MID")
while not joystick.get_button(BUTTON_MID) and not skip:
for event in pygame.event.get():
print(event.type)
if event.type == 1540:
screen.fill(RED)
win = False
skip = True
if event.type == pygame.QUIT:
pygame.quit()
quit()
if win:
screen.fill(GREEN)
elif action == 2: # Press mid
#print("press X")
display_text("press: LOW")
while not joystick.get_button(BUTTON_LOW) and not skip:
for event in pygame.event.get():
print(event.type)
if event.type == 1540:
screen.fill(RED)
win = False
skip = True
if event.type == pygame.QUIT:
pygame.quit()
quit()
if win:
screen.fill(GREEN)
elif action == 3: # Press low
#print("press Y")
display_text("press: EJECT")
while not joystick.get_button(BUTTON_EJECT) and not skip:
for event in pygame.event.get():
print(event.type)
if event.type == 1540:
screen.fill(RED)
win = False
skip = True
if event.type == pygame.QUIT:
pygame.quit()
quit()
if win:
screen.fill(GREEN)
elif action == 4: # Press low
#print("press Y")
display_text("press: INTAKE")
while not joystick.get_button(BUTTON_INTAKE) and not skip:
for event in pygame.event.get():
print(event.type)
if event.type == 1540:
screen.fill(RED)
win = False
skip = True
if event.type == pygame.QUIT:
pygame.quit()
quit()
if win:
screen.fill(GREEN)
elif action == 5: # Press low
#print("press Y")
display_text("press: ARM DOWN")
while not joystick.get_button(BUTTON_ARM_DOWN) and not skip:
for event in pygame.event.get():
print(event.type)
if event.type == 1540:
screen.fill(RED)
win = False
skip = True
if event.type == pygame.QUIT:
pygame.quit()
quit()
if win:
screen.fill(GREEN)
elif action == 6: # Press low
#print("press Y")
display_text("press: ARM INTAKE")
while not joystick.get_button(BUTTON_ARM_INTAKE) and not skip:
for event in pygame.event.get():
print(event.type)
if event.type == 1540:
screen.fill(RED)
win = False
skip = True
if event.type == pygame.QUIT:
pygame.quit()
quit()
if win:
screen.fill(GREEN)
# Update the screen
pygame.display.update()
print("sleeping")
#sleep(1)
# Start the game
if joystick is not None:
display_text("Press A to start")
while not joystick.get_button(BUTTON_A):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
display_text("Starting...")
game_loop()
else:
print("No joystick found")