def __init__(self, player): """ Create level 1. """ # Call the parent constructor Level.__init__(self, player) self.background = pygame.image.load("imagenes/background_02.png").convert() self.background.set_colorkey(constantes.BLANCO) self.level_limit = -1000 # Array with type of platform, and x, y location of the platform. level = [ ] # Go through the array above and add platforms for platform in level: block = platforms.Platform(platform[0]) block.rect.x = platform[1] block.rect.y = platform[2] block.player = self.player self.platform_list.add(block) # Add a custom moving platform #block = platforms.MovingPlatform(platforms.STONE_PLATFORM_MIDDLE) #block.rect.x = 1500 #block.rect.y = 300 #block.boundary_top = 100 #block.boundary_bottom = 550 #block.mover_y = -1 #block.player = self.player #block.nivel = self #self.platform_list.add(block)
def __init__(self, player): """ Create level 1. """ # Call the parent constructor Level.__init__(self, player) self.background = pygame.image.load( "imagenes/background_02.png").convert() self.background.set_colorkey(constantes.BLANCO) self.level_limit = -1000 # Array with type of platform, and x, y location of the platform. level = [] #Artefactos botellas = pygame.image.load("imagenes/botellas.png").convert() botellas.set_colorkey(constantes.BLANCO) borracho = pygame.image.load("imagenes/borracho.png").convert() borracho.set_colorkey(constantes.BLANCO) farol = pygame.image.load("imagenes/farol.png").convert() farol.set_colorkey(constantes.BLANCO) self.background.blit(botellas, (700, 550)) self.background.blit(borracho, (800, 450)) self.background.blit(farol, (1000, 460)) # Go through the array above and add platforms for platform in level: block = platforms.Platform(platform[0]) block.rect.x = platform[1] block.rect.y = platform[2] block.player = self.player self.platform_list.add(block)
def __init__(self): pygame.init() self.size = (1024, 600) self.surface = pygame.display.set_mode(self.size) self.clock = pygame.time.Clock() self.gameboard = Gameboard() self.player = Player(self.gameboard) self.level = Level(self.gameboard) self.level.update(0, self.gameboard) self.player.screenshot = self.printscreen self.view = Gameview(self.surface, self) self.time = 0 while not (self.player.quit or self.player.hasLost()): self.clock.tick(Game.FPS) unit = self.gameboard.units.getSelected() if unit is None: continue if (int(self.time) is not int(self.gameboard.units.time)): self.level.update(int(self.gameboard.units.time), self.gameboard) self.gameboard.update() self.time = self.gameboard.units.time unit.owner.turn(self, unit) self.view.draw() self.gameboard.update()
def __init__(self): self.display = Display() self.player = Charactor(self) self.level = Level(self) self.hud = Hud() self.prepare_stage()
def __init__(self): pygame.init() self.size = (1024, 600) self.surface = pygame.display.set_mode(self.size) self.clock = pygame.time.Clock() self.gameboard = Gameboard() self.player = Player(self.gameboard) self.level = Level(self.gameboard) self.level.update(0, self.gameboard) self.player.screenshot = self.printscreen self.view = Gameview(self.surface, self) self.time = 0 while not (self.player.quit or self.player.hasLost()): self.clock.tick(Game.FPS) unit = self.gameboard.units.getSelected() if unit is None: continue if(int(self.time) is not int(self.gameboard.units.time)): self.level.update(int(self.gameboard.units.time), self.gameboard) self.gameboard.update() self.time = self.gameboard.units.time unit.owner.turn(self, unit) self.view.draw() self.gameboard.update()
class Game(object): FPS = 30 def __init__(self): pygame.init() self.size = (1024, 600) self.surface = pygame.display.set_mode(self.size) self.clock = pygame.time.Clock() self.gameboard = Gameboard() self.player = Player(self.gameboard) self.level = Level(self.gameboard) self.level.update(0, self.gameboard) self.player.screenshot = self.printscreen self.view = Gameview(self.surface, self) self.time = 0 while not (self.player.quit or self.player.hasLost()): self.clock.tick(Game.FPS) unit = self.gameboard.units.getSelected() if unit is None: continue if (int(self.time) is not int(self.gameboard.units.time)): self.level.update(int(self.gameboard.units.time), self.gameboard) self.gameboard.update() self.time = self.gameboard.units.time unit.owner.turn(self, unit) self.view.draw() self.gameboard.update() def printscreen(self): date = time.gmtime() fileName = "screenshot_" + \ str(date[0]) + '-' + \ str(date[1]) + '-' + \ str(date[2]) + '-' + \ str(date[3]-8) + '-' + \ str(date[4]) + '-' + \ str(date[5]) + \ '.jpg' pygame.image.save(self.surface, fileName)
class Game(object): FPS = 30 def __init__(self): pygame.init() self.size = (1024, 600) self.surface = pygame.display.set_mode(self.size) self.clock = pygame.time.Clock() self.gameboard = Gameboard() self.player = Player(self.gameboard) self.level = Level(self.gameboard) self.level.update(0, self.gameboard) self.player.screenshot = self.printscreen self.view = Gameview(self.surface, self) self.time = 0 while not (self.player.quit or self.player.hasLost()): self.clock.tick(Game.FPS) unit = self.gameboard.units.getSelected() if unit is None: continue if(int(self.time) is not int(self.gameboard.units.time)): self.level.update(int(self.gameboard.units.time), self.gameboard) self.gameboard.update() self.time = self.gameboard.units.time unit.owner.turn(self, unit) self.view.draw() self.gameboard.update() def printscreen(self): date = time.gmtime() fileName = "screenshot_" + \ str(date[0]) + '-' + \ str(date[1]) + '-' + \ str(date[2]) + '-' + \ str(date[3]-8) + '-' + \ str(date[4]) + '-' + \ str(date[5]) + \ '.jpg' pygame.image.save(self.surface, fileName)
def __init__(self): pygame.init() self.WINDOW_WIDTH = 800 self.WINDOW_HEIGHT = 600 self.DISPlAY_SURF = pygame.display.set_mode((self.WINDOW_WIDTH, self.WINDOW_HEIGHT), pygame.DOUBLEBUF, 32) self.clock = pygame.time.Clock() self.players = pygame.sprite.Group() self.player = characters.Mage(self.DISPlAY_SURF, "Sasha") self.players.add(self.player) self.keys_down = [] self.level = Level(self.DISPlAY_SURF, [self.player]) self._main_loop() # Must be last line!
def defineWinScreen(self): self.win_screen = Level() Level.level_list.pop(self.win_screen.level_num) self.win_screen.background.fill(constants.BLACK) self.win_screen.start_pos = (50, -120) platform0 = Platform(constants.SCREEN_WIDTH, 60) platform0.rect.x = 0 platform0.rect.y = -60 self.win_screen.platform_list.add(platform0) font = pygame.font.SysFont('Helvetica', 64, True, False) text = font.render("You Win", True, constants.WHITE) x = constants.SCREEN_WIDTH/2 - text.get_rect().width/2 y = 250 self.win_screen.background.blit(text, [x, y])
class Game: def __init__(self): pygame.init() self.WINDOW_WIDTH = 800 self.WINDOW_HEIGHT = 600 self.DISPlAY_SURF = pygame.display.set_mode((self.WINDOW_WIDTH, self.WINDOW_HEIGHT), pygame.DOUBLEBUF, 32) self.clock = pygame.time.Clock() self.players = pygame.sprite.Group() self.player = characters.Mage(self.DISPlAY_SURF, "Sasha") self.players.add(self.player) self.keys_down = [] self.level = Level(self.DISPlAY_SURF, [self.player]) self._main_loop() # Must be last line! def _main_loop(self): while True: self.DISPlAY_SURF.fill((25, 255, 255)) for event in pygame.event.get(): if event.type == KEYDOWN: self.keys_down.append(event.key) elif event.type == KEYUP: self.keys_down.remove(event.key) elif event.type == QUIT: pygame.quit() sys.exit() for key in self.keys_down: self.player.press_key(key) self.level.update() self.level.draw_level() self.players.update() self.players.draw(self.DISPlAY_SURF) pygame.display.update() self.clock.tick(60)
def __init__(self): self.framerate = 0 self.number_meanies = 32 self.bricks_in_level = 0 self.display = Display() self.hud = Hud() pygame.key.set_repeat(100,5) self.meanies = pygame.sprite.Group() self.player = Charactor(self) self.player.speed = 2 self.level = Level(self) self.level.load_level(0) self.start_game()
def __init__(self, context, screen, prev_state): """ Init with context, main PyGame surface and the previous state if you want to be able to go back :param context: The field in the main application which contains the current GameState. Current GameState has input events pumped into it, is updated and then drawn on the screen. Used by the current state to switch to the other GameState :param screen: Main PyGame surface to draw the objects/UI on :param prev_state: The state to which we will return :return: """ update_level_count() self.context = context self.screen = screen self.prev_state = prev_state self.level = Level(screen, (0, 0), (K_LEFT, K_UP, K_RIGHT), 0, self.finish_game) self.game_finished = False self.end_labels = None
def main(): from drawing import draw pygame.init() window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Valkyrie") level = Level.load(Level.ONE, AssetFactory()) state = level.initial_game_state running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_q: running = False update(state, level.update) draw(window, state) pygame.quit()
def test_level_labels(self): image_size = tf.convert_to_tensor([32, 32], dtype=tf.int32) class_ids = tf.convert_to_tensor([100, 200, 300, 400]) boxes = tf.convert_to_tensor([ [0, 0, 16, 16], [8, 8, 24, 24], [16, 16, 32, 32], [-4, -4, 20, 20], ]) boxes = boxes / tf.concat([image_size, image_size], 0) boxes = tf.to_float(boxes) level = Level(16, [(1, 1)], [1, 1.5]) factor = 2**4 actual = dataset.level_labels(image_size, class_ids, boxes, level, factor) expected = ( tf.convert_to_tensor([ [[100, 400], [0, 0]], [[0, 0], [300, 0]], ]), tf.convert_to_tensor([ [ [[0., 0., .5, .5], [-.125, -.125, .625, .625]], [[0., 0., 0., 0.], [0., 0., 0., 0.]], ], [ [[0., 0., 0., 0.], [0., 0., 0., 0.]], [[.5, .5, 1., 1.], [0., 0., 0., 0.]], ], ]), ) a, e = self.evaluate([actual, expected]) assert np.array_equal(a[0], e[0]) assert a[0].shape == (2, 2, 2) assert np.array_equal(a[1], e[1]) assert a[1].shape == (2, 2, 2, 4)
def load_level(level: Level, game_screen: GameScreen): tile_x = int(game_screen.w / level.TILE_X_NUM) tile_y = int(game_screen.h / level.TILE_Y_NUM) offset_w = game_screen.x_offset + int( round((game_screen.w - tile_x * level.TILE_X_NUM) / 2)) offset_h = game_screen.y_offset + int( round((game_screen.h - tile_y * level.TILE_Y_NUM) / 2)) level.offset_tile_x = offset_w level.offset_tile_y = offset_h constants.VELOCITY_MOVEMENT = level.VELOCITY_MOVEMENT constants.VELOCITY_JUMP = level.VELOCITY_JUMP constants.VELOCITY_MAX_FALL = level.VELOCITY_MAX_FALL constants.TILE_X = tile_x constants.TILE_Y = tile_y # constants.TILE_X_NUM = level.TILE_X_NUM # constants.TILE_Y_NUM = level.TILE_Y_NUM entities = pygame.sprite.Group() platforms = [] # build the level player_p1 = Player(0, 0, "dummy") player_p2 = Player(0, 0, "dummy") y = offset_h for level_row in level.get_level(): x = offset_w for level_block in level_row: if level_block == "P": e = PlatformBlock(x, y) platforms.append(e) entities.add(e) if level_block == "E": e = StairsBlock(x, y) platforms.append(e) entities.add(e) if level_block == "B": e = BarBlock(x, y) platforms.append(e) entities.add(e) if level_block == "H": e = BarHBlock(x, y) platforms.append(e) entities.add(e) if level_block == "G": e = GoalBlock(x, y) platforms.append(e) entities.add(e) if level_block == "L": e = GoalBlockLeft(x, y) platforms.append(e) entities.add(e) if level_block == "R": e = GoalBlockRight(x, y) platforms.append(e) entities.add(e) if level_block == "Y": player_p1 = Player( x, y, "Y", constants.PLAYER_P1_COLOR_BG, constants.IMAGE_P1, flip=True, force_background=level.player_force_background, jump_sound=constants.PLAYER_P1_JUMP) level.num_players = level.num_players + 1 if level_block == "X": player_p2 = Player( x, y, "X", constants.PLAYER_P2_COLOR_BG, constants.IMAGE_P2, flip=True, force_background=level.player_force_background, jump_sound=constants.PLAYER_P2_JUMP) level.num_players = level.num_players + 1 x += tile_x y += tile_y if player_p1.name is not "Dummy": platforms.append(player_p1) entities.add(player_p1) if player_p2.name is not "Dummy": entities.add(player_p2) platforms.append(player_p2) return entities, platforms, player_p1, player_p2
bird_body = a.body pig_body = b.body pigs_to_remove = [] for pig in pigs: if pig_body == pig.body: pigs_to_remove.append(pig) for pig in pigs_to_remove: space.remove(pig.shape, pig.shape.body) pigs.remove(pig) space.add_collision_handler(0, 2).post_solve = Physics.post_solve_bird_wood space.add_collision_handler(0, 1).post_solve = post_solve_bird_pig level = Level(Physics.columns, Physics.beams, space, pigs) level.number = 0 level.load_level() while Running: dt = clock.tick(30) dt = 1.0 / 50.0 / 2. for event in pygame.event.get(): if event.type == pygame.QUIT: Running = False if event.type == pygame.KEYDOWN: SlingShot.controls(event) if event.key == pygame.K_SPACE: bird = Bird(SlingShot.AngleStart, ParabolaList[0][0], ParabolaList[0][1] + 20, space, SlingShot.SpeedMultiplayer)
from levels import Level # a turtle crossing game. Get the turtle to the finish line without hitting a car. Move forward with spacebar turtle.listen() # generate screen screen = Screen() screen.setup(height=600, width=600) screen.tracer(0) # generate game objects car = Cars() racer_turtle = Racer() finish_line = RaceLine(250) start_line = RaceLine(-250) level_start = Level() # instructions = level_start.instructions() # start game game_on = True while game_on: time.sleep(level_start.time_set) screen.update() for car in car_list: car.move() # generate one car after previous reaches a given coordinate if car_list[-1].xcor() in range(200, 250): car = Cars() # Detect if turtle collides with car, end game if car.distance(racer_turtle) <= 20: level_start.game_over() game_on = False
def level_cam(self): self.level = Level()
def load_level(level_loaded: Level): tile_x = level_loaded.TILE_X tile_y = level_loaded.TILE_Y offset_w = level_loaded.offset_w offset_h = level_loaded.offset_h entities = pygame.sprite.Group() platforms = [] enemies = [] players = [] # block : -1 # empty : 0 # ground : 1 # stairs : 2 # bar : 3 # fly : 4 # build the level y = offset_h for i_row in range(0, level_loaded.TILE_Y_NUM): x = offset_w level_row = level_loaded.level[i_row] for j_col in range(0, level_loaded.TILE_X_NUM): level_block = level_row[j_col] level_loaded.level_matrix[i_row][j_col] = 0 if level_block == "▉" or level_block == "P": e = PlatformBlock(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = -1 if level_block == "╬" or level_block == "E": e = StairsBlock(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = 2 if level_block == "-" or level_block == "B": e = BarBlock(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = 3 if level_block == "_" or level_block == "H": e = BarHBlock(x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = 3 if level_block == "⊟" or level_block == "G": e = GoalBlock(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = -1 if level_block == "⊏" or level_block == "L": e = GoalBlockLeft(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = -1 if level_block == "⊐" or level_block == "R": e = GoalBlockRight(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = -1 if level_block == "⭐" or level_block == "S": e = StarItem(level_loaded, x, y) platforms.append(e) entities.add(e) if level_block == "g": e = Ghost( level_loaded, x, y, "Ghost", bg_color="#72A877", image_file='images/sprites/characters/enemies/ghost.png', flip=True, jump_sound=constants.PLAYER_P1_JUMP ) entities.add(e) enemies.append(e) if level_block == "t": e = Enemy( level_loaded, x, y, "Tritannus", None, None, None, None, bg_color="#72A877", image_file='images/winx_raw/tritannus_01.png', flip=True, jump_sound=constants.PLAYER_P1_JUMP ) entities.add(e) enemies.append(e) if level_block == "1" or level_block == "Y": e = Player( level_loaded, x, y, "Y", K_UP, K_DOWN, K_RIGHT, K_LEFT, constants.PLAYER_P1_COLOR_BG, 'images/winx_raw/tritannus_01.png', 'images/winx_raw/tritannus_01.png', # constants.IMAGE_P1, # constants.IMAGE_P1_TRANSFORMED, flip=True, jump_sound=constants.PLAYER_P1_JUMP ) level_loaded.num_players = level_loaded.num_players + 1 players.append(e) player_p1 = e if level_block == "2" or level_block == "X": e = Player( level_loaded, x, y, "X", K_w, K_s, K_d, K_a, constants.PLAYER_P2_COLOR_BG, constants.IMAGE_P2, constants.IMAGE_P2_TRANSFORMED, flip=True, jump_sound=constants.PLAYER_P2_JUMP ) level_loaded.num_players = level_loaded.num_players + 1 players.append(e) player_p2 = e x += tile_x y += tile_y for i_row in range(0, level_loaded.TILE_Y_NUM-2): for j_col in range(0, level_loaded.TILE_X_NUM): if i_row < level_loaded.TILE_Y_NUM-1: if \ level_loaded.level_matrix[i_row+2][j_col] in [0, 4] and \ level_loaded.level_matrix[i_row+1][j_col] in [0, 4] and \ level_loaded.level_matrix[i_row][j_col] == 0: level_loaded.level_matrix[i_row][j_col] = 4 for p in players: entities.add(p) platforms.append(p) return entities, platforms, enemies, players
def define_levels(self, level_num, init): # Define Level 0 if init or level_num == 0: if init: level0 = Level() level0.start_pos = (100, constants.GROUND_HEIGHT - Player.HEIGHT) x = constants.SCREEN_WIDTH - self.player.rect.width y = constants.GROUND_HEIGHT - Player.HEIGHT level0.end_pos = (x, y) else: level0 = Level.level_list[0] level0.platform_list.empty() level0.enemy_list.empty() level0.tripped_projectiles.empty() platform0 = Platform(constants.SCREEN_WIDTH, 60) platform0.rect.x = 0 platform0.rect.y = constants.SCREEN_HEIGHT - platform0.rect.height level0.platform_list.add(platform0) for i in range(4, 8): tp0 = TrippedProjectile() tp0.player = self.player tp0.rect.x = i * 100 tp0.rect.y = constants.GROUND_HEIGHT - 250 level0.tripped_projectiles.add(tp0) hidden_wall = Platform(20, constants.SCREEN_HEIGHT) hidden_wall.rect.x = -20 hidden_wall.rect.y = 0 level0.platform_list.add(hidden_wall) # Define Level 1 if init or level_num == 1: if init: level1 = Level() level1.start_pos = (0, constants.GROUND_HEIGHT - Player.HEIGHT) x = constants.SCREEN_WIDTH - self.player.rect.width y = constants.GROUND_HEIGHT - Player.HEIGHT level1.end_pos = (x, y) else: level1 = Level.level_list[1] level1.platform_list.empty() level1.enemy_list.empty() level1.tripped_projectiles.empty() platform0 = Platform(200, 60) platform0.rect.x = 0 platform0.rect.y = constants.SCREEN_HEIGHT - platform0.rect.height level1.platform_list.add(platform0) platform1 = MovingPlatform(100, 20) platform1.player = self.player right = constants.SCREEN_WIDTH - platform1.rect.width - 140 platform1.rect.x = right platform1.rect.y = constants.SCREEN_HEIGHT - 140 platform1.x_change = -4 platform1.boundary_left = 140 platform1.boundary_right = right level1.platform_list.add(platform1) platform2 = Platform(200, 60) platform2.rect.x = constants.SCREEN_WIDTH - platform2.rect.width platform2.rect.y = constants.SCREEN_HEIGHT - platform2.rect.height level1.platform_list.add(platform2) # Define Level 2 if init or level_num == 2: if init: level2 = Level() level2.start_pos = (0, constants.GROUND_HEIGHT - Player.HEIGHT) x = constants.SCREEN_WIDTH - self.player.rect.width y = constants.GROUND_HEIGHT - Player.HEIGHT level2.end_pos = (x, y) else: level2 = Level.level_list[2] level2.platform_list.empty() level2.enemy_list.empty() level2.tripped_projectiles.empty() platform0 = Platform(constants.SCREEN_WIDTH, 60) platform0.rect.x = 0 platform0.rect.y = constants.SCREEN_HEIGHT - platform0.rect.height level2.platform_list.add(platform0) platform1 = Platform(20, constants.SCREEN_HEIGHT - 100) platform1.rect.x = constants.SCREEN_WIDTH / 2 platform1.rect.y = 100 level2.platform_list.add(platform1) platform2 = MovingPlatform(100, 20) platform2.player = self.player right = constants.SCREEN_WIDTH - platform2.rect.width - 140 platform2.rect.x = right platform2.rect.y = constants.SCREEN_HEIGHT - 140 platform2.x_change = -4 platform2.boundary_left = 40 platform2.boundary_right = right + 100 level2.platform_list.add(platform2) platform3 = MovingPlatform(100, 20) platform3.player = self.player platform3.rect.x = 200 platform3.rect.y = 300 platform3.y_change = -2 platform3.boundary_bottom = constants.GROUND_HEIGHT - 140 platform3.boundary_top = 100 level2.platform_list.add(platform3) cannon0 = Cannon("left") cannon0.game = self cannon0.rect.x = constants.SCREEN_WIDTH - Cannon.WIDTH cannon0.rect.y = constants.GROUND_HEIGHT - 80 level2.platform_list.add(cannon0) cannon1 = Cannon("left") cannon1.game = self cannon1.rect.x = constants.SCREEN_WIDTH - Cannon.WIDTH cannon1.rect.y = cannon0.rect.y - 60 cannon1.fire_counter = 45 level2.platform_list.add(cannon1) tp0 = TrippedProjectile() tp0.player = self.player tp0.rect.x = 700 tp0.rect.y = 50 level2.tripped_projectiles.add(tp0) # define final level if init or level_num == len(Level.level_list) - 1: if init: final_level = Level() x = 100 y = constants.GROUND_HEIGHT - Player.HEIGHT final_level.start_pos = (x, y) x = constants.SCREEN_WIDTH - self.player.rect.width final_level.end_pos = (x, y) else: final_level = Level.level_list[level_num] final_level.platform_list.empty() final_level.enemy_list.empty() final_level.tripped_projectiles.empty() platform0 = Platform(constants.SCREEN_WIDTH, 60) platform0.rect.x = 0 platform0.rect.y = constants.SCREEN_HEIGHT - platform0.rect.height final_level.platform_list.add(platform0) hidden_wall = Platform(20, constants.SCREEN_HEIGHT) hidden_wall.rect.x = -20 hidden_wall.rect.y = 0 final_level.platform_list.add(hidden_wall) cannon0 = Cannon("left") cannon0.game = self cannon0.rect.x = constants.SCREEN_WIDTH - Cannon.WIDTH - 112 cannon0.rect.y = constants.GROUND_HEIGHT - Cannon.HEIGHT final_level.platform_list.add(cannon0) boss = Boss() boss.game = self final_level.enemy_list.add(boss) final_level.platform_list.add(boss.health_bar)
def __init__(self, player): """ Create level 1. """ # Call the parent constructor Level.__init__(self, player) #Imagen de fondo self.background = pygame.image.load("imagenes/fondorasta.jpg").convert() #Establecemos el sonido de fondo #sonido=pygame.mixer.Sound("") #sonido.play() self.background.set_colorkey(constantes.BLANCO) self.level_limit = -15100 #COMIDAS self.lista_de_comidas.add(Amarillo(1480,535)) self.lista_de_comidas.add(Azul(800,280)) self.lista_de_comidas.add(Celeste(1820,250)) self.lista_de_comidas.add(Naranja(1580,430)) self.lista_de_comidas.add(Rojo(1110,140)) self.lista_de_comidas.add(Verde(1820,430)) self.lista_de_comidas.add(Violeta(1420,535)) self.lista_de_comidas.add(Rojo(2050,520)) self.lista_de_comidas.add(Azul(2150,520)) self.lista_de_comidas.add(Verde(2250,520)) self.lista_de_comidas.add(Celeste(2620,520)) self.lista_de_comidas.add(Naranja(2800,520)) self.lista_de_comidas.add(Violeta(2620,380)) self.lista_de_comidas.add(Amarillo(2800,380)) self.lista_de_comidas.add(Rojo(3100,420)) self.lista_de_comidas.add(Azul(3120,250)) self.lista_de_comidas.add(Rojo(3550,70)) self.lista_de_comidas.add(Verde(3500,70)) self.lista_de_comidas.add(Naranja(3400,500)) self.lista_de_comidas.add(Celeste(3600,500)) self.lista_de_comidas.add(Amarillo(3800,500)) self.lista_de_comidas.add(Rojo(4155,120)) self.lista_de_comidas.add(Azul(4800,500)) self.lista_de_comidas.add(Verde(4600,200)) self.lista_de_comidas.add(Violeta(4900,50)) self.lista_de_comidas.add(Amarillo(4950,50)) self.lista_de_comidas.add(Rojo(5150,500)) self.lista_de_comidas.add(Naranja(5000,500)) self.lista_de_comidas.add(Rojo(5245,270)) self.lista_de_comidas.add(Violeta(5600,380)) self.lista_de_comidas.add(Verde(5500,520)) self.lista_de_comidas.add(Celeste(5700,520)) self.lista_de_comidas.add(Azul(6300,430)) self.lista_de_comidas.add(Amarillo(6500,430)) self.lista_de_comidas.add(Naranja(6500,430)) self.lista_de_comidas.add(Verde(6700,380)) self.lista_de_comidas.add(Rojo(6800,380)) #Artefactos autorojo = pygame.image.load("imagenes/auto3.png").convert() autorojo.set_colorkey(constantes.BLANCO) #autorojo = pygame.transform.rotate(autorojo,90) self.background.blit(autorojo, (1980, 500)) # ubicacion de las plataformas. level = [ [platforms.PISO, 0, 580], [platforms.PISO, 6000, 580], [platforms.PISO, 12000, 580], [platforms.LADRILLO1, 560, 427], [platforms.LADRILLO2, 980, 247], [platforms.LADRILLO3, 1200, 247], [platforms.LADRILLO2, 1390, 297], [platforms.LADRILLO3, 1620, 367], [platforms.LADRILLO3, 1750, 367], [platforms.LADRILLO3, 2500, 445], [platforms.LADRILLO3, 2630, 445], [platforms.LADRILLO3, 2760, 445], [platforms.LADRILLO1, 3000, 367], [platforms.LADRILLO1, 3200, 317], [platforms.LADRILLO1, 3800, 317], [platforms.LADRILLO2, 5200, 320], [platforms.LADRILLO3, 5400, 460], [platforms.LADRILLO3, 5530, 460], [platforms.LADRILLO3, 5660, 460], [platforms.LADRILLO1, 5900, 350], [platforms.LADRILLO1, 5955, 305], [platforms.LADRILLO1, 6010, 260], [platforms.LADRILLO1, 6065, 215], [platforms.LADRILLO3, 6130, 215], [platforms.LADRILLO3, 6260, 215], [platforms.LADRILLO3, 6390, 215], [platforms.LADRILLO3, 6520, 215], [platforms.LADRILLO1, 7350, 450], [platforms.LADRILLO1, 7600, 400], [platforms.LADRILLO2, 7850, 350], [platforms.LADRILLO3, 8100, 300], [platforms.LADRILLO3, 8500, 350], [platforms.LADRILLO3, 8630, 350], [platforms.LADRILLO3, 8760, 350], [platforms.LADRILLO1, 9500, 450], [platforms.LADRILLO2, 10000, 450], [platforms.LADRILLO1, 10250, 350], [platforms.LADRILLO3, 11000, 380], [platforms.LADRILLO3, 11130, 380], [platforms.LADRILLO3, 11260, 380], [platforms.LADRILLO3, 11390, 240], [platforms.LADRILLO3, 11520, 240], [platforms.LADRILLO3, 11650, 240], [platforms.LADRILLO3, 11780, 240], [platforms.LADRILLO3, 11910, 240], [platforms.LADRILLO3, 12050, 240] ] # Go through the array above and add platforms for platform in level: block = platforms.Platform(platform[0]) block.rect.x = platform[1] block.rect.y = platform[2] block.player = self.player self.platform_list.add(block) level = [ [platforms.PISO, 100, 0], [platforms.LADRILLO3, 1550, 444], [platforms.LADRILLO3, 1550, 367], [platforms.LADRILLO3, 1880, 367], [platforms.LADRILLO3, 4300, 444], [platforms.LADRILLO3, 4300, 380], [platforms.LADRILLO3, 4300, 300], [platforms.LADRILLO3, 6900, 444], [platforms.LADRILLO3, 6900, 380], [platforms.LADRILLO3, 8890, 444], [platforms.LADRILLO3, 8890, 380], [platforms.LADRILLO3, 11390, 270], [platforms.LADRILLO3, 11000, 435] ] # Go through the array above and add platforms for platform in level: block = platforms.VerticalPlatform(platform[0]) block.rect.x = platform[1] block.rect.y = platform[2] block.player = self.player self.platform_list.add(block) #Add a custom moving platform block = platforms.MovingPlatform(platforms.LADRILLO3) block.rect.x = 690 block.rect.y = 375 block.boundary_left = 690 block.boundary_right = 800 block.mover_x = 1 block.player = self.player block.nivel = self self.platform_list.add(block) block = platforms.MovingPlatform(platforms.LADRILLO2) block.rect.x = 3300 block.rect.y = 200 block.boundary_left = 3300 block.boundary_right = 3580 block.mover_x = 1 block.player = self.player block.nivel = self self.platform_list.add(block) block = platforms.MovingPlatform(platforms.LADRILLO1) block.rect.x = 4150 block.rect.y = 180 block.boundary_top = 180 block.boundary_bottom = 490 block.mover_y = 1 block.player = self.player block.nivel = self self.platform_list.add(block) block = platforms.MovingPlatform(platforms.LADRILLO3) block.rect.x = 4850 block.rect.y = 100 block.boundary_left = 4850 block.boundary_right = 4950 block.mover_x = -1 block.player = self.player block.nivel = self self.platform_list.add(block) block = platforms.MovingPlatform(platforms.LADRILLO1) block.rect.x = 4650 block.rect.y = 150 block.boundary_top = 150 block.boundary_bottom = 490 block.mover_y = -1 block.player = self.player block.nivel = self self.platform_list.add(block) block = platforms.MovingPlatform(platforms.LADRILLO1) block.rect.x = 6700 block.rect.y = 180 block.boundary_top = 180 block.boundary_bottom = 455 block.mover_y = -1 block.player = self.player block.nivel = self self.platform_list.add(block) block = platforms.MovingPlatform(platforms.LADRILLO2) block.rect.x = 8350 block.rect.y = 120 block.boundary_top = 120 block.boundary_bottom = 400 block.mover_y = -1 block.player = self.player block.nivel = self self.platform_list.add(block) block = platforms.MovingPlatform(platforms.LADRILLO3) block.rect.x = 10400 block.rect.y = 300 block.boundary_left = 10400 block.boundary_right = 10700 block.mover_x = 1 block.player = self.player block.nivel = self self.platform_list.add(block)
def level_setup(self): for level in range(len(levels)): new_level = Level(self.screen, self.object_handler, levels[str(level + 1)]) new_level.create() self.game_levels.append(new_level)
class Game(Display, Controller): def __init__(self): self.framerate = 0 self.number_meanies = 32 self.bricks_in_level = 0 self.display = Display() self.hud = Hud() pygame.key.set_repeat(100,5) self.meanies = pygame.sprite.Group() self.player = Charactor(self) self.player.speed = 2 self.level = Level(self) self.level.load_level(0) self.start_game() # self.testgroup = pygame.sprite.Group() # self.init_meanies(0) # def init_meanies(self, counter): # # while counter < self.number_meanies: # # mx = random.randint( 0, self.display.bgmax[0]-40 ) # my = random.randint( 0, self.display.bgmax[1]-40 ) # # meanie = Meanie() # meanie.state.set_random_move_index() # meanie.rect.center = mx,my # self.testgroup.add(meanie) # # if meanie.overlaps(self.testgroup): # self.testgroup.remove(meanie) # meanie.kill() # self.init_meanies(counter) # else: # self.meanies.add(meanie) # counter += 1 # # if counter == self.number_meanies: # self.start_game() def start_game(self): self.gameRunning = True self.mainloop() def mainloop(self): self.clock = pygame.time.Clock() self.player.rect.center = [self.display.arena[0]/2, self.display.arena[1]/2] controller = Controller() while self.gameRunning: dirty = [] self.clock.tick(FPS) self.framerate = self.clock.get_fps() self.display.paint_background(dirty) self.player.state.set_current_state(self) d = self.display.paint_sprite(self.player) dirty.insert(len(dirty), d) self.steine.update() self.ladders.update() self.barrels.update() self.meanies.update(dirty) self.hud.showinfo(self, dirty) controller.handle_keyboard_input(self.player) self.player.update() pygame.display.update(dirty)
def test_level_anchor_boxes(): level = Level(32, [(1, 4)], [2**0, 2**1]) assert np.array_equal(level.anchor_sizes, [[16, 64], [32, 128]])
def level2Representation(self): rep = Level.load_rep('level/level2.txt') objects = { 'lever': {'l', 'm'}, 'door': {'p', 'q'}, 'player': {'j'}, 'enemy': {'e'}, 'object': {'a'}, 'robot': {'r'} } toggle_objects = { 'l': {'p'}, 'm': {'q'} } tile_map = { 'x': pygame.image.load('img/wall.png'), ',': pygame.image.load('img/dark_gray_tile.png'), '.': pygame.image.load('img/light_gray_tile.png'), '-': pygame.image.load('img/dark_red_tile.png') } level = Level( rep, objects, toggle_objects, self.width, self.height, Dimensions.width, Dimensions.height ) class_map = { 'lever': Lever, 'robot': Robot, 'enemy': Enemy, 'player': MainCharacter, 'door': Door, 'object': Treasure } values = { 'l':{'image':'img/lever_a_0.png', 'screen': self.screen}, 'm': {'image': 'img/lever_b_0.png', 'screen': self.screen}, 'p':{'toggled':False}, 'q': {'toggled': False}, 'j': {}, 'e': {}, 'a': {}, 'r': {} } coords = level.coordinates(['x']) unwalkable = {x for k in coords for x in coords[k]} level2Dict = { 'levelIndex': 2, 'rep': rep, 'objects': objects, 'toggle_objects': toggle_objects, 'tile_map': tile_map, 'level': level, 'class_map': class_map, 'values': values, 'coords': coords, 'unwalkable': unwalkable, 'config_ai' : self.level2AI } return level2Dict
class Game(): framerate = 0 number_meanies = 32 def __init__(self): self.display = Display() self.player = Charactor(self) self.level = Level(self) self.hud = Hud() self.prepare_stage() def prepare_stage(self): self.level.load_level(0) self.start_game() def start_game(self): self.gameRunning = True self.mainloop() def mainloop(self): self.clock = pygame.time.Clock() self.player.rect.center = [self.display.arena[0]/2, self.display.arena[1]/2] controller = Controller() while self.gameRunning: dirty = [] self.clock.tick(FPS) self.framerate = self.clock.get_fps() self.display.paint_background(dirty) self.player.state.set_current_state() d = self.display.paint_sprite(self.player) dirty.insert(len(dirty), d) self.steine.update() self.ladders.update() self.barrels.update() self.meanies.update(dirty) self.hud.showinfo(self, dirty) controller.handle_keyboard_input(self.player) self.player.update() ## Just for debugging - paint hitareas of charactor ## d = self.draw_hitarea(self.player.feet()) dirty.insert(len(dirty), d) d = self.draw_hitarea(self.player.middle(), (0,0,255)) dirty.insert(len(dirty), d) d = self.draw_hitarea(self.player.head(), (0,255,255)) dirty.insert(len(dirty), d) pygame.display.update(dirty) def draw_hitarea(self, sprite, color=pygame.Color(255,0,0)): sfc = pygame.Surface([sprite.rect.width, sprite.rect.height]) sfc.fill(color) dummy = pygame.sprite.Sprite() dummy.image = sfc dummy.rect = sprite.rect d = self.display.paint_sprite(dummy) return d
if item.item_name == saved_weapon: player_inventory = Backpack(item, save_data[3]) else: player_inventory = Backpack(None, save_data[3]) saved_items = save_data[4].split(',') for item in items_list: for saved_item in saved_items: if saved_item == item.item_name: player_inventory.items.append(item) level_no = save_data[1] level_type = save_data[6] level_layout = save_data[7] level_coords = get_level(level_layout) current_level = Level(level_no, level_type, level_coords) hero.rect.x = current_level.level_entrance[0] + tile_width hero.rect.y = current_level.level_entrance[1] # Build a list of items already found on the level so that reloading doesn't respawn all items saved_found_items = save_data[5].split('.') if saved_found_items[0]: for found_item in saved_found_items: found_item_split = found_item.split(',') if not found_items_rects: found_items_rects = [ pygame.Rect(int(found_item_split[0]), int(found_item_split[1]), tile_width, tile_height) ] else:
def main(): """ Main Program """ pygame.init() # Set the height and width of the screen pygame.display.set_caption("Deep Ocean") pygame.mixer.music.load(YB) pygame.mixer.music.play(-1) # Create the player player = Player() bullet_list = pygame.sprite.Group() # Create all the levels level_list = [] # player, level, background, creatures_list, shark_list, bubble_number, shell_number, diver_x level_list.append( Level(player, 1, BACK, PLATFORM, CREATURES, SHARK, 20, 20, player.rect.x)) level_list.append( Level(player, 2, BACK, PLATFORM, CREATURES, SHARK, 15, 15, player.rect.x)) level_list.append( Level(player, 3, BACK, PLATFORM, CREATURES, SHARK, 10, 10, player.rect.x)) # Set the current level current_level_no = 0 current_level = level_list[current_level_no] active_sprite_list = pygame.sprite.Group() player.level = current_level player.rect.x = 340 player.rect.y = SCREEN_HEIGHT - player.rect.height active_sprite_list.add(player) arm = Arm(current_level) active_sprite_list.add(arm) # static images score_shell = SpriteOnScreen(SCORE_SHELL, 13, 16) tank = SpriteOnScreen(HEALTHBAR, 30, 450) score_number = Score(screen, current_level.player.pearls, current_level) active_sprite_list.add(score_shell, tank, score_number) # Loop until the user clicks the close button. done = False over = False # Used to manage how fast the screen updates clock = pygame.time.Clock() shot = pygame.mixer.Sound(SHOT) shot.set_volume(0.5) endsound = pygame.mixer.Sound(GAME_OVER) endsound.set_volume(0.1) # -------- Main Program Loop ----------- while not done: for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop if event.type == pygame.MOUSEBUTTONDOWN: # Fire a bullet if the user clicks the mouse button shot.play() # Get the mouse position pos = pygame.mouse.get_pos() mouse_x = pos[0] mouse_y = pos[1] # Create the bullet based on where we are, and where we want to go. bullet = Bullet(player.rect.x, player.rect.y, mouse_x, mouse_y, arm) # Add the bullet to the lists active_sprite_list.add(bullet) bullet_list.add(bullet) if event.type == pygame.KEYDOWN: # if event.key == pygame.K_LEFT: # player.go_left() # player.stop() # player.go_left() if event.key == pygame.K_RIGHT: player.go_right() if event.key == pygame.K_UP: player.jump() if event.key == pygame.K_DOWN: player.dive() if event.type == pygame.KEYUP: # if event.key == pygame.K_LEFT and player.change_x < 0: # player.stop() if event.key == pygame.K_RIGHT and player.change_x > 0: player.stop() if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: player.collect() current_level.collision_det(current_level.enemy_list) current_level.collision_det(current_level.treasure_list) current_level.collision_det(current_level.bubbles_list) current_level.collision_det2(bullet_list, current_level.enemy_list) # Update the player. active_sprite_list.update() # Update items in the level current_level.update() # If the player gets near the right side, shift the world left (-x) if player.rect.right >= 500: diff = player.rect.right - 500 player.rect.right = 500 current_level.shift_world(-diff) # If the player gets near the left side, shift the world right (+x) if player.rect.left <= 120: diff = 120 - player.rect.left player.rect.left = 120 current_level.shift_world(diff) # If the player gets to the end of the level, go to the next level current_position = player.rect.x + current_level.world_shift if current_position < current_level.level_limit: player.rect.x = 120 if current_level_no < len(level_list) - 1: current_level_no += 1 current_level = level_list[current_level_no] player.level = current_level for bullet in bullet_list: # See if it hit a block # block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True) # Remove the bullet if it flies up off the screen if bullet.rect.y < -10: bullet_list.remove(bullet) active_sprite_list.remove(bullet) # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT current_level.draw(screen) HealthBar(screen, current_level.player.oxygen) active_sprite_list.draw(screen) # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT # Limit to 60 frames per second clock.tick(FPS) # Go ahead and update the screen with what we've drawn. pygame.display.flip() if current_level.player.oxygen < 20: endsound.play() if current_level.player.oxygen <= 0: over = True if over == True: return game_over(current_level.player.pearls) pygame.quit()
def _init_levels(self) -> List[Level]: levels: List[Level] = [] level1 = Level(self, 1, (1, 4)) level1.add_item(Platform(self, (1, 1), (10, 1))) level1.add_item(Platform(self, (6, 2), (1, 1))) level1.add_item(Platform(self, (14, 2), (4, 1))) level1.add_item(Platform(self, (17, 1), (5, 1))) level1.add_item(Platform(self, (24, 1), (4, 1))) level1.add_item(Platform(self, (29, 2), (2, 1))) level1.add_item(Platform(self, (32, 4), (2, 1))) level1.add_item(Platform(self, (33, 3), (1, 2))) level1.add_item(Platform(self, (34, 2), (2, 1))) level1.add_item(Platform(self, (36, 3), (1, 2))) level1.add_item(Platform(self, (40, 5), (1, 2))) level1.add_item(Platform(self, (36, 4), (4, 1))) level1.add_item(Trap(self, (34, 3), (2, 1))) level1.add_item(Finish(self, (40, 7), (1, 2))) level2 = Level(self, 2, (1, 4)) level2.add_item(Platform(self, (0, 1), (10, 1))) level2.add_item(Platform(self, (9, 3), (1, 2))) level2.add_item(Platform(self, (10, 3), (7, 1))) level2.add_item(Platform(self, (20, 3), (7, 1))) level2.add_item(Platform(self, (30, 3), (7, 1))) level2.add_item(Platform(self, (40, 4), (2, 1))) level2.add_item(Platform(self, (44, 4), (2, 1))) level2.add_item(Platform(self, (48, 4), (2, 1))) level2.add_item(Platform(self, (52, 4), (2, 1))) level2.add_item(Platform(self, (56, 4), (2, 1))) level2.add_item(Trap(self, (10, 2), (48, 1))) level2.add_item(Finish(self, (58, 2), (5, 1))) level3 = Level(self, 3, (1, 4)) level3.add_item(Platform(self, (0, 1), (4, 1))) level3.add_item(Platform(self, (4, 2), (1, 1))) level3.add_item(Platform(self, (5, 3), (1, 1))) level3.add_item(Platform(self, (6, 4), (1, 1))) level3.add_item(Platform(self, (7, 5), (1, 1))) level3.add_item(Platform(self, (8, 6), (1, 1))) level3.add_item(Platform(self, (11, 6), (2, 1))) level3.add_item(Platform(self, (15, 6), (2, 1))) level3.add_item(Platform(self, (19, 6), (2, 1))) level3.add_item(Platform(self, (23, 6), (2, 1))) level3.add_item(Platform(self, (27, 6), (2, 1))) level3.add_item(Platform(self, (31, 6), (2, 1))) level3.add_item(Platform(self, (33, 6), (1, 1))) level3.add_item(Platform(self, (34, 5), (1, 1))) level3.add_item(Platform(self, (35, 4), (1, 1))) level3.add_item(Platform(self, (36, 3), (1, 1))) level3.add_item(Platform(self, (37, 2), (1, 1))) level3.add_item(Finish(self, (38, 2), (4, 1))) level4 = Level(self, 4, (1, 4)) level4.add_item(Platform(self, (0, 1), (10, 1))) level4.add_item(Platform(self, (9, 3), (1, 2))) level4.add_item(Platform(self, (10, 3), (3, 1))) level4.add_item(Platform(self, (15, 4), (4, 1))) level4.add_item(Platform(self, (20, 5), (2, 1))) level4.add_item(Platform(self, (24, 3), (2, 1))) level4.add_item(Platform(self, (28, 3), (2, 1))) level4.add_item(Platform(self, (33, 4), (4, 1))) level4.add_item(Platform(self, (39, 5), (4, 1))) level4.add_item(Platform(self, (45, 6), (2, 1))) level4.add_item(Platform(self, (48, 7), (2, 1))) level4.add_item(Platform(self, (54, 4), (2, 1))) level4.add_item(Trap(self, (10, 2), (48, 1))) level4.add_item(Finish(self, (58, 2), (3, 1))) level4.add_item(Trap(self, (61, 2), (2, 1))) levels.append(level1) levels.append(level2) levels.append(level3) levels.append(level4) return levels
class GameStateRunning(object): """ Running GameState. Contains some of the gameplay logic and a Level, handles some of the input events... """ def __init__(self, context, screen, prev_state): """ Init with context, main PyGame surface and the previous state if you want to be able to go back :param context: The field in the main application which contains the current GameState. Current GameState has input events pumped into it, is updated and then drawn on the screen. Used by the current state to switch to the other GameState :param screen: Main PyGame surface to draw the objects/UI on :param prev_state: The state to which we will return :return: """ update_level_count() self.context = context self.screen = screen self.prev_state = prev_state self.level = Level(screen, (0, 0), (K_LEFT, K_UP, K_RIGHT), 0, self.finish_game) self.game_finished = False self.end_labels = None def restart_level(self): """ Helper function that restarts the level (and gets overridden in GameStateRunningMulti :return: """ self.level.start_level(self.level.level_number) def handle_input(self, events): """ Handles incoming input events :param events: input events from the main app :return: """ for event in events: if event.type == QUIT: sys.exit(0) if not self.game_finished: if event.type == KEYDOWN: if event.key == K_ESCAPE: self.context["gamestate"] = GameStatePauseMenu(self.context, self.screen, self, LEVEL_WIDTH) elif event.type == KEYUP and event.key == K_RETURN: self.context["gamestate"] = self.prev_state if not self.game_finished: self.level.handle_input(events) def finish_game(self): """ Function triggered on the game end, passed to the Level :return: """ self.game_finished = True self.end_labels = ( Assets.menu_font.render("Congratulations!", 1, (255, 255, 255)), Assets.font.render("Your final score: " + str(self.level.player.score), 1, (255, 255, 255)), Assets.font.render("Press enter to return to menu", 1, (255, 255, 255)) ) overlay = Surface((LEVEL_WIDTH, LEVEL_HEIGHT)) overlay.set_alpha(128) overlay.fill((0, 0, 0)) self.screen.blit(overlay, (0, 0)) y = LEVEL_HEIGHT / 2 - sum([label.get_rect().height / 2 for label in self.end_labels]) offset = 0 for label in self.end_labels: self.screen.blit(label, (LEVEL_WIDTH / 2 - label.get_rect().width / 2, y + offset)) offset = offset + label.get_rect().height def draw(self): """ Method called each frame to (re)draw the objects and UI :return: """ if not self.game_finished: self.level.draw() def update(self): """ Method called each frame, to update the state of entities based on input events and previous state of the game :return: """ if not self.game_finished: self.level.update()