3 Virtual Joystick from Keyboard 16 windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
17 windowSurface.fill(BLACK)
18 pygame.display.set_caption(
'Virtual RC Joystick')
21 background = pygame.Surface(windowSurface.get_size())
22 background = background.convert()
23 background.fill((250, 250, 250))
26 dir = os.path.dirname(__file__)
27 filename = os.path.join(dir,
'../media/sticks.png')
28 img = pygame.image.load(filename)
30 windowSurface.blit(img,(0,0))
34 def __init__(self, name, stick, key_up, key_down, spring_back=True, incr_val=0.2):
87 if abs(int(round(self.
val)) - int(self.
emit_val)) > 0.001:
93 def set_display(self, offset_height, offset_width, horizontal):
98 filename = os.path.join(dir,
'../media/hg.png')
100 filename = os.path.join(dir,
'../media/hb.png')
103 filename = os.path.join(dir,
'../media/vg.png')
105 filename = os.path.join(dir,
'../media/vb.png')
128 pygame.display.flip()
131 if event.type == KEYUP:
132 if (event.key == self.
key_up):
136 elif event.type == KEYDOWN:
137 if (event.key == self.
key_up):
158 uinput.ABS_X + (0, 255, 0, 0),
159 uinput.ABS_Y + (0, 255, 0, 0),
160 uinput.ABS_THROTTLE + (0, 255, 0, 0),
161 uinput.ABS_RUDDER + (0, 255, 0, 0),
167 roll_stick =
stick_state(
'Roll', uinput.ABS_X, K_RIGHT, K_LEFT)
168 roll_stick.set_display(21, 39,
True)
169 sticks.append(roll_stick)
170 pitch_stick =
stick_state(
'Pitch', uinput.ABS_Y, K_UP, K_DOWN)
171 pitch_stick.set_display(328, 198,
False)
172 sticks.append(pitch_stick)
173 thr_stick =
stick_state(
'Throttle', uinput.ABS_THROTTLE, K_w, K_s,
False)
174 thr_stick.set_display(328, 95,
False)
175 sticks.append(thr_stick)
176 rud_stick =
stick_state(
'Yaw', uinput.ABS_RUDDER, K_d, K_a)
177 rud_stick.set_display(360, 39,
True)
178 sticks.append(rud_stick)
180 with uinput.Device(events)
as device:
183 for event
in pygame.event.get():
185 stick.update_event(event)
187 stick.update_stick(device)
190 if __name__ ==
"__main__":
def update_stick(self, device)
def set_display(self, offset_height, offset_width, horizontal)
def update_event(self, event)
def __init__(self, name, stick, key_up, key_down, spring_back=True, incr_val=0.2)