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:
|
76408e86 |
#actual size of the window
|
539d5ff4 |
SCREEN_WIDTH, SCREEN_HEIGHT = 50,70
|
76408e86 |
MAP_WIDTH, MAP_HEIGHT = SCREEN_WIDTH, SCREEN_HEIGHT - 17
INVENTORY_WIDTH = 50
BAR_WIDTH = 25
PANEL_HEIGHT = SCREEN_HEIGHT - MAP_HEIGHT - 2
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT
MSG_X = BAR_WIDTH + 2
MSG_WIDTH, MSG_HEIGHT = SCREEN_WIDTH - BAR_WIDTH - 2, PANEL_HEIGHT - 1
ROOM_MIN_SIZE, ROOM_MAX_SIZE = 7, 19
MAX_ROOMS = 51
MAX_ROOM_MONSTERS, MAX_ROOM_ITEMS = 9, 6
CONFUSE_NUM_TURNS = 17
LIMIT_FPS = 20 #20 frames-per-second maximum
color_dark_wall = libtcod.Color(60, 60, 60)
color_light_wall = libtcod.Color(127,127,127)
color_dark_ground = libtcod.Color(150,150,150)
color_light_ground = libtcod.Color(200,200,200)
|
539d5ff4 |
def message(self, msg, color):
utilities.message(self.game_msgs, self.MSG_HEIGHT, self.MSG_WIDTH, msg, color)
|
76408e86 |
|
539d5ff4 |
def __init__(self, app_name='test app', screen_width=SCREEN_WIDTH, screen_height=SCREEN_HEIGHT):
|
76408e86 |
print '__init__'
|
539d5ff4 |
libtcod.console_init_root(
screen_width, screen_height, app_name, False
|
76408e86 |
)
|
539d5ff4 |
self.game_msgs = []
global message
message = functools.partial(utilities.message, self.game_msgs, self.MSG_HEIGHT, self.MSG_WIDTH)
|
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()
libtcod.console_flush()
|
539d5ff4 |
yield 1
|
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)
|
539d5ff4 |
#utilities.render_bar(self.panel, 1,1, self.BAR_WIDTH, 'HP',
# self.player.fighter.hp,
# self.player.fighter.max_hp,
# libtcod.red,
# libtcod.darker_red
#)
|
76408e86 |
|
539d5ff4 |
#y = 1
#for line, color in self.game_msgs:
# libtcod.console_set_default_foreground(self.panel, color)
# libtcod.console_print_ex(self.panel, self.MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line)
# y += 1
|
76408e86 |
libtcod.console_blit(self.panel, 0,0, self.SCREEN_WIDTH,self.PANEL_HEIGHT, 0,0, self.PANEL_Y)
def inventory_menu(self, header):
|
539d5ff4 |
index = self.menu(self.con, header, self.player.get_item_names(), self.INVENTORY_WIDTH)
|
76408e86 |
if index is not None:
return self.player.get_item(index)
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()
|
539d5ff4 |
def menu(self, con, header, options, width):
if len(options) > 26: raise ValueError('too many items')
|
76408e86 |
|
539d5ff4 |
print con
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)
|
76408e86 |
|
539d5ff4 |
libtcod.console_set_default_foreground(window, libtcod.white)
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
libtcod.console_blit(window, 0,0, width,height, 0, x,y, 1.0, 0.7)
|
76408e86 |
|
539d5ff4 |
key = libtcod.Key()
mouse = libtcod.Mouse()
libtcod.console_flush()
libtcod.sys_wait_for_event(libtcod.KEY_PRESSED, key, mouse, True)
|
76408e86 |
|
539d5ff4 |
index = key.c - ord('a')
if index >= 0 and index < len(options): return index
return None
|
76408e86 |
|