forked from drewtorg/roguelike
/
game.py
444 lines (359 loc) · 13.9 KB
/
game.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import libtcodpy as libtcod
from tile import Tile
from object import Object
from rect import Rect
from map import Map
from equipment import Equipment
import components as Components
import textwrap
class Game:
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
LIMIT_FPS = 20
MAP_WIDTH = 80
MAP_HEIGHT = 43
FOV_ALGO = 0
FOV_LIGHT_WALLS = True
TORCH_RADIUS = 5
COLOR_LIT = libtcod.lighter_grey
COLOR_UNLIT = libtcod.dark_grey
BAR_WIDTH = 20
PANEL_HEIGHT = 7
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT
MSG_X = BAR_WIDTH + 2
MSG_WIDTH = SCREEN_WIDTH - BAR_WIDTH
MSG_HEIGHT = PANEL_HEIGHT - 1
INVENTORY_WIDTH = 50
LEVEL_UP_BASE = 200
LEVEL_UP_FACTOR = 150
LEVEL_SCREEN_WIDTH = 40
CHARACTER_SCREEN_WIDTH = 30
libtcod.console_set_custom_font('fonts/terminal8x12_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'Rougelike', False)
libtcod.sys_set_fps(LIMIT_FPS)
main_console = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
@staticmethod
def new_game():
Game.state = 'playing'
Game.dungeon_level = 1
Game.game_msgs = []
Game.mouse = libtcod.Mouse()
Game.key = libtcod.Key()
Game.inventory = []
Game.panel = libtcod.console_new(Game.SCREEN_WIDTH, Game.PANEL_HEIGHT)
Game.map = Map(Game.MAP_WIDTH, Game.MAP_HEIGHT)
libtcod.console_clear(Game.main_console)
_fighter_component = Components.Fighter(hp=100, dexterity=4, accuracy=20, power=4, xp=0, death_function=Components.player_death)
Game.player = Object(Game.map.origin[0], Game.map.origin[1], '@', 'Drew', libtcod.pink, blocks=True, fighter=_fighter_component)
Game.player.level = 1
Game.map.add_object(Game.player)
_equipment_component = Equipment(slot='right hand', power_bonus=2)
_obj = Object(0, 0, '-', 'dagger', libtcod.sky, equipment=_equipment_component)
Game.inventory.append(_obj)
_equipment_component.equip()
Game.message('Welcome stranger! Prepare to perish in the Tombs of the Ancient Kings.', libtcod.light_green)
@classmethod
def message(cls, new_msg, color=libtcod.white):
new_msg_lines = textwrap.wrap(new_msg, Game.MSG_WIDTH)
for line in new_msg_lines:
if len(cls.game_msgs) == Game.MSG_HEIGHT:
del cls.game_msgs[0]
cls.game_msgs.append( (line, color) )
@staticmethod
def render_messages():
y = 1
for (line, color) in Game.game_msgs:
libtcod.console_set_default_foreground(Game.panel, color)
libtcod.console_print_ex(Game.panel, Game.MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line)
y += 1
@staticmethod
def get_names_under_mouse():
(x, y) = (Game.mouse.cx, Game.mouse.cy)
names = [obj.name.capitalize() for obj in Game.map.objects
if obj.x == x and obj.y == y and Game.map.is_in_fov(obj)]
names = ', '.join(names)
return names
@staticmethod
def render_look():
libtcod.console_set_default_foreground(Game.panel, libtcod.light_grey)
libtcod.console_print_ex(Game.panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, Game.get_names_under_mouse())
@staticmethod
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color):
bar_width = int(float(value) / maximum * total_width)
#render the background
libtcod.console_set_default_background(Game.panel, back_color)
libtcod.console_rect(Game.panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN)
#render the bar on top
libtcod.console_set_default_background(Game.panel, bar_color)
if bar_width > 0:
libtcod.console_rect(Game.panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN)
#render the text
libtcod.console_set_default_foreground(Game.panel, libtcod.white)
libtcod.console_print_ex(Game.panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' + str(maximum))
@staticmethod
def render_all():
Game.map.recompute_fov()
Game.render_walls_and_floor()
Game.render_all_objects()
libtcod.console_blit(Game.main_console, 0, 0, Game.SCREEN_WIDTH, Game.SCREEN_HEIGHT, 0, 0, 0)
Game.render_panel()
libtcod.console_blit(Game.panel, 0, 0, Game.SCREEN_WIDTH, Game.PANEL_HEIGHT, 0, 0, Game.PANEL_Y)
libtcod.console_flush()
@staticmethod
def render_panel():
libtcod.console_set_default_background(Game.panel, libtcod.black)
libtcod.console_clear(Game.panel)
Game.render_messages()
Game.render_bar(1, 1, Game.BAR_WIDTH, 'HP', Game.player.fighter.hp, Game.player.fighter.max_hp, libtcod.light_red, libtcod.dark_red)
Game.render_look()
libtcod.console_print_ex(Game.panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Dungeon level ' + str(Game.dungeon_level))
@staticmethod
def render_all_objects():
for object in Game.map.objects:
if object != Game.player:
Game.draw_object(object)
Game.draw_object(Game.player)
@staticmethod
def render_walls_and_floor():
for y in range(Game.MAP_HEIGHT):
for x in range(Game.MAP_WIDTH):
# visible = True
visible = libtcod.map_is_in_fov(Game.map.fov_map, x, y)
wall = Game.map[x][y].block_sight
if not visible:
if Game.map[x][y].explored:
if wall:
libtcod.console_put_char_ex(Game.main_console, x, y, '#', Map.COLOR_UNLIT, libtcod.black)
else:
libtcod.console_put_char_ex(Game.main_console, x, y, '.', Map.COLOR_UNLIT, libtcod.black)
else:
Game.map[x][y].explored = True
if wall:
libtcod.console_put_char_ex(Game.main_console, x, y, '#', Map.COLOR_LIT, libtcod.black)
else:
libtcod.console_put_char_ex(Game.main_console, x, y, '.', Map.COLOR_LIT, libtcod.black)
@staticmethod
def draw_object(object):
if Game.map.is_in_fov(object):
libtcod.console_set_default_foreground(Game.main_console, object.color)
libtcod.console_put_char(Game.main_console, object.x, object.y, object.char, libtcod.BKGND_NONE)
elif object.always_visible and Game.map[object.x][object.y].explored:
darker = object.color * .65
libtcod.console_set_default_foreground(Game.main_console, darker)
libtcod.console_put_char(Game.main_console, object.x, object.y, object.char, libtcod.BKGND_NONE)
@staticmethod
def menu(header, options, width):
if header == '':
header_height = 0
if len(options) > 26:
raise ValueError('Cannot have a menu with more than 26 options')
header_height = libtcod.console_get_height_rect(Game.main_console, 0, 0, width, Game.SCREEN_HEIGHT, header)
height = len(options) + header_height
window = libtcod.console_new(width, height)
libtcod.console_set_default_foreground(window, libtcod.white)
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)
y = header_height
letter_index = ord('a')
for option_text in options:
text = '(' + chr(letter_index) + ')' + option_text
libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text)
y += 1
letter_index += 1
x = Game.SCREEN_WIDTH/2 - width/2
y = Game.SCREEN_HEIGHT/2 - height/2
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) #0.7 is the transparency
libtcod.console_flush()
key = libtcod.console_wait_for_keypress(True)
key = libtcod.console_wait_for_keypress(True)
index = key.c - ord('a')
if index >= 0 and index < len(options):
return index
return None
@staticmethod
def inventory_menu(header):
if len(Game.inventory) == 0:
options = ['Inventory is empty.']
else:
options = []
for item in Game.inventory:
text = item.name
if item.equipment and item.equipment.is_equipped:
text = text + ' (on ' + item.equipment.slot + ')'
options.append(text)
index = Game.menu(header, options, Game.INVENTORY_WIDTH)
if index is None or len(Game.inventory) == 0:
return None
return Game.inventory[index].item
@staticmethod
def main_menu():
img = libtcod.image_load('img/menu_background1.png')
while not libtcod.console_is_window_closed():
libtcod.image_blit_2x(img, 0, 0, 0)
libtcod.console_set_default_foreground(0, libtcod.light_yellow)
libtcod.console_print_ex(0, Game.SCREEN_WIDTH/2, Game.SCREEN_HEIGHT/2 - 4, libtcod.BKGND_NONE, libtcod.CENTER, 'TITLE HERE')
libtcod.console_print_ex(0, Game.SCREEN_WIDTH/2, Game.SCREEN_HEIGHT - 2, libtcod.BKGND_NONE, libtcod.CENTER, 'By Drew')
choice = Game.menu('', ['Play a new game', 'Continue current game', 'Quit'], 24)
if choice == 0:
Game.new_game()
Game.run()
elif choice == 1:
try:
Game.run()
except:
Game.msgbox('\n No saved game to load.\n', 24)
continue
elif choice == 2:
break
@staticmethod
def msgbox(text, width=50):
Game.menu(text, [], width)
@staticmethod
def target_tile(max_range=None):
box = libtcod.console_new(1, 1)
x = Game.player.x
y = Game.player.y
libtcod.console_set_default_background(box, libtcod.orange)
libtcod.console_clear(box)
key = Game.key
while (x, y) != (0, 0):
Game.render_all()
libtcod.console_blit(box, 0, 0, 1, 1, 0, x, y, 1.0, 0.5) #0.7 is the transparency
libtcod.console_flush()
key = libtcod.console_wait_for_keypress(True)
key = libtcod.console_wait_for_keypress(True)
if key.vk == libtcod.KEY_ESCAPE:
return (None, None)
direction = Game.getDirection(key)
if direction is not None:
x += direction[0]
y += direction[1]
if direction == (0, 0):
if Game.map.is_tile_in_fov(x, y) and (max_range is None or Game.player.distance(x, y) <= max_range):
return (x, y)
else:
Game.message('That is out of range.', libtcod.red)
@staticmethod
def target_monster(max_range=None):
while True:
(x, y) = Game.target_tile(max_range)
if x is None:
return None
for obj in Game.map.objects:
if obj.x == x and obj.y == y and obj.fighter and obj != Game.player:
return obj
@staticmethod
def update():
for object in Game.map.objects:
object.update()
@staticmethod
def next_level():
Game.message('You take a moment to rest, and recover your strength.', libtcod.light_violet)
Game.player.fighter.heal(Game.player.fighter.max_hp / 2)
Game.message('You descend deeper into the heart of the dungeon...', libtcod.red)
libtcod.console_clear(Game.main_console)
Game.dungeon_level += 1
Game.map = Map(Game.MAP_WIDTH, Game.MAP_HEIGHT)
Game.player.x = Game.map.origin[0]
Game.player.y = Game.map.origin[1]
Game.map.add_object(Game.player)
@staticmethod
def check_level_up():
level_up_exp = Game.get_exp_to_level()
while Game.player.fighter.xp >= level_up_exp:
Game.player.level += 1
Game.player.fighter.xp -= level_up_exp
Game.message('Your battle skills grow stronger! You reached level ' + str(Game.player.level) + '!', libtcod.yellow)
choice = None
while choice is None:
choice = Game.menu('Level up! Choose a stat to raise:\n',
['Constitution (+20 HP, from (' + str(Game.player.fighter.max_hp) + ')',
'Strength (+1 attack, from (' + str(Game.player.fighter.power) + ')',
'Agility (+1 dexterity, from (' + str(Game.player.fighter.dexterity) + ')'], Game.LEVEL_SCREEN_WIDTH)
if choice == 0:
Game.player.fighter.base_max_hp += 20
Game.player.fighter.hp += 20
elif choice == 1:
Game.player.fighter.base_power += 1
elif choice == 2:
Game.player.fighter.base_dexterity += 1
Game.render_all()
@staticmethod
def get_exp_to_level():
return Game.LEVEL_UP_BASE + Game.player.level * Game.LEVEL_UP_FACTOR
@staticmethod
def try_pick_up():
for object in Game.map.objects:
if object.x == Game.player.x and object.y == Game.player.y and object.item:
object.item.pick_up()
return
Game.message('You wait.', libtcod.green)
@staticmethod
def getDirection(key):
if key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_KP8:
return (0, -1)
elif key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_KP2:
return (0, 1)
elif key.vk == libtcod.KEY_LEFT or key.vk == libtcod.KEY_KP4:
return (-1, 0)
elif key.vk == libtcod.KEY_RIGHT or key.vk == libtcod.KEY_KP6:
return (1, 0)
elif key.vk == libtcod.KEY_HOME or key.vk == libtcod.KEY_KP7:
return (-1, -1)
elif key.vk == libtcod.KEY_PAGEUP or key.vk == libtcod.KEY_KP9:
return (1, -1)
elif key.vk == libtcod.KEY_END or key.vk == libtcod.KEY_KP1:
return (-1, 1)
elif key.vk == libtcod.KEY_PAGEDOWN or key.vk == libtcod.KEY_KP3:
return (1, 1)
elif key.vk == libtcod.KEY_KP5 or key.vk == libtcod.KEY_ENTER:
return (0, 0)
return None
@staticmethod
def handle_keys():
Game.key = libtcod.console_wait_for_keypress(True)
Game.key = libtcod.console_wait_for_keypress(True)
if Game.key.vk == libtcod.KEY_ESCAPE:
return 'exit'
if Game.state == 'playing':
#movement keys
Game.map.fov_recompute = True
direction = Game.getDirection(Game.key)
if direction is None:
Game.map.fov_recompute = False
key_char = chr(Game.key.c)
if key_char == 'g':
Game.try_pick_up()
if key_char == 'i':
chosen_item = Game.inventory_menu('Press the key next to an item to use it.\n')
if chosen_item is not None:
chosen_item.use()
if key_char == 'd':
chosen_item = Game.inventory_menu('Press the key next to an item to drop it.\n')
if chosen_item is not None:
chosen_item.drop()
if key_char == '<':
if Game.map.stairs.x == Game.player.x and Game.map.stairs.y == Game.player.y:
Game.next_level()
if key_char == 'c':
level_up_exp = Game.get_exp_to_level()
Game.msgbox('Character Information\n\nLevel: ' + str(Game.player.level) + '\nExperience: ' + str(Game.player.fighter.xp) +
'\nExperience to level up: ' + str(level_up_exp) + '\n\nMaximum HP: ' + str(Game.player.fighter.max_hp) +
'\nAttack: ' + str(Game.player.fighter.power) + '\nDexterity: ' + str(Game.player.fighter.dexterity), Game.CHARACTER_SCREEN_WIDTH)
return 'didnt-take-turn'
elif direction == (0, 0):
Game.try_pick_up()
pass
else:
Game.player.move_or_attack(direction[0], direction[1])
@staticmethod
def run():
player_action = None
while not libtcod.console_is_window_closed():
libtcod.console_set_default_foreground(0, Game.COLOR_LIT)
Game.render_all()
Game.check_level_up()
player_action = Game.handle_keys()
if player_action == 'exit':
break
if Game.state == 'playing' and player_action != 'didnt-take-turn':
Game.update()