class HomeScene(Scene): def __init__(self, game): Scene.__init__(self, game) self.initialize_bg(game.screen) self.initialize_menu() def initialize_bg(self, screen): self.floor = Floor(MAP_SPEED) screen.fill((0, 153, 204)) def initialize_menu(self): self.menu = Menu(WIDTH) def on_update(self, time): self.floor.refresh() self.menu.next() def on_event(self, event): if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: self.game.change_scene(PlayScene(self.game, IaFlappyEngine())) def on_draw(self, screen): screen.blit(self.floor.image, self.floor.rect) screen.blit(self.menu, self.menu.get_rect())
def _init_sprites(self): """Initialises all sprites according to display size and filemap. Sprite size parameters will be adjusted depending from the display size. Spawn location for all sprites will be obtained from the .csv file. Each level has own .csv file with spawn information. Each sprite types will be separated into own groups and also into list of all sprites. """ obstacle_size = self._size[0] / 40 object_w = self._size[0] / 20 object_h = self._size[1] / 12 filename = LEVEL_PATHS[self._level.level_number] with open(filename) as file: row_nr = 0 for row in file: parts = row.strip().split(",") column_nr = 0 for part in parts: if part == "f": floor = Floor(self._level, column_nr * object_w, row_nr * object_h, object_w, obstacle_size, "f") self.floors.add(floor) if part == "l": lava = Floor(self._level, column_nr * object_w, row_nr * object_h, object_w, obstacle_size, "l") self.lavas.add(lava) if part == "g": goal = Floor(self._level, column_nr * object_w, row_nr * object_h, object_w, obstacle_size, "g") self.goals.add(goal) if part == "x": obstacle = Obstacle( self._level, column_nr * object_w + object_w / 2, (row_nr + 1) * object_h, obstacle_size, obstacle_size) self.obstacle.add(obstacle) column_nr += 1 row_nr += 1 self.all_sprites.add(self.player, self.floors, self.lavas, self.obstacle, self.goals)
def test_handle_goal(self): self.level.sprites.goals.add( Floor(self, 0, self.base_height, self.width, self.floor_thickness, "g")) self.level._handle_goal() self.assertTrue(self.level.finished)
def test_player_is_not_alive_when_collide_with_lava(self): self.level.sprites.lavas.add( Floor(self, 0, self.base_height, self.width, self.floor_thickness, "l")) self.assertFalse(self.level._player_is_alive())
def initialize_bg(self, screen): self.floor = Floor(MAP_SPEED) screen.fill((0, 153, 204))
class PlayScene(Scene): add_pipes = True distance = 0 def __init__(self, game, flappy_engine): Scene.__init__(self, game) self.flappy_engine = flappy_engine self.image_loader = ImageLoader() self.initialize_bg(game.screen) self.initialize_pipes() self.initialize_pipes() self.initialize_score() def initialize_score(self): self.score = Score(GAME_WIDTH, WIDTH - GAME_WIDTH, self.flappy_engine.get_birds()) def initialize_pipes(self): self.pipes = [] pipes = Pipes(GAME_WIDTH, HEIGHT, MAP_SPEED) self.pipes.append(pipes) def initialize_bg(self, screen): self.floor = Floor(MAP_SPEED, GAME_WIDTH) screen.fill((0, 153, 204)) def on_update(self, time): self.game.screen.fill((0, 153, 204)) self.floor.refresh() next_pipe = next(pipes for pipes in self.pipes if not pipes.visited) self.flappy_engine.on_update(next_pipe.get_x(), next_pipe.get_y()) self.refresh_pipes() self.check_collision() self.refresh_birds_score() self.check_if_all_birds_are_dead() self.distance += MAP_SPEED * int(time * 0.1) def on_event(self, event): self.flappy_engine.on_event(event) def on_draw(self, screen): for pipes in self.pipes: pipes.draw(screen) screen.blit(self.floor.image, self.floor.rect) self.flappy_engine.draw(screen) self.score.draw(screen) def refresh_pipes(self): if GAME_WIDTH - self.pipes[-1].get_x() > 170: pipes = Pipes(GAME_WIDTH, HEIGHT, MAP_SPEED) self.pipes.append(pipes) for pipes in self.pipes: pipes.increase_x() self.pipes = [ pipes for pipes in self.pipes if pipes.get_x() + pipes.get_width() > 0 ] def check_collision(self): first_not_visited_pipe = next(pipes for pipes in self.pipes if not pipes.visited) self.flappy_engine.check_pipes_collision(first_not_visited_pipe) self.flappy_engine.check_floor_collision(self.floor) def check_if_all_birds_are_dead(self): from scenes.home_scene import HomeScene if all(bird.dead for bird in self.flappy_engine.get_birds()): self.game.change_scene(HomeScene(self.game)) def refresh_birds_score(self): first_not_visited_pipe = next(pipes for pipes in self.pipes if not pipes.visited) not_dead_birds = [ bird for bird in self.flappy_engine.get_birds() if not bird.dead ] if first_not_visited_pipe.get_x() + first_not_visited_pipe.get_width( ) < GAME_WIDTH / 2: for pipes in self.pipes: if not pipes.visited: pipes.visited = True break for bird in not_dead_birds: bird.score += 1 for bird in not_dead_birds: bird.distance = self.distance
def loadLevel(levelName): global data, font font = pygame.font.Font("_res/Exo2.ttf", int(round(60 * displayManager.SCALE))) resources = "_res/" textures = {} sounds = {} guns = {} entities = {} data = LoadedData() tree = cElementTree.parse(resources + levelName + ".xml") root = tree.getroot() pygame.mixer.music.set_volume(.5) pygame.mixer.music.load(resources + "GameMusic.ogg") pygame.mixer.music.play(-1) for element in root: if element.tag == "resources": resources = element.text elif element.tag == "textures": for texture in element: textureName = texture.findtext("name") if textureName is None: continue image = pygame.image.load(resources + textureName + ".png") scale = 4 * displayManager.SCALE textures[textureName] = pygame.transform.scale( image, (int(image.get_width() * scale), int(image.get_height() * scale))).convert_alpha() elif element.tag == "sounds": for sound in element: soundName = sound.findtext("name") if soundName is None: continue sounds[soundName] = pygame.mixer.Sound(file=resources + soundName + ".ogg") elif element.tag == "guns": for gun in element: name = gun.findtext("name") gunType = gun.findtext("type") texture = textures[gun.findtext("texture")] bulletDamage = float(gun.findtext("bulletDamage")) bulletNumber = int(gun.findtext("bulletNumber")) magazineSize = int(gun.findtext("magazineSize")) bulletSpeed = int(gun.findtext("bulletSpeed")) reloadTime = float(gun.findtext("reloadTime")) shootDelay = float(gun.findtext("shootDelay")) shootSound = sounds[gun.findtext("shootSound")] if gunType == "Pistol": guns[name] = Pistol(0, 0, texture, bulletDamage, bulletNumber, magazineSize, bulletSpeed, reloadTime, shootDelay, shootSound) elif gunType == "Shotgun": guns[name] = Shotgun(0, 0, texture, bulletDamage, bulletNumber, magazineSize, bulletSpeed, reloadTime, shootDelay, shootSound) else: print(gunType) elif element.tag == "player": player = element texture = textures[player.findtext("texture")] posX = displayManager.WIDTH * float(player.findtext("posX")) posZ = 1000 * displayManager.SCALE * float(player.findtext("posZ")) health = float(player.findtext("health")) gunData = player.find("gun") gun = copy(guns[gunData.findtext("name")]) gun.offsetX = int( gunData.findtext("offsetX")) * displayManager.SCALE gun.mainOffsetY = int( gunData.findtext("offsetY")) * displayManager.SCALE walkSound = sounds[player.findtext("walkSound")] data.player = Player(posX, 0, posZ, texture, gun, health, walkSound) name = player.findtext("name") if name is not None: entities[name] = data.player elif element.tag == "level": for level in element: if level.tag == "floors": startX = 0 for floor in level: length = int(floor.findtext("length")) texture = textures[floor.findtext("texture")] rectangle = texture.get_rect() rectangle.move_ip( startX, displayManager.HEIGHT - rectangle.height) startX += rectangle.width * length data.floors.append(Floor(texture, rectangle, length)) elif level.tag == "backgrounds": startX = 0 for background in level: length = int(background.findtext("length")) elif level.tag == "foregrounds": startX = 0 for foreground in level: length = int(foreground.findtext("length")) elif level.tag == "entities": for entity in level: data.entities.append( loadEntity(entity, textures, guns, entities)) else: print(level.tag) elif element.tag == "events": for event in element: eventType = event.findtext("type") onData = event.find("on") entity = entities[onData.findtext("entityName")] attribute = onData.findtext("attributeName") value = onData.find("value") compFunc = getattr(operator, value.attrib["mode"]) scaled = bool(int(value.attrib["scaled"])) value = evalBoolFloat(value.text) if scaled: value *= displayManager.WIDTH effects = [] effectsData = event.find("effects") if effectsData is not None: for effect in effectsData: if effect.tag == "exitGame": effects.append(ExitGame()) elif effect.tag == "spawnEntity": spawnEntities = [] for entityElem in effect: spawnEntities.append( loadEntity(entityElem, textures, guns, entities)) effects.append(SpawnEntity(spawnEntities)) else: print(effect.tag) eventData = event.find("data") if eventType == "textPause": texts = eventData.findtext("text").split("\n") for i in range(len(texts)): texts[i] = texts[i].strip() if texts[0] == "": del texts[0] if texts[-1] == "": del texts[-1] cameraPos = eventData.findtext("cameraPos") if cameraPos is not None: cameraPos = float(cameraPos) * displayManager.WIDTH data.events.append( TextPause(entity, attribute, value, compFunc, effects, texts, cameraPos)) elif eventType == "instantEvent": data.events.append( InstantEvent(entity, attribute, value, compFunc, effects)) else: print(eventType) else: print(element.tag) return data