Пример #1
0
class MyGame:
    """ Our custom Window Class"""
    def __init__(self):
        """ Initializer """

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        # Variables that will hold sprite lists
        self.shape_list = []

        self.performance_timing = PerformanceTiming(results_file=RESULTS_FILE,
                                                    start_n=0,
                                                    increment_n=100,
                                                    end_time=60)

        # Initialize Pygame
        pygame.init()
        pygame.display.set_caption(SCREEN_TITLE)

        # Set the height and width of the screen
        self.screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])

        # This is a list of every sprite. All blocks and the player block as well.
        self.coin_list = pygame.sprite.Group()

        self.font = pygame.font.SysFont('Calibri', 25, True, False)

    def add_shapes(self, amount):
        for i in range(amount):
            x = random.randrange(0, SCREEN_WIDTH)
            y = random.randrange(0, SCREEN_HEIGHT)
            width = random.randrange(10, 30)
            height = random.randrange(10, 30)
            angle = random.randrange(0, 360)

            d_x = random.randrange(-3, 4)
            d_y = random.randrange(-3, 4)
            d_angle = random.randrange(-3, 4)

            red = random.randrange(256)
            green = random.randrange(256)
            blue = random.randrange(256)
            alpha = random.randrange(256)

            shape_type = random.randrange(2)
            # shape_type = 0

            if shape_type == 0:
                shape = Rectangle(x, y, width, height, angle, d_x, d_y,
                                  d_angle, (red, green, blue, alpha))
            elif shape_type == 1:
                shape = Ellipse(x, y, width, height, angle, d_x, d_y, d_angle,
                                (red, green, blue, alpha))
            # elif shape_type == 2:
            #     shape = Line(x, y, width, height, angle, d_x, d_y,
            #                  d_angle, (red, green, blue, alpha))

            self.shape_list.append(shape)

    def on_draw(self):
        """ Draw everything """

        # Start timing how long this takes
        self.performance_timing.start_timer('draw')

        # Clear the screen
        self.screen.fill((0, 0, 0))

        for shape in self.shape_list:
            shape.draw(self.screen)

        pygame.display.flip()

        # Stop timing how long this takes
        self.performance_timing.stop_timer('draw')

    def update(self, _delta_time):
        # Start update timer
        self.performance_timing.start_timer('update')

        for shape in self.shape_list:
            shape.move()

        # Stop timing the update
        self.performance_timing.stop_timer('update')

        # Figure out if we need more coins
        if self.performance_timing.target_n > len(self.shape_list):
            new_coin_amount = self.performance_timing.target_n - len(
                self.shape_list)
            self.add_shapes(new_coin_amount)
Пример #2
0
class MyGame:
    """ Our custom Window Class"""
    def __init__(self):
        """ Initializer """

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        # Variables that will hold sprite lists
        self.coin_list = None

        self.performance_timing = PerformanceTiming(results_file=RESULTS_FILE,
                                                    start_n=0,
                                                    increment_n=1000,
                                                    end_time=60)

        # Initialize Pygame
        pygame.init()

        # Set the height and width of the screen
        self.screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])

        # This is a list of every sprite. All blocks and the player block as well.
        self.coin_list = pygame.sprite.Group()
        self.player_list = pygame.sprite.Group()

        # Create the player instance
        self.player = Player()

        self.player.rect.x = random.randrange(SPRITE_SIZE,
                                              SCREEN_WIDTH - SPRITE_SIZE)
        self.player.rect.y = random.randrange(SPRITE_SIZE,
                                              SCREEN_HEIGHT - SPRITE_SIZE)
        self.player.change_x = 3
        self.player.change_y = 5

        self.player_list.add(self.player)

        self.font = pygame.font.SysFont('Calibri', 25, True, False)

    def add_coins(self, amount):

        # Create the coins
        for i in range(amount):
            # Create the coin instance
            # Coin image from kenney.nl
            coin = Coin()

            # Position the coin
            coin.rect.x = random.randrange(SPRITE_SIZE,
                                           SCREEN_WIDTH - SPRITE_SIZE)
            coin.rect.y = random.randrange(SPRITE_SIZE,
                                           SCREEN_HEIGHT - SPRITE_SIZE)

            # Add the coin to the lists
            self.coin_list.add(coin)

    def on_draw(self):
        """ Draw everything """

        # Start timing how long this takes
        self.performance_timing.start_timer('draw')

        # Clear the screen
        self.screen.fill((59, 122, 87))

        # Draw all the spites
        self.coin_list.draw(self.screen)
        self.player_list.draw(self.screen)

        pygame.display.flip()

        # Stop timing how long this takes
        self.performance_timing.stop_timer('draw')

    def update(self, _delta_time):
        # Start update timer
        self.performance_timing.start_timer('update')

        # Start update timer
        self.player_list.update()

        if self.player.rect.x < 0 and self.player.change_x < 0:
            self.player.change_x *= -1
        if self.player.rect.y < 0 and self.player.change_y < 0:
            self.player.change_y *= -1

        if self.player.rect.x > SCREEN_WIDTH and self.player.change_x > 0:
            self.player.change_x *= -1
        if self.player.rect.y > SCREEN_HEIGHT and self.player.change_y > 0:
            self.player.change_y *= -1

        coin_hit_list = pygame.sprite.spritecollide(self.player,
                                                    self.coin_list, False)
        for coin in coin_hit_list:
            coin.rect.x = random.randrange(SCREEN_WIDTH)
            coin.rect.y = random.randrange(SCREEN_HEIGHT)

        # Stop timing the update
        self.performance_timing.stop_timer('update')

        # Figure out if we need more coins
        if self.performance_timing.target_n > len(self.coin_list):
            new_coin_amount = self.performance_timing.target_n - len(
                self.coin_list)
            self.add_coins(new_coin_amount)
