Full Screen Example

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
12import arcade
13from arcade.types import LRBT
14
15SPRITE_SCALING = 0.5
16
17WINDOW_WIDTH = 800
18WINDOW_HEIGHT = 600
19WINDOW_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 GameView(arcade.View):
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__()
38
39 # This will get the size of the window, and set the viewport to match.
40 # So if the window is 1000x1000, then so will our viewport. If
41 # you want something different, then use those coordinates instead.
42 self.background_color = arcade.color.AMAZON
43 self.example_image = arcade.load_texture(":resources:images/tiles/boxCrate_double.png")
44
45 # The camera used to update the viewport and projection on screen resize.
46 self.camera = arcade.camera.Camera2D(
47 position=(0, 0),
48 projection=LRBT(left=0, right=WINDOW_WIDTH, bottom=0, top=WINDOW_HEIGHT),
49 viewport=self.window.rect
50 )
51
52 def on_draw(self):
53 """
54 Render the screen.
55 """
56
57 self.clear()
58 with self.camera.activate():
59 # Draw some boxes on the bottom so we can see how they change
60 for count in range(20):
61 x = count * 128
62 y = count * 128
63 width = 128
64 height = 128
65 arcade.draw_texture_rect(self.example_image, arcade.XYWH(x, y, width, height))
66
67 arcade.draw_rect_outline(
68 LRBT(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT),
69 color=arcade.color.WHITE,
70 border_width=5,
71 )
72
73 # Draw text on the screen so the user has an idea of what is happening
74 text_size = 18
75 arcade.draw_text(
76 "Press F to toggle between full screen and windowed mode, unstretched.",
77 x=WINDOW_WIDTH // 2,
78 y=WINDOW_HEIGHT // 2 - 20,
79 color=arcade.color.WHITE,
80 font_size=text_size,
81 anchor_x="center",
82 )
83 arcade.draw_text(
84 "Press S to toggle between full screen and windowed mode, stretched.",
85 x=WINDOW_WIDTH // 2,
86 y=WINDOW_HEIGHT // 2 + 20,
87 color=arcade.color.WHITE,
88 font_size=text_size,
89 anchor_x="center",
90 )
91
92 def on_key_press(self, key, modifiers):
93 """Called whenever a key is pressed. """
94 if key == arcade.key.F:
95 # User hits f. Flip between full and not full screen.
96 self.window.set_fullscreen(not self.window.fullscreen)
97
98 # Get the window coordinates. Match viewport to window coordinates
99 # so there is a one-to-one mapping.
100 self.camera.viewport = self.window.rect
101 self.camera.projection = arcade.LRBT(0.0, self.width, 0.0, self.height)
102
103 if key == arcade.key.S:
104 # User hits s. Flip between full and not full screen.
105 self.window.set_fullscreen(not self.window.fullscreen)
106
107 # Instead of a one-to-one mapping, stretch/squash window to match the
108 # constants. This does NOT respect aspect ratio. You'd need to
109 # do a bit of math for that.
110 self.camera.projection = LRBT(
111 left=0,
112 right=WINDOW_WIDTH,
113 bottom=0,
114 top=WINDOW_HEIGHT,
115 )
116 self.camera.viewport = self.window.rect
117
118 if key == arcade.key.ESCAPE:
119 self.window.close()
120
121
122def main():
123 """ Main function """
124 # Create a window class. This is what actually shows up on screen
125 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
126
127 # Create the GameView
128 game = GameView()
129
130 # Show GameView on f
131 window.show_view(game)
132
133 # Start the arcade game loop
134 arcade.run()
135
136
137if __name__ == "__main__":
138 main()