-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
739 lines (589 loc) · 22.5 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import sys
import shelve
sys.path.append('./libtcod')
import libtcodpy as libtcod
import math
import textwrap
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
MAP_WIDTH = 80
MAP_HEIGHT = 45
INVENTORY_WIDTH = 50
BAR_WIDTH = 20
PANEL_HEIGHT = 7
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT
BAR_WIDTH = 20
BAR_HEIGHT = 1
MSG_X = BAR_WIDTH + 2
MSG_WIDTH = SCREEN_WIDTH - BAR_WIDTH - 2
MSG_HEIGHT = PANEL_HEIGHT - 1
LIMIT_FPS = 20
FOV_ALGO = 0 # libtcod default FOV algorithm
FOV_LIGHT_WALLS = True
TORCH_RADIUS = 10
MAX_ROOM_MONSTERS = 5
MAX_ROOM_ITEMS = 2
color_dark_wall = libtcod.Color(0, 0, 100)
color_light_wall = libtcod.Color(130, 110, 50)
color_dark_ground = libtcod.Color(50, 50, 150)
color_light_ground = libtcod.Color(200, 180, 50)
class Tile:
# tile of the map and its properties
def __init__(self, blocked, block_sight = None):
self.explored = False
self.blocked = blocked
# by default, if a tile is blocked it also blocks sight
if block_sight is None: block_sight = blocked
self.block_sight = block_sight
class Object:
def __init__(self, x, y, char, name, color, blocks = False, fighter=None, ai=None,
item=None):
self.x = x
self.y = y
self.char = char
self.name = name
self.color = color
self.blocks = blocks # does this block movement
self.fighter = fighter
if self.fighter:
self.fighter.owner = self # let component know owner
self.ai = ai
if self.ai:
self.ai.owner = self
self.item = item
if self.item:
self.item.owner = self
def move(self, dx, dy):
# move by given amount
if not is_blocked(self.x + dx, self.y + dy):
self.x += dx
self.y += dy
def move_towards(self, target_x, target_y):
# vector from this object to the target, and distance
dx = target_x - self.x
dy = target_y - self.y
distance = math.sqrt(dx ** 2 + dy ** 2)
# normalize it to length 1 (preserving direction), then round it and
# convert to integer so the movement is restricted to the map grid
dx = int(round(dx / distance))
dy = int(round(dy / distance))
self.move(dx, dy)
def distance_to(self, other):
#return the distance to another object
dx = other.x - self.x
dy = other.y - self.y
return math.sqrt(dx ** 2 + dy ** 2)
def draw(self):
# set color and draw character at this object's position
if libtcod.map_is_in_fov(fov_map, self.x, self.y):
libtcod.console_set_default_foreground(con, self.color)
libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
def send_to_back(self):
# make this object be drawn first, so all others appear above it
global objects
objects.remove(self)
objects.insert(0, self)
def is_visible(self):
return libtcod.map_is_in_fov(fov_map, self.x, self.y)
def clear(self):
# erase character
libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)
class Fighter:
# combat related properties and methods (monster, player, NPC).
def __init__(self, hp, defense, power, death_function=None):
self.max_hp = hp
self.hp = self.max_hp
self.defense = defense
self.power = power
self.death_function = death_function
def take_damage(self, damage):
if self.hp > 0:
self.hp -= damage
# check for death - if there's a death function, call it
if self.hp<= 0:
function = self.death_function
if function is not None:
function(self.owner)
def attack(self, target):
damage = self.power - target.fighter.defense
if damage > 0:
message("The " + self.owner.name + " attacks the " + target.name + "!")
target.fighter.take_damage(damage)
else:
message("The " + self.owner.name + "'s attack bounces off!")
def heal(self, amount):
self.hp += amount
if self.hp > self.max_hp:
self.hp = self.max_hp
class BasicMonster:
# AI for a basic monster
def take_turn(self):
monster = self.owner
if monster.is_visible():
# move towards player if far away
if monster.distance_to(player) >= 2:
monster.move_towards(player.x, player.y)
# attack if player is still alive + close enough
elif player.fighter.hp > 0:
monster.fighter.attack(player)
class PlayerAI:
# controls things that happen to the player on each turn
def take_turn(self):
player = player
pass
class Item:
# item that can be picked up and used
def __init__(self, use_function=None):
self.use_function = use_function
def pick_up(self):
if len(inventory) >= 26:
message('Your inventory is full, cannot pick up the ' + self.owner.name)
else:
inventory.append(self.owner)
objects.remove(self.owner)
message('You picked up a ' + self.owner.name + '.')
def use(self):
if self.use_function is None:
message('The ' + self.owner.name + ' cannot be used.')
else:
if self.use_function() != 'cancelled':
# destroy after use, unless it was canceled
inventory.remove(self.owner)
def drop(self):
objects.append(self.owner)
inventory.remove(self.owner)
self.owner.x = player.x
self.owner.y = player.y
message('You dropped a ' + self.owner.name + '.')
class Rect:
# a rectangle on the map, used for a room
def __init__(self, x, y, w, h):
self.x1 = x
self.y1 = y
self.x2 = x + w
self.y2 = y + h
def center(self):
center_x = (self.x1 + self.x2) / 2
center_y = (self.y1 + self.y2) / 2
return (center_x, center_y)
def intersect(self, other):
# does it intersect with anything?
return (self.x1 <= other.x2 and self.x2 >= other.x1 and
self.y1 <= other.y2 and self.y2 >= other.y1)
################################
# Map generator #
################################
def create_room(room):
global map
# go through tiles in the rectangle and make them passable
for x in range(room.x1 + 1, room.x2):
for y in range(room.y1 + 1, room.y2):
map[x][y].blocked = False
map[x][y].block_sight = False
def make_map():
global map, objects
objects = [player]
ROOM_MAX_SIZE = 10
ROOM_MIN_SIZE = 6
MAX_ROOMS = 30
# fill map with "blocked" tiles
map = [[ Tile(True)
for y in range(MAP_HEIGHT)]
for x in range(MAP_WIDTH) ]
rooms = []
num_rooms = 0
for r in range(MAX_ROOMS):
# 0 identifies "stream" to get random number from
w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1)
y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1)
new_room = Rect(x, y, w, h)
# interesting
failed = False
# check for intersections
for other_room in rooms:
if new_room.intersect(other_room):
failed = True
break
if not failed:
# no intersections, room is valid
create_room(new_room)
# add contents
place_objects(new_room)
(new_x, new_y) = new_room.center()
# print "room number" to see drawing order
#room_no = Object(new_x, new_y, chr(65+num_rooms), 'room number', libtcod.white)
#objects.insert(0, room_no) # draw first, so monsters are on top
if num_rooms == 0:
# if this is the first room, where player starts
player.x = new_x
player.y = new_y
else:
# other rooms exist
# connect to previous rooms with tunnel
# center crds of previous room
(prev_x, prev_y) = rooms[num_rooms - 1].center()
# draw a coin
if libtcod.random_get_int(0, 0, 1) == 1:
# first move horizontally, then vertically
create_h_tunnel(prev_x, new_x, prev_y)
create_v_tunnel(prev_y, new_y, new_x)
else:
# first move vertically, then horizontally
create_v_tunnel(prev_y, new_y, prev_x)
create_h_tunnel(prev_x, new_x, new_y)
rooms.append(new_room)
num_rooms += 1
def create_h_tunnel(x1, x2, y):
global map
for x in range(min(x1, x2), max(x1, x2) + 1):
map[x][y].blocked = False
map[x][y].block_sight = False
def create_v_tunnel(y1, y2, x):
global map
for y in range(min(y1, y2), max(y1, y2) + 1):
map[x][y].blocked = False
map[x][y].block_sight = False
def is_blocked(x, y):
# test map tile
if map[x][y].blocked:
return True
# check for blocking objects
for object in objects:
if object.blocks and object.x == x and object.y == y:
return True
return False
def place_objects(room):
place_monsters(room)
place_items(room)
def place_monsters(room):
# choose random number of monsters
num_monsters = libtcod.random_get_int(0, 0, MAX_ROOM_MONSTERS)
# place monster
for n in range(num_monsters):
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)
if not is_blocked(x, y):
if libtcod.random_get_int(0, 0, 100) <= 60:
fighter_component = Fighter(hp=10, defense=0, power=3, death_function=monster_death)
ai_component = BasicMonster()
monster = Object(x, y, 'd', 'dog', libtcod.desaturated_green,
blocks = True, fighter=fighter_component, ai=ai_component)
else:
fighter_component = Fighter(hp=16, defense=0, power=3)
ai_component = BasicMonster()
monster = Object(x, y, 'T', 'tiger', libtcod.desaturated_green,
blocks = True, fighter=fighter_component, ai=ai_component)
num_monsters += 1
objects.append(monster)
def place_items(room):
num_items = libtcod.random_get_int(0, 0, MAX_ROOM_ITEMS)
for i in range(num_items):
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)
if not is_blocked (x, y):
item_component = Item(use_function = cast_heal)
item = Object(x, y, '!', 'healing potion', libtcod.violet, item=item_component)
objects.append(item)
item.send_to_back()
def handle_keys():
global fov_recompute
global key
#libtcod.sys_wait_for_event(1, key, None, True)
key = libtcod.console_wait_for_keypress(True)
if key.vk == libtcod.KEY_ENTER and key.lalt:
# Alt + Enter: toggle fullscreen
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
elif key.vk == libtcod.KEY_ESCAPE:
return 'exit' # exit game
if game_state == 'playing':
# movement keys
if libtcod.console_is_key_pressed(libtcod.KEY_UP):
player_move_or_attack(0, -1)
fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN):
player_move_or_attack(0, 1)
fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT):
player_move_or_attack(-1, 0)
fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
player_move_or_attack(1, 0)
fov_recompute = True
else:
# other keys
key_char = chr(key.c)
if key_char == 'g':
# pick up an item
for object in objects:
if object.x == player.x and object.y == player.y and object.item:
object.item.pick_up()
break
if key_char == 'i':
chosen_item = inventory_menu('Press the key next to an item to use it, or any other to cancel\n')
if chosen_item is not None:
chosen_item.use()
if key_char == 'd':
chosen_item = inventory_menu('Drop which item?')
if chosen_item is not None:
chosen_item.drop()
return 'didnt-take-turn'
def player_move_or_attack(dx, dy):
global fov_recompute
# coordinates player is moving to or attacking
x = player.x + dx
y = player.y + dy
# try to find an attackable object there
target = None
for object in objects:
if object.fighter and object.x == x and object.y == y:
target = object
break
# attack if target found, move otherwise
if target is not None:
player.fighter.attack(target)
else:
player.move(dx, dy)
fov_recompute = True
# Death
def player_death(player):
# game over
player.char = '%'
global game_state
game_state = 'dead'
message( 'You died!')
def monster_death(monster):
message( "The " + monster.name + " dies!")
monster.char = '%'
monster.ai = None
monster.blocks = False
monster.fighter = None
monster.name = monster.name + ' corpse'
monster.send_to_back()
################################
# Items #
################################
def cast_heal():
if player.fighter.hp == player.fighter.max_hp:
message('You are already at full health.')
return cancelled
heal_hp = player.fighter.max_hp / 5
message('You feel your wounds healing.', libtcod.light_violet)
player.fighter.heal(heal_hp)
################################
# GUI #
################################
def menu(header, options, width):
if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options')
header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header)
# check for empty header
if header == '':
header_height = 0
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 = SCREEN_WIDTH/2 - width/2
y = SCREEN_HEIGHT/2 - height/2
# last two parameters: foreground and background opacity
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)
#libtcod.flush = True
libtcod.console_flush()
#key = libtcod.wait_for_event(key, True)
# flushes second event from handle_keys
libtcod.console_wait_for_keypress(True)
key = libtcod.console_wait_for_keypress(True)
# return option
index = key.c - ord('a')
if index >= 0 and index < len(options): return index
return None
def inventory_menu(header):
if len(inventory) == 0:
options = ['Inventory is empty.']
else:
options = [item.name for item in inventory]
index = menu(header, options, INVENTORY_WIDTH)
if index is None or len(inventory) == 0: return None
return inventory[index].item
def render_bar(x, y, w, h, name, value, maximum, bar_color, back_color):
# width of bar on screen
bar_width = (w * value) / maximum
# render background
libtcod.console_set_default_background(panel, back_color)
libtcod.console_rect(panel, x, y, w, h, False, libtcod.BKGND_SCREEN)
# render bar
libtcod.console_set_default_background(panel, bar_color)
if bar_width > 0:
libtcod.console_rect(panel, x, y, bar_width, h,
False, libtcod.BKGND_SCREEN)
# render label
label = name + ": " + str(value) + "\/" + str(maximum)
libtcod.console_set_default_foreground(panel, libtcod.white)
# (console, x, y, background flag, alignment, string)
libtcod.console_print_ex(panel, x + w / 2, y,
libtcod.BKGND_NONE, libtcod.CENTER, label)
def message(new_msg, color = libtcod.white):
# split message if necessary
new_msg_lines = textwrap.wrap(new_msg, MSG_WIDTH)
for line in new_msg_lines:
if len(game_msgs) == MSG_HEIGHT:
del game_msgs[0]
game_msgs.append( (line, color) )
def render_all():
# draw everything
global fov_recompute
for object in objects:
if object != player:
object.draw()
# player is always drawn last i.e. on top
player.draw()
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
visible = libtcod.map_is_in_fov(fov_map, x, y)
wall = map[x][y].block_sight
if not visible:
if map[x][y].explored:
if wall:
libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET)
else:
libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET)
else:
if wall:
libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET)
else:
libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET)
map[x][y].explored = True
if fov_recompute:
# recompute FOV if needed
fov_recompute = False
libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)
# blit to root console
libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
# render HUD
libtcod.console_set_default_background(panel, libtcod.black)
libtcod.console_clear(panel)
render_bar(1, 1, BAR_WIDTH, BAR_HEIGHT, 'HP',
player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red)
# message log
y = 1
for (line, color) in game_msgs:
libtcod.console_set_default_foreground(panel, color)
libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line)
y += 1
libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def msgbox(text, width=50):
menu(text, [], width)
def main_menu():
libtcod.console_print_ex(0, SCREEN_WIDTH/2, SCREEN_HEIGHT/2-4,
libtcod.BKGND_NONE, libtcod.CENTER, "Kobold Hole")
while not libtcod.console_is_window_closed():
choice = menu('', ['New game', 'Continue game', 'Quit'], 24)
if choice == 0:
new_game()
play_game()
if choice == 1:
try:
load_game()
play_game
except:
msgbox('\n No saved game to load.\n', 24)
continue
if choice == 2:
break
# does not work
def pause_menu():
while not libtcod.console_is_window_closed():
choice = menu('', ['Continue', 'Save', 'Quit without saving'], 24)
if choice == 0:
break
elif choice == 1:
save_game()
# todo: check for existing save
elif choice == 2:
break
################################
# Game engine #
################################
def new_game():
global player, inventory, game_msgs, game_state
fighter_component = Fighter(hp=30, defense=2, power=5, death_function=player_death)
player = Object(0, 0, '@', 'player', libtcod.white, blocks=True, fighter=fighter_component)
make_map()
initialize_fov()
game_state = "playing"
inventory = []
# List of messages
game_msgs = []
message('Welcome to Kobold Hole.', libtcod.green)
def initialize_fov():
global fov_recompute, fov_map
libtcod.console_clear(con)
fov_recompute = True
fov_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
libtcod.map_set_properties(fov_map, x, y, not map[x][y].block_sight, not map[x][y].blocked)
def play_game():
global player_action
player_action = None
global key, mouse
key = libtcod.Key()
mouse = libtcod.Mouse()
# Main loop
while not libtcod.console_is_window_closed():
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse)
render_all()
# "flush" i.e. present changes to screen
libtcod.console_flush()
# clear character at last position
for object in objects:
object.clear()
player_action = handle_keys()
if player_action == 'exit':
save_game()
break
if game_state == 'playing' and player_action != 'didnt-take-turn':
for object in objects:
if object.ai:
object.ai.take_turn()
def save_game():
# open new empty shelve to write game data
file = shelve.open('savegame', 'n')
# repeat for all state variables
file['map'] = map
file['objects'] = objects
# avoid loading two player objects: shelve doesn't keep shared references
file['player_index'] = objects.index(player)
file['inventory'] = inventory
file['game_msgs'] = game_msgs
file['game_state'] = game_state
file.close()
def load_game():
#open the previously saved shelve and load the game data
global map, objects, player, inventory, game_msgs, game_state
file = shelve.open('savegame', 'r')
map = file['map']
objects = file['objects']
player = objects[file['player_index']] #get index of player in objects list and access it
inventory = file['inventory']
game_msgs = file['game_msgs']
game_state = file['game_state']
file.close()
initialize_fov()
libtcod.console_set_custom_font('consolas10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/vogue', False)
# Create off-screen console
con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
# HUD off-screen console
panel = libtcod.console_new(SCREEN_WIDTH, PANEL_HEIGHT)
libtcod.sys_set_fps(LIMIT_FPS)
main_menu()