def handleActivation(self, level, worldPos): if worldPos.x >= 0 and worldPos.x <= level.size.x and worldPos.y >= 0 and worldPos.y <= level.size.y and level.getEntity( worldPos) == None: pos = vector2D.Vector2D(round(worldPos.x), round(worldPos.y)) size = vector2D.Vector2D(1, 1) if self.currentTool == const.STATIC_BLOCK: level.addEntity( entity.Entity(pos=pos, size=size, textureFile=const.TEXTURE_DIR + level.texturePack + "staticBlock.png", typ=self.currentTool)) elif self.currentTool == const.DYNAMIC_BLOCK: level.addEntity( entity.Entity(pos=pos, size=size, textureFile=const.TEXTURE_DIR + level.texturePack + "dynamicBlock.png", typ=self.currentTool)) elif self.currentTool == const.PLAYER: pos.y -= 0.5 size.y = 2 level.addEntity( entity.Entity(pos=pos, size=size, textureFile=const.TEXTURE_DIR + level.texturePack + "player.png", typ=self.currentTool)) elif self.currentTool == const.FINISH_BLOCK: level.addEntity( entity.Entity(pos=pos, size=size, textureFile=const.TEXTURE_DIR + level.texturePack + "finishBlock.png", typ=self.currentTool)) elif self.currentTool == const.GATE: pos.y -= 0.5 size.y = 2 level.addEntity( entity.Entity(pos=pos, size=size, textureFile=const.TEXTURE_DIR + level.texturePack + "gate.png", typ=self.currentTool)) elif self.currentTool == const.LEVER: level.addEntity( entity.Entity(pos=pos, size=size, textureFile=const.TEXTURE_DIR + level.texturePack + "lever.png", typ=self.currentTool))
def getWorldPos(self, pos): aux = pos.getSub( vector2D.Vector2D(const.WINDOW_WIDTH / 2, const.WINDOW_HEIGHT / 2)) aux.scale(1.0 / self.camera.scale) return self.camera.pos.getAdd(aux)
def handleEvents(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: self.running = False pressed = pygame.key.get_pressed() if pressed[pygame.K_d]: self.level.renderer.camera.pos.x += 1 if pressed[pygame.K_a]: self.level.renderer.camera.pos.x -= 1 if pressed[pygame.K_w]: self.level.renderer.camera.pos.y -= 1 if pressed[pygame.K_s]: self.level.renderer.camera.pos.y += 1 mx, my = pygame.mouse.get_pos() worldPos = self.level.renderer.getWorldPos(vector2D.Vector2D(mx, my)) buttonPressed = False if not pygame.mouse.get_pressed()[0]: for button in self.buttons: button.released = True for button in self.buttons: if button.isOverlapping(vector2D.Vector2D(mx, my)): buttonPressed = True button.color = const.RED if pygame.mouse.get_pressed()[0]: if button.text == const.SAVE: if button.released: self.level.save() button.released = False else: self.toolMng.currentTool = button.text self.txtBlock.setText(self.toolMng.currentTool) else: button.color = const.DARK_GREY if not buttonPressed and pygame.mouse.get_pressed()[0]: self.toolMng.handleActivation(self.level, worldPos) if pygame.mouse.get_pressed()[2]: entity = self.level.getEntity(worldPos) if entity != None: self.level.removeEntity(entity)
def __init__(self, pos, center, size, borderThickness, text): if center: self.pos = pos else: self.pos = vector2D.Vector2D(pos.x + size.x / 2, pos.y + size.y / 2) self.size = size self.bt = borderThickness self.setText(text) self.color = const.GREY
def render(self, window): pos = vector2D.Vector2D(-0.5, -0.5).getSub( self.camera.getOrigin()).scale(self.camera.scale) size = self.size.copy().scale(self.camera.scale) pygame.draw.rect(window, const.DARK_GREY, [pos.x, pos.y, size.x, size.y]) for sprite in self.sprites: drawSize = sprite.size.copy() drawSize.scale(self.camera.scale) drawPos = sprite.pos.getSub(self.camera.getOrigin()) drawPos.scale(self.camera.scale) sprite.image = pygame.transform.scale(sprite.image, drawSize.getTuple()) sprite.rect = sprite.image.get_rect() sprite.rect.center = drawPos.getTuple() self.sprites.draw(window)
def main(): validInput = False print("Window size:") print("0) 400*400") print("1) 600*600") print("2) 800*800") print("3) 1000*1000") while not validInput: option = int(raw_input("> ")) validInput = True if option == 0: const.WINDOW_WIDTH = 400 const.WINDOW_HEIGHT = 400 elif option == 1: const.WINDOW_WIDTH = 600 const.WINDOW_HEIGHT = 600 elif option == 2: const.WINDOW_WIDTH = 800 const.WINDOW_HEIGHT = 800 elif option == 3: const.WINDOW_WIDTH = 1000 const.WINDOW_HEIGHT = 1000 else: validInput = False print("Error: Invalid input") print("") lvlEditor = None levelFile = None validInput = False while not validInput: print("l - Load level") print("n - Creat new level") cmd = raw_input("> ") if cmd == "l" or cmd == "n": levelName = raw_input("Level name: ") levelFile = levelName + ".txt" validInput = True else: print("Error: Invalid input") if cmd == "l" and not os.path.isfile(LEVEL_DIR + levelFile): validInput = False print("Error: Invalid level name") if cmd == "l": lvlEditor = levelEditor.LevelEditor( level=level.Level(levelFile, levelName)) else: texturePack = "DefaultPack/" validInput = False while not validInput: levelWidth = input("Level width: ") levelHeight = input("Level height: ") if levelWidth > 0 and levelHeight > 0: validInput = True else: print("Error: Invalid Input") lvlEditor = levelEditor.LevelEditor( level=level.Level(file=levelFile, name=levelName, texturePack=texturePack, size=vector2D.Vector2D(levelWidth, levelHeight))) lvlEditor.run()
def getOrigin(self): return self.pos.getSub( vector2D.Vector2D(self.size.x / 2, self.size.y / 2))
def __init__(self, level): pygame.init() self.level = level cameraSize = vector2D.Vector2D(const.WINDOW_WIDTH/20, const.WINDOW_HEIGHT/20) level.setCamera(camera = camera.Camera(pos = vector2D.Vector2D(int(level.size.x/2), int(level.size.y/2)), size = cameraSize, windowSize = vector2D.Vector2D(const.WINDOW_WIDTH, const.WINDOW_HEIGHT))) self.toolMng = toolManager.ToolManager() self.running = True self.window = pygame.display.set_mode((const.WINDOW_WIDTH, const.WINDOW_HEIGHT)) pygame.display.set_caption("Level Editor") self.clock = pygame.time.Clock() blockSize = vector2D.Vector2D(const.WINDOW_WIDTH / 10, const.WINDOW_WIDTH / 30) thickness = const.WINDOW_WIDTH / 150 dist = blockSize.x + thickness * 3.5 aux = thickness*4 self.txtBlock = textBlock.TextBlock(pos = vector2D.Vector2D(aux, thickness*4), center = False, size = blockSize, borderThickness = thickness, text = self.toolMng.currentTool); aux += dist self.buttons = [] self.buttons.append(button.Button(pos = vector2D.Vector2D(aux, thickness*4), center = False, size = blockSize, borderThickness = thickness, text = const.STATIC_BLOCK)); aux += dist self.buttons.append(button.Button(pos = vector2D.Vector2D(aux, thickness*4), center = False, size = blockSize, borderThickness = thickness, text = const.DYNAMIC_BLOCK)); aux += dist self.buttons.append(button.Button(pos = vector2D.Vector2D(aux, thickness*4), center = False, size = blockSize, borderThickness = thickness, text = const.PLAYER)); aux += dist self.buttons.append(button.Button(pos = vector2D.Vector2D(aux, thickness*4), center = False, size = blockSize, borderThickness = thickness, text = const.FINISH_BLOCK)); aux += dist self.buttons.append(button.Button(pos = vector2D.Vector2D(aux, thickness*4), center = False, size = blockSize, borderThickness = thickness, text = const.GATE)); aux += dist self.buttons.append(button.Button(pos = vector2D.Vector2D(aux, thickness*4), center = False, size = blockSize, borderThickness = thickness, text = const.LEVER)); aux += dist self.buttons.append(button.Button(pos = vector2D.Vector2D(aux, thickness*4), center = False, size = blockSize, borderThickness = thickness, text = const.SAVE))