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