-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimon.py
135 lines (120 loc) · 3.7 KB
/
Simon.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
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
'''
Game of Simon, for Lilygo T-HMI
Made by Lesept (May 2023)
'''
import tft_config
import s3lcd
import vga1_bold_16x32 as font
from machine import SPI, Pin
from xpt2046 import Touch
from time import sleep
from random import randint
# Touch handler
t_x,t_y = 0,0
def touch_handler(x,y):
global t_x,t_y
t_x = x
t_y = y
# Display the 4 buttons
def displayButtons(cols, radius, xc, yc):
for i in range(2):
for j in range(2):
ncol = j * 2 + i
tft.fill_circle(xc[i], yc[j], radius + 5, s3lcd.WHITE)
tft.fill_circle(xc[i], yc[j], radius, cols[ncol])
tft.show()
# Animate button touch
def animateButton(color, xc, yc, radius):
tft.fill_circle(xc, yc, radius + 5, s3lcd.WHITE)
for i in range (int(radius) // 2):
tft.fill_circle(xc, yc, int(radius) // 2 + i, color)
tft.show()
def splashScreen():
tft.text(font, "Play SIMON", 45, 120, s3lcd.WHITE)
tft.text(font, "3", 117, 160, s3lcd.WHITE)
tft.show()
sleep(1)
tft.text(font, "2", 117, 160, s3lcd.WHITE)
tft.show()
sleep(1)
tft.text(font, "1", 117, 160, s3lcd.WHITE)
tft.show()
sleep(1)
tft.clear(s3lcd.BLACK)
# Display
tft = tft_config.config(tft_config.WIDE)
tft.init()
tft.clear(s3lcd.BLACK)
orientation = 0
tft.rotation(orientation)
width = tft.width()
height = tft.height()
# Touch interface
spi = SPI(1, baudrate=1000000)
spi.init(sck=Pin(01), mosi=Pin(03), miso=Pin(04))
cs = Pin(2, mode=Pin.OUT, value=1)
int_pin = Pin(9)
xpt = Touch(spi, cs=cs, int_pin=int_pin, int_handler=touch_handler)
xpt.calibrate(170, 1842, 139, 1830, 320, 240, 0)
splashScreen()
# Game variables
colors = []
score = 0
cols = [s3lcd.GREEN, s3lcd.RED, s3lcd.YELLOW, s3lcd.BLUE]
space = 30
radius = int((width - 3 * space) / 4)
xCenter = [space + radius, width - space - radius]
yCenter = [100, 230]
displayButtons(cols, radius, xCenter, yCenter)
gameOver = False
delay = 0.5
try:
while not gameOver:
# Draw new color
newColor = randint(0, 3)
colors.append(newColor)
# print(colors)
sleep(1)
# Animate buttons
for color in colors:
xc = xCenter[color % 2]
yc = yCenter[color // 2]
animateButton(cols[color], xc, yc, radius)
sleep(delay)
# Player's turn
count = 0
while count < len(colors):
# Wait for button touches
while not xpt.is_touched() of t_x < 0 or t_y < 0:
pass
button = -1
# Check if it is correct
for i in range(4):
xc = xCenter[i % 2]
yc = yCenter[i // 2]
if xc - radius < t_x < xc + radius and yc - radius < t_y < yc + radius:
button = i
break
# Animate the button touched
# print(f'Expected color: {colors[count]} - Button touched is {button}')
if button != -1:
animateButton(cols[button], xc, yc, radius)
# Stop if wrong button
if colors[count] != button:
gameOver = True
break
count += 1
score = len(colors) - 1
print(f'Score: {score}')
tft.fill_rect(40, 130, 180, 80, s3lcd.MAGENTA)
tft.text(font, "GAME OVER", 55, 140, s3lcd.WHITE, s3lcd.MAGENTA)
text = f'SCORE: {score}'
tft.text(font, text, 65, 170, s3lcd.WHITE, s3lcd.MAGENTA)
tft.show()
sleep(3)
except KeyboardInterrupt:
pass
tft.clear(s3lcd.BLACK) # Clear display
tft.show()
Pin(38).value(0) # Backlight OFF
tft.deinit()