Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- from math import ceil
- tile_files = [
- "../pictures/tiles1.png"
- ]
- tile_files = [pygame.image.load(f) for f in tile_files]
- class Tile(pygame.sprite.Sprite):
- """ Create 32x32 Tile Sprite """
- def __init__(self, i, file_index, tile_x, tile_y):
- pygame.sprite.Sprite.__init__(self)
- self.tile_index = i
- self.image = tile_files[file_index].subsurface((tile_x*32, tile_y*32, 32, 32))
- self.image = self.image.convert_alpha()
- self.rect = self.image.get_rect()
- self.rect.x = (i%8)*37+4
- self.rect.y = (i/8)*37+4
- class Scroll_Button(pygame.sprite.Sprite):
- """ Create Tile Area Scrolling Button """
- def __init__(self, image, btn_x, btn_y, scroll_dir):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load(image)
- self.rect = self.image.get_rect()
- self.rect.x = btn_x
- self.rect.y = btn_y
- self.scroll_dir = scroll_dir
- class Editor:
- """ Main Map Editor Class """
- tile_area = None
- buttons = None
- selected_tile = None
- def __init__(self):
- """ Init a window and surfaces """
- pygame.init()
- pygame.display.set_caption("Map Editor")
- self.map_preview_font = pygame.font.Font(None, 128)
- self.running = True
- self.clock = pygame.time.Clock()
- self.screen = pygame.display.set_mode((1324,600), DOUBLEBUF)
- self.map_preview = pygame.Surface((1024,600))
- self.button_panel = pygame.Surface((300,30)).convert()
- self.screen.fill((255,255,255))
- self.load_tiles()
- self.loop()
- def load_tiles(self):
- """ Get all tiles and add them to sprite group, create 2 scrolling buttons and mouse object """
- self.tiles = pygame.sprite.Group()
- tiles = []
- num_of_tiles = 0
- # ~ # Count tiles and create list # ~ #
- for i, f in enumerate(tile_files):
- _, _, width, height = f.get_rect()
- num_of_tiles += width/32 * height/32
- tiles += [(i, x, y) for y in range(height/32) for x in range(width/32)]
- # ~ # Create Tile Area And Add Tiles + Scrolling Offset/Limit # ~ #
- height = 37*ceil(float(num_of_tiles)/8)+10
- self.tile_area = pygame.Surface((300, height))
- self.scroll_offset = 0
- self.scroll_limit = -height+570
- for j, (i, x, y) in enumerate(tiles):
- self.tiles.add(Tile(j, i, x, y))
- # ~ # Create Scrolling Buttons # ~ #
- self.buttons = pygame.sprite.Group()
- btn_up = Scroll_Button("../pictures/btn_up.png", 0, 570, 5)
- self.buttons.add(btn_up)
- btn_down = Scroll_Button("../pictures/btn_down.png", 100, 570, -5)
- self.buttons.add(btn_down)
- # ~ # Mouse # ~ #
- self.mouse = pygame.sprite.Sprite()
- self.mouse.rect = pygame.Rect((0,0,0,0))
- def get_events(self):
- """ Check for pygame events """
- for event in pygame.event.get():
- # ~ # Quit Editor # ~ #
- if event.type==QUIT or (event.type==KEYDOWN and event.key==K_ESCAPE):
- self.running = False
- # ~ # Select Tile # ~ #
- if event.type==MOUSEBUTTONDOWN:
- clicked = pygame.sprite.spritecollide(self.mouse, self.tiles, False)
- for t in clicked:
- self.selected_tile = t
- print t.tile_index
- # ~ # Scrolling Tile Area # ~ #
- scroll = pygame.sprite.spritecollide(self.mouse, self.buttons, False)
- for b in scroll:
- if self.scroll_offset >= 0 and b.scroll_dir > 0:
- continue
- elif self.scroll_offset <= self.scroll_limit and b.scroll_dir < 0:
- continue
- self.scroll_offset+=b.scroll_dir
- for s in self.tiles:
- s.rect.y+=b.scroll_dir
- def loop(self):
- """ Main program loop, display everything 60 times per second """
- while self.running:
- # ~ # Update Mouse Sprite # ~ #
- pos = pygame.mouse.get_pos()
- self.mouse.rect.x = pos[0]
- self.mouse.rect.y = pos[1]
- # ~ # Events # ~ #
- self.get_events()
- # ~ # Show map preview # ~ #
- self.screen.fill((200,200,200))
- self.screen.blit(self.map_preview, (300, 0))
- txt2 = self.map_preview_font.render("Map Preview", True, (255,255,255))
- self.map_preview.blit(txt2, (300,236))
- # ~ # Show tile menu # ~ #
- self.tile_area.fill((0,0,0))
- self.tiles.draw(self.tile_area)
- self.screen.blit(self.tile_area, (0, 0))
- self.button_panel.fill((200,200,200))
- self.screen.blit(self.button_panel, (0, 570))
- self.buttons.draw(self.screen)
- pygame.draw.line(self.screen, (255,0,0), (300,0), (300, 600), 1)
- self.clock.tick(60)
- pygame.display.update()
- if __name__=="__main__":
- editor = Editor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement