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安装文档 。
简单绘图
您可以打开一个窗口并仅需几行代码即可创建简单的图形。 让我们创建一个绘制笑脸的示例,如下图所示:
![微笑的脸图像](https://i-blog.csdnimg.cn/blog_migrate/e2ca70f30c4321df77987df3c830df1c.png)
下面的脚本显示了如何使用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