all repos — Legends-RPG @ cb2c441948d0a97c4525338090a60b636f5b8c09

A fantasy mini-RPG built with Python and Pygame.

map_editor.py (view raw)

  1"""Basic map editor that creates a .txt file to work with my
  2tilemap module. Probably not the best way to do it."""
  3
  4import os
  5import sys
  6import pygame as pg
  7
  8from data import constants as c
  9from data import setup
 10
 11
 12class SheetSelectorBox(object):
 13    """The box to choose which sprite sheet to work with"""
 14    def __init__(self, editor):
 15        self.image = pg.Surface((200, 750))
 16        self.image.fill(c.WHITE)
 17        self.rect = self.image.get_rect()
 18        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
 19        self.editor = editor
 20        self.rect_dict = self.make_rect_dict()
 21        self.rect_to_draw = self.rect_dict['tileset1']
 22        self.tileset_selected = 'tileset1'
 23        self.draw_sheet_names()
 24
 25    def make_rect_dict(self):
 26        sheet_list = ['tileset1',
 27                      'tileset2',
 28                      'tileset3']
 29        rect_list = [pg.Rect(0, (i*50), 190, 50) for i in range(3)]
 30
 31        return dict(zip(sheet_list, rect_list))
 32
 33
 34    def draw_sheet_names(self):
 35        sheet_list = ['tileset1',
 36                      'tileset2',
 37                      'tileset3']
 38
 39        for i, sheet in enumerate(sheet_list):
 40            font_render = self.font.render(sheet, True, c.NEAR_BLACK)
 41            font_rect = font_render.get_rect(x=10, y=10+(i*50))
 42            self.image.blit(font_render, font_rect)
 43
 44    def update(self):
 45        self.check_for_click(self.editor.click_point)
 46
 47    def check_for_click(self, click_point):
 48        if click_point:
 49            for key in self.rect_dict:
 50                if self.rect_dict[key].collidepoint(click_point):
 51                    if self.editor.mouse_clicked:
 52                        self.rect_to_draw = self.rect_dict[key]
 53                        self.tileset_selected = key
 54                        self.editor.mouse_clicked = False
 55
 56    def draw(self, surface):
 57        """Draw box to surface"""
 58        surface.blit(self.image, self.rect)
 59        if self.rect_to_draw:
 60            pg.draw.rect(surface, c.DARK_RED, self.rect_to_draw, 10)
 61
 62
 63class SpriteSheetDisplay(pg.sprite.Sprite):
 64    def __init__(self, selector):
 65        self.selector = selector
 66        self.image = self.set_image()
 67        self.rect = self.image.get_rect(x=200)
 68
 69
 70    def set_image(self):
 71        key = self.selector.tileset_selected
 72        sheet = setup.GFX[key]
 73        rect = sheet.get_rect()
 74        surface = pg.Surface(rect.size)
 75        surface.blit(sheet, rect)
 76
 77        return surface
 78
 79    def update(self):
 80        self.image = self.set_image()
 81
 82    def draw(self, surface):
 83        surface.blit(self.image, self.rect)
 84
 85
 86class NewMapGrid(object):
 87    def __init__(self):
 88        self.rect = pg.Rect(0, 200, 1200, 550)
 89
 90    def draw(self, surface):
 91        pg.draw.rect(surface, c.NEAR_BLACK, self.rect)
 92        self.draw_grid_lines(surface)
 93
 94    def draw_grid_lines(self, surface):
 95        for column in range(self.rect.width/16):
 96            pg.draw.line(surface, c.LIGHT_BLUE, (column*16, 200), (column*16, 750))
 97            for row in range(self.rect.height/16):
 98                pg.draw.line(surface, c.LIGHT_BLUE, (0, 200+(row*16)), (1200, 200+(row*16)))
 99
100
101
102
103class MapCreator(object):
104    """A simple map tile editor"""
105    def __init__(self):
106        self.dimensions = self.set_dimensions()
107        self.screen = self.setup_pygame()
108        self.screen_rect = self.screen.get_rect()
109        self.clock = pg.time.Clock()
110        self.fps = 60.0
111        self.keys = pg.key.get_pressed()
112        self.done = False
113        self.mouse_clicked = False
114        self.click_point = None
115        self.sheet_selector_box = SheetSelectorBox(self)
116        self.spritesheet_display = SpriteSheetDisplay(self.sheet_selector_box)
117        self.new_map_grid = NewMapGrid()
118
119
120    def set_dimensions(self):
121        if sys.argv[-1] == 'map_editor.py':
122            dimensions = 10, 10
123        else:
124            dimensions = sys.argv[-1]
125
126        print dimensions
127
128        return dimensions
129
130
131    def setup_pygame(self):
132        """Set up pygame and return the main surface"""
133        os.environ['SDL_VIDEO_CENTERED'] = '1'
134        pg.init()
135        pg.display.set_mode((1200, 750))
136        surface = pg.display.get_surface()
137        surface.fill(c.BLACK_BLUE)
138
139        return surface
140
141    def main_loop(self):
142        while not self.done:
143            self.event_loop()
144            self.update()
145            pg.display.update()
146            self.clock.tick(self.fps)
147
148    def event_loop(self):
149        for event in pg.event.get():
150            self.keys = pg.key.get_pressed()
151            if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
152                self.done = True
153            elif event.type == pg.MOUSEBUTTONDOWN:
154                self.mouse_clicked = True
155                self.click_point = pg.mouse.get_pos()
156
157    def update(self):
158        self.sheet_selector_box.update()
159        self.spritesheet_display.update()
160
161        self.screen.fill(c.BLACK_BLUE)
162        self.sheet_selector_box.draw(self.screen)
163        self.spritesheet_display.draw(self.screen)
164        self.new_map_grid.draw(self.screen)
165
166
167if __name__ == "__main__":
168    map_creator = MapCreator()
169    map_creator.main_loop()
170    pg.quit()
171    sys.exit()