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 https://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
 12from __future__ import annotations
 13
 14import arcade
 15
 16SPRITE_SCALING = 0.5
 17
 18SCREEN_WIDTH = 800
 19SCREEN_HEIGHT = 600
 20SCREEN_TITLE = "Full Screen Example"
 21
 22# How many pixels to keep as a minimum margin between the character
 23# and the edge of the screen.
 24VIEWPORT_MARGIN = 40
 25
 26MOVEMENT_SPEED = 5
 27
 28
 29class MyGame(arcade.Window):
 30    """ Main application class. """
 31
 32    def __init__(self):
 33        """
 34        Initializer
 35        """
 36        # Open a window in full screen mode. Remove fullscreen=True if
 37        # you don't want to start this way.
 38        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE, fullscreen=True)
 39
 40        # This will get the size of the window, and set the viewport to match.
 41        # So if the window is 1000x1000, then so will our viewport. If
 42        # you want something different, then use those coordinates instead.
 43        width, height = self.get_size()
 44        self.set_viewport(0, width, 0, height)
 45        self.background_color = arcade.color.AMAZON
 46        self.example_image = arcade.load_texture(":resources:images/tiles/boxCrate_double.png")
 47
 48    def on_draw(self):
 49        """
 50        Render the screen.
 51        """
 52
 53        self.clear()
 54
 55        # Get viewport dimensions
 56        left, screen_width, bottom, screen_height = self.get_viewport()
 57
 58        text_size = 18
 59        # Draw text on the screen so the user has an idea of what is happening
 60        arcade.draw_text("Press F to toggle between full screen and windowed mode, unstretched.",
 61                         screen_width // 2, screen_height // 2 - 20,
 62                         arcade.color.WHITE, text_size, anchor_x="center")
 63        arcade.draw_text("Press S to toggle between full screen and windowed mode, stretched.",
 64                         screen_width // 2, screen_height // 2 + 20,
 65                         arcade.color.WHITE, text_size, anchor_x="center")
 66
 67        # Draw some boxes on the bottom so we can see how they change
 68        for x in range(64, 800, 128):
 69            y = 64
 70            width = 128
 71            height = 128
 72            arcade.draw_texture_rectangle(x, y, width, height, self.example_image)
 73
 74    def on_key_press(self, key, modifiers):
 75        """Called whenever a key is pressed. """
 76        if key == arcade.key.F:
 77            # User hits f. Flip between full and not full screen.
 78            self.set_fullscreen(not self.fullscreen)
 79
 80            # Get the window coordinates. Match viewport to window coordinates
 81            # so there is a one-to-one mapping.
 82            width, height = self.get_size()
 83            self.set_viewport(0, width, 0, height)
 84
 85        if key == arcade.key.S:
 86            # User hits s. Flip between full and not full screen.
 87            self.set_fullscreen(not self.fullscreen)
 88
 89            # Instead of a one-to-one mapping, stretch/squash window to match the
 90            # constants. This does NOT respect aspect ratio. You'd need to
 91            # do a bit of math for that.
 92            self.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT)
 93
 94
 95def main():
 96    """ Main function """
 97    MyGame()
 98    arcade.run()
 99
100
101if __name__ == "__main__":
102    main()