all repos — Legends-RPG @ f2dca0d393a15a440a0a97993509345ca515d575

A fantasy mini-RPG built with Python and Pygame.

Finished basic overworld map using tmx.  Still need to add portals.
Justin Armstrong justinmeister@gmail.com
Fri, 11 Apr 2014 00:32:15 -0700
commit

f2dca0d393a15a440a0a97993509345ca515d575

parent

5dc45a6a3236bb0f37cda02f65aecb54b0e7212e

M data/setup.pydata/setup.py

@@ -25,6 +25,7 @@ FONTS = tools.load_all_fonts(os.path.join('resources', 'fonts'))

MUSIC = tools.load_all_music(os.path.join('resources', 'music')) GFX = tools.load_all_gfx(os.path.join('resources', 'graphics')) SFX = tools.load_all_sfx(os.path.join('resources', 'sound')) +TMX = tools.load_all_tmx(os.path.join('resources', 'tmx')) FONT = pg.font.Font(FONTS['Fixedsys500c'], 20)
M data/states/level_state.pydata/states/level_state.py

@@ -12,12 +12,15 @@ from .. import tools, collision

from .. import tilemap as tm from .. components import person, textbox from . import player_menu +from .. import tilerender +from .. import setup class LevelState(tools._State): def __init__(self): super(LevelState, self).__init__() self.name = None + self.tmx_map = None self.map_width = None self.map_height = None

@@ -27,31 +30,69 @@ self.game_data = game_data

self.current_time = current_time self.state = 'normal' self.allow_input = False - self.background = tm.create_map_layer1(self.name, - self.map_width, - self.map_height) - self.foreground = tm.create_map_layer2(self.name, - self.map_width, - self.map_height) - self.viewport = tm.create_viewport(self.background) - self.blockers = tm.create_blockers(self.name) - self.level_surface = tm.make_level_surface(self.background) + self.renderer = tilerender.Renderer(self.tmx_map) + self.map_image = self.renderer.make_2x_map() + + self.viewport = self.make_viewport(self.map_image) + self.level_surface = self.make_level_surface(self.map_image) self.level_rect = self.level_surface.get_rect() + self.player = None + self.portals = None self.player = person.Player(game_data['last direction']) + self.player = self.make_player() + self.blockers = self.make_blockers() self.sprites = pg.sprite.Group() - self.start_positions = tm.set_sprite_positions(self.player, - self.sprites, - self.name, - self.game_data) - self.set_sprite_dialogue() + #self.start_positions = tm.set_sprite_positions(self.player, + # self.sprites, + # self.name, + # self.game_data) + #self.set_sprite_dialogue() self.collision_handler = collision.CollisionHandler(self.player, self.blockers, self.sprites) + self.dialogue_handler = textbox.TextHandler(self) self.state_dict = self.make_state_dict() - self.portals = tm.make_level_portals(self.name) + #self.portals = tm.make_level_portals(self.name) self.menu_screen = player_menu.Player_Menu(game_data, self) + def make_viewport(self, map_image): + """Create the viewport to view the level through""" + map_rect = map_image.get_rect() + return setup.SCREEN.get_rect(bottom=map_rect.bottom) + + def make_level_surface(self, map_image): + """Creates the surface all images are blitted to""" + map_rect = map_image.get_rect() + size = map_rect.size + return pg.Surface(size).convert() + + def make_player(self): + """Makes the player and sets location""" + player = person.Player(self.game_data['last direction']) + + for object in self.renderer.tmx_data.getObjects(): + property_dict = object.__dict__ + if property_dict['name'] == 'Player start': + player.rect.x = int(property_dict['posx']) * 32 + player.rect.y = int(property_dict['posy']) * 32 + + return player + + def make_blockers(self): + """Make the blockers for the level""" + blockers = [] + + for object in self.renderer.tmx_data.getObjects(): + properties = object.__dict__ + if properties['name'] == 'blocker': + left = properties['x'] * 2 + top = ((properties['y']) * 2) - 32 + blocker = pg.Rect(left, top, 32, 32) + blockers.append(blocker) + + return blockers + def set_sprite_dialogue(self): """Sets unique dialogue for each sprite"""

@@ -72,9 +113,9 @@ """Update level normally"""

self.check_for_dialogue() self.check_for_portals() self.player.update(keys, current_time) - self.sprites.update(current_time) + #self.sprites.update(current_time) self.collision_handler.update(keys, current_time) - self.dialogue_handler.update(keys, current_time) + #self.dialogue_handler.update(keys, current_time) self.check_for_menu(keys) self.viewport_update()

@@ -83,6 +124,7 @@

