arcade 物理系统_如何使用Python和Arcade库创建2D游戏

arcade 物理系统

对于那些学习编程的人来说, Python是一种出色的语言,它对于想要“完成工作”并且不花很多时间在样板代码上的人来说是完美的语言。 Arcade是一个用于创建2D视频游戏的Python库,该库易于开始使用,并且在您获得经验时非常有能力。 在本文中,我将解释如何开始使用Python和Arcade对视频游戏进行编程。

在使用PyGame库教了学生之后,我开始在Arcade上进行开发。 我使用PyGame进行了近十年的现场教学,并开发了ProgramArcadeGames.com进行在线教学。 PyGame很棒,但是最终我觉得我在浪费时间去掩盖那些从未修复过的错误

我担心教诸如事件循环之类的事情 ,而这不再是我们编写代码的方式。 我在整个章节中解释了为什么将y坐标反转。 因为PyGame很少更新,并且它基于旧的SDL 1库,而不是像OpenGL这样的更现代的库,所以我对未来并不抱太大希望。

安装

像许多其他软件包一样,可以通过PyPi使用Arcade,这意味着您可以使用pip命令(或pipenv命令)安装Arcade。 如果您已经安装了Python,则可能只需在Windows上打开命令提示符并键入:



   
   
pip install arcade

或在MacOS和Linux上输入:



   
   
pip3 install arcade

有关更详细的安装说明,您可以参考Arcade安装文档

简单绘图

您可以打开一个窗口并仅需几行代码即可创建简单的图形。 让我们创建一个绘制笑脸的示例,如下图所示:

微笑的脸图像

下面的脚本显示了如何使用Arcade的绘图命令来执行此操作。 注意,您不需要知道如何使用 ,甚至不需要定义函数 。 带有快速视觉反馈的编程非常适合想要开始学习编程的任何人。



   
   
import arcade

# Set constants for the screen size
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

# Open the window. Set the window title and dimensions (width and height)
arcade. open_window ( SCREEN_WIDTH , SCREEN_HEIGHT , "Drawing Example" )

# Set the background color to white.
# For a list of named colors see:
# http://arcade.academy/arcade.color.html
# Colors can also be specified in (red, green, blue) format and
# (red, green, blue, alpha) format.
arcade. set_background_color ( arcade. color . WHITE )

# Start the render process. This must be done before any drawing commands.
arcade. start_render ( )

# Draw the face
x = 300
y = 300
radius = 200
arcade. draw_circle_filled ( x , y , radius , arcade. color . YELLOW )

# Draw the right eye
x = 370
y = 350
radius = 20
arcade. draw_circle_filled ( x , y , radius , arcade. color . BLACK )

# Draw the left eye
x = 230
y = 350
radius = 20
arcade. draw_circle_filled ( x , y , radius , arcade. color . BLACK )

# Draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
arcade. draw_arc_outline ( x , y , width , height , arcade. color . BLACK , start_angle , end_angle , 10 )

# Finish drawing and display the result
arcade. finish_render ( )

# Keep the window open until the user hits the 'close' button
arcade. run ( )

使用功能

当然,在全局上下文中编写代码不是一种好形式。 值得庆幸的是,通过使用函数来改进程序很容易。 在这里,我们可以看到使用函数在特定(x,y)位置绘制松树的示例:



   
   
def draw_pine_tree ( x , y ) :
    """ This function draws a pine tree at the specified location. """
   
    # Draw the triangle on top of the trunk.
    # We need three x, y points for the triangle.
    arcade. draw_triangle_filled ( x + 40 , y ,       # Point 1
                                x , y - 100 ,       # Point 2
                                x + 80 , y - 100 , # Point 3
                                arcade. color . DARK_GREEN )

    # Draw the trunk
    arcade. draw_lrtb_rectangle_filled ( x + 30 , x + 50 , y - 100 , y - 140 ,
                                      arcade. color . DARK_BROWN )

有关完整示例,请参见带有功能的图纸

类和功能

经验更丰富的程序员将知道,现代图形程序首先将图形信息加载到图形卡上,然后要求图形卡稍后批量绘制。 Arcade也支持这一点。 分别绘制10,000个矩形大约需要0.800秒。 批量提取它们只需不到0.001秒的时间。

