Bouncing Rectangle#

This shows how to animate an item you can draw using the drawing primitives.

../../_images/bouncing_rectangle.png
bouncing_rectangle.py#
  1"""
  2This simple animation example shows how to bounce a rectangle
  3on the screen.
  4
  5If Python and Arcade are installed, this example can be run
  6from the command line with:
  7python -m arcade.examples.bouncing_rectangle
  8"""
  9
 10import arcade
 11
 12# --- Set up the constants
 13
 14# Size of the screen
 15SCREEN_WIDTH = 600
 16SCREEN_HEIGHT = 600
 17SCREEN_TITLE = "Bouncing Rectangle Example"
 18
 19# Rectangle info
 20RECT_WIDTH = 50
 21RECT_HEIGHT = 50
 22RECT_COLOR = arcade.color.DARK_BROWN
 23
 24BACKGROUND_COLOR = arcade.color.ALMOND
 25
 26
 27class Item:
 28    """ This class represents our rectangle """
 29
 30    def __init__(self):
 31
 32        # Set up attribute variables
 33
 34        # Where we are
 35        self.center_x = 0
 36        self.center_y = 0
 37
 38        # Where we are going
 39        self.change_x = 0
 40        self.change_y = 0
 41
 42    def update(self):
 43        # Move the rectangle
 44        self.center_x += self.change_x
 45        self.center_y += self.change_y
 46        # Check if we need to bounce of right edge
 47        if self.center_x > SCREEN_WIDTH - RECT_WIDTH / 2:
 48            self.change_x *= -1
 49        # Check if we need to bounce of top edge
 50        if self.center_y > SCREEN_HEIGHT - RECT_HEIGHT / 2:
 51            self.change_y *= -1
 52        # Check if we need to bounce of left edge
 53        if self.center_x < RECT_WIDTH / 2:
 54            self.change_x *= -1
 55        # Check if we need to bounce of bottom edge
 56        if self.center_y < RECT_HEIGHT / 2:
 57            self.change_y *= -1
 58
 59    def draw(self):
 60        # Draw the rectangle
 61        arcade.draw_rectangle_filled(self.center_x,
 62                                     self.center_y,
 63                                     RECT_WIDTH,
 64                                     RECT_HEIGHT,
 65                                     RECT_COLOR)
 66
 67
 68class MyGame(arcade.Window):
 69    """ Main application class. """
 70
 71    def __init__(self, width, height, title):
 72        super().__init__(width, height, title)
 73
 74        # Create our rectangle
 75        self.item = Item()
 76        self.item.center_x = 200
 77        self.item.center_y = 300
 78        self.item.change_x = 2
 79        self.item.change_y = 3
 80
 81        # Set background color
 82        self.background_color = BACKGROUND_COLOR
 83
 84    def on_update(self, delta_time):
 85        # Move the rectangle
 86        self.item.update()
 87
 88    def on_draw(self):
 89        """ Render the screen. """
 90
 91        # Clear screen
 92        self.clear()
 93        # Draw the rectangle
 94        self.item.draw()
 95
 96
 97def main():
 98    """ Main function """
 99    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
100    arcade.run()
101
102
103if __name__ == "__main__":
104    main()