我们使用一些必要的Cookie来确保网站正常运行。

我们使用可选的 Cookie,具体详情请参阅我们的Cookie政策,用于记住您的设置并了解您如何使用我们的网站。

制作你自己的像素编辑程序 | Wireframe #68

想绘制自己的精灵图吗?那么 Mark 为你带来了这款像素编辑器,内容来自新一期的《Wireframe》杂志。

每个游戏都需要图形,如果你像我们一样喜欢制作复古游戏,就需要一个程序来绘制它们。你可以使用 Microsoft Paint 之类的免费程序,但也可以编写自己的自定义像素编辑程序,我们在这里就要用 Python 和 Pygame Zero 来实现。设置基础绘图工具相对容易,之后你就可以开始为自己的游戏制作精灵图了。

Our Python and Pygame Zero pixel editor up and running.
我们的基础像素编辑程序。你可以通过各种方式将它扩展成自己喜欢的样子。

首先,我们需要设置一个绘图表面,让你能够查看和编辑单个像素。为此,我们需要一个可以放大图像的函数。如果我们使用 600 × 600 像素的区域,并以实际精灵图六倍的尺寸显示图像,这意味着最终得到的是 100 × 100 像素的图像。我们可以通过将空白精灵图声明为 Pygame Surface 来设置它。我们还要设置一个变量来定义用作透明色的颜色(本例中为品红色),并将 Surface 填充为该颜色,同时将空白精灵图的 colorkey 属性设置为透明色。现在可以开始设置绘图系统了。

在绘图区域上显示网格很有用,这样我们就能看到各个像素在哪里。我们需要绘制由 100 条水平线和 100 条垂直线组成的网格,但与其每帧重绘网格(那样会很慢),我们可以把网格绘制到一个新的图像表面上,然后在draw() function. This means that the grid will be drawn using a blit call and will be much quicker than drawing it line by line each frame. We create our grid image at the beginning of the program by making a Pygame Surface of 600 × 600 pixels. We set the colorkey property of the image to our chosen transparency colour, fill in the image with the transparent colour, and then start drawing the grid with two loops, one for the x direction and one for the y direction.

我们需要设置一些变量来跟踪当前鼠标坐标和当前绘图颜色等内容,初始绘图颜色可以设置为红色之类。我们传递给pygame.Color() function are red, green, and blue values, so (255, 0, 0) represents red. We also need to define which drawing tool we’re using, but more on that later.

接下来设置界面。如果使用标准的 800 × 600 Pygame Zero 窗口,将其中 600 像素用作绘图区域后,会剩下 200 像素宽的一列。如果绘图区域在左侧,我们可以把工具界面放在右侧。在这个工具区中,我们需要一个调色板、一些用于切换不同绘图模式的按钮、精灵图的实际大小预览,或许还要一个保存按钮,以便完成后保存作品。

A screenshot of paint package Deluxe Paint on the Commodore Amiga.
早期的像素绘画程序,比如 Amiga 上的 Deluxe Paint,提供了一系列用于创建背景和精灵图的工具。

调色板可能是界面中最简单的部分。我们只需要绘制一张包含各种不同颜色的图像。我们使用的调色板在所有颜色之间都有渐变,因此可以选择任意色度。如果你想坚持更明确的调色板,也可以使用颜色较少的图像。在工具区绘制调色板图像后,更改绘图颜色所要做的就是捕获on_mouse_down() event and test to see if the mouse is over our palette image. If it is, we change the curColour variable to the colour of the pixel under the mouse.

我们需要展示正在绘制的精灵图的实际样子,因此将我们为精灵图创建的 Surface 绘制到工具界面面板上。它最初是透明的,所以最好在精灵图周围绘制一个边框,以便看清它的位置。我们还希望同一图像在绘图区域中以六倍大小显示——为此可以使用pygame.transform.scale() function, which will return us a surface that is 600 pixels square but a larger copy of our sprite, which is 100 pixels square. We can blit this larger surface to the screen and then blit the grid surface we made earlier over the top.

