def EVT_MouseButtonDown(self, event): if event.button == 1: if self.over_image(self.toolbar, (0,0)): if self.over_coordinates(150, 50, (0,0)) and self.toolbar_pos[1] == 0: self.quit(SaveLevel(self.screen, self.lvl, self.lvl_name)) else: for item in self.toolbar_items.sprites(): if self.over_image(item.image, ( item.rect.left, item.rect.top+self.toolbar_pos[1])): if isinstance(item, level.tile): self.selected = level.tile(item.image, item.solid) elif isinstance(item, sprites.hero): self.selected = self.hero else: self.selected = sprites.Sprite(item.image, pos=item.pos) elif self.over_image( self.edit_window, (150,0)) and self.selected != None: if self.selected.image == self.hero.image: pos = ( (pygame.mouse.get_pos()[0]-150)+self.display.pos[0], pygame.mouse.get_pos()[1]+self.display.pos[1]) self.hero.rect.center = pos self.lvl['hero'] = self.hero.rect.topleft self.selected = None elif self.selected.image in [ sprite.image for sprite in sprites.enemies]: pos = ( (pygame.mouse.get_pos()[0]-150)+self.display.pos[0], pygame.mouse.get_pos()[1]+self.display.pos[1]) self.selected.pos = (pos[0]-(self.selected.rect.width/2), pos[1]-(self.selected.rect.height/2)) self.selected.update() self.lvl['enemies'] += [self.selected] self.sprites.add(self.selected) self.selected = sprites.Sprite(self.selected.image, pos=self.selected.pos) else: self.mouse_down = True self.draw()
def __init__(self, screen, lvl, lvl_name): Menu.__init__(self, screen, ['Hero', 'Tiles', 'Enemies'], '') #The reload()s are here because the tiles and sprites kept dissappearing #in the toolbar when the editor was visited twice. reload(sprites) reload(level) self.lvl = lvl self.lvl_name = lvl_name self.background = pygame.Surface((len(lvl['tiles'])*20, len(lvl['tiles'][0])*20)) #tiles self.tiles = sprites.Group() y = 0 for row in lvl['tiles']: x = 0 for tile in row: tile = level.tile(tile.image) tile.rect.center = (x,y) self.tiles.add(tile) x += 20 y += 20 self.tiles.draw(self.background) self.edit_window = pygame.Surface((650, 600)) self.display = scrolling.scrolling_display( self.background, self.edit_window, ( (self.background.get_width()/2)-(self.edit_window.get_width()/2), (self.background.get_height()/2)-(self.edit_window.get_height()/2))) #toolbar self.toolbar_background = pygame.image.load( os.path.join('data', 'flag2.bmp')) self.toolbar = pygame.Surface((150, 10000)) self.toolbar.blit(self.toolbar_background, (0,0)) self.toolbar.blit(self.toolbar_background, (0,600)) self.toolbar.blit(self.toolbar_background, (0,1200)) self.toolbar_items = sprites.Group() self.font = pygame.font.SysFont('Verdana', 30) self.font.set_underline(True) self.labels = [self.font.render(option, True, (0,0,0)) for option in self.options] self.font.set_underline(False) self.toolbar.blit( self.labels[0], ((self.toolbar.get_width()/2)-(self.labels[0].get_width()/2), 50)) if self.lvl_name == '__NEW__': self.hero = sprites.hero((-1000,0)) self.hero.rect.center = (-1000,0) else: self.hero = sprites.hero(lvl['hero']) self.hero.rect.center = lvl['hero'] self.h = sprites.Sprite( self.hero.image, [self.toolbar_items], ((self.toolbar.get_width()/2)-(self.hero.image.get_width()/2), self.labels[0].get_rect().bottom+55)) self.hero.release() self.h.release() self.toolbar.blit( self.labels[1], ((self.toolbar.get_width()/2)-(self.labels[1].get_width()/2), self.h.rect.bottom+5)) x = 0 y = self.h.rect.bottom+self.labels[1].get_height()+10 for tile in level.tiles: tile.rect = tile.rect.move((x,y)) self.toolbar_items.add(tile) if tile.rect.right > 150: x = 0 y += 22 else: x += 22 for sprite in self.toolbar_items.sprites(): if sprite.rect.bottom > y: y = sprite.rect.bottom+5 self.toolbar.blit( self.labels[2], ((self.toolbar.get_width()/2)-(self.labels[2].get_width()/2), y)) x = 22 y += 50 for enemy in sprites.enemies: enemy.rect = enemy.rect.move((x,y)) self.toolbar_items.add(enemy) if x == 22: x += 55 else: x = 22 y += 55 self.toolbar_items.draw(self.toolbar) self.toolbar_pos = (0,0) self.sprites = sprites.Group(lvl['enemies']+[self.hero]) self.scrolling = (0,0) self.selected = None self.mouse_down = False self.draw() self.toolbar_scrolling = 0