📜  如何使用 Arcade 模块为对象设置动画?

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

如何使用 Arcade 模块为对象设置动画?

先决条件:街机图书馆

编程的世界非常广阔,动画是其关键灵魂。在本教程中,您将学习如何使用 Arcade 模块在Python中为对象设置动画。 Arcade 是当今的编程模块,用于开发具有扣人心弦的声音和图形的 2D 游戏。

在开始本文之前,您必须修改您对街机Python模块的概念。为了解释动画对象的整个概念,让我们举一个例子来完全理解它。你们都必须知道嵌套循环是用 C、 Python或Java的,并且可能已经使用它创建了很多模式。在这里,我们将在Arcade Python库的帮助下为一个盒子制作动画。

循序渐进的方法:

Step1)导入街机库。

Python3
# Import required module 
import arcade


Python3
# Set up the constants
  
# Size of the screen
SCREEN_WIDTH = 720
SCREEN_HEIGHT = 480
SCREEN_TITLE = "Bouncing Box"
  
# Size of the rectangle
RECT_WIDTH = 50
RECT_HEIGHT = 50


Python3
# Explicilt function generate animated bouncing box
def on_draw(delta_time):
  
    # Start the render.
    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
  
    # Figure out if we hit the edge and need to reverse.
    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


Python3
# Set initial positions
on_draw.center_x = 100
on_draw.center_y = 50
on_draw.delta_x = 115
on_draw.delta_y = 130


Python3
# Driver code
  
# Open up our window
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.set_background_color(arcade.color.WHITE)
  
# Tell the computer to call the draw command at the specified interval.
arcade.schedule(on_draw, 1 / 80)
  
# Run the program
arcade.run()


Python3
# Import required module
import arcade
  
  
# Set up the constants
  
# Size of the screen
SCREEN_WIDTH = 720
SCREEN_HEIGHT = 480
SCREEN_TITLE = "Bouncing Box"
  
# Size of the rectangle
RECT_WIDTH = 50
RECT_HEIGHT = 50
  
  
# Explicilt function generate animated bouncing box
def on_draw(delta_time):
  
    # Start the render.
    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
  
    # Figure out if we hit the edge and need to reverse.
    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
  
  
# Set initial positions
on_draw.center_x = 100
on_draw.center_y = 50
on_draw.delta_x = 115
on_draw.delta_y = 130
  
  
# Driver code
  
# Open up our window
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.set_background_color(arcade.color.WHITE)
  
# Tell the computer to call the draw command at the specified interval.
arcade.schedule(on_draw, 1 / 80)
  
# Run the program
arcade.run()


Step2)这里我们将指定一些参数,稍后我们将在程序中使用这些参数来声明屏幕的宽度、高度和标题。

蟒蛇3

# Set up the constants
  
# Size of the screen
SCREEN_WIDTH = 720
SCREEN_HEIGHT = 480
SCREEN_TITLE = "Bouncing Box"
  
# Size of the rectangle
RECT_WIDTH = 50
RECT_HEIGHT = 50

Step3)定义绘制盒子的函数arcade.draw_rectangle_filled( ) 根据 delta 向量修改矩形位置。

蟒蛇3

# Explicilt function generate animated bouncing box
def on_draw(delta_time):
  
    # Start the render.
    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
  
    # Figure out if we hit the edge and need to reverse.
    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

步骤 4: - 现在,定义特定于函数的变量。此外,我们需要给它们初始值。然后这些值将在函数调用之间保持不变。

蟒蛇3

# Set initial positions
on_draw.center_x = 100
on_draw.center_y = 50
on_draw.delta_x = 115
on_draw.delta_y = 130

Step5)现在,最后一步是定义主函数。在其中,您需要定义背景颜色。

蟒蛇3

# Driver code
  
# Open up our window
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.set_background_color(arcade.color.WHITE)
  
# Tell the computer to call the draw command at the specified interval.
arcade.schedule(on_draw, 1 / 80)
  
# Run the program
arcade.run()

以下是上述方法的完整程序:

蟒蛇3

# Import required module
import arcade
  
  
# Set up the constants
  
# Size of the screen
SCREEN_WIDTH = 720
SCREEN_HEIGHT = 480
SCREEN_TITLE = "Bouncing Box"
  
# Size of the rectangle
RECT_WIDTH = 50
RECT_HEIGHT = 50
  
  
# Explicilt function generate animated bouncing box
def on_draw(delta_time):
  
    # Start the render.
    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
  
    # Figure out if we hit the edge and need to reverse.
    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
  
  
# Set initial positions
on_draw.center_x = 100
on_draw.center_y = 50
on_draw.delta_x = 115
on_draw.delta_y = 130
  
  
# Driver code
  
# Open up our window
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.set_background_color(arcade.color.WHITE)
  
# Tell the computer to call the draw command at the specified interval.
arcade.schedule(on_draw, 1 / 80)
  
# Run the program
arcade.run()

输出: