all repos — Legends-RPG @ 6387912ba1c75cd67b42aa13d443f3b07598983d

A fantasy mini-RPG built with Python and Pygame.

Almost finished tmx map of town, a few minor bugs
Justin Armstrong justinmeister@gmail.com
Fri, 11 Apr 2014 18:47:19 -0700
commit

6387912ba1c75cd67b42aa13d443f3b07598983d

parent

688dcc38d8767fb3422534ee6f653630f4c19acd

M data/components/portal.pydata/components/portal.py

@@ -9,5 +9,5 @@ def __init__(self, x, y, name):

super(Portal, self).__init__() self.image = pg.Surface((32, 32)) self.image.fill(c.BLACK) - self.rect = pg.Rect(x*32, y*32, 32, 32) + self.rect = pg.Rect(x, y, 32, 32) self.name = name
M data/states/level_state.pydata/states/level_state.py

@@ -9,8 +9,7 @@

import copy import pygame as pg from .. import tools, collision -from .. import tilemap as tm -from .. components import person, textbox +from .. components import person, textbox, portal from . import player_menu from .. import tilerender from .. import setup

@@ -53,7 +52,7 @@ 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 = self.make_level_portals() self.menu_screen = player_menu.Player_Menu(game_data, self) def make_viewport(self, map_image):

@@ -64,7 +63,12 @@

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 + map_width = map_rect.width + if self.name == 'town': + map_height = map_rect.height - 32 + else: + map_height = map_rect.height + size = map_width, map_height return pg.Surface(size).convert() def make_player(self):

@@ -72,10 +76,12 @@ """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 + properties = object.__dict__ + if properties['name'] == 'player start': + posx = properties['x'] * 2 + posy = (properties['y'] * 2) - 32 + player.rect.x = posx + player.rect.y = posy return player

@@ -107,6 +113,21 @@ 'menu': self.goto_menu}

return state_dict + def make_level_portals(self): + """Make the portals to switch state""" + portal_group = pg.sprite.Group() + + for object in self.renderer.tmx_data.getObjects(): + properties = object.__dict__ + if properties['name'] == 'portal': + posx = properties['x'] * 2 + posy = (properties['y'] * 2) - 32 + new_state = properties['new state'] + portal_group.add(portal.Portal(posx, posy, new_state)) + + + return portal_group + def running_normally(self, surface, keys, current_time): """Update level normally"""

@@ -124,16 +145,14 @@

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': self.player.location = self.player.get_tile_location() self.next = portal.name + print self.next self.update_game_data() self.done = True - """ - pass def check_for_menu(self, keys):

@@ -209,8 +228,6 @@ """Blits all images to screen"""

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) - - surface.blit(self.level_surface, (0, 0), self.viewport) self.dialogue_handler.draw(surface)
M data/states/town/town.pydata/states/town/town.py