def check_for_portals(self): """Check if the player walks into a door, requiring a level change""" + """ portal = pg.sprite.spritecollideany(self.player, self.portals) if portal and self.player.state == 'resting':

@@ -90,6 +132,8 @@ self.player.location = self.player.get_tile_location()

self.next = portal.name self.update_game_data() self.done = True + """ + pass def check_for_menu(self, keys):

@@ -98,7 +142,6 @@ if keys[pg.K_RETURN] and self.allow_input:

if self.player.state == 'resting': self.state = 'menu' self.allow_input = False - if not keys[pg.K_RETURN]: self.allow_input = True

@@ -135,7 +178,7 @@

def handling_dialogue(self, surface, keys, current_time): """Update only dialogue boxes""" - self.dialogue_handler.update(keys, current_time) + #self.dialogue_handler.update(keys, current_time) self.draw_level(surface)

@@ -147,8 +190,9 @@

def check_for_dialogue(self): """Check if the level needs to freeze""" - if self.dialogue_handler.textbox: - self.state = 'dialogue' + #if self.dialogue_handler.textbox: + # self.state = 'dialogue' + pass def update(self, surface, keys, current_time): """Updates state"""

@@ -162,14 +206,15 @@ self.viewport.clamp_ip(self.level_rect)

def draw_level(self, surface): """Blits all images to screen""" - self.level_surface.blit(self.background, self.viewport, self.viewport) + self.level_surface.blit(self.map_image, self.viewport, self.viewport) self.level_surface.blit(self.player.image, self.player.rect) self.sprites.draw(self.level_surface) - self.level_surface.blit(self.foreground, self.viewport, self.viewport) + surface.blit(self.level_surface, (0, 0), self.viewport) self.dialogue_handler.draw(surface) +
M data/states/overworld/overworld.pydata/states/overworld/overworld.py

@@ -5,13 +5,14 @@ """

from .. import level_state from ... import constants as c +from ... import setup class Overworld(level_state.LevelState): def __init__(self): super(Overworld, self).__init__() self.name = c.OVERWORLD - self.map_width = 30 - self.map_height = 37 + self.tmx_map = setup.TMX['overworld'] +
A data/tilerender.py

@@ -0,0 +1,45 @@

+""" +This is a test of using the pytmx library with Tiled. +""" +import sys +import pygame as pg +import pytmx + +class Renderer(object): + """ + This object renders tile maps from Tiled + """ + def __init__(self, filename): + tm = pytmx.load_pygame(filename, pixelalpha=True) + self.size = tm.width * tm.tilewidth, tm.height * tm.tileheight + self.tmx_data = tm + + def render(self, surface): + + tw = self.tmx_data.tilewidth + th = self.tmx_data.tileheight + gt = self.tmx_data.getTileImageByGid + + if self.tmx_data.background_color: + surface.fill(self.tmx_data.background_color) + + for layer in self.tmx_data.visibleLayers: + if isinstance(layer, pytmx.TiledLayer): + for x, y, gid in layer: + tile = gt(gid) + if tile: + surface.blit(tile, (x * tw, y * th)) + + elif isinstance(layer, pytmx.TiledObjectGroup): + pass + + elif isinstance(layer, pytmx.TiledImageLayer): + image = gt(layer.gid) + if image: + surface.blit(image, (0, 0)) + + def make_2x_map(self): + temp_surface = pg.Surface(self.size) + self.render(temp_surface) + temp_surface = pg.transform.scale2x(temp_surface) + return temp_surface
M data/tools.pydata/tools.py

@@ -132,6 +132,10 @@ def load_all_fonts(directory, accept=('.ttf')):

return load_all_music(directory, accept) +def load_all_tmx(directory, accept=('.tmx')): + return load_all_music(directory, accept) + + def load_all_sfx(directory, accept=('.wav','.mp3','.ogg','.mdi')): effects = {} for fx in os.listdir(directory):
A resources/tmx/__init__.py

@@ -0,0 +1,1 @@

+__author__ = 'justin'
A resources/tmx/overworld.tmx

@@ -0,0 +1,161 @@

+<?xml version="1.0" encoding="UTF-8"?> +<map version="1.0" orientation="orthogonal" width="30" height="37" tilewidth="16" tileheight="16"> + <tileset firstgid="1" name="tileset1" tilewidth="16" tileheight="16"> + <image source="../graphics/tileset1.png" width="160" height="80"/> + </tileset> + <tileset firstgid="51" name="tileset2" tilewidth="16" tileheight="16"> + <image source="../graphics/tileset2.png" width="160" height="180"/> + </tileset> + <tileset firstgid="161" name="evilcastle" tilewidth="16" tileheight="16"> + <image source="../graphics/evilcastle.png" width="64" height="64"/> + </tileset> + <tileset firstgid="177" name="houseoverworld" tilewidth="16" tileheight="16"> + <image source="../graphics/houseoverworld.png" width="64" height="64"/> + </tileset> + <tileset firstgid="193" name="house1" tilewidth="16" tileheight="16"> + <image source="../graphics/house1.png" width="16" height="16"/> + </tileset> + <layer name="Tile Layer 1" width="30" height="37"> + <data encoding="base64" compression="zlib"> + eJztls0KgCAQhH2ojJBC6hD9vP/7tAcPsqi4thtUe/hQuowzO8Q6Y4z7GR7YgS2cB7pPQrozcBZQ3Wd0z8zs42+4E6n70KDLwZLQ7QAL9AEJXew3hX3Ab0k7zoGqgzOs8ZtiZM60llUgU/Wrfin/NU6/lJw5/VJy5vRLyVn7rH2WyJl7J6vZB6T2wFLelLl6xJ2878wVvwPD5belXzYDZ4/fQE0/lO9zAVP6EKM= + </data> + </layer> + <layer name="Tile Layer 2" width="30" height="37"> + <data encoding="base64" compression="zlib"> + eJztzlkOglAAQ9G3/12oOAAyqDgwCjvjJuyhBNKbnO82BKfugCNOiIS7Z1xwxU24GyNBirtwN0OOAqVwd29Nax9wzjnn3GZ74IkXKuHuGx988RPu1mjQohPu9hjwxyjcdc4tzdmDFtI= + </data> + </layer> + <objectgroup name="Object Layer 1" width="30" height="37"> + <object name="Player start" x="272" y="480" width="16" height="16"> + <properties> + <property name="person" value="player"/> + <property name="posx" value="17"/> + <property name="posy" value="30"/> + </properties> + </object> + <object name="blocker" gid="120" x="288" y="512"/> + <object name="blocker" gid="120" x="304" y="512"/> + <object name="blocker" gid="120" x="320" y="512"/> + <object name="blocker" gid="120" x="336" y="496"/> + <object name="blocker" gid="120" x="336" y="480"/> + <object name="blocker" gid="120" x="336" y="464"/> + <object name="blocker" gid="120" x="336" y="448"/> + <object name="blocker" gid="120" x="352" y="432"/> + <object name="blocker" gid="120" x="368" y="416"/> + <object name="blocker" gid="120" x="368" y="400"/> + <object name="blocker" gid="120" x="368" y="384"/> + <object name="blocker" gid="120" x="368" y="368"/> + <object name="blocker" gid="120" x="288" y="480"/> + <object name="blocker" gid="120" x="288" y="464"/> + <object name="blocker" gid="120" x="288" y="448"/> + <object name="blocker" gid="120" x="288" y="432"/> + <object name="blocker" gid="120" x="272" y="432"/> + <object name="blocker" gid="120" x="256" y="432"/> + <object name="blocker" gid="120" x="240" y="448"/> + <object name="blocker" gid="120" x="240" y="464"/> + <object name="blocker" gid="120" x="240" y="480"/> + <object name="blocker" gid="120" x="272" y="512"/> + <object name="blocker" gid="120" x="256" y="512"/> + <object name="blocker" gid="120" x="240" y="512"/> + <object name="blocker" gid="120" x="224" y="512"/> + <object name="blocker" gid="120" x="208" y="512"/> + <object name="blocker" gid="120" x="192" y="496"/> + <object name="blocker" gid="120" x="192" y="480"/> + <object name="blocker" gid="120" x="192" y="464"/> + <object name="blocker" gid="120" x="192" y="448"/> + <object name="blocker" gid="120" x="176" y="432"/> + <object name="blocker" gid="120" x="160" y="416"/> + <object name="blocker" gid="120" x="160" y="400"/> + <object name="blocker" gid="120" x="160" y="336"/> + <object name="blocker" gid="120" x="160" y="336"/> + <object name="blocker" gid="120" x="160" y="352"/> + <object name="blocker" gid="120" x="160" y="352"/> + <object name="blocker" gid="120" x="160" y="368"/> + <object name="blocker" gid="120" x="160" y="384"/> + <object name="blocker" gid="120" x="160" y="320"/> + <object name="blocker" gid="120" x="160" y="304"/> + <object name="blocker" gid="120" x="160" y="288"/> + <object name="blocker" gid="120" x="160" y="272"/> + <object name="blocker" gid="120" x="160" y="256"/> + <object name="blocker" gid="120" x="160" y="240"/> + <object name="blocker" gid="120" x="160" y="240"/> + <object name="blocker" gid="120" x="160" y="224"/> + <object name="blocker" gid="120" x="160" y="192"/> + <object name="blocker" gid="120" x="160" y="208"/> + <object name="blocker" gid="120" x="144" y="192"/> + <object name="blocker" gid="120" x="128" y="192"/> + <object name="blocker" gid="120" x="112" y="192"/> + <object name="blocker" gid="120" x="96" y="176"/> + <object name="blocker" gid="120" x="80" y="160"/> + <object name="blocker" gid="120" x="64" y="160"/> + <object name="blocker" gid="120" x="48" y="160"/> + <object name="blocker" gid="120" x="32" y="160"/> + <object name="blocker" gid="120" x="16" y="160"/> + <object name="blocker" gid="120" x="0" y="144"/> + <object name="blocker" gid="120" x="0" y="128"/> + <object name="blocker" gid="120" x="0" y="112"/> + <object name="blocker" gid="120" x="0" y="96"/> + <object name="blocker" gid="120" x="0" y="80"/> + <object name="blocker" gid="120" x="0" y="64"/> + <object name="blocker" gid="120" x="16" y="48"/> + <object name="blocker" gid="120" x="32" y="32"/> + <object name="blocker" gid="120" x="48" y="32"/> + <object name="blocker" gid="120" x="64" y="32"/> + <object name="blocker" gid="120" x="80" y="32"/> + <object name="blocker" gid="120" x="96" y="32"/> + <object name="blocker" gid="120" x="112" y="32"/> + <object name="blocker" gid="120" x="128" y="32"/> + <object name="blocker" gid="120" x="144" y="32"/> + <object name="blocker" gid="120" x="160" y="48"/> + <object name="blocker" gid="120" x="160" y="64"/> + <object name="blocker" gid="120" x="160" y="80"/> + <object name="blocker" gid="120" x="160" y="96"/> + <object name="blocker" gid="120" x="176" y="96"/> + <object name="blocker" gid="120" x="192" y="96"/> + <object name="blocker" gid="120" x="224" y="96"/> + <object name="blocker" gid="120" x="208" y="96"/> + <object name="blocker" gid="120" x="240" y="96"/> + <object name="blocker" gid="120" x="256" y="96"/> + <object name="blocker" gid="120" x="272" y="96"/> + <object name="blocker" gid="120" x="288" y="96"/> + <object name="blocker" gid="120" x="304" y="96"/> + <object name="blocker" gid="120" x="320" y="96"/> + <object name="blocker" gid="120" x="336" y="96"/> + <object name="blocker" gid="120" x="352" y="96"/> + <object name="blocker" gid="120" x="368" y="96"/> + <object name="blocker" gid="120" x="384" y="96"/> + <object name="blocker" gid="120" x="400" y="96"/> + <object name="blocker" gid="120" x="416" y="96"/> + <object name="blocker" gid="120" x="432" y="96"/> + <object name="blocker" gid="120" x="448" y="112"/> + <object name="blocker" gid="120" x="448" y="128"/> + <object name="blocker" gid="120" x="448" y="144"/> + <object name="blocker" gid="120" x="448" y="160"/> + <object name="blocker" gid="120" x="448" y="176"/> + <object name="blocker" gid="120" x="432" y="192"/> + <object name="blocker" gid="120" x="416" y="192"/> + <object name="blocker" gid="120" x="400" y="192"/> + <object name="blocker" gid="120" x="384" y="192"/> + <object name="blocker" gid="120" x="368" y="192"/> + <object name="blocker" gid="120" x="368" y="208"/> + <object name="blocker" gid="120" x="368" y="224"/> + <object name="blocker" gid="120" x="368" y="240"/> + <object name="blocker" gid="120" x="368" y="256"/> + <object name="blocker" gid="120" x="368" y="272"/> + <object name="blocker" gid="120" x="368" y="288"/> + <object name="blocker" gid="120" x="368" y="304"/> + <object name="blocker" gid="120" x="368" y="320"/> + <object name="blocker" gid="120" x="368" y="336"/> + <object name="blocker" gid="120" x="368" y="352"/> + <object name="blocker" gid="120" x="96" y="112"/> + <object name="blocker" gid="120" x="48" y="112"/> + <object name="blocker" gid="120" x="48" y="96"/> + <object name="blocker" gid="120" x="48" y="80"/> + <object name="blocker" gid="120" x="48" y="64"/> + <object name="blocker" gid="120" x="64" y="64"/> + <object name="blocker" gid="120" x="80" y="64"/> + <object name="blocker" gid="120" x="96" y="64"/> + <object name="blocker" gid="120" x="96" y="80"/> + <object name="blocker" gid="120" x="96" y="96"/> + </objectgroup> +</map>