-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation.py
77 lines (68 loc) · 2.24 KB
/
navigation.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
import sys
import pygame as pg
from scipy.interpolate import interp1d
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
rect = pg.Rect(300, 220, 20, 20)
velocity = (0, 0)
done = False
pg.joystick.init()
joystick = pg.joystick.Joystick(0)
print(joystick)
joystick.init()
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
axis1 = joystick.get_axis( 1 ) #for forward movement
axis0 = joystick.get_axis( 0 ) #for left and right movement
axis3 = joystick.get_axis( 4 ) #for speed control
button = joystick.get_button( 0 )
print(axis3)
axis3*=-1 #to reverse the mapping
axis3+=1 #now range = (0,2)
if axis3 < 0.005:
axis3=0
m = interp1d([0,2],[0,12]) #linear mapping funtion
speed = int(m(axis3)) #stm can handle only integer
print("speed",speed)
hat = joystick.get_hat( 0 )
keys = pg.key.get_pressed()
if keys[pg.K_a] or axis0 <= -0.85 and hat == (0,0): #to move left
if button == 0:
rect.x -= speed
if button ==1:
rect.x -= 5
elif keys[pg.K_d] or axis0 >= 0.85 and hat == (0,0): #to move right
if button == 0:
rect.x += speed
if button ==1:
rect.x += 5
elif keys[pg.K_w] or axis1 <= -0.85 and hat == (0,0): #to move up
if button == 0:
rect.y -= speed
if button ==1:
rect.y -= 5
elif keys[pg.K_s] or axis1 >= 0.85 and hat == (0,0): #to move down
if button == 0:
rect.y += speed
if button ==1:
rect.y += 5
if hat == (-1,0): #to move left
rect.x -= 3
if hat == (1,0): #to move right
rect.x += 3
if hat == (0,1): #to move up
rect.y -= 3
if hat == (0,-1): #to move down
rect.y += 3
screen.fill((40, 40, 40))
pg.draw.rect(screen, (150, 200, 20), rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
sys.exit()