Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import libtcodpy as libtcod
- import math
- import maps #import maps.py, text with map information
- #actual size of the window
- SCREEN_WIDTH = 80
- SCREEN_HEIGHT = 50
- #size of the map
- MAP_WIDTH = 80
- MAP_HEIGHT = 30
- LIMIT_FPS = 20 #20 frames-per-second maximum
- 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)
- FOV_ALGO = 0 #default FOV algorithm
- FOV_LIGHT_WALLS = True
- TORCH_RADIUS = 10
- class Tile:
- #a tile of the map and its properties
- def __init__(self, blocked, block_sight = None):
- self.explored = False
- self.blocked = blocked
- self.field = 0
- #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:
- #this is a generic object: the player, a monster, an item, the stairs...
- #it's always represented by a character on screen.
- def __init__(self, x, y, char, name, color, blocks=False, fighter=None, ai=None):
- self.x = x
- self.y = y
- self.char = char
- self.name = name
- self.color = color
- self.blocks = blocks
- self.fighter = fighter
- if self.fighter: #let the fighter component know who owns it
- self.fighter.owner = self
- self.ai = ai
- if self.ai: #let the AI component know who owns it
- self.ai.owner = self
- def move(self, dx, dy):
- #move by the given amount, if the destination is not blocked
- 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)
- print "hi"
- 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):
- #only show if it's visible to the player
- if libtcod.map_is_in_fov(fov_map, self.x, self.y):
- #set the color and then draw the character that represents this object at its position
- libtcod.console_set_default_foreground(con, self.color)
- libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
- def clear(self):
- #erase the character that represents this object
- 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 = hp
- self.defense = defense
- self.power = power
- self.death_function = death_function
- def attack(self, target):
- #a simple formula for attack damage
- damage = self.power - target.fighter.defense
- if damage > 0:
- #make the target take some damage
- print self.owner.name.capitalize() + ' attacks ' + target.name + ' for ' + str(damage) + ' hit points.'
- target.fighter.take_damage(damage)
- else:
- print self.owner.name.capitalize() + ' attacks ' + target.name + ' but it has no effect!'
- def take_damage(self, damage):
- #apply damage if possible
- if damage > 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)
- class BasicMonster:
- #AI for a basic monster.
- def take_turn(self):
- #a basic monster takes its turn. if you can see it, it can see you
- monster = self.owner
- if libtcod.map_is_in_fov(fov_map, monster.x, monster.y):
- #move towards player if far away
- if monster.distance_to(player) >= 2:
- monster.move_towards(player.x, player.y)
- #close enough, attack! (if the player is still alive.)
- elif player.fighter.hp > 0:
- monster.fighter.attack(player)
- def is_blocked(x, y):
- #first test the map tile
- if map[x][y].blocked:
- return True
- #now check for any blocking objects
- for object in objects:
- if object.blocks and object.x == x and object.y == y:
- return True
- return False
- def load_font():
- a = 0
- for x in range(0,16):
- libtcod.console_map_ascii_codes_to_font(a, 16, 0, x)
- a+=16
- def make_customgenericmap(room_number = 0):
- global map, objects
- if room_number == 0 or room_number == 57:
- smap = maps.portal
- elif room_number == 64:
- smap = maps.portal2
- objects = [player]
- MAP_HEIGHT1 = len(smap)
- MAP_WIDTH1 = len(smap[0])
- map = [[Tile(True) for y in range(MAP_HEIGHT1)] for x in range(MAP_WIDTH1)]
- for y in range(MAP_HEIGHT1):
- for x in range(MAP_WIDTH1):
- if smap[y][x] != '#': #black background
- map[x][y] = Tile(False)
- if smap[y][x] == 'x': #player spawns here
- player.x = x
- player.y = y
- if smap[y][x] == 'w': # walls
- map[x][y].field = 1
- if smap[y][x] == 'u':
- map[x][y].field = 2
- if smap[y][x] == 'a':
- map[x][y].field = 3 #warp
- warp = Object(x, y, 254, 'warp', libtcod.white, blocks=True)
- objects.append(warp)
- if smap[y][x] == 'o': #Orc
- fighter_component = Fighter(hp=10, defense=0, power=3)
- ai_component = BasicMonster()
- monster = Object(x, y, 254, 'orc', libtcod.white, blocks=True, fighter=fighter_component, ai=ai_component)
- objects.append(monster)
- def render_all():
- global fov_map, color_dark_wall, color_light_wall
- global color_dark_ground, color_light_ground
- global fov_recompute
- if fov_recompute:
- #recompute FOV if needed (the player moved or something)
- fov_recompute = False
- libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)
- #go through all tiles, and set their background color according to the FOV
- 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 it's not visible right now, the player can only see it if it's explored
- 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:
- #it's visible
- 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 )
- #since it's visible, explore it
- map[x][y].explored = True
- # if wall:
- # libtcod.console_set_char_background(con, x, y, libtcod.black, libtcod.BKGND_SET )
- #elif map[x][y].field == 1: #walls
- # libtcod.console_put_char(con, x, y, 253, libtcod.BKGND_NONE)
- #elif map[x][y].field == 2:
- # libtcod.console_put_char(con, x, y, 255)
- #elif map[x][y].field == 3:
- # libtcod.console_put_char(con, x, y, 254)
- #elif map[x][y].field == 4:
- #libtcod.console_put_char(con, x, y, "o")
- #else:
- # libtcod.console_set_char_background(con, x, y, libtcod.black, libtcod.BKGND_SET )
- #draw all objects in the list
- for object in objects:
- object.draw()
- #blit the contents of "con" to the root console
- libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
- #show the player's stats
- libtcod.console_set_default_foreground(con, libtcod.white)
- libtcod.console_print_ex(con, 1, SCREEN_HEIGHT - 2, libtcod.BKGND_NONE, libtcod.LEFT,
- 'HP: ' + str(player.fighter.hp) + '/' + str(player.fighter.max_hp))
- def player_move_or_attack(dx, dy):
- global fov_recompute
- #the coordinates the player is moving to/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
- def handle_keys():
- #key = libtcod.console_check_for_keypress() #real-time
- key = libtcod.console_wait_for_keypress(True) #turn-based
- 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)
- elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN):
- player_move_or_attack(0, 1)
- elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT):
- player_move_or_attack(-1, 0)
- elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
- player_move_or_attack(1, 0)
- else:
- return 'didnt-take-turn'
- #if libtcod.console_is_key_pressed(libtcod.KEY_ENTER):
- #Enter warp, if player is on it
- #if warp.x == player.x and warp.y == player.y:
- #make_customgenericmap(warp_calc)
- #############################################
- # Initialization & Main Loop
- #############################################
- #libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
- libtcod.console_set_custom_font("term8x82.png", libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW, 16, 16)
- libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'Gargoyle', False)
- libtcod.sys_set_fps(LIMIT_FPS)
- con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
- #Player Classes
- fighter_component = Fighter(hp=30, defense=2, power=5)
- #create object representing the player
- player = Object( 0, 0, 255, 'player', libtcod.white, blocks=True, fighter=fighter_component, ai=None)
- #the objects list
- objects = []
- #generate map (at this point it's not drawn to the screen)
- #make_map()
- make_customgenericmap()
- #FoV map
- 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)
- #Game states
- fov_recompute = True
- game_state = 'playing'
- player_action = None
- while not libtcod.console_is_window_closed():
- #render the screen
- render_all()
- libtcod.console_flush()
- #erase all objects at their old locations, before they move
- for object in objects:
- object.clear()
- #handle keys and exit game if needed
- exit = handle_keys()
- if player_action == 'exit':
- break
- #let monsters take their turn
- if game_state == 'playing' and player_action != 'didnt-take-turn':
- for object in objects:
- if object.ai:
- object.ai.take_turn()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement