Full Screen Example

Screenshot of a program demoing how to use full-screen
full_screen_example.py
  1"""
  2Use sprites to scroll around a large screen.
  3
  4Simple program to show basic sprite usage.
  5
  6Artwork from http://kenney.nl
  7
  8If Python and Arcade are installed, this example can be run from the command line with:
  9python -m arcade.examples.full_screen_example
 10"""
 11
 12import arcade
 13import os
 14
 15SPRITE_SCALING = 0.5
 16
 17SCREEN_WIDTH = 800
 18SCREEN_HEIGHT = 600
 19SCREEN_TITLE = "Full Screen Example"
 20
 21# How many pixels to keep as a minimum margin between the character
 22# and the edge of the screen.
 23VIEWPORT_MARGIN = 40
 24
 25MOVEMENT_SPEED = 5
 26
 27
 28class MyGame(arcade.Window):
 29    """ Main application class. """
 30
 31    def __init__(self):
 32        """
 33        Initializer
 34        """
 35        # Open a window in full screen mode. Remove fullscreen=True if
 36        # you don't want to start this way.
 37        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE, fullscreen=True)
 38
 39        # Set the working directory (where we expect to find files) to the same
 40        # directory this .py file is in. You can leave this out of your own
 41        # code, but it is needed to easily run the examples using "python -m"
 42        # as mentioned at the top of this program.
 43        file_path = os.path.dirname(os.path.abspath(__file__))
 44        os.chdir(file_path)
 45
 46        # This will get the size of the window, and set the viewport to match.
 47        # So if the window is 1000x1000, then so will our viewport. If
 48        # you want something different, then use those coordinates instead.
 49        width, height = self.get_size()
 50        self.set_viewport(0, width, 0, height)
 51        arcade.set_background_color(arcade.color.AMAZON)
 52        self.example_image = arcade.load_texture(":resources:images/tiles/boxCrate_double.png")
 53
 54    def on_draw(self):
 55        """
 56        Render the screen.
 57        """
 58
 59        arcade.start_render()
 60
 61        # Get viewport dimensions
 62        left, screen_width, bottom, screen_height = self.get_viewport()
 63
 64        text_size = 18
 65        # Draw text on the screen so the user has an idea of what is happening
 66        arcade.draw_text("Press F to toggle between full screen and windowed mode, unstretched.",
 67                         screen_width // 2, screen_height // 2 - 20,
 68                         arcade.color.WHITE, text_size, anchor_x="center")
 69        arcade.draw_text("Press S to toggle between full screen and windowed mode, stretched.",
 70                         screen_width // 2, screen_height // 2 + 20,
 71                         arcade.color.WHITE, text_size, anchor_x="center")
 72
 73        # Draw some boxes on the bottom so we can see how they change
 74        for x in range(64, 800, 128):
 75            y = 64
 76            width = 128
 77            height = 128
 78            arcade.draw_texture_rectangle(x, y, width, height, self.example_image)
 79
 80    def on_key_press(self, key, modifiers):
 81        """Called whenever a key is pressed. """
 82        if key == arcade.key.F:
 83            # User hits f. Flip between full and not full screen.
 84            self.set_fullscreen(not self.fullscreen)
 85
 86            # Get the window coordinates. Match viewport to window coordinates
 87            # so there is a one-to-one mapping.
 88            width, height = self.get_size()
 89            self.set_viewport(0, width, 0, height)
 90
 91        if key == arcade.key.S:
 92            # User hits s. Flip between full and not full screen.
 93            self.set_fullscreen(not self.fullscreen)
 94
 95            # Instead of a one-to-one mapping, stretch/squash window to match the
 96            # constants. This does NOT respect aspect ratio. You'd need to
 97            # do a bit of math for that.
 98            self.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT)
 99
100
101def main():
102    """ Main method """
103    MyGame()
104    arcade.run()
105
106
107if __name__ == "__main__":
108    main()