all repos — Legends-RPG @ 587b711b61bf0efc9991a9f12f41f585d634af04

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.draw_sheet_names()
 21        self.button_rects = self.create_button_rects()
 22        
 23
 24
 25    def draw_sheet_names(self):
 26        sheet_list = ['tileset1',
 27                      'tileset2',
 28                      'tileset3']
 29
 30        for i, sheet in enumerate(sheet_list):
 31            font_render = self.font.render(sheet, True, c.NEAR_BLACK)
 32            font_rect = font_render.get_rect(x=10, y=10+(i*50))
 33            self.image.blit(font_render, font_rect)
 34
 35
 36
 37
 38    def create_button_rects(self):
 39        rect_dict = {}
 40
 41        for i in range(3):
 42            new_rect = pg.Rect(0, (i*50), 200, 50)
 43            rect_dict['rect'+str(i)] = new_rect
 44
 45        return rect_dict
 46
 47
 48    def update(self):
 49        self.check_for_click(self.editor.click_point)
 50
 51
 52    def check_for_click(self, click_point):
 53        for rect in self.button_rects:
 54            if self.button_rects[rect].collidepoint(click_point):
 55                if self.editor.mouse_clicked:
 56                    self.rect_to_draw = self.button_rects[rect]
 57                    self.editor.mouse_clicked = False
 58
 59
 60    def draw(self, surface):
 61        """Draw box to surface"""
 62        surface.blit(self.image, self.rect)
 63        pg.draw.rect(surface, c.DARK_RED, self.rect_to_draw, 10)
 64
 65
 66
 67class MapCreator(object):
 68    """A simple map tile editor"""
 69    def __init__(self):
 70        self.screen = self.setup_pygame()
 71        self.screen_rect = self.screen.get_rect()
 72        self.clock = pg.time.Clock()
 73        self.fps = 60.0
 74        self.keys = pg.key.get_pressed()
 75        self.done = False
 76        self.mouse_click = False
 77        self.sheet_selector_box = SheetSelectorBox(self)
 78
 79
 80    def setup_pygame(self):
 81        """Set up pygame and return the main surface"""
 82        os.environ['SDL_VIDEO_CENTERED'] = '1'
 83        pg.init()
 84        pg.display.set_mode((1100, 750))
 85        surface = pg.display.get_surface()
 86        surface.fill(c.BLACK_BLUE)
 87
 88        return surface
 89
 90
 91    def main_loop(self):
 92        while not self.done:
 93            self.event_loop()
 94            self.update()
 95            pg.display.update()
 96            self.clock.tick(self.fps)
 97
 98
 99    def event_loop(self):
100        for event in pg.event.get():
101            self.keys = pg.key.get_pressed()
102            if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
103                self.done = True
104            elif event.type == pg.MOUSEBUTTONDOWN:
105                self.mouse_click = True
106                self.click_point = pg.mouse.get_pos()
107
108
109    def update(self):
110        self.sheet_selector_box.update()
111        self.sheet_selector_box.draw(self.screen)
112
113
114if __name__ == "__main__":
115    map_creator = MapCreator()
116    map_creator.main_loop()
117    pg.quit()
118    sys.exit()