76408e86 |
import libtcodpy as libtcod
import maps
import objects
import utilities
import levels
|
539d5ff4 |
import functools
|
76408e86 |
class Cursor(object):
def __init__(self, con, char, x,y):
self.con = con
self.char = char
self.color = libtcod.white
self.x = x
self.y = y
def draw(self):
libtcod.console_set_default_foreground(self.con, self.color)
libtcod.console_put_char(self.con, self.x,self.y, self.char, libtcod.BKGND_NONE)
def clear(self):
libtcod.console_put_char(self.con, self.x,self.y, ' ', libtcod.BKGND_NONE)
|
539d5ff4 |
class GameBase:
|
1d2d26d2 |
def message(self, msg, color=None):
|
6f02f5b0 |
if color is None:
color = libtcod.white
|
160f4887 |
utilities.message(self.game_msgs, self.MSG_HEIGHT, self.MSG_WIDTH, msg, color=color)
|
76408e86 |
|
6f02f5b0 |
def __init__(self, app_name='test app', screen_width=None, screen_height=None):
|
76408e86 |
print '__init__'
|
6f02f5b0 |
if screen_width is None:
screen_width, screen_height = self.SCREEN_WIDTH, self.SCREEN_HEIGHT
|
539d5ff4 |
libtcod.console_init_root(
screen_width, screen_height, app_name, False
|
76408e86 |
)
|
539d5ff4 |
self.game_msgs = []
global message
|
6f02f5b0 |
message = self.message
|
76408e86 |
|
539d5ff4 |
self.game_state = 'playing'
self.player_action = 'didnt-take-turn'
|
76408e86 |
|
539d5ff4 |
x,y = None,None
|
76408e86 |
|
539d5ff4 |
self.con = libtcod.console_new(self.MAP_WIDTH, self.MAP_HEIGHT)
self.panel = libtcod.console_new(self.SCREEN_WIDTH, self.PANEL_HEIGHT)
self.cursor = Cursor(self.con, 'X', 0,0)
|
76408e86 |
|
539d5ff4 |
self.key = libtcod.Key()
self.mouse = libtcod.Mouse()
|
76408e86 |
|
539d5ff4 |
libtcod.sys_set_fps(self.LIMIT_FPS)
|
76408e86 |
def main(self):
libtcod.console_set_default_foreground(0, libtcod.white)
libtcod.console_set_default_foreground(self.con, libtcod.white)
|
539d5ff4 |
message('Welcome to the arena!')
|
76408e86 |
while not libtcod.console_is_window_closed():
|
539d5ff4 |
yield 0
|
76408e86 |
self.render_all()
|
539d5ff4 |
yield 1
|
dd5e7f8c |
libtcod.console_flush()
|
76408e86 |
self.handle_keys()
|
539d5ff4 |
yield 2
|
76408e86 |
if self.player_action == 'exit':
break
|
539d5ff4 |
def select(self, cb):
self.game_state = 'selecting'
self.select_cb = cb
self.cursor.x, self.cursor.y = self.player.x, self.player.y
|
76408e86 |
def handle_keys(self):
libtcod.sys_check_for_event(
libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE|libtcod.KEY_PRESSED,
self.key,self.mouse
)
self.player_action = 'didnt-take-turn'
if self.game_state == 'playing':
|
539d5ff4 |
_, result = self.mvkeyhandler(self.key, self)
if result is not None:
self.player_action = result
|
76408e86 |
elif self.game_state == 'selecting':
|
539d5ff4 |
_, result = self.selectkeyhandler(self.key, self)
|
76408e86 |
if self.key.vk == libtcod.KEY_ESCAPE and self.key.lalt:
self.player_action = 'exit'
return self.player_action
def render_all(self):
libtcod.console_blit(self.con, 0,0, self.SCREEN_WIDTH,self.SCREEN_HEIGHT, 0,0, 0)
libtcod.console_set_default_background(self.panel, libtcod.black)
libtcod.console_clear(self.panel)
libtcod.console_blit(self.panel, 0,0, self.SCREEN_WIDTH,self.PANEL_HEIGHT, 0,0, self.PANEL_Y)
|
b9c8e0f9 |
def Inventory_menu(self, header, items):
data = [(item.display_name, item.ident) for item in items]
|
1d2d26d2 |
display = [x[0] for x in data]
|
4d5d99b0 |
index = self.menu(header, display, self.INVENTORY_WIDTH)
|
b9c8e0f9 |
return index, data
|
76408e86 |
|
b9c8e0f9 |
def inventory_menu(self, header):
index, data = self.Inventory_menu(header, self.player.get_items())
|
76408e86 |
if index is not None:
|
1d2d26d2 |
return self.player.get_item(data[index][1])
|
76408e86 |
|
b9c8e0f9 |
def mod_menu(self, header):
index, data = self.Inventory_menu(header, self.player.get_mods())
if index is not None:
return self.player.get_mod(data[index][1])
|
76408e86 |
def get_names_under_mouse(self):
x,y = self.mouse.cx, self.mouse.cy
names = ', '.join(
|
539d5ff4 |
obj.name for obj in self.level.iter_objects()
if
|
76408e86 |
(obj.x,obj.y) == (x,y)
and
self.player.can_see(obj)
)
return names.capitalize()
|
6f02f5b0 |
def menu(self, header, options, width, back_color=libtcod.black, fore_color=libtcod.white):
|
b9c8e0f9 |
|
4d5d99b0 |
if self.con is None: self.con = 0
|
539d5ff4 |
if len(options) > 26: raise ValueError('too many items')
|
76408e86 |
|
4d5d99b0 |
con = self.con
|
76408e86 |
|
539d5ff4 |
header_height = libtcod.console_get_height_rect(con, 0,0, width, self.SCREEN_HEIGHT, header)
height = len(options) + header_height
window = libtcod.console_new(width, height)
|
b9c8e0f9 |
print 'window id is:', window
print
|
76408e86 |
|
6f02f5b0 |
libtcod.console_set_default_foreground(window, fore_color)
|
539d5ff4 |
libtcod.console_print_rect(window, 0,0, width,height, header)
|
76408e86 |
|
539d5ff4 |
y = header_height
for option_text in zip('abcdefghijklmnopqrstuvwxyz', options):
text = '(%s) %s' % option_text
libtcod.console_print(window, 0, y, text)
y += 1
|
76408e86 |
|
539d5ff4 |
x = self.SCREEN_WIDTH/2 - width/2
y = self.SCREEN_HEIGHT/2 - height/2
|
b9c8e0f9 |
libtcod.console_blit(window, 0,0, width,height, 0, x,y, 1.0, 0.9)
|
76408e86 |
|
539d5ff4 |
key = libtcod.Key()
mouse = libtcod.Mouse()
libtcod.console_flush()
libtcod.sys_wait_for_event(libtcod.KEY_PRESSED, key, mouse, True)
|
76408e86 |
|
b9c8e0f9 |
libtcod.console_clear(window)
libtcod.console_blit(window, 0,0, width,height, 0, x,y, 1.0, 0.9)
libtcod.console_delete(window)
libtcod.console_flush()
|
539d5ff4 |
index = key.c - ord('a')
if index >= 0 and index < len(options): return index
return None
|
76408e86 |
|