-
Notifications
You must be signed in to change notification settings - Fork 1
/
joystick.py
63 lines (55 loc) · 1.6 KB
/
joystick.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
from sense_hat import SenseHat
from sensegame import Joystick
import random
def random_color():
new_color = [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)]
index = random.randint(0, 2)
new_color[index] -= 100
return new_color
def hello(arg):
global color
if arg == "UP":
new_value = position[1] - 1
if new_value < 0:
new_value = 7
color = random_color()
position[1] = new_value
elif arg == "DOWN":
new_value = position[1] + 1
if new_value > 7:
new_value = 0
color = random_color()
position[1] = new_value
elif arg == "LEFT":
new_value = position[0] - 1
if new_value < 0:
new_value = 7
color = random_color()
position[0] = new_value
elif arg == "RIGHT":
new_value = position[0] + 1
if new_value > 7:
new_value = 0
color = random_color()
position[0] = new_value
elif arg == "CENTER":
pass
#sense.set_pixel(position[0], position[1], color)
old_color = sense.get_pixel(*position)
if not old_color == [0, 0, 0]:
color = [
int((old_color[0] + color[0])/2),
int((old_color[1] + color[1])/2),
int((old_color[2] + color[2])/2)
]
sense.set_pixel(*position, color)
if __name__ == '__main__':
sense = SenseHat()
sense.clear()
joystick = Joystick()
# (x, y)
position = [4, 4]
color = (255, 0, 0)
joystick.register_key_press_callback(hello)
while True:
pass