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