-
Notifications
You must be signed in to change notification settings - Fork 0
/
rogueflip.py
250 lines (194 loc) · 7.62 KB
/
rogueflip.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
Rogueflip is a roguelike dungeon crawler for the flipdot display. Levels can
be created with the tiled map editor (https://www.mapeditor.org/) and a corresponding tileset.
"""
import time
import pygame
import flipdotfont
import pytmx # https://github.com/bitcraft/PyTMX
DEFAULT_TMX_WORLD_FILE="ressources/rogueflip_world.tmx"
class Game:
"""A roguelike for a flipdot display."""
def __init__(self, flipdotdisplay, worldfile=DEFAULT_TMX_WORLD_FILE):
self.fdd = flipdotdisplay
self.game_running = False
pygame.init()
if pygame.joystick.get_count() > 0:
print("Joystick found")
self.joystick = pygame.joystick.Joystick(0)
self.joystick.init()
self.world = World(worldfile)
# top left position of current view inside the world
self.window_top_left = [0, 0]
self.player = self.world.find_player()
print("Player placed at", self.player.pos)
self.coins = self.world.find_coins()
print("Found", len(self.coins), "coins.")
self.fdd = flipdotdisplay
def run(self):
"""Start the game running in an endless loop."""
assert self.world.map.width % self.fdd.width == 0 and \
self.world.map.height % self.fdd.height == 0, \
"Width and height of the map must be a multiple of display width and height"
self.game_running = True
while self.game_running:
self.tick()
for x in range(self.fdd.width):
for y in range(self.fdd.height):
val = self.handle_px(x, y)
self.fdd.px(x, y, val)
self.fdd.show()
def handle_px(self, x, y):
x_, y_ = self.window_top_left[0] + x, self.window_top_left[1] + y
self.handle_input()
if self.player.pos == [x_, y_]:
return self.player.draw()
for coin in self.coins:
if coin.pos == [x_, y_]:
return coin.draw()
return self.world.is_wall(x_, y_)
def tick(self):
self.player.tick()
for coin in self.coins:
coin.tick()
def handle_input(self):
"""Handle user input from keyboard or joystick."""
plx, ply = self.player.pos
dx, dy = 0, 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
# handle keyboard input
if event.key == pygame.K_w:
dy = -1
elif event.key == pygame.K_a:
dx = -1
elif event.key == pygame.K_s:
dy = +1
elif event.key == pygame.K_d:
dx = +1
elif event.type == pygame.JOYAXISMOTION:
# handle joystick
dx += round(self.joystick.get_axis(0))
dy += round(self.joystick.get_axis(1))
else:
# ignore other events
continue
if not self.world.is_wall(plx+dx, ply+dy):
self.player.pos = [plx + dx, ply + dy]
self.player_try_collect_coin()
if len(self.coins) == 0:
self.show_win_message()
if not self.player_in_window():
self.move_window(dx, dy)
def show_win_message(self):
font = flipdotfont.big_font()
flipdotfont.TextScroller(self.fdd, "you", font)
self.fdd.show()
time.sleep(2)
flipdotfont.TextScroller(self.fdd, "win", font)
self.fdd.show()
time.sleep(2)
def player_try_collect_coin(self):
# remove coin under player
self.coins = [c for c in self.coins if c.pos != self.player.pos]
def player_in_window(self):
"""Check if the player is inside the windows."""
wtlx, wtly = self.window_top_left
px, py = self.player.pos
hor_inside = wtlx <= px < wtlx + self.fdd.width
ver_inside = wtly <= py < wtly + self.fdd.height
return hor_inside and ver_inside
def move_window(self, dx, dy):
"""Move the visible window by the amount of dx*width and dy*height of
the flipdotdisplay."""
self.window_top_left[0] += self.fdd.width * dx
self.window_top_left[1] += self.fdd.height * dy
class World:
WALL = 'wall'
PLAYER = 'player'
COIN = 'coin'
BACK = 'back'
def __init__(self, worldfile):
self.pixels = [] # list of tile types
self.map = pytmx.TiledMap(worldfile)
self.default_layer = 0
assert len(self.map.layers) == 1, "Assuming everything in one layer."
def get_type(self, x, y):
return self.map.get_tile_properties(x, y, self.default_layer)['type']
def is_onboard(self, x, y):
return 0 <= x < self.map.width and 0 <= y < self.map.height
def is_wall(self, x, y):
return self.is_onboard(x, y) and self.get_type(x, y) == World.WALL
def is_player(self, x, y):
return self.is_onboard(x,y) and self.get_type(x, y) == World.PLAYER
def is_coin(self, x, y):
return self.is_onboard(x,y) and self.get_type(x, y) == World.COIN
def _find_game_objects(self, typ):
gobjs = []
for x in range(self.map.width):
for y in range(self.map.height):
if self.get_type(x, y) == typ:
blink_int = self.map.get_tile_properties(
x, y, self.default_layer)['blink_interval']
en = GameObject(x, y, blink_int)
gobjs.append(en)
return gobjs
def find_player(self):
return self._find_game_objects(World.PLAYER)[0]
def find_coins(self):
return self._find_game_objects(World.COIN)
class GameObject:
def __init__(self, x, y, blink_interval=0.5):
self.pos = [x, y]
self.blink_interval = blink_interval
self.last_updated = time.time()
self.blink_on = False
def tick(self):
"""A method that should be invoked each frame."""
if time.time() - self.last_updated > self.blink_interval:
self.blink_on = not self.blink_on
self.last_updated = time.time()
def draw(self):
"""Determine whether the character should be drawn now."""
return self.blink_on
def run_simulator():
print("running a sample game in the simulator")
import configuration as conf
import flipdotsim
fdd = flipdotsim.FlipDotSim(width=conf.WIDTH, height=conf.HEIGHT)
run_with_flipdotdisplay(fdd)
def run_remote_display():
print("running a sample game on a remote display")
import configuration as conf
import net
fdd = net.RemoteDisplay(host=conf.remote_display["host"],
port=conf.remote_display["port"],
width=conf.WIDTH, height=conf.HEIGHT)
run_with_flipdotdisplay(fdd)
def run_with_flipdotdisplay(fdd):
g = Game(fdd, DEFAULT_TMX_WORLD_FILE)
g.run()
def test_roguegame():
import flipdotsim
import threading
import pygame.event
def user_event_generator():
print("creating events")
# run to the next screen
for k in 'waaasssssdssssasssssssssssssddddddddd':
key = ord(k)
print("posting key event", key)
pygame.event.post(
pygame.event.Event(pygame.KEYDOWN, key=key))
time.sleep(0.1)
g.game_running = False
fdd = flipdotsim.FlipDotSim()
g = Game(fdd, 'ressources/rogueflip_testworld.tmx')
th = threading.Thread(target=user_event_generator)
th.start()
g.run()
if __name__ == "__main__":
run_simulator()
#run_remote_display()