窗口类

较大的程序通常会从Window类派生,或使用装饰器 。 这使程序员可以编写代码来处理绘图,更新和处理来自用户的输入。 下面是用于启动基于Window的程序的模板。



   
   
import arcade

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600


class MyGame ( arcade. Window ) :
    """ Main application class. """

    def __init__ ( self , width , height ) :
        super ( ) . __init__ ( width , height )

        arcade. set_background_color ( arcade. color . AMAZON )

    def setup ( self ) :
        # Set up your game here
        pass

    def on_draw ( self ) :
        """ Render the screen. """
        arcade. start_render ( )
        # Your drawing code goes here

    def update ( self , delta_time ) :
        """ All the logic to move, and the game logic goes here. """
        pass


def main ( ) :
    game = MyGame ( SCREEN_WIDTH , SCREEN_HEIGHT )
    game. setup ( )
    arcade. run ( )


if __name__ == "__main__" :
    main ( )

Window类具有几种方法,您的程序可以覆盖这些方法来为程序提供功能。 以下是一些最常用的方法:

  • on_draw :所有绘制屏幕的代码都在这里。
  • update :所有用于移动物品并执行游戏逻辑的代码都在此处。 每秒大约称为60次。
  • on_key_press :按下按键时处理事件,例如为玩家提供速度。
  • on_key_release :释放键时处理,在这里您可能会阻止播放器移动。
  • on_mouse_motion :每次鼠标移动时都会调用它。
  • on_mouse_press :按下鼠标按钮时调用。
  • set_viewport :当您拥有比一个屏幕上可以看到的世界大得多的世界时,此功能可用于滚动游戏。 调用set_viewport允许程序员设置该世界的哪个部分当前可见。

精灵

Sprite是在Arcade中创建2D位图对象的简单方法。 Arcade的方法可以轻松绘制,移动和设置精灵动画。 您还可以轻松地使用精灵检测对象之间的碰撞。

创建一个精灵

从图形创建Arcade Sprite类的实例很容易。 程序员只需要图像的文件名即可基于其精灵,还可以选择一个数字来按比例放大或缩小图像。 例如:



   
   
SPRITE_SCALING_COIN = 0.2

coin = arcade. Sprite ( "coin_01.png" , SPRITE_SCALING_COIN )

这段代码将使用存储在coin_01.png的图像创建一个精灵。 图像将缩小到其原始高度和宽度的20%。

精灵收集硬币

精灵列表

精灵通常被组织成列表。 这些列表使管理精灵更加容易。 列表中的精灵将使用OpenGL批量绘制精灵。 下面的代码设置了一个具有玩家的游戏,以及一堆硬币供玩家收集。 我们使用两个列表,一个用于玩家,一个用于硬币。



   
   
def setup ( self ) :
    """ Set up the game and initialize the variables. """

    # Create the sprite lists
    self . player_list = arcade. SpriteList ( )
    self . coin_list = arcade. SpriteList ( )

    # Score
    self . score = 0

    # Set up the player
    # Character image from kenney.nl
    self . player_sprite = arcade. Sprite ( "images/character.png" , SPRITE_SCALING_PLAYER )
    self . player_sprite . center_x = 50 # Starting position
    self . player_sprite . center_y = 50
    self . player_list . append ( self . player_sprite )

    # Create the coins
    for i in range ( COIN_COUNT ) :

        # Create the coin instance
        # Coin image from kenney.nl
        coin = arcade. Sprite ( "images/coin_01.png" , SPRITE_SCALING_COIN )

        # Position the coin
        coin. center_x = random . randrange ( SCREEN_WIDTH )
        coin. center_y = random . randrange ( SCREEN_HEIGHT )

        # Add the coin to the lists
        self . coin_list . append ( coin )

我们可以轻松地绘制硬币列表中的所有硬币:



   
   
def on_draw ( self ) :
    """ Draw everything """
    arcade. start_render ( )
    self . coin_list . draw ( )
    self . player_list . draw ( )

检测精灵冲突

函数check_for_collision_with_list允许我们查看一个精灵是否运行到列表中的另一个精灵。 我们可以使用它来查看玩家精灵与之接触的所有硬币。 使用简单的for循环,我们可以摆脱游戏中的硬币并提高得分。



   
   
