-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
138 lines (102 loc) · 4.15 KB
/
main.py
File metadata and controls
138 lines (102 loc) · 4.15 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
import math
import pygame
import joy
class Main:
def __init__(self):
self.SCREEN_WIDTH = 800
self.SCREEN_HEIGHT = 450
self.screen = pygame.display.set_mode((self.SCREEN_WIDTH,self.SCREEN_HEIGHT))
self.objects = []
self.mode = 1
pygame.font.init()
self.font = pygame.font.Font(pygame.font.get_default_font(),32)
def setupGame(self):
self.clock = pygame.time.Clock()
self.FPS = 60
self.joy = joy.Joystick_L()
self.joy2 = joy.Joystick_R()
self.objects.append(self.joy)
self.objects.append(self.joy2)
l = self.objects[0].get_numbuttons()
self.buttonArr1 = [0 for i in range(l)]
def runGame(self):
self.gameRunning = 1
while self.gameRunning:
buttonArr1 = [0 for i in range(len(buttonArr1))]
self.getInput()
self.compute()
self.draw(self.screen)
self.clock.tick(self.FPS)
#self.leftX = pygame.joystick.Joystick(0).get_axis(0)
#self.leftY = -1 * pygame.joystick.Joystick(0).get_axis(1)
#self.rightX = pygame.joystick.Joystick(1).get_axis(0)
#self.rightY = -1 * pygame.joystick.Joystick(1).get_axis(1)
self.leftX = self.objects[0].get_axis(0)
self.leftY = self.objects[0].get_axis(1)
#self.rightX = self.objects[1].get_axis(0)
#self.rightY = self.objects[1].get_axis(1)
#handle buttons
for event in pygame.event.get(pygame.JOYBUTTONUP): #event handling loop
#handle mode switching - buttons 8/9 on both sticks
print(event)
if (event.button == 7): #button 8 increases mode
buttonArr1[7] = 1
if (self.mode == 3):
self.mode = 1
else:
self.mode = self.mode + 1
print("Mode is now: " + str(self.mode))
if (event.button == 8): #button 9 decreases mode
buttonArr1[8] = 1
if (self.mode == 1):
self.mode = 3
else:
self.mode = self.mode - 1
print("Mode is now: " + str(self.mode))
def getInput(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.gameRunning = 0
#if(self.joy.joyInitL == True):
pygame.display.set_caption(str(self.joy.x) + ', ' + str(self.joy.y) + ', ' + str(self.joy.rad) + ', ' + str(self.joy.throttle))
def draw(self,surface):
self.screen.fill((255,255,255))
#for o in self.objects:
# o.draw(self.screen)
r = 200
w = surface.get_width()
h = surface.get_height()
for i in xrange(self.joy.numButtons):
if self.joy.buttons[i]:
col = (0,255,0)
else:
col = (64,0,64)
text = self.font.render(str(i),1,col)
surface.blit(text,text.get_rect(centerx=w*(i+1)/(self.joy.numButtons+1),centery=h/2-30))
x = int(round(w/2+self.joy.x*r))
y = int(round(h/2+self.joy.y*r))
pygame.draw.aaline(surface,(128,128,128),(w/2,h/2),(x,y),1)
pygame.draw.circle(surface,(0,0,0),(x,y),8,4)
#pygame.draw.circle(surface,(0,255,255),(w/2,h/2),r,2)
for i in xrange(self.joy2.numButtons):
if self.joy2.buttons[i]:
col = (0,255,0)
else:
col = (64,0,64)
text = self.font.render(str(i),1,col)
surface.blit(text,text.get_rect(centerx=w*(i+1)/(self.joy2.numButtons+1),centery=h/2+30))
x1 = int(round(w/2+self.joy2.x*r))
y1 = int(round(h/2+self.joy2.y*r))
pygame.draw.aaline(surface,(128,0,0),(w/2,h/2),(x1,y1),1)
pygame.draw.circle(surface,(0,0,0),(x1,y1),8,4)
pygame.draw.circle(surface,(0,255,255),(w/2,h/2),r,2)
pygame.display.flip()
def compute(self):
i = 0
while i < len(self.objects):
self.objects[i].compute()
i += 1
m = Main()
m.setupGame()
m.runGame()
pygame.quit()