-
Notifications
You must be signed in to change notification settings - Fork 0
/
rl_2.py
230 lines (200 loc) · 7.21 KB
/
rl_2.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
from time import perf_counter
from enum import IntEnum
from collections.abc import Sequence
import PyBearLibTerminal as terminal
import random
import yaml
class Game_States(IntEnum):
MAIN_MENU = 0
NEW_GAME = 1
IN_GAME = 2
OPTIONS = 3
HISTORY = 4
class Thing:
def __init__(self, glyph, x, y):
self.glyph = glyph
self.x = x
self.y = y
def __str__(self):
return 'Thing: glyph={}, x={}, y={}'.format(self.glyph, self.x, self.y)
class Tile:
def __init__(self):
self.glyph = '#'
class Map(Sequence):
def __init__(self, name, width, height, min_rooms, max_rooms):
self.name = name
self.width = width
self.height = height
self.min_rooms = min_rooms
self.max_rooms = max_rooms
self.layout = [[Tile() for y in range(height)] for x in range(width)]
def __getitem__(self, key):
return self.layout[key]
def __len__(self):
return self.width * self.height
def __str__(self):
info = 'Map: name={}, width={}, height={}\n'.format(self.name, self.width, self.height)
pic = '\n'.join([str([' . ' * len(y)]) for y in self.layout])
return info + pic
class Viewport:
def __init__(self, left_edge, top_edge):
self.left_edge = left_edge
self.top_edge = top_edge
class GameEngine:
def __init__(self):
self.window_width = 80
self.window_height = 45
self.screen_width = 58
self.screen_height = 36
self.cellsize = "12x12"
self.update_interval_ms = 0.033
self.update_per_frame_limit = 10
self.offset = Viewport(0, 0)
self.actors = []
self.state = Game_States.IN_GAME
def initialize_blt(self):
terminal.open()
terminal.set(
"window: size={}x{}, cellsize={}, title='Roguelike';"
"font: default;"
"input: filter=[keyboard+];"
"".format(
str(self.window_width),
str(self.window_height),
self.cellsize))
terminal.clear()
terminal.refresh()
terminal.color("white")
def load_world_data(self):
with open('data/world.yaml', 'r') as world_yaml:
self.world_data = yaml.load(world_yaml)
def generate_level(self, name):
current_level_data = self.world_data[name]
try:
self.current_world = Map(
name=current_level_data['name'],
width=current_level_data['width'],
height=current_level_data['height'],
min_rooms=current_level_data['min_rooms'],
max_rooms=current_level_data['max_rooms'],)
except Exception as ex:
print('you fucked up:', ex)
return None
self.generate_player('human')
def generate_player(self, race):
with open('data/player.yaml', 'r') as player_yaml:
race_options = yaml.load(player_yaml)
self.races = race_options
chosen_race = race_options[race]
try:
self.pc = Thing(chosen_race['char'], 10, 10)
except Exception as ex:
print('you fucked up:', ex)
return None
def run(self):
UPDATE_INTERVAL_MS = self.update_interval_ms
UPDATE_PER_FRAME_LIMIT = self.update_per_frame_limit
clock = 0
previous_time = perf_counter()
next_update = 0
proceed = True
while proceed:
current_time = perf_counter()
if current_time - previous_time > (UPDATE_INTERVAL_MS * UPDATE_PER_FRAME_LIMIT):
clock += UPDATE_INTERVAL_MS * UPDATE_PER_FRAME_LIMIT
else:
clock += current_time - previous_time
while clock >= next_update:
time_elapsed = UPDATE_INTERVAL_MS
time_current = next_update
self.update(time_elapsed, time_current)
next_update += UPDATE_INTERVAL_MS
previous_time = perf_counter()
self.render()
if terminal.has_input():
key = terminal.read()
if key is terminal.TK_CLOSE:
proceed = False
else:
proceed = self.process_input(key)
def update(self, time_elapsed, time_current):
for actor in self.actors:
actor.take_turn()
def render(self):
terminal.clear()
self.render_viewport()
self.render_UI()
self.render_message_bar()
terminal.refresh()
def render_viewport(self):
self.set_offset()
for r_idx, row in enumerate(self.current_world):
for c_idx, tile in enumerate(row):
terminal.put(r_idx, c_idx, tile.glyph)
terminal.put(self.pc.x, self.pc.y, self.pc.glyph)
def render_UI(self):
pass
def render_message_bar(self):
pass
def set_offset(self):
"""
From: http://www.roguebasin.com/index.php?title=Scrolling_map
"""
center_x = self.screen_width // 2
center_y = self.screen_height // 2
if self.pc.x < center_x:
self.offset.left_edge = 0
elif self.pc.x > self.current_world.width - center_x:
self.offset.left_edge = self.current_world.width - self.screen_width
else:
self.offset.left_edge = self.pc.x - center_x
if self.pc.y < center_y:
self.offset.top_edge = 0
elif self.pc.y > self.current_world.height - center_y:
self.offset.top_edge = self.current_world.height - self.screen_height
else:
self.offset.top_edge = self.pc.y - center_y
def process_input(self, key):
if key == terminal.TK_Q and terminal.check(terminal.TK_SHIFT):
return False
elif key == terminal.TK_ESCAPE:
return False
if self.state == Game_States.MAIN_MENU:
self.menu_input(key)
elif self.state == Game_States.IN_GAME:
self.in_game_input(key)
return True
def menu_input(self, key):
pass
def in_game_input(self, key):
key_released = (terminal.TK_KEY_RELEASED, 0)[terminal.check(terminal.TK_SHIFT)]
if key == terminal.TK_L | key_released:
self.move_pc(1, 0)
elif key == terminal.TK_N | key_released:
self.move_pc(1, 1)
elif key == terminal.TK_J | key_released:
self.move_pc(0, 1)
elif key == terminal.TK_B | key_released:
self.move_pc(-1, 1)
elif key == terminal.TK_H | key_released:
self.move_pc(-1, 0)
elif key == terminal.TK_Y | key_released:
self.move_pc(-1, -1)
elif key == terminal.TK_K | key_released:
self.move_pc(0, -1)
elif key == terminal.TK_U | key_released:
self.move_pc(1, -1)
def move_pc(self, tx, ty):
dx, dy = self.pc.x + tx, self.pc.y + ty
if self.current_world.width > dx >= 0 and self.current_world.height > dy >= 0:
self.pc.x = dx
self.pc.y = dy
def cleanup(self):
terminal.close()
if __name__ == "__main__":
game = GameEngine()
game.initialize_blt()
game.load_world_data()
game.generate_level('debug')
game.run()
game.cleanup()