def update ( self , delta_time ) :
    # Generate a list of all coin sprites that collided with the player.
    coins_hit_list = arcade. check_for_collision_with_list ( self . player_sprite , self . coin_list )

    # Loop through each colliding sprite, remove it, and add to the score.
    for coin in coins_hit_list:
        coin. kill ( )
        self . score + = 1

有关完整示例,请参见collect_coins.py

游戏物理

许多游戏都包含某种物理学。 最简单的是自上而下的程序,可防止播放器穿过墙壁。 平台开发人员通过重力和移动平台增加了更多的复杂性。 有些游戏使用具有质量,摩擦力,弹簧等的完整2D物理引擎。

自上而下的游戏

春天搬到墙上

对于简单的自上而下的游戏,街机程序需要玩家(或其他任何人)无法穿过的墙壁列表。 我通常将其称为wall_list 。 然后在Window类的设置代码中使用以下命令创建一个物理引擎:



   
   
self . physics_engine = arcade. PhysicsEngineSimple ( self . player_sprite , self . wall_list )

player_sprite一个运动矢量,它具有两个属性change_xchange_y 。 一个简单的例子就是让玩家随键盘移动。 例如,这可能在Window类的自定义子级中:



   
   
MOVEMENT_SPEED = 5

def on_key_press ( self , key , modifiers ) :
    """Called whenever a key is pressed. """

    if key == arcade. key . UP :
        self . player_sprite . change_y = MOVEMENT_SPEED
    elif key == arcade. key . DOWN :
        self . player_sprite . change_y = -MOVEMENT_SPEED
    elif key == arcade. key . LEFT :
        self . player_sprite . change_x = -MOVEMENT_SPEED
    elif key == arcade. key . RIGHT :
        self . player_sprite . change_x = MOVEMENT_SPEED

def on_key_release ( self , key , modifiers ) :
    """Called when the user releases a key. """

    if key == arcade. key . UP or key == arcade. key . DOWN :
        self . player_sprite . change_y = 0
    elif key == arcade. key . LEFT or key == arcade. key . RIGHT :
        self . player_sprite . change_x = 0

尽管该代码设置了播放器的速度,但它不会移动播放器。 在Window类的update方法中,调用physics_engine.update()将移动播放器,但不会移动穿过墙壁。



   
   
def update ( self , delta_time ) :
    """ Movement and game logic """

      self . physics_engine . update ( )

有关完整示例,请参见sprite_move_walls.py

平台游戏

精灵平铺的地图

移至侧视图Platformer相当容易。 程序员只需要将物理引擎切换到PhysicsEnginePlatformer并添加重力常数即可。



   
   
self . physics_engine = arcade. PhysicsEnginePlatformer ( self . player_sprite ,
                                                      self . wall_list ,
                                                     gravity_constant = GRAVITY )

您可以使用Tiled之类的程序来放置构成关卡的图块/块。

有关示例,请参见sprite_tiled_map.py

对于完整的2D物理,您可以集成PyMunk库。

通过例子学习

最好的学习方法之一就是以身作则。 Arcade库中有很多示例程序 ,人们可以借鉴这些示例程序来创建游戏。 这些示例均显示了多年来学生在我的课堂或在线上要求的游戏概念。

安装Arcade后,运行这些演示中的任何一个都非常容易。 每个样本在程序的开头都有注释,您可以在命令行上键入以下命令来运行样本,例如:



   
   
python -m arcade.examples.sprite_moving_platforms

摘要

Arcade使您可以使用易于理解的代码开始对图形和游戏进行编程。 许多新的程序员起步不久就创造了出色的游戏 。 试试看!


要了解更多信息,请在2018年PyCon克利夫兰参加Paul Vincent Craven的演讲`` 使用Arcade轻松进行2D游戏创作''

翻译自: https://opensource.com/article/18/4/easy-2d-game-creation-python-and-arcade