我们可以通过使用on_mouse_move() event, and in doing so, we can then draw our brush that will change the pixel colours of our sprite. This will not only show where the pixels will be changed if we click the mouse button, but also what colour is going to be used. We’re going to set up two brushes: the first will represent one pixel in size, and the second a larger circle to fill in more pixels at once. As we track the brush around the drawing area, we’ll either draw a square six pixels by six pixels in the current colour, or if the second tool is used, a circle of radius 24, which will end up drawing a circle radius four pixels on our small sprite.

A chunk of Python and Pygame Zero code for our pixel editing program.
这是 Mark 像素编辑程序的一部分代码。要让它在你的系统上运行,首先需要安装 Pygame Zero。完整说明可在这里,完整代码清单在我们的你可以从 Estefannie 的….

现在我们就有了在绘图区域中移动画笔并看到它将要绘制位置的基本方法。如果点击鼠标按钮,我们希望程序更改精灵图中像素的颜色。为此,我们只需要捕获on_mouse_down event, and after checking that we’re over the drawing area, divide the mouse x and y coordinates by six to get the position on the small sprite, then either set the pixel to the current drawing colour at that location or draw a circle if we’re using the larger drawing tool. Because our drawing area is just a scaled-up version of the smaller sprite, the next time the draw() function is called, we will see the drawing area update.

on_mouse_down() 事件提供了鼠标按钮被按下的那一刻,但通常绘图时我们希望按住按钮并拖动鼠标来连续绘制线条。我们可以这样实现:当收到 on_mouse_down() 事件时,将名为 drawing 的变量设为 ‘True’,当收到 on_mouse_up() 事件时再将其设回 ‘False’。然后,当我们检测到 on_mouse_move() 事件且 drawing 变量为 True 时,就知道鼠标按钮正被按住,并在鼠标光标移动时持续设置其下方的像素。这意味着我们可以绘制线条,而不只是一次设置一个像素,或用较大的画笔画出圆形斑点。

现在我们需要一种在不同画笔大小之间切换的方法。我们可以在工具栏界面上创建一些按钮。为此,我们需要单个像素的按钮图像、较大的圆形画笔按钮图像,以及这两个按钮的高亮版本。在绘制工具栏区域时,我们检查 curTool 变量是否设置为 ‘1’。如果是,就绘制单像素按钮的高亮版本;如果不是,则绘制普通版本。第二个工具按钮也做同样处理,但检查 curTool 变量是否设置为 ‘2’。

我们还需要能够将像素擦除回透明状态。考虑到这一点,我们可以向工具箱中添加第三个按钮,即橡皮擦。我们可以通过一个变量来切换它的开关eraseOn much the same way as the other buttons, but these variables can work together so that the eraser can erase single pixels or larger circles much the same as when we’re drawing with colour. All we need to do if the eraser tool is selected when the mouse is clicked is draw with the transColour colour, which we used to set the colorkey property of the sprite.

现在,我们可以在放大的绘图区域上使用两种不同大小的画笔以任意颜色绘制,并擦除所犯的错误,同时还可以在工具栏界面中看到精灵图以实际大小显示。现在我们需要的只是保存作品的方法。最简单的方法是:在工具箱面板上再制作一个标签为 ‘Save’ 的按钮,然后等待鼠标点击该按钮。你可能想像其他按钮一样添加高亮状态,让用户看到它已被按下。当按钮被点击时,我们只需调用pygame.image.save() function and that will save our sprite image to the file name of our choosing. We’ll save the image as a PNG file as that will enable us to have transparent pixels. To make sure that the transparent colour’s handled correctly when saved, we’ll call the convert_alpha() function on our sprite surface to make the transparent pixels compatible with the PNG format.

至此,我们就有了一个基础像素编辑程序。你还可以添加更多工具和功能,使它更实用、更易用。你可以整合 Tkinter 模块来提供加载和保存对话框,或者使用其他形状作为绘图画笔。可能性无穷无尽,但我们把探索留给你自己。

获取你的 Wireframe #68

你可以在更多类似文章在 Wireframe 第68期中,可直接从树莓派出版社购买——我们支持全球配送。

The cover of Wireframe issue 68, featuring Mina The Hollower.
神勇鼠:Yacht Club Games 的 Mina The Hollower 是 Wireframe #68 的封面明星。

如果你更喜欢数字版杂志,你也可以下载 Wireframe 第68期作为免费PDF!

暂无评论

评论已关闭