Пример #3
0
class MyGame(arcade.Window):
    """ Our custom Window Class"""
    def __init__(self):
        """ Initializer """
        # Call the parent class initializer
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        arcade.cleanup_texture_cache()

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        # Variables that will hold sprite lists
        self.coin_list = None
        self.sprite_count_list = []

        self.performance_timing = PerformanceTiming(results_file=RESULTS_FILE,
                                                    start_n=0,
                                                    increment_n=1000,
                                                    end_time=60)

        arcade.set_background_color(arcade.color.AMAZON)

    def add_coins(self, amount):

        # Create the coins
        for i in range(amount):
            # Create the coin instance
            # Coin image from kenney.nl
            coin = Coin("../resources/coinGold.png", SPRITE_SCALING_COIN)

            # Position the coin
            coin.center_x = random.randrange(SPRITE_SIZE,
                                             SCREEN_WIDTH - SPRITE_SIZE)
            coin.center_y = random.randrange(SPRITE_SIZE,
                                             SCREEN_HEIGHT - SPRITE_SIZE)

            coin.change_x = random.randrange(-3, 4)
            coin.change_y = random.randrange(-3, 4)

            # Add the coin to the lists
            self.coin_list.append(coin)

    def setup(self):
        """ Set up the game and initialize the variables. """

        # Sprite lists
        self.coin_list = arcade.SpriteList(use_spatial_hash=False,
                                           is_static=True)

    def on_draw(self):
        """ Draw everything """

        # Start timing how long this takes
        self.performance_timing.start_timer('draw')

        # Clear the screen
        arcade.start_render()

        # Draw all the sprites
        self.coin_list.draw()

        # Stop timing how long this takes
        self.performance_timing.stop_timer('draw')

    def update(self, delta_time):
        # Start update timer
        self.performance_timing.start_timer('update')

        # Stop timing the update
        self.performance_timing.stop_timer('update')

        # Figure out if we need more coins
        if self.performance_timing.target_n > len(self.coin_list):
            new_coin_amount = self.performance_timing.target_n - len(
                self.coin_list)
            self.add_coins(new_coin_amount)

        # End the program run
        if self.performance_timing.end_run():
            # Save screenshot
            image = arcade.get_image()
            image.save(RESULTS_IMAGE, 'PNG')
            self.close()
Пример #4
0
class MyGame(arcade.Window):
    """ Main application class. """
    def __init__(self, buffered):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        self.shape_list = None
        self.buffered = buffered

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        if self.buffered:
            self.results_file = "../../result_data/arcade/moving_shapes_buffered.csv"
            self.results_image = "../../result_data/arcade/moving_shapes_buffered.png"
        else:
            self.results_file = "../../result_data/arcade/moving_shapes_unbuffered.csv"
            self.results_image = "../../result_data/arcade/moving_shapes_unbuffered.png"

        self.performance_timing = PerformanceTiming(
            results_file=self.results_file,
            start_n=0,
            increment_n=20,
            end_time=60)

    def setup(self):
        """ Set up the game and initialize the variables. """
        self.shape_list = []

    def add_shapes(self, amount):
        for i in range(amount):
            x = random.randrange(0, SCREEN_WIDTH)
            y = random.randrange(0, SCREEN_HEIGHT)
            width = random.randrange(10, 30)
            height = random.randrange(10, 30)
            angle = random.randrange(0, 360)

            d_x = random.randrange(-3, 4)
            d_y = random.randrange(-3, 4)
            d_angle = random.randrange(-3, 4)

            red = random.randrange(256)
            green = random.randrange(256)
            blue = random.randrange(256)
            alpha = random.randrange(256)

            shape_type = random.randrange(2)
            # shape_type = 2

            if not self.buffered:
                if shape_type == 0:
                    shape = Rectangle(x, y, width, height, angle, d_x, d_y,
                                      d_angle, (red, green, blue, alpha))
                elif shape_type == 1:
                    shape = Ellipse(x, y, width, height, angle, d_x, d_y,
                                    d_angle, (red, green, blue, alpha))
                elif shape_type == 2:
                    shape = Line(x, y, width, height, angle, d_x, d_y, d_angle,
                                 (red, green, blue, alpha))
            else:
                if shape_type == 0:
                    shape = RectangleBuffered(x, y, width, height, angle, d_x,
                                              d_y, d_angle,
                                              (red, green, blue, alpha))
                elif shape_type == 1:
                    shape = EllipseBuffered(x, y, width, height, angle, d_x,
                                            d_y, d_angle,
                                            (red, green, blue, alpha))
                elif shape_type == 2:
                    shape = LineBuffered(x, y, width, height, angle, d_x, d_y,
                                         d_angle, (red, green, blue, alpha))

            self.shape_list.append(shape)

    def on_update(self, dt):
        """ Move everything """

        # Start update timer
        self.performance_timing.start_timer('update')

        for shape in self.shape_list:
            shape.move()

        # Stop timing the update
        self.performance_timing.stop_timer('update')

        # Figure out if we need more coins
        if self.performance_timing.target_n > len(self.shape_list):
            new_coin_amount = self.performance_timing.target_n - len(
                self.shape_list)
            self.add_shapes(new_coin_amount)

        # End the program run
        if self.performance_timing.end_run():
            # Save screenshot
            image = arcade.get_image()
            image.save(self.results_image, 'PNG')
            self.close()

    def on_draw(self):
        """
        Render the screen.
        """
        # Start timing how long this takes
        self.performance_timing.start_timer('draw')

        arcade.start_render()

        for shape in self.shape_list:
            shape.draw()

        # Stop timing how long this takes
        self.performance_timing.stop_timer('draw')
