How to animate an object using Arcade module?
Last Updated :
11 Oct, 2020
Prerequisite: Arcade Library
The world of Programming is very vast, and animation is its key soul. In this, tutorial you will learn how to animate objects in Python using Arcade Module. Arcade is a present day Programming module used for developing 2D games with gripping sound and graphics.
Before starting with the article, you must revise your concepts of arcade Python module. To explain the whole concept of animating an object, Let’s take an example to understand it completely. You all must aware with the nested loop being in C, Python or Java and have probably created a lot of patterns using it. Here, we will animate a box with the help of arcade Python library.
Step-by-step Approach:
Step1) Import arcade library.
Step2) Here we will specify some parameters, that we will be using later in the program to declare width, height and title of the screen.
Python3
SCREEN_WIDTH = 720
SCREEN_HEIGHT = 480
SCREEN_TITLE = "Bouncing Box"
RECT_WIDTH = 50
RECT_HEIGHT = 50
|
Step3) Define the function for drawing the box arcade.draw_rectangle_filled( ) Modify rectangles position based on the delta vector.
Python3
def on_draw(delta_time):
arcade.start_render()
arcade.draw_rectangle_filled(on_draw.center_x, on_draw.center_y,
RECT_WIDTH, RECT_HEIGHT,
arcade.color.GREEN)
on_draw.center_x + = on_draw.delta_x * delta_time
on_draw.center_y + = on_draw.delta_y * delta_time
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
|
Step4:-Now, define function-specific variables. Also, we need to give them initial values. Then the values will persist between function calls.
Python3
on_draw.center_x = 100
on_draw.center_y = 50
on_draw.delta_x = 115
on_draw.delta_y = 130
|
Step5) Now, last step is define the main function. Under which, you need to define background colors.
Python3
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.set_background_color(arcade.color.WHITE)
arcade.schedule(on_draw, 1 / 80 )
arcade.run()
|
Below is the complete program of the above approach:
Python3
import arcade
SCREEN_WIDTH = 720
SCREEN_HEIGHT = 480
SCREEN_TITLE = "Bouncing Box"
RECT_WIDTH = 50
RECT_HEIGHT = 50
def on_draw(delta_time):
arcade.start_render()
arcade.draw_rectangle_filled(on_draw.center_x, on_draw.center_y,
RECT_WIDTH, RECT_HEIGHT,
arcade.color.GREEN)
on_draw.center_x + = on_draw.delta_x * delta_time
on_draw.center_y + = on_draw.delta_y * delta_time
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
on_draw.center_x = 100
on_draw.center_y = 50
on_draw.delta_x = 115
on_draw.delta_y = 130
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.set_background_color(arcade.color.WHITE)
arcade.schedule(on_draw, 1 / 80 )
arcade.run()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...