Open In App

Making an object jump with gravity using arcade module in Python3

Last Updated : 11 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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):
  
    # Start the render.
    arcade.start_render()
  
    # Draw ball
    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




# Figure out if we hit the left or 
# right edge and need to reverse.
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




# See if we hit the bottom
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():
    
    # Open up our window
    arcade.open_window(WIDTH, HEIGHT, TITLE)
    arcade.set_background_color(arcade.color.GREEN)
  
    # Tell the computer to call the draw 
    # command at the specified interval.
    arcade.schedule(draw, 1 / 80)
  
    # Run the program
    arcade.run()
  
    # When done running the program, close the window.
    arcade.close_window()
  
main()


Complete Code for better Understanding:

Python3




import arcade
  
  
# Size of the screen
WIDTH = 600
HEIGHT = 600
TITLE = "Robust Ball "
  
# Size of the circle.
BALL_RADIUS = 50
  
# How strong the gravity is.
GRAVITATIONAL_CONSTANT = 0.3
  
# Percent of velocity maintained on a bounce.
BOUNCE = 0.9
  
  
def draw(_delta_time):
  
    # Start the render.
    arcade.start_render()
  
    # Draw ball
    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
  
    # Figure out if we hit the left or 
    # right edge and need to reverse.
    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
  
    # See if we hit the bottom
    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():
    # Open up our window
    arcade.open_window(WIDTH, HEIGHT, TITLE)
    arcade.set_background_color(arcade.color.GREEN)
  
    # Tell the computer to call the draw command 
    # at the specified interval.
    arcade.schedule(draw, 1 / 80)
  
    # Run the program
    arcade.run()
  
    # When done running the program, close the window.
    arcade.close_window()
  
  
main()


Output:-



Similar Reads

How to animate an object using Arcade module?
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 con
3 min read
How to show a timer on screen using arcade in Python3?
Prerequisite: Arcade library Arcade is a modern framework, which is used to make 2D video games. In this, article, you will learn how to show an on-screen timer using the arcade module of Python. Displaying a timer on screen is not tough job, just follow the below steps:- Step 1: First of all import the arcade module of Python C/C++ Code Step 2: De
2 min read
Draw a parabola using Arcade in Python3
Arcade is a Python library which is used for developing 2Dimensional Games. Arcade needs support for OpenGL 3.3+. In arcade, basic drawing does not require knowledge on how to define functions or classes or how to do loops, simply we have inbuilt functions for drawing primitives. Arcade inbuilt function for drawing parabola:- 1. arcade.draw_parabol
3 min read
Draw a circle using Arcade in Python3
The arcade library is a high-tech Python Package with advanced set of tools for making 2D games with gripping graphics and sound. It is Object-oriented and is especially built for Python 3.6 and above versions. Arcade inbuilt functions to draw circle :- 1. arcade.draw_circle_outline( ) : This function is used to draw the outline of a circle. Syntax
3 min read
Arcade inbuilt functions to draw polygon in Python3
The arcade library is a high-tech Python Package with advanced set of tools for making 2D games with gripping graphics and sound. It is Object-oriented and is especially built for Python 3.6 and above versions. Arcade has two inbuilt functions for drawing a polygon: 1. arcade.draw_polygon_outline( ) : This function is used to draw the outline of th
2 min read
Arcade inbuilt functions to draw point(s) in Python3
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. In this article, we will learn what are the arcade inbuilt functions to draw point. Arcade library is a modern framework, which makes draw
3 min read
Python | Making an object jump in PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. So, in this article, we will learn how
3 min read
Making Label Jump using PyQt5 in Python
Prerequisite: Introduction to PyQt5 PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library. NOw, we will see how we can make the label jump. Sometimes there is a need to make the label jump while design
4 min read
Draw a tree using arcade library in Python
Drawing a tree isn't a tough task in Python using the turtle module. But, what if you can draw it using Arcade Module as well. Arcade is an object-oriented library. It can be installed like any other Python Package using an import arcade. Approach: Import arcade.Define a function to draw trees. Here, we are drawing a pine tree made up of a rectangl
3 min read
Draw an arc using Arcade in Python
The arcade library is a high-tech Python Package with an advanced set of tools for making 2D games with gripping graphics and sound. It is Object-oriented and is specially built for Python 3.6 and above versions. Arcade has two inbuilt functions for drawing arc: 1: arcade.draw_arc_outline ( ): This function is used to draw an arc which is useful fo
2 min read
Article Tags :
Practice Tags :