forked from DebianJoe/advanced
/
ApplicationLibtcod.py
625 lines (532 loc) · 23.9 KB
/
ApplicationLibtcod.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
#!/usr/bin/python
# This file contains a test implementation of the application class.
# This one is based on libtcod.
import libtcodpy as libtcod
# You can import everything you need from module Game
# the Game module will chain load other modules
from Game import Game
from Game import Player
import Actors
import Libraries
import Maps
import CONSTANTS
import Utilities
import textwrap
import random
import timeit
#actual size of the window
SCREEN_WIDTH = 85
SCREEN_HEIGHT = 50
#libtcod parameters
LIMIT_FPS = 20 # 20 frames-per-second maximum
PANEL_HEIGHT = 7
BAR_WIDTH = 20
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT
MSG_X = BAR_WIDTH + 2
MSG_WIDTH = SCREEN_WIDTH - BAR_WIDTH - 2
MSG_HEIGHT = PANEL_HEIGHT - 1
#libtcod colors
COLOR_DARK_WALL = libtcod.light_orange * libtcod.dark_grey * 0.2
COLOR_DARK_GROUND = libtcod.orange * 0.4
COLOR_LIGHT_WALL = libtcod.light_orange * 0.3
COLOR_LIGHT_GROUND = libtcod.orange * 0.9
class ApplicationLibtcod():
"""
This class represents a running instance of the application.
It connects the game logic to the user interface.
It is a test implementation of a GUI based on libtcod.
"""
#TODO easy: this class needs better documentation comments
_game = None
@property
def game(self):
"""
The game object used by this application
"""
return self._game
_mapConsole = None
@property
def mapConsole(self):
"""
libtcod console (off screen) used to draw the main map
"""
return self._mapConsole
_panelConsole = None
@property
def panelConsole(self):
"""
libtcod console (off screen) used to draw the panel
"""
return self._panelConsole
_messages = None
@property
def messages(self):
"""
returns the most recent game messages
"""
return self._messages
def addMessage(self, new_msg): # , color = libtcod.white):
#split the message if necessary, among multiple lines
new_msg_lines = textwrap.wrap(new_msg, MSG_WIDTH)
for line in new_msg_lines:
#only keep the last messages
if len(self.messages) == MSG_HEIGHT:
del self.messages[0]
#add the new line
self.messages.append(line) # (line, color) )
def __init__(self):
"""
Constructor that creates a new instance of the application
"""
#Initialize libtcod
libtcod.console_set_custom_font('./media/arial10x10.png',
libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT,
'Crunchbang Project', False)
libtcod.sys_set_fps(LIMIT_FPS)
#Initialize ofscreen consoles
self._mapConsole = libtcod.console_new(CONSTANTS.MAP_WIDTH, CONSTANTS.MAP_HEIGHT)
self._panelConsole = libtcod.console_new(SCREEN_WIDTH, PANEL_HEIGHT)
#Prepare to receive messages from the game utilities
#(this allows the utilities to send game messages to this application)
self._messages = []
Utilities.application = self
#Create a new game object for this application
#self._game = Game(self)
##########################################################################
# show functions
# These functions all use a similar technique to show something on screen
# 1) They store what was on screen before them
# 2) They show something new
# 3) They go into a loop handling user input
# 4) Once the loop breaks they restore what was on screen before them
# This allows to create multiple menu's and screens on top of eachother
##########################################################################
def showMenu(self, header, options, width):
"""
This function will show a menu. The application waits for user input
before returning the selected option.
The function will return None if the user escapes the menu.
Arguments
header - String, text for the header
options - String list, text for the options
width - Width (in characters) of the menu box
"""
if len(options) > 26:
raise ValueError('Cannot have a menu with more than 26 options.')
#calculate total height for the header (after auto-wrap)
header_height = libtcod.console_get_height_rect(0, 0, 0, width, SCREEN_HEIGHT, header)
if header == '':
header_height = 0
#add one line per option
height = len(options) + header_height
#create an off-screen console that represents the menu's window
window = libtcod.console_new(width, height)
#print the header, with auto-wrap
libtcod.console_set_default_foreground(window, libtcod.white)
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)
#print all the options
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
#Show in the middle of the screen
x = SCREEN_WIDTH / 2 - width / 2
y = SCREEN_HEIGHT / 2 - height / 2
#store the current view
behind_window = libtcod.console_new(width, height)
libtcod.console_blit(0, x, y, width, height, behind_window, 0, 0, 1.0, 1.0)
#blit the contents of "window" to the root console
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 1.0)
#present the root console to the player and wait for a key-press
libtcod.console_flush()
returnMsg = ''
while not libtcod.console_is_window_closed():
key = libtcod.console_wait_for_keypress(True)
#TODO: Remove in next libtcod version
#Attention: dirty hack, bug in libtcod fires keypress twice...
key = libtcod.console_wait_for_keypress(True)
#convert the ASCII code to an index
index = key.c - ord('a')
#if it corresponds to an option, return it
if index >= 0 and index < len(options):
returnMsg = index
break
#if it is the escape key return None
if key.vk == libtcod.KEY_ESCAPE:
returnMsg = None
break
#Clean up (restore whatever was behind this window)
libtcod.console_blit(behind_window, 0, 0, width, height, 0, x, y, 1.0, 1.0)
libtcod.console_flush()
return returnMsg
def showMessage(self, header, message, width):
"""
This function will show a pop up message in the middle of the screen.
It waits for the user to acknowledge the message by hitting enter or
escape
"""
#calculate total height for the header (after auto-wrap)
header_height = libtcod.console_get_height_rect(0, 0, 0, width, SCREEN_HEIGHT, header)
if header == '':
header_height = 0
#calculate total height for the message (after auto-wrap)
msg_height = libtcod.console_get_height_rect(0, 0, 0, width, SCREEN_HEIGHT, message)
if message == '':
msg_height = 0
height = header_height + msg_height
#create an off-screen console that represents the message window
window = libtcod.console_new(width, height)
#print the header, with auto-wrap
libtcod.console_set_default_foreground(window, libtcod.red)
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)
#print the message, with auto-wrap
libtcod.console_set_default_foreground(window, libtcod.white)
libtcod.console_print_rect_ex(window, 0, header_height, width, height, libtcod.BKGND_NONE, libtcod.LEFT, message)
#center the pop up on the screen
x = SCREEN_WIDTH / 2 - width / 2
y = SCREEN_HEIGHT / 2 - height / 2
#store the current view
behind_window = libtcod.console_new(width, height)
libtcod.console_blit(0, x, y, width, height, behind_window, 0, 0, 1.0, 1.0)
#blit the contents of "window" to the root console
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 1.0)
#present the root console to the player and wait for a key-press
libtcod.console_flush()
#Loop until player accepts message using enter or escape
returnMsg = ''
while not libtcod.console_is_window_closed():
key = libtcod.console_wait_for_keypress(True)
#TODO: Remove in next libtcod version
#Attention: dirty hack, bug in libtcod fires keypress twice...
key = libtcod.console_wait_for_keypress(True)
#Wait for enter or escape
if key.vk == libtcod.KEY_ESCAPE:
returnMsg = 'Escape'
break
if key.vk == libtcod.KEY_ENTER:
returnMsg = 'Enter'
break
#Clean up (restore whatever was behind this window)
libtcod.console_blit(behind_window, 0, 0, width, height, 0, x, y, 1.0, 1.0)
libtcod.console_flush()
return returnMsg
def showWelcomeScreen(self):
#store the current view
behind_window = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
libtcod.console_blit(0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, behind_window, 0, 0, 1.0, 1.0)
#show the background image, at twice the regular console resolution
img = libtcod.image_load('./media/menu_background.png')
libtcod.image_blit_2x(img, 0, 0, 0)
while not libtcod.console_is_window_closed():
#show options and wait for the player's choice
choice = self.showMenu('Main menu:\n----------',
['Start a new game', # Choice 0
'Continue previous game', # Choice 1
'Go to debug mode', # Choice 2
'Quit'], # Choice 3
36)
#interpret choice
if choice is None:
continue
if choice == 0:
print "Start a new game"
self.newGame()
self.showGameScreen()
continue
elif choice == 1:
print "Continue previous game"
self.showMessage('Oops...',
'I don\'t know how to load a game yet :-)', 36)
elif choice == 2: # quit
print "Go to debug mode"
self.showDebugScreen()
elif choice == 3:
print "Quiting"
break
#Clean up (restore whatever was behind this window)
libtcod.console_blit(behind_window, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 1.0, 1.0)
libtcod.console_flush()
def showDebugScreen(self):
#store the current view
behind_window = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
libtcod.console_blit(0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, behind_window, 0, 0, 1.0, 1.0)
#show the background image, at twice the regular console resolution
img = libtcod.image_load('./media/menu_debug.png')
libtcod.image_blit_2x(img, 0, 0, 0)
while not libtcod.console_is_window_closed():
#show options and wait for the player's choice
choice = self.showMenu('Select debug option:',
['Run some test code!', # Choice 0
'Show me some game stuff!', # Choice 1
'Back'], # Choice 2
36)
#interpret choice
if choice is None:
continue
if choice == 0:
print "Running some test code!"
self.runTestCode()
self.showMessage('Test code complete!',
'There might be some output in the console...', 36)
continue
elif choice == 1:
print "Showing some game stuff!"
self.newGame()
self.showGameScreen()
elif choice == 2: # quit
print "Back"
break
#Clean up (restore whatever was behind this window)
libtcod.console_blit(behind_window, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 1.0, 1.0)
libtcod.console_flush()
def showGameScreen(self):
#store the current view
behind_window = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
libtcod.console_blit(0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, behind_window, 0, 0, 1.0, 1.0)
#main loop
while not libtcod.console_is_window_closed():
#render the screen
self.renderAll()
#refresh visual
libtcod.console_flush()
#handle keys and exit game if needed
#this allows the player to play his turn
if self.handleKeys() == 'exit':
break
#Let the game play a turn
self.game.playTurn()
#Clean up (restore whatever was behind this window)
libtcod.console_blit(behind_window, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 1.0, 1.0)
libtcod.console_flush()
def useInventory(self):
if self.game is not None and self.game.player is not None:
header = "Select item to use, escape to cancel"
width = 45
options = []
items = self.game.player.inventoryItems
for item in items:
options.append(item.name)
selection = self.showMenu(header, options, width)
if selection is not None:
self.game.player.tryUseItem(items[selection])
def dropInventory(self):
if self.game is not None and self.game.player is not None:
header = "Select item to drop, escape to cancel"
width = 45
options = []
items = self.game.player.inventoryItems
for item in items:
options.append(item.name)
selection = self.showMenu(header, options, width)
if selection is not None:
self.game.player.tryDropItem(items[selection])
##########################################################################
# DebugScreen functions
##########################################################################
def runTestCode(self):
"""
This function ties into the debug menu. It is meant to allow execution
of some test code. Feel free to change the contents of this function.
"""
start = timeit.default_timer()
monsters = []
lib = Libraries.MonsterLibrary()
stop = timeit.default_timer()
time = stop - start
print 'Created library in ' + str(time) + ' seconds'
for i in range(0, 10000):
myRandom = lib.getRandomMonster(random.randint(0,80))
monsters.append(myRandom)
#lib = Libraries.ItemLibrary()
#myItem = lib.createItem('heal')
#print myItem
#myItem = lib.createItem('sword')
#print myItem
#myItem = lib.createItem('cloak')
#print myItem
#myItem = lib.createItem('fireball')
#print myItem
stop = timeit.default_timer()
time = stop - start
print 'Created ' +str(len(monsters)) + ' monsters in ' + str(time) + ' seconds'
#myMap = Maps.TownMap(CONSTANTS.MAP_WIDTH, CONSTANTS.MAP_HEIGHT)
#print myMap
##########################################################################
# GameScreen functions
##########################################################################
def newGame(self):
self._messages = []
self._game = Game(self)
def loadGame(self, fileName):
self.game.loadGame(fileName)
def saveGame(self, fileName):
self.game.saveGame(fileName)
def renderAll(self):
"""
This function renders the main screen
"""
con = self.mapConsole
libtcod.console_clear(con)
level = self.game.currentLevel
# draw the map tiles
for tile in level.map.explored_tiles:
if tile.blocked:
# these are wall tiles
if tile.inView:
# the player can see these
bg_color = COLOR_LIGHT_WALL
else:
# these are out of sight
bg_color = COLOR_DARK_WALL
else:
# and these are floor tiles...
if tile.inView:
bg_color = COLOR_LIGHT_GROUND
else:
bg_color = COLOR_DARK_GROUND
libtcod.console_set_char_background(
con, tile.x, tile.y, bg_color, libtcod.BKGND_SET)
# draw any actors standing on this tile.
# includes Monsters and Portals
for myActor in tile.actors:
if myActor.inView:
actor_color = libtcod.white
# NOTE if the Actor base stores it's own color there is no
# need for type checking.
if type(myActor) is Actors.Portal:
actor_color = libtcod.purple
elif type(myActor) is Actors.Monster:
actor_color = libtcod.green
libtcod.console_set_default_foreground(con, actor_color)
libtcod.console_put_char(
con, tile.x, tile.y, myActor.char, libtcod.BKGND_NONE)
#Redraw player character (makes sure it is on top)
player = self.game.player
libtcod.console_set_default_foreground(con, libtcod.white)
libtcod.console_put_char(
con, player.tile.x, player.tile.y,
player.char, libtcod.BKGND_NONE)
#blit the contents of "con" to the root console
libtcod.console_blit(con, 0, 0, CONSTANTS.MAP_WIDTH, CONSTANTS.MAP_HEIGHT, 0, 0, 0)
##Notes on field of view
##
##Joe: Explain, the Fov is treated as a secondary map
##The question is do we wish to deal with it the same way?
##Ideally, if rooms are set before map is drawn, you save the
##generation of colors until inside FOV range, where you change
##the colors of them to match your final scheme, and then turn
##them back to the "shadowed" colors once player has set an
##"explored" option.
##NOTE Wesley:
## I did this before in another project as handled by the Game.
## After each move the Game does a look_around() and marks a monster
## or tile as seen == True and in_range == True (if in fov).
## Then we just draw tiles where seen, and monsters where in_range.
## I will try add this tonight, it is pure python and simple and good
## for learning how these things work :)
#TODO medium: create a GUI panel
#Frostlock: this needs some game message log first in the game logic
panel = self.panelConsole
libtcod.console_set_default_background(panel, libtcod.black)
libtcod.console_clear(panel)
##print the game messages, one line at a time
if self.messages is not None:
y = 1
for line in self.messages:
libtcod.console_set_default_foreground(panel, libtcod.white)
libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE,
libtcod.LEFT, line)
y += 1
if player is not None:
#Player health bar
self.renderBar(panel, 1, 1, BAR_WIDTH, 'HP',
player.currentHitPoints, player.maxHitPoints,
libtcod.dark_red, libtcod.darker_gray)
#Player xp bar
self.renderBar(panel, 1, 2, BAR_WIDTH, 'XP',
player.xp, player.nextLevelXp,
libtcod.darker_green, libtcod.darker_gray)
if self.game.currentLevel is not None:
#Dungeon level
libtcod.console_print_ex(panel, 1, 3, libtcod.BKGND_NONE,
libtcod.LEFT, str(self.game.currentLevel.name))
#TODO: display names of objects under the mouse
# Frost: this would require running this loop constantly which is not
# happening at the moment. Currently it pauses to wait for the player to
# hit a key.
#libtcod.console_set_default_foreground(panel, libtcod.light_gray)
#libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE,
# libtcod.LEFT, get_names_under_mouse())
#blit the contents of "panel" to the root console
libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH,
PANEL_HEIGHT, 0, 0, PANEL_Y)
def renderBar(self, panel, x, y, total_width,
name, value, maximum, bar_color, back_color):
"""
Helper function to render interface bars
"""
#render a bar (HP, experience, etc). first calculate the width of the bar
bar_width = int(float(value) / maximum * total_width)
#render the background first
libtcod.console_set_default_background(panel, back_color)
libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN)
#now render the bar on top
libtcod.console_set_default_background(panel, bar_color)
if bar_width > 0:
libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN)
#finally, some centered text with the values
libtcod.console_set_default_foreground(panel, libtcod.white)
libtcod.console_print_ex(panel, x + total_width / 2, y,
libtcod.BKGND_NONE, libtcod.CENTER,
name + ': ' + str(value) + '/' + str(maximum))
def handleKeys(self):
key = libtcod.console_wait_for_keypress(True)
#TODO: Remove in next libtcod version
#Attention: dirty hack, bug in libtcod fires keypress twice...
key = libtcod.console_wait_for_keypress(True)
key_char = chr(key.c)
if key.vk == libtcod.KEY_ESCAPE:
return 'exit'
if self.game.state == Game.PLAYING:
player = self.game.player
#movement keys
if key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_KP8:
player.tryMoveOrAttack(0, -1)
elif key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_KP2:
player.tryMoveOrAttack(0, 1)
elif key.vk == libtcod.KEY_LEFT or key.vk == libtcod.KEY_KP4:
player.tryMoveOrAttack(-1, 0)
elif key.vk == libtcod.KEY_RIGHT or key.vk == libtcod.KEY_KP6:
player.tryMoveOrAttack(1, 0)
elif key.vk == libtcod.KEY_HOME or key.vk == libtcod.KEY_KP7:
player.tryMoveOrAttack(-1, -1)
elif key.vk == libtcod.KEY_PAGEUP or key.vk == libtcod.KEY_KP9:
player.tryMoveOrAttack(1, -1)
elif key.vk == libtcod.KEY_END or key.vk == libtcod.KEY_KP1:
player.tryMoveOrAttack(-1, 1)
elif key.vk == libtcod.KEY_PAGEDOWN or key.vk == libtcod.KEY_KP3:
player.tryMoveOrAttack(1, 1)
#portal keys
elif key_char == '>':
player.tryFollowPortalDown()
elif key_char == '<':
player.tryFollowPortalUp()
#inventory
elif key_char == 'i':
self.useInventory()
elif key_char == 'd':
self.dropInventory()
#interact
elif key_char == ',':
player.tryPickUp()
# update field of vision
self.game.currentLevel.map.updateFieldOfView(
player.tile.x, player.tile.y)
#This is where it all starts!
if __name__ == '__main__':
myApplication = ApplicationLibtcod()
myApplication.showWelcomeScreen()