Making an object jump with gravity using arcade module in Python3
Last Updated :
11 Oct, 2020
The Arcade library is a modern Python Module used widely for developing 2D video games with compelling graphics and sound. Arcade is an object-oriented library. It can be installed like any other Python Package. Now, it’s up to the necessity of the developer, what type of game, graphics, and sound he/she wants to use using this toolkit. So, in this article, we will learn how to make an object jump with gravity using the Arcade library in Python. And for understanding that concept, let’s take an example of a robust ball.
Following are the steps:
Step 1: Import arcade module .
import arcade
Step 2: Define parameters of the output screen.
# Size of the screen
WIDTH = 600
HEIGHT = 600
TITLE = "Robust Ball "
Step 3: Define the radius of the ball.
# Size of the ball.
BALL_RADIUS = 50
Step 4: Define the value of gravitational constant.
# gravity
GRAVITATIONAL_CONSTANT = 0.3
Step 5: Define a bouncing velocity for the ball.
# Percent of velocity maintained on a bounce.
BOUNCE = 0.9
Step 6: Define a function to draw a ball using arcade.draw_circle_filled() .
Python3
def draw(_delta_time):
arcade.start_render()
arcade.draw_circle_filled(draw.x, draw.y, BALL_RADIUS,
arcade.color.RED)
draw.x + = draw.delta_x
draw.y + = draw.delta_y
draw.delta_y - = GRAVITATIONAL_CONSTANT
|
Step 7: Define function to Figure out if we hit the left or right edge so that we can reverse.
Python3
if draw.x < BALL_RADIUS and draw.delta_x < 0 :
draw.delta_x * = - BOUNCE
elif draw.x > WIDTH - BALL_RADIUS and draw.delta_x > 0 :
draw.delta_x * = - BOUNCE
|
Step 8: Also, Define function to Figure out if we hit the bottom so that we can reverse.
Python3
if draw.y < BALL_RADIUS and draw.delta_y < 0 :
if draw.delta_y * - 1 > GRAVITATIONAL_CONSTANT * 15 :
draw.delta_y * = - BOUNCE
else :
draw.delta_y * = - BOUNCE / 2
|
Step 9: Use the above defined function and provide input to them.
Python3
draw.x = BALL_RADIUS
draw.y = HEIGHT
draw.delta_x = 3
draw.delta_y = 3
|
Step 10: Last and foremost step is to define the main function that contains arcade function to assign output window a proper width, height and title.
Python3
def main():
arcade.open_window(WIDTH, HEIGHT, TITLE)
arcade.set_background_color(arcade.color.GREEN)
arcade.schedule(draw, 1 / 80 )
arcade.run()
arcade.close_window()
main()
|
Complete Code for better Understanding:
Python3
import arcade
WIDTH = 600
HEIGHT = 600
TITLE = "Robust Ball "
BALL_RADIUS = 50
GRAVITATIONAL_CONSTANT = 0.3
BOUNCE = 0.9
def draw(_delta_time):
arcade.start_render()
arcade.draw_circle_filled(draw.x, draw.y, BALL_RADIUS,
arcade.color.RED)
draw.x + = draw.delta_x
draw.y + = draw.delta_y
draw.delta_y - = GRAVITATIONAL_CONSTANT
if draw.x < BALL_RADIUS and draw.delta_x < 0 :
draw.delta_x * = - BOUNCE
elif draw.x > WIDTH - BALL_RADIUS and draw.delta_x > 0 :
draw.delta_x * = - BOUNCE
if draw.y < BALL_RADIUS and draw.delta_y < 0 :
if draw.delta_y * - 1 > GRAVITATIONAL_CONSTANT * 15 :
draw.delta_y * = - BOUNCE
else :
draw.delta_y * = - BOUNCE / 2
draw.x = BALL_RADIUS
draw.y = HEIGHT
draw.delta_x = 3
draw.delta_y = 3
def main():
arcade.open_window(WIDTH, HEIGHT, TITLE)
arcade.set_background_color(arcade.color.GREEN)
arcade.schedule(draw, 1 / 80 )
arcade.run()
arcade.close_window()
main()
|
Output:-
Share your thoughts in the comments
Please Login to comment...