forked from murilopolese/pixelkit-micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadventure.py
More file actions
188 lines (188 loc) · 13.2 KB
/
adventure.py
File metadata and controls
188 lines (188 loc) · 13.2 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
import pixelkit as kit
from time import sleep
from random import randint as random
world = [[247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247],[3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3],[247,247,247,247,52,247,247,247,247,52,247,247,52,247,247,247,247,247,247,52,247,247,52,247,247,247,247,247,52,247,247,247],[247,247,247,247,52,247,247,247,247,52,247,247,52,52,52,3,3,52,247,52,52,52,52,247,247,247,247,247,52,247,247,247],[247,52,52,52,52,52,52,247,247,52,52,52,52,247,52,3,3,52,52,52,247,247,52,247,247,52,52,52,52,52,52,247],[247,52,247,247,247,247,52,247,247,52,247,247,52,247,247,247,247,247,247,52,247,247,52,247,247,52,247,247,247,247,52,247],[3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3],[247,247,52,247,247,52,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,52,247,247,52,247,247],[247,247,52,247,247,52,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,52,247,247,52,247,247],[3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3],[247,52,247,247,247,247,52,247,247,52,247,247,52,247,247,247,52,247,247,52,247,247,52,247,247,52,247,247,247,247,52,247],[247,52,52,52,52,52,52,247,247,52,247,247,52,52,52,3,3,247,247,52,52,52,52,247,247,52,52,52,52,52,52,247],[247,247,247,52,247,247,247,247,247,52,52,52,52,247,247,3,3,52,52,52,247,247,52,247,247,247,247,52,247,247,247,247],[247,247,247,52,247,247,247,247,247,52,247,247,52,247,247,52,247,247,247,52,247,247,52,247,247,247,247,52,247,247,247,247],[3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3],[247,247,247,247,247,247,247,247,247,247,247,247,3,247,247,247,247,247,247,247,3,247,247,247,247,247,247,247,247,247,247,247],[247,52,52,52,52,52,52,52,247,247,247,247,3,247,247,247,247,247,247,247,3,247,247,247,52,52,52,52,52,52,52,247],[247,52,247,247,247,52,247,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,247,52,247,247,247,52,247],[247,52,247,247,247,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247],[3,52,52,247,247,52,52,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,52,52,247,247,52,52,3],[247,247,52,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,247,52,247,52,247,247,52,247,247],[247,247,52,247,247,52,247,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,247,52,247,247,52,247,247],[247,52,52,52,52,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,52,52,52,52,247],[247,52,247,247,247,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247],[247,52,247,247,247,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,247,247,247,52,247],[247,52,52,52,52,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,52,52,52,52,247],[247,247,52,247,247,52,247,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,247,52,247,247,52,247,247],[247,247,52,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,247,52,247,52,247,247,52,247,247],[3,52,52,247,247,52,52,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,52,52,247,247,52,52,3],[247,52,247,247,247,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247],[247,52,247,247,247,52,247,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,247,52,247,247,247,52,247],[247,3,247,247,247,3,247,3,247,247,247,247,3,247,247,247,247,247,247,247,3,247,247,247,3,247,3,247,247,247,3,247],[247,3,247,247,247,3,247,3,247,247,247,247,3,247,247,247,247,247,247,247,3,247,247,247,3,247,3,247,247,247,3,247],[247,52,247,247,247,52,247,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,247,52,247,247,247,52,247],[247,52,247,247,247,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247],[3,52,52,247,247,52,52,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,52,52,247,247,52,52,3],[247,247,52,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,247,52,247,52,247,247,52,247,247],[247,247,52,247,247,52,247,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,247,52,247,247,52,247,247],[247,52,52,52,52,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,52,52,52,52,247],[247,52,247,247,247,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247],[247,52,247,247,247,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,247,247,247,52,247],[247,52,52,52,52,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,52,52,52,52,247],[247,247,52,247,247,52,247,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,247,52,247,247,52,247,247],[247,247,52,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,247,52,247,52,247,247,52,247,247],[3,52,52,247,247,52,52,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,52,52,247,247,52,52,3],[247,52,247,247,247,52,247,52,247,247,52,247,52,247,52,247,247,247,52,247,52,247,52,247,52,247,52,247,247,247,52,247],[247,52,247,247,247,52,247,52,247,52,52,52,52,52,52,247,247,52,52,52,52,52,52,247,52,247,52,247,247,247,52,247],[247,52,52,52,52,52,52,52,247,247,247,247,3,247,247,247,247,247,247,247,3,247,247,247,52,52,52,52,52,52,52,247],[247,247,247,247,247,247,247,247,247,247,247,247,3,247,247,247,247,247,247,247,3,247,247,247,247,247,247,247,247,247,247,247],[3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3],[247,247,247,247,52,247,247,247,247,52,247,247,52,247,247,247,247,247,247,52,247,247,52,247,247,247,247,247,52,247,247,247],[247,247,247,247,52,247,247,247,247,52,247,247,52,52,52,3,3,52,247,52,52,52,52,247,247,247,247,247,52,247,247,247],[247,52,52,52,52,52,52,247,247,52,52,52,52,247,52,3,3,52,52,52,247,247,52,247,247,52,52,52,52,52,52,247],[247,52,247,247,247,247,52,247,247,52,247,247,52,247,247,247,247,247,247,52,247,247,52,247,247,52,247,247,247,247,52,247],[3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3],[247,247,52,247,247,52,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,52,247,247,52,247,247],[247,247,52,247,247,52,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,52,247,247,52,247,247],[3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3],[247,52,247,247,247,247,52,247,247,52,247,247,52,247,247,247,247,247,247,52,247,247,52,247,247,52,247,247,247,247,52,247],[247,52,52,52,52,52,52,247,247,52,247,247,52,52,52,3,3,52,247,52,52,52,52,247,247,52,52,52,52,52,52,247],[247,247,247,52,247,247,247,247,247,52,52,52,52,247,52,3,3,52,52,52,247,247,52,247,247,247,247,52,247,247,247,247],[247,247,247,52,247,247,247,247,247,52,247,247,52,247,247,247,247,247,247,52,247,247,52,247,247,247,247,52,247,247,247,247],[3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3,3,52,52,52,52,52,52,3],[247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247,247]]
width = int(16*4)
height = int(8*4)
colors = {
'52': [0, 0, 0], # path
'3': [4, 0, 3], # portal
'247': [5, 0, 10], # wall
'player': [10, 10, 0],
'heading': [10, 5, 0],
'treasure': [25, 0, 0],
'npc': [15, 0, 15],
'npc_heading': [5, 10, 5]
}
UP = [0, -1]
DOWN = [0, 1]
LEFT = [-1, 0]
RIGHT = [1, 0]
dirs = [UP, RIGHT, DOWN, LEFT]
heading = 1
game_over = False
p_pos = [1, 4] # player position
p_dir = dirs[heading] # player direction
w_pos = [0, 0] # world position
t_pos = []
npcs = [] # [x, y, heading]
score = 0
def rect(x, y, width, height, color):
for h in range(y, y+height):
for w in range(x, x+width):
kit.set_pixel(w, h, color)
def draw_world(w, x, y):
for wx in range(0, 16):
for wy in range(0, 8):
nx = ((x*16) + wx + width) % width
ny = ((y*8) + wy + height) % height
d = str(w[nx][ny])
if d in colors.keys():
c = colors[d]
for t in t_pos:
if nx == t[0] and ny == t[1]:
c = colors['treasure']
else:
c = colors['3']
kit.set_pixel(wx, wy, c)
def draw_player(p_pos):
x = p_pos[0]
y = p_pos[1]
np = [ x % 16, y % 8 ]
nd = [
(x + p_dir[0]) % 16,
(y + p_dir[1]) % 8
]
kit.set_pixel(np[0], np[1], colors['player'])
kit.set_pixel(nd[0], nd[1], colors['heading'])
def draw_npcs(npcs, w_pos):
x = w_pos[0]
y = w_pos[1]
for npc in npcs:
npc_quadrant = calc_w_pos(npc[0], npc[1])
if npc_quadrant[0] == w_pos[0] and npc_quadrant[1] == w_pos[1]:
np = [ npc[0] % 16, npc[1] % 8 ]
nd = [
(npc[0] + dirs[npc[2]][0]) % 16,
(npc[1] + dirs[npc[2]][1]) % 8
]
kit.set_pixel(np[0], np[1], colors['npc'])
kit.set_pixel(nd[0], nd[1], colors['npc_heading'])
def add(a, b):
return [ a[0]+b[0], a[1]+b[1] ]
def calc_w_pos(x, y):
wx = int(x / 16) % 4
wy = int(y / 8) % 4
return [wx, wy]
def get_random_position():
return [random(0, width-1), random(0, height-1)]
def place_treasure(w):
nt_pos = get_random_position()
if w[nt_pos[0]][nt_pos[1]] != 52:
return place_treasure(w)
else:
return nt_pos
def draw_treasure_map(p_pos, t_pos):
kit.clear()
p_quad = calc_w_pos(p_pos[0], p_pos[1])
rect(p_quad[0]*4, p_quad[1]*2, 4, 2, colors['player'])
for t in t_pos:
t_quad = calc_w_pos(t[0], t[1])
rect(t_quad[0]*4, t_quad[1]*2, 4, 2, colors['treasure'])
for i in range(0, score):
kit.set_pixel(i%16, int(i/16), colors['heading'])
kit.render()
sleep(1)
def left():
global heading
global p_dir
heading = (heading - 1 + 4) % 4
p_dir = dirs[heading]
def right():
global heading
global p_dir
heading = (heading + 1 + 4) % 4
p_dir = dirs[heading]
kit.on_button_b = right
kit.on_button_a = left
t_pos.append(place_treasure(world))
draw_treasure_map(p_pos, t_pos)
npcs.append([15, 5, 2])
while not game_over:
kit.check_controls()
# Calculate which world position/quadrant player is
w_pos = calc_w_pos(p_pos[0], p_pos[1])
# Draw the correct section of the world
draw_world(world, w_pos[0], w_pos[1])
# Player is always visible so always draw player
draw_player(p_pos)
# Draw npcs if they are on the same world position/quadrant
draw_npcs(npcs, w_pos)
# Calculate player next step
np_pos = add(p_pos, p_dir)
np_pos[0] %= width
np_pos[1] %= height
state = world[np_pos[0]][np_pos[1]]
# Calculate npcs next move
for i, npc in enumerate(npcs):
n_pos = add(npc, dirs[npc[2]])
n_pos[0] %= width
n_pos[1] %= height
n_state = world[n_pos[0]][n_pos[1]]
# Move npc if next step is steppable
if (n_state == 52 or n_state == 3):
npcs[i] = [n_pos[0], n_pos[1], npc[2]]
else:
if random(0, 10) > 8:
npcs[i][2] = (npc[2] - 1) % 4
else:
npcs[i][2] = (npc[2] + 1) % 4
# Randomize heading
if random(0, 10) > 8:
npcs[i][2] = (npc[2] - 1) % 4
# Check if player os on an npc
for npc in npcs:
if npc[0] == np_pos[0] and npc[1] == np_pos[1]:
game_over = True
# Move player if next step is steppable
if (state == 52 or state == 3):
p_pos = np_pos
# Check if user is on top of a treasure
for i, t in enumerate(t_pos):
if p_pos[0] == t[0] and p_pos[1] == t[1]:
score += 1
t_pos.pop(i)
if score < 7:
t_pos.append(place_treasure(world))
x = get_random_position()
world[x[0]][x[1]] = 247
elif score < 15:
if len(t_pos) == 0:
t_pos.append(place_treasure(world))
t_pos.append(place_treasure(world))
elif score < 23:
if len(t_pos) == 0:
for i in range(0, 3):
t_pos.append(place_treasure(world))
n_pos = place_treasure(world)
npcs.append([n_pos[0], n_pos[1], 1])
elif score < 32:
if len(t_pos) == 0:
for i in range(0, 4):
t_pos.append(place_treasure(world))
for i in range(0, 2):
n_pos = place_treasure(world)
npcs.append([n_pos[0], n_pos[1], 1])
else:
kit.clear()
while True:
kit.set_pixel(random(0, 15), random(0, 7), [random(0, 30), random(0, 30), random(0, 30)])
kit.render()
sleep(0.01)
draw_treasure_map(p_pos, t_pos)
kit.render()
sleep(0.1)
kit.clear()
for i in range(0, score):
kit.set_pixel(i%16, int(i/16), colors['heading'])
kit.render()