Пример #5
0
class MyGameCollision(arcade.Window):
    """ Our custom Window Class"""

    def __init__(self):
        """ Initializer """
        # Call the parent class initializer
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        arcade.cleanup_texture_cache()

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        # Variables that will hold sprite lists
        self.coin_list = None
        self.player_list = None
        self.player = None

        self.performance_timing = PerformanceTiming(results_file=RESULTS_FILE,
                                                    start_n=0,
                                                    increment_n=1000,
                                                    end_time=60)

        arcade.set_background_color(arcade.color.AMAZON)

        # Open file to save timings
        self.results_file = open(RESULTS_FILE, "w")

        self.frame = 0

    def add_coins(self, amount):

        # Create the coins
        for i in range(amount):
            # Create the coin instance
            # Coin image from kenney.nl
            coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)

            # Position the coin
            coin.center_x = random.randrange(SPRITE_SIZE, SCREEN_WIDTH - SPRITE_SIZE)
            coin.center_y = random.randrange(SPRITE_SIZE, SCREEN_HEIGHT - SPRITE_SIZE)

            # Add the coin to the lists
            self.coin_list.append(coin)

    def setup(self):
        """ Set up the game and initialize the variables. """

        # Sprite lists
        self.coin_list = arcade.SpriteList(use_spatial_hash=USE_SPATIAL_HASHING)
        self.player_list = arcade.SpriteList()
        self.player = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING_PLAYER)
        self.player.center_x = random.randrange(SCREEN_WIDTH)
        self.player.center_y = random.randrange(SCREEN_HEIGHT)
        self.player.change_x = 3
        self.player.change_y = 5
        self.player_list.append(self.player)

    def on_draw(self):
        """ Draw everything """

        # Start timing how long this takes
        self.performance_timing.start_timer('draw')

        arcade.start_render()
        self.coin_list.draw()
        self.player_list.draw()

        # Stop timing how long this takes
        self.performance_timing.stop_timer('draw')

    def update(self, delta_time):
        # Start update timer
        self.performance_timing.start_timer('update')

        self.player_list.update()
        if self.player.center_x < 0 and self.player.change_x < 0:
            self.player.change_x *= -1
        if self.player.center_y < 0 and self.player.change_y < 0:
            self.player.change_y *= -1

        if self.player.center_x > SCREEN_WIDTH and self.player.change_x > 0:
            self.player.change_x *= -1
        if self.player.center_y > SCREEN_HEIGHT and self.player.change_y > 0:
            self.player.change_y *= -1

        coin_hit_list = arcade.check_for_collision_with_list(self.player, self.coin_list)
        for coin in coin_hit_list:
            coin.center_x = random.randrange(SCREEN_WIDTH)
            coin.center_y = random.randrange(SCREEN_HEIGHT)

        # Stop timing the update
        self.performance_timing.stop_timer('update')

        # Figure out if we need more coins
        if self.performance_timing.target_n > len(self.coin_list):
            new_coin_amount = self.performance_timing.target_n - len(self.coin_list)
            self.add_coins(new_coin_amount)

        # End the program run
        if self.performance_timing.end_run():
            # Save screenshot
            image = arcade.get_image()
            image.save(RESULTS_IMAGE, 'PNG')
            self.close()
            import pyglet
            pyglet.app.exit()