Bouncing Rectangle

This shows how to animate an image. It does not use classes, but it does use function variables.

If you are familiar with classes, see some of the following examples as well:

Bouncing Rectangle Example from Paul Vincent Craven on Vimeo.

bouncing_rectangle.py
 1"""
 2This simple animation example shows how to bounce a rectangle
 3on the screen.
 4
 5It assumes a programmer knows how to create functions already.
 6
 7It does not assume a programmer knows how to create classes. If you do know
 8how to create classes, see the starting template for a better example:
 9
10http://arcade.academy/examples/starting_template.html
11
12Or look through the examples showing how to use Sprites.
13
14A video walk-through of this example is available at:
15https://vimeo.com/168063840
16
17If Python and Arcade are installed, this example can be run from the command line with:
18python -m arcade.examples.bouncing_rectangle
19
20"""
21
22import arcade
23
24# --- Set up the constants
25
26# Size of the screen
27SCREEN_WIDTH = 600
28SCREEN_HEIGHT = 600
29SCREEN_TITLE = "Bouncing Rectangle Example"
30
31# Size of the rectangle
32RECT_WIDTH = 50
33RECT_HEIGHT = 50
34
35
36def on_draw(delta_time):
37    """
38    Use this function to draw everything to the screen.
39    """
40
41    # Start the render. This must happen before any drawing
42    # commands. We do NOT need a stop render command.
43    arcade.start_render()
44
45    # Draw a rectangle.
46    # For a full list of colors see:
47    # http://arcade.academy/arcade.color.html
48    arcade.draw_rectangle_filled(on_draw.center_x, on_draw.center_y,
49                                 RECT_WIDTH, RECT_HEIGHT,
50                                 arcade.color.ALIZARIN_CRIMSON)
51
52    # Modify rectangles position based on the delta
53    # vector. (Delta means change. You can also think
54    # of this as our speed and direction.)
55    on_draw.center_x += on_draw.delta_x * delta_time
56    on_draw.center_y += on_draw.delta_y * delta_time
57
58    # Figure out if we hit the edge and need to reverse.
59    if on_draw.center_x < RECT_WIDTH // 2 \
60            or on_draw.center_x > SCREEN_WIDTH - RECT_WIDTH // 2:
61        on_draw.delta_x *= -1
62    if on_draw.center_y < RECT_HEIGHT // 2 \
63            or on_draw.center_y > SCREEN_HEIGHT - RECT_HEIGHT // 2:
64        on_draw.delta_y *= -1
65
66
67# Below are function-specific variables. Before we use them
68# in our function, we need to give them initial values. Then
69# the values will persist between function calls.
70#
71# In other languages, we'd declare the variables as 'static' inside the
72# function to get that same functionality.
73#
74# Later on, we'll use 'classes' to track position and velocity for multiple
75# objects.
76on_draw.center_x = 100  # type: ignore # dynamic attribute on function obj  # Initial x position
77on_draw.center_y = 50   # type: ignore # dynamic attribute on function obj  # Initial y position
78on_draw.delta_x = 115   # type: ignore # dynamic attribute on function obj  # Initial change in x
79on_draw.delta_y = 130   # type: ignore # dynamic attribute on function obj  # Initial change in y
80
81
82def main():
83    # Open up our window
84    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
85    arcade.set_background_color(arcade.color.WHITE)
86
87    # Tell the computer to call the draw command at the specified interval.
88    arcade.schedule(on_draw, 1 / 80)
89
90    # Run the program
91    arcade.run()
92
93
94if __name__ == "__main__":
95    main()