Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
 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])
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
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]])
Ejemplo n.º 7
0
        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)
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
 def level_cam(self):
     self.level = Level()
Ejemplo n.º 10
0
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()
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
0
                    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: