-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_mario.py
More file actions
218 lines (187 loc) · 7.11 KB
/
example_mario.py
File metadata and controls
218 lines (187 loc) · 7.11 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# ----------------------------------------------------------------------------
# TinyDrawer for Raspberry Pi Pico is licensed under the MLT License.
# Created by Saranomy 2024.
# ----------------------------------------------------------------------------
from machine import Pin, SPI, PWM
import framebuf, time, random, micropython, math
from lcd_1inch14 import LCD_1inch14
from tinydrawer import TinyDrawer
# setup for lcd_1inch14.py
BL = 13 # blacklight/BLK pin
DC = 8 # data/command pin
RST = 12 # reset/RES pin
MOSI = 11 # serial data line/SDA pin
SCK = 10 # serial clock line/SDL pin
CS = 9 # chip select pin
# input pins
PIN_A = 15
PIN_B = 17
PIN_UP = 2
PIN_CTRL = 3
PIN_LEFT = 16
PIN_DOWN = 18
PIN_RIGHT = 20
# static variables
FPS = 30
SHOW_FPS = True
display_w = 240
display_h = 135
td = TinyDrawer(
"""
000877004fff94ff000000000000000000000000000000000000000000000000
008888804f9994f9000000000000000000000000000000000000000000000000
004f5f004999949900000000000aa000000aa000000000000000000000000000
00fffff0449994440008880000aa9a00000aa000000000000000000000000000
000f44004f494ff90088788000aa9a00000aa000000000000000000000000000
008c8c8049f4f9990078887000aa9a00000aa000000000000000000000000000
007aca70499499990000f000000aa000000aa000000000000000000000000000
0004040049949999000fff000000000000000000000000000000000000000000
0008778094999499000777000000000000000000000000000000000000000000
0088880044444444007787700000000000000000000000000000000000000000
004f5f0099949994008777800000000000000000000000000000000000000000
00fffff0444444440000f0000000000000000000000000000000000000000000
000f440094999499000fff000000000000000000000000000000000000000000
008c8c874444444400fcccf00000000000000000000000000000000000000000
070aca0099949994000777000000000000000000000000000000000000000000
0004040044444444000404000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
""", display_w = display_w, display_h = display_h)
class Coin:
def __init__(self, x, y, fb):
self.x, self.y = x, y
self.count = 0
def draw(self):
self.count += 1
if self.count > 10:
n = 4
else:
n = 3
if self.count > 20:
self.count = 0
td.spr(fb, n, self.x, self.y)
class Player:
def __init__(self, x, y, fb):
self.x, self.y = x, y
self.y_floor = self.y
self.vy = 0
self.facing_left = False
self.fb = fb
self.autoplay = True
self.is_luigi = False
self.next_move = 0
self.countdown = 0
def move(self, dx):
self.facing_left = dx < 0
self.x += dx * td.zoom
if self.x < -8 * td.zoom:
self.x = td.display_w
elif self.x > td.display_w:
self.x = -8 * td.zoom
def draw(self):
self.y += self.vy
if self.y < self.y_floor:
self.vy += td.zoom
else:
self.y = self.y_floor
self.vy = 0
if self.y == self.y_floor:
n = 0
else:
n = 8
if self.autoplay:
if self.countdown == 0:
self.countdown = random.randint(3, 30)
self.next_move = random.randint(-1, 1)
self.countdown -= 1
self.move(self.next_move)
if not self.next_move == 0 and random.randint(0, 6) == 0:
self.jump()
if self.is_luigi:
td.pal(8, 11)
td.spr(fb, n, self.x, self.y, flip_x=self.facing_left)
td.pal()
def jump(self):
if self.vy == 0 and self.y == self.y_floor:
self.vy = -td.zoom * 4
if __name__=='__main__':
pwm = PWM(Pin(BL))
pwm.freq(1000)
pwm.duty_u16(65535) # brightness [0-32768-65535]
fb = LCD_1inch14(CS, RST, DC, MOSI, SCK, width=display_w, height=display_h)
td.zoom = 4
step = 8 * td.zoom
player = Player(2 * step, display_h - 2 * step, fb)
coin = Coin(step, display_h - 4 * step, fb)
keyA = Pin(PIN_A, Pin.IN, Pin.PULL_UP)
keyB = Pin(PIN_B, Pin.IN, Pin.PULL_UP)
key2 = Pin(PIN_UP, Pin.IN, Pin.PULL_UP)
key3 = Pin(PIN_CTRL, Pin.IN, Pin.PULL_UP)
key4 = Pin(PIN_LEFT, Pin.IN, Pin.PULL_UP)
key5 = Pin(PIN_DOWN, Pin.IN, Pin.PULL_UP)
key6 = Pin(PIN_RIGHT, Pin.IN, Pin.PULL_UP)
elapsed_time = 1000
f = FPS
tiles = [
[-1,9,-1,-1,-1,11,-1], # top row
[-1,-1,-1,-1,-1,2,10], # bottom row
]
# clear the entire screen
fb.fill(td.color(1))
# paint the area that won't be updated
for i in range(0, math.ceil(display_w/step)):
td.spr(fb, 1, i * step, display_h - step)
# report stat before entering draw loop
micropython.mem_info()
while(1):
start_time = time.ticks_ms()
# clear 3 rows above the ground
fb.fill_rect(0, display_h - 4 * step, display_w, 3 * step, td.color(1))
if SHOW_FPS:
fb.text(f"{f} fps", int(display_w / 2), display_h - 4 * step, td.color(7))
if(keyA.value() == 0): # a
player.jump()
if(keyB.value() == 0): # b
player.autoplay = not player.autoplay
if(key2.value() == 0): # up
player.move(0)
if(key3.value() == 0): # ctrl
player.move(0)
if(key4.value() == 0): # left
player.move(-1)
if(key5.value() == 0): # down
player.move(0)
if(key6.value() == 0): # right
player.move(1)
# draw tiles
for y in range(len(tiles)):
for x in range(len(tiles[0])):
if tiles[y][x] >= 0:
td.spr(fb, tiles[y][x], x * 8 * td.zoom, display_h - 8 * td.zoom * 2 * len(tiles) + (y + 1) * 8 * td.zoom)
coin.draw()
player.draw()
# ship the frame
fb.show()
# update fps
end_time = time.ticks_ms()
elapsed_time = time.ticks_diff(end_time, start_time)
frame_time = 1000 / FPS
if elapsed_time < frame_time:
time.sleep((frame_time - elapsed_time) / 1000)
f = FPS
elif SHOW_FPS:
f = "{:.1f}".format(1000 / elapsed_time)