-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontroller_test.py
executable file
·96 lines (79 loc) · 3.78 KB
/
controller_test.py
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
#!/usr/bin/env python
import pygame
import spriteManager
import settingsManager
def main():
pygame.init()
pygame.joystick.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Joystick Test')
joystick_count = pygame.joystick.get_count()
joysticks = []
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
offset = 0
for i in range(joystick.get_numaxes() / 2):
joysticks.append(JoystickMonitor(offset))
offset += 200
clock = pygame.time.Clock()
status = True
while status:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.JOYAXISMOTION:
joystick = pygame.joystick.Joystick(event.joy)
if event.axis == 0 or event.axis == 1:
xaxis = 0
yaxis = 1
stick = 0
x, y = (joystick.get_axis(xaxis) * 100,joystick.get_axis(yaxis) * 100)
joysticks[stick].relocateJoystick(x,y)
elif event.axis == 3 or event.axis == 4:
xaxis = 4
yaxis = 3
stick = 1
x, y = (joystick.get_axis(xaxis) * 100,joystick.get_axis(yaxis) * 100)
joysticks[stick].relocateJoystick(x,y)
screen.fill([100,100,100])
for monitor in joysticks:
monitor.draw(screen,monitor.rect.topleft,1.0)
pygame.display.flip()
clock.tick(60)
class JoystickMonitor(spriteManager.Sprite):
def __init__(self,_position):
spriteManager.Sprite.__init__(self)
self.joystick_sprite = spriteManager.ImageSprite(settingsManager.createPath('sprites/joyTest.png'))
self.j_loc_sprite = spriteManager.RectSprite(pygame.Rect(0,0,10,10),[255,255,255])
self.j_status = spriteManager.TextSprite('No Input','Orbitron Medium')
self.j_status.rect.midtop = self.joystick_sprite.rect.midbottom
self.rect = pygame.Rect(_position,0,self.j_loc_sprite.rect.height + self.j_status.rect.height, self.j_loc_sprite.rect.width)
def relocateJoystick(self,_x,_y):
self.j_loc_sprite.rect.centerx = _x + 100
self.j_loc_sprite.rect.centery = _y + 100
if abs(_x) <= 10 and abs(_y) <= 10:
#dead zone
self.j_status.changeText('No Input')
if _y > 10 and _y >= abs(_x):
self.j_status.changeText('Down')
if _y < -10 and -_y >= abs(_x):
self.j_status.changeText('Up')
if _x < -10 and -_x >= abs(_y):
self.j_status.changeText('Left')
if _x > 10 and _x >= abs(_y):
self.j_status.changeText('Right')
if _y > 70 and _y >= abs(_x):
self.j_status.changeText('Smash Down')
if _y < -70 and -_y >= abs(_x):
self.j_status.changeText('Smash Up')
if _x < -70 and -_x >= abs(_y):
self.j_status.changeText('Smash Left')
if _x > 70 and _x >= abs(_y):
self.j_status.changeText('Smash Right')
def draw(self,_screen,_offset,_scale):
ox, oy = _offset
self.joystick_sprite.draw(_screen, (self.joystick_sprite.rect.left + ox,self.joystick_sprite.rect.top + oy), _scale)
self.j_loc_sprite.draw(_screen, (self.j_loc_sprite.rect.left + ox,self.j_loc_sprite.rect.top + oy), _scale)
self.j_status.draw(_screen, (self.j_status.rect.left + ox,self.j_status.rect.top + oy), _scale)
if __name__ == '__main__': main()