arcade 物理系统

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learn and use Python and PyGame to design and build cool arcade games. In Program Arcade Games: With Python and PyGame, Second Edition, Dr. Paul Vincent Craven teaches you how to create fun and simple quiz games; integrate and start using graphics; animate graphics; integrate and use game controllers; add sound and bit-mapped graphics; and build grid-based games. After reading and using this book, you'll be able to learn to program and build simple arcade game applications using one of today's most popular programming languages, Python. You can even deploy onto Steam and other Linux-based game systems as well as Android, one of today's most popular mobile and tablet platforms. You'll learn: How to create quiz games How to integrate and start using graphics How to animate graphics How to integrate and use game controllers How to add sound and bit-mapped graphics How to build grid-based games Audience This book assumes no prior programming knowledge. Table of Contents Chapter 1: Before Getting Started… Chapter 2: Create a Custom Calculator Chapter 3: What Is a Computer Language? Chapter 4: Quiz Games and If Statements Chapter 5: Guessing Games with Random Numbers and Loops Chapter 6: Introduction to Graphics Chapter 7: Back to Looping Chapter 8: Introduction to Lists Chapter 9: Introduction to Animation Chapter 10: Functions Chapter 11: Controllers and Graphics Chapter 12: Bitmapped Graphics and Sound Chapter 13: Introduction to Classes Chapter 14: Introduction to Sprites Chapter 15: Libraries and Modules Chapter 16: Searching Chapter 17: Array-Backed Grids Chapter 18: Sorting Chapter 19: Exceptions Chapter 20: Recursion Chapter 21: Formatting Chapter 22: Exercises
### 回答1: 我可以帮你使用Pythonarcade创建一个平台类游戏arcade提供了一个简单而强大的API,可以帮助你创建2D游戏。它还有一个文档,可以帮助你开始创建游戏,并且拥有许多示例代码,可以让你学习如何使用这个。 ### 回答2: 使用Pythonarcade可以很方便地制作平台类游戏arcade提供了许多内置函数和类,用于处理游戏循环、精灵、碰撞检测等常见的游戏开发任务。 首先,我们需要创建一个游戏窗口。通过arcade提供的Window类,我们可以创建一个指定大小的窗口,并设置背景颜色。 接下来,我们可以创建角色或者其他游戏中的物体,这些物体被称为精灵。arcade提供了Sprite类用于创建精灵。我们可以设置精灵的外观、位置、大小等属性。 在游戏循环中,我们可以监听用户输入,例如键盘按键、鼠标点击等事件。通过事件处理函数,我们可以根据用户的操作来更新精灵的状态。 在平台类游戏中,通常会涉及到角色的跳跃、移动、碰撞检测等功能。arcade提供了一些内置函数和方法来处理这些功能。例如,我们可以使用移动函数来控制角色的水平和垂直移动。我们还可以使用碰撞检测函数来判断角色是否与其他物体发生了碰撞。 此外,arcade还可以处理音效、动画等高级功能,以及绘制背景、地图等元素。它还提供了一些内置的工具函数,用于计算距离、旋转等常见操作。 总的来说,使用Pythonarcade可以实现一个简单而又有趣的平台类游戏。通过利用arcade提供的丰富功能,可以很方便地创建精灵、处理用户输入、实现碰撞检测等游戏开发任务。无论是新手还是有经验的开发者,都可以轻松上手使用arcade制作自己的平台类游戏。 ### 回答3: 使用Pythonarcade可以很方便地制作平台类游戏。首先,我们需要导入arcade。然后创建一个继承自arcade.Window的类,作为游戏窗口。 在窗口类中,我们可以定义一些属性,比如玩家的位置和角色模型等。然后,我们可以使用arcade提供的绘图函数来渲染游戏界面。 接着,我们可以定义一些事件处理函数,比如按键事件或鼠标事件。根据不同的事件,我们可以更新游戏状态,比如移动玩家或触发一些特殊的效果。 在游戏循环中,我们可以使用arcade提供的定时器函数来设定游戏帧率。每一帧,我们可以调用draw函数来绘制游戏界面,并在需要时更新游戏状态。 此外,我们可以使用arcade提供的物理引擎功能,比如重力或碰撞检测,来增加游戏的真实性和挑战性。 最后,当玩家达到游戏的胜利条件或失败条件时,我们可以显示游戏结束画面,并提供一些重新开始或退出游戏的选项。 总的来说,使用Pythonarcade可以很方便地制作一个平台类游戏。通过定义游戏窗口、绘制游戏界面、处理事件和更新游戏状态等步骤,我们可以创建一个有趣且具有挑战性的游戏体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值