@@ -6,13 +6,14 @@ """

from .. import level_state from ... import constants as c +from ... import setup class Town(level_state.LevelState): def __init__(self): super(Town, self).__init__() self.name = c.TOWN - self.map_width = 25 - self.map_height = 50 + self.tmx_map = setup.TMX['town'] + def set_sprite_dialogue(self): """Sets unique dialogue for each sprite"""
D map_editor.py

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

-"""Basic map editor that creates a .txt file to work with my -tilemap module. Probably not the best way to do it.""" - -import os -import sys -import pygame as pg - -from data import constants as c -from data import setup - - -class SheetSelectorBox(object): - """The box to choose which sprite sheet to work with""" - def __init__(self, editor): - self.image = pg.Surface((200, 750)) - self.image.fill(c.WHITE) - self.rect = self.image.get_rect() - self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22) - self.editor = editor - self.rect_dict = self.make_rect_dict() - self.rect_to_draw = self.rect_dict['tileset1'] - self.tileset_selected = 'tileset1' - self.draw_sheet_names() - - def make_rect_dict(self): - sheet_list = ['tileset1', - 'tileset2', - 'tileset3'] - rect_list = [pg.Rect(0, (i*50), 190, 50) for i in range(3)] - - return dict(zip(sheet_list, rect_list)) - - - def draw_sheet_names(self): - sheet_list = ['tileset1', - 'tileset2', - 'tileset3'] - - for i, sheet in enumerate(sheet_list): - font_render = self.font.render(sheet, True, c.NEAR_BLACK) - font_rect = font_render.get_rect(x=10, y=10+(i*50)) - self.image.blit(font_render, font_rect) - - def update(self): - self.check_for_click(self.editor.click_point) - - def check_for_click(self, click_point): - if click_point: - for key in self.rect_dict: - if self.rect_dict[key].collidepoint(click_point): - if self.editor.mouse_clicked: - self.rect_to_draw = self.rect_dict[key] - self.tileset_selected = key - self.editor.mouse_clicked = False - - def draw(self, surface): - """Draw box to surface""" - surface.blit(self.image, self.rect) - if self.rect_to_draw: - pg.draw.rect(surface, c.DARK_RED, self.rect_to_draw, 10) - - -class SpriteSheetDisplay(pg.sprite.Sprite): - def __init__(self, selector): - self.selector = selector - self.image = self.set_image() - self.rect = self.image.get_rect(x=200) - - - def set_image(self): - key = self.selector.tileset_selected - sheet = setup.GFX[key] - rect = sheet.get_rect() - surface = pg.Surface(rect.size) - surface.blit(sheet, rect) - - return surface - - def update(self): - self.image = self.set_image() - - def draw(self, surface): - surface.blit(self.image, self.rect) - - -class NewMapGrid(object): - def __init__(self): - self.rect = pg.Rect(0, 200, 1200, 550) - - def draw(self, surface): - pg.draw.rect(surface, c.NEAR_BLACK, self.rect) - self.draw_grid_lines(surface) - - def draw_grid_lines(self, surface): - for column in range(self.rect.width/16): - pg.draw.line(surface, c.LIGHT_BLUE, (column*16, 200), (column*16, 750)) - for row in range(self.rect.height/16): - pg.draw.line(surface, c.LIGHT_BLUE, (0, 200+(row*16)), (1200, 200+(row*16))) - - - - -class MapCreator(object): - """A simple map tile editor""" - def __init__(self): - self.dimensions = self.set_dimensions() - self.screen = self.setup_pygame() - self.screen_rect = self.screen.get_rect() - self.clock = pg.time.Clock() - self.fps = 60.0 - self.keys = pg.key.get_pressed() - self.done = False - self.mouse_clicked = False - self.click_point = None - self.sheet_selector_box = SheetSelectorBox(self) - self.spritesheet_display = SpriteSheetDisplay(self.sheet_selector_box) - self.new_map_grid = NewMapGrid() - - - def set_dimensions(self): - if sys.argv[-1] == 'map_editor.py': - dimensions = 10, 10 - else: - dimensions = sys.argv[-1] - - print dimensions - - return dimensions - - - def setup_pygame(self): - """Set up pygame and return the main surface""" - os.environ['SDL_VIDEO_CENTERED'] = '1' - pg.init() - pg.display.set_mode((1200, 750)) - surface = pg.display.get_surface() - surface.fill(c.BLACK_BLUE) - - return surface - - def main_loop(self): - while not self.done: - self.event_loop() - self.update() - pg.display.update() - self.clock.tick(self.fps) - - def event_loop(self): - for event in pg.event.get(): - self.keys = pg.key.get_pressed() - if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]: - self.done = True - elif event.type == pg.MOUSEBUTTONDOWN: - self.mouse_clicked = True - self.click_point = pg.mouse.get_pos() - - def update(self): - self.sheet_selector_box.update() - self.spritesheet_display.update() - - self.screen.fill(c.BLACK_BLUE) - self.sheet_selector_box.draw(self.screen) - self.spritesheet_display.draw(self.screen) - self.new_map_grid.draw(self.screen) - - -if __name__ == "__main__": - map_creator = MapCreator() - map_creator.main_loop() - pg.quit() - sys.exit()
M resources/tmx/overworld.tmxresources/tmx/overworld.tmx

@@ -26,13 +26,6 @@ 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"/>

@@ -157,5 +150,16 @@ <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"/> + <object name="portal" gid="139" x="272" y="480"> + <properties> + <property name="new state" value="town"/> + </properties> + </object> + <object name="portal" gid="139" x="256" y="480"> + <properties> + <property name="new state" value="town"/> + </properties> + </object> + <object name="player start" gid="123" x="272" y="496"/> </objectgroup> </map>
A resources/tmx/town.tmx

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

+<?xml version="1.0" encoding="UTF-8"?> +<map version="1.0" orientation="orthogonal" width="25" height="51" 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> + <layer name="Tile Layer 1" width="25" height="51"> + <data encoding="base64" compression="zlib"> + eJztlc8KgCAMh733Rl58gB2jl4q6Rc+bBwOJmVv5Z8iC7yDu54eOGBhjQFEGZ/Vs6iBxeE4BPcNwDL5kXMb/ZA7UvLM6xnHsEdg656DkWziWCGydc1DyLRyAZLGz3nrOyZdkxP+Dev4UqOGJz2/lqf1eUntuGdwOTsYO9Fbq6ONIzU9OjQRHav5xaiQ4gLCfqynp6DHHOff4izrqO1p8Fz0Qegc= + </data> + </layer> + <layer name="Tile Layer 2" width="25" height="51"> + <data encoding="base64" compression="zlib"> + eJztVlsKxCAQyzH6vfjtkXZ7/1NsF1qQwceMmilbDAxFahPiqGkAF+Go6MAfHPhZOpKfsV7Lhx4ePqQOC+zzAfT52M6y6ljQo/GDZa1eZ1n5mT0H+D0Hlg8Llg89Uh9v8U6OWyh97+nD69+EAXbWAn2Z/jlqH+Bv6Vz8Gp0af0zm1HRG+YGxPRCTZxBjBtj3lARz717PJ57BmXqaPTyKf+dHgX9235k+ZMbmfFhzvIS7+v2UczHjnIcKPzJjrZa8W1vZGsV8jU5uLTTZas3H3HzvDPwCwBwqqw== + </data> + </layer> + <objectgroup name="Object Layer 1" width="25" height="51"> + <object name="player start" gid="123" x="176" y="800"/> + <object name="blocker" gid="120" x="112" y="800"/> + <object name="blocker" gid="120" x="112" y="784"/> + <object name="blocker" gid="120" x="96" y="768"/> + <object name="blocker" gid="120" x="80" y="784"/> + <object name="blocker" gid="120" x="64" y="768"/> + <object name="blocker" gid="120" x="64" y="752"/> + <object name="blocker" gid="120" x="48" y="768"/> + <object name="blocker" gid="120" x="32" y="752"/> + <object name="blocker" gid="120" x="32" y="736"/> + <object name="blocker" gid="120" x="32" y="720"/> + <object name="blocker" gid="120" x="32" y="704"/> + <object name="blocker" gid="120" x="48" y="688"/> + <object name="blocker" gid="120" x="80" y="688"/> + <object name="blocker" gid="120" x="80" y="672"/> + <object name="blocker" gid="120" x="80" y="640"/> + <object name="blocker" gid="120" x="80" y="656"/> + <object name="blocker" gid="120" x="64" y="640"/> + <object name="blocker" gid="120" x="48" y="624"/> + <object name="blocker" gid="120" x="64" y="608"/> + <object name="blocker" gid="120" x="64" y="592"/> + <object name="blocker" gid="120" x="48" y="608"/> + <object name="blocker" gid="120" x="32" y="592"/> + <object name="blocker" gid="120" x="16" y="576"/> + <object name="blocker" gid="120" x="16" y="544"/> + <object name="blocker" gid="120" x="16" y="560"/> + <object name="blocker" gid="120" x="16" y="512"/> + <object name="blocker" gid="120" x="32" y="528"/> + <object name="blocker" gid="120" x="64" y="528"/> + <object name="blocker" gid="120" x="80" y="528"/> + <object name="blocker" gid="120" x="96" y="528"/> + <object name="blocker" gid="120" x="128" y="528"/> + <object name="blocker" gid="120" x="112" y="528"/> + <object name="blocker" gid="120" x="144" y="528"/> + <object name="blocker" gid="120" x="160" y="528"/> + <object name="blocker" gid="120" x="48" y="528"/> + <object name="blocker" gid="120" x="32" y="496"/> + <object name="blocker" gid="120" x="48" y="480"/> + <object name="blocker" gid="120" x="80" y="496"/> + <object name="blocker" gid="120" x="80" y="480"/> + <object name="blocker" gid="120" x="48" y="464"/> + <object name="blocker" gid="120" x="32" y="448"/> + <object name="blocker" gid="120" x="16" y="432"/> + <object name="blocker" gid="120" x="16" y="416"/> + <object name="blocker" gid="120" x="16" y="400"/> + <object name="blocker" gid="120" x="0" y="384"/> + <object name="blocker" gid="120" x="16" y="368"/> + <object name="blocker" gid="120" x="48" y="368"/> + <object name="blocker" gid="120" x="48" y="336"/> + <object name="blocker" gid="120" x="48" y="352"/> + <object name="blocker" gid="120" x="48" y="320"/> + <object name="blocker" gid="120" x="32" y="320"/> + <object name="blocker" gid="120" x="16" y="320"/> + <object name="blocker" gid="120" x="80" y="368"/> + <object name="blocker" gid="120" x="112" y="368"/> + <object name="blocker" gid="120" x="112" y="352"/> + <object name="blocker" gid="120" x="112" y="336"/> + <object name="blocker" gid="120" x="112" y="320"/> + <object name="blocker" gid="120" x="96" y="320"/> + <object name="blocker" gid="120" x="80" y="320"/> + <object name="blocker" gid="120" x="80" y="336"/> + <object name="blocker" gid="120" x="80" y="352"/> + <object name="blocker" gid="120" x="272" y="368"/> + <object name="blocker" gid="120" x="272" y="352"/> + <object name="blocker" gid="120" x="272" y="320"/> + <object name="blocker" gid="120" x="272" y="336"/> + <object name="blocker" gid="120" x="288" y="320"/> + <object name="blocker" gid="120" x="304" y="320"/> + <object name="blocker" gid="120" x="304" y="320"/> + <object name="blocker" gid="120" x="304" y="336"/> + <object name="blocker" gid="120" x="304" y="336"/> + <object name="blocker" gid="120" x="304" y="352"/> + <object name="blocker" gid="120" x="304" y="368"/> + <object name="blocker" gid="120" x="336" y="368"/> + <object name="blocker" gid="120" x="336" y="336"/> + <object name="blocker" gid="120" x="336" y="352"/> + <object name="blocker" gid="120" x="336" y="320"/> + <object name="blocker" gid="120" x="352" y="320"/> + <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="336"/> + <object name="blocker" gid="120" x="368" y="352"/> + <object name="blocker" gid="120" x="368" y="352"/> + <object name="blocker" gid="120" x="368" y="368"/> + <object name="blocker" gid="120" x="400" y="384"/> + <object name="blocker" gid="120" x="400" y="384"/> + <object name="blocker" gid="120" x="400" y="384"/> + <object name="blocker" gid="120" x="384" y="384"/> + <object name="blocker" gid="120" x="368" y="400"/> + <object name="blocker" gid="120" x="368" y="416"/> + <object name="blocker" gid="120" x="368" y="432"/> + <object name="blocker" gid="120" x="368" y="432"/> + <object name="blocker" gid="120" x="352" y="448"/> + <object name="blocker" gid="120" x="336" y="464"/> + <object name="blocker" gid="120" x="320" y="480"/> + <object name="blocker" gid="120" x="320" y="496"/> + <object name="blocker" gid="120" x="336" y="480"/> + <object name="blocker" gid="120" x="352" y="496"/> + <object name="blocker" gid="120" x="368" y="512"/> + <object name="blocker" gid="120" x="224" y="528"/> + <object name="blocker" gid="120" x="208" y="528"/> + <object name="blocker" gid="120" x="256" y="528"/> + <object name="blocker" gid="120" x="240" y="528"/> + <object name="blocker" gid="120" x="288" y="528"/> + <object name="blocker" gid="120" x="272" y="528"/> + <object name="blocker" gid="120" x="320" y="528"/> + <object name="blocker" gid="120" x="320" y="528"/> + <object name="blocker" gid="120" x="336" y="528"/> + <object name="blocker" gid="120" x="352" y="528"/> + <object name="blocker" gid="120" x="304" y="528"/> + <object name="blocker" gid="120" x="368" y="544"/> + <object name="blocker" gid="120" x="368" y="560"/> + <object name="blocker" gid="120" x="368" y="560"/> + <object name="blocker" gid="120" x="368" y="576"/> + <object name="blocker" gid="120" x="368" y="592"/> + <object name="blocker" gid="120" x="368" y="608"/> + <object name="blocker" gid="120" x="368" y="608"/> + <object name="blocker" gid="120" x="352" y="624"/> + <object name="blocker" gid="120" x="336" y="624"/> + <object name="blocker" gid="120" x="320" y="624"/> + <object name="blocker" gid="120" x="304" y="624"/> + <object name="blocker" gid="120" x="304" y="624"/> + <object name="blocker" gid="120" x="304" y="640"/> + <object name="blocker" gid="120" x="304" y="656"/> + <object name="blocker" gid="120" x="304" y="656"/> + <object name="blocker" gid="120" x="304" y="672"/> + <object name="blocker" gid="120" x="336" y="672"/> + <object name="blocker" gid="120" x="352" y="688"/> + <object name="blocker" gid="120" x="336" y="704"/> + <object name="blocker" gid="120" x="336" y="720"/> + <object name="blocker" gid="120" x="336" y="736"/> + <object name="blocker" gid="120" x="320" y="752"/> + <object name="blocker" gid="120" x="304" y="752"/> + <object name="blocker" gid="120" x="288" y="768"/> + <object name="blocker" gid="120" x="272" y="784"/> + <object name="blocker" gid="120" x="256" y="768"/> + <object name="blocker" gid="120" x="256" y="784"/> + <object name="blocker" gid="120" x="272" y="800"/> + <object name="blocker" gid="120" x="0" y="304"/> + <object name="blocker" gid="120" x="0" y="272"/> + <object name="blocker" gid="120" x="0" y="288"/> + <object name="blocker" gid="120" x="0" y="256"/> + <object name="blocker" gid="120" x="0" y="224"/> + <object name="blocker" gid="120" x="0" y="224"/> + <object name="blocker" gid="120" x="0" y="240"/> + <object name="blocker" gid="120" x="16" y="208"/> + <object name="blocker" gid="120" x="32" y="208"/> + <object name="blocker" gid="120" x="48" y="208"/> + <object name="blocker" gid="120" x="64" y="208"/> + <object name="blocker" gid="120" x="80" y="208"/> + <object name="blocker" gid="120" x="96" y="208"/> + <object name="blocker" gid="120" x="112" y="208"/> + <object name="blocker" gid="120" x="128" y="208"/> + <object name="blocker" gid="120" x="144" y="208"/> + <object name="blocker" gid="120" x="160" y="208"/> + <object name="blocker" gid="120" x="224" y="208"/> + <object name="blocker" gid="120" x="208" y="208"/> + <object name="blocker" gid="120" x="240" y="208"/> + <object name="blocker" gid="120" x="256" y="208"/> + <object name="blocker" gid="120" x="272" y="208"/> + <object name="blocker" gid="120" x="288" y="208"/> + <object name="blocker" gid="120" x="304" y="208"/> + <object name="blocker" gid="120" x="320" y="208"/> + <object name="blocker" gid="120" x="336" y="208"/> + <object name="blocker" gid="120" x="368" y="208"/> + <object name="blocker" gid="120" x="352" y="208"/> + <object name="blocker" gid="120" x="400" y="304"/> + <object name="blocker" gid="120" x="384" y="304"/> + <object name="blocker" gid="120" x="384" y="272"/> + <object name="blocker" gid="120" x="384" y="288"/> + <object name="blocker" gid="120" x="384" y="256"/> + <object name="blocker" gid="120" x="384" y="240"/> + <object name="blocker" gid="120" x="384" y="224"/> + <object name="portal" gid="139" x="192" y="816"> + <properties> + <property name="new state" value="overworld"/> + </properties> + </object> + <object name="portal" gid="139" x="176" y="816"> + <properties> + <property name="new state" value="overworld"/> + </properties> + </object> + <object gid="139" x="64" y="688"/> + <object name="portal" gid="139" x="320" y="672"> + <properties> + <property name="new state" value="Inn"/> + </properties> + </object> + <object name="portal" gid="139" x="96" y="368"> + <properties> + <property name="new state" value="armor shop"/> + </properties> + </object> + <object name="portal" gid="139" x="32" y="368"> + <properties> + <property name="new state" value="weapon shop"/> + </properties> + </object> + <object name="portal" gid="139" x="288" y="368"> + <properties> + <property name="new state" value="potion shop"/> + </properties> + </object> + <object name="portal" gid="139" x="352" y="368"> + <properties> + <property name="new state" value="magic shop"/> + </properties> + </object> + </objectgroup> +</map>