📜  使用 Python3 中的 Arcade 模块利用重力使物体跳跃

📅  最后修改于: 2022-05-13 01:55:15.324000             🧑  作者: Mango

使用 Python3 中的 Arcade 模块利用重力使物体跳跃

Arcade 库是一个现代Python模块,广泛用于开发具有引人入胜的图形和声音的 2D 视频游戏。 Arcade 是一个面向对象的库。它可以像任何其他Python包一样安装。现在,这取决于开发人员的必要性,他/她希望使用此工具包使用什么类型的游戏、图形和声音。因此,在本文中,我们将学习如何使用Python中的 Arcade 库使物体在重力作用下跳跃。为了理解这个概念,让我们举一个坚固的球的例子。

以下是步骤:

第一步:导入街机模块。

import arcade

步骤 2:定义输出屏幕的参数。

# Size of the screen
WIDTH = 600
HEIGHT = 600
TITLE = "Robust Ball "

第 3 步:定义球的半径。

# Size of the ball.
BALL_RADIUS = 50

第 4 步:定义引力常数值。

#  gravity 
GRAVITATIONAL_CONSTANT = 0.3

第 5 步:定义球的弹跳速度。

# Percent of velocity maintained on a bounce.
BOUNCE = 0.9

第 6 步:定义一个函数来使用 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


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


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


Python3
draw.x = BALL_RADIUS
draw.y = HEIGHT
draw.delta_x = 3
draw.delta_y = 3


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()


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()


第 7 步:定义函数以判断我们是否击中了左边缘或右边缘,以便我们可以反转。

蟒蛇3

# 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

第 8 步:另外,定义函数以确定我们是否触底,以便我们可以反转。

蟒蛇3

# 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

第 9 步:使用上面定义的函数并为它们提供输入。

蟒蛇3

draw.x = BALL_RADIUS
draw.y = HEIGHT
draw.delta_x = 3
draw.delta_y = 3

第 10 步:最后也是最重要的一步是定义包含 Arcade函数的 main函数,为输出窗口分配适当的宽度、高度和标题。

蟒蛇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()

完整代码以更好地理解:

蟒蛇3

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()

输出:-