简体   繁体   English

PyGame,如何将图像绘制成矩形

[英]PyGame, How to draw image into rectangle

I have rectangles representing blocks in a game I am making. 我有矩形代表正在制作的游戏中的方块。 I know how to fill the rectangles with solid colours but how would I fill it with an image? 我知道如何用纯色填充矩形,但是如何用图像填充呢?

To render an image with PyGame you need to first load that image, then blit it to the screen at the specified coordinates. 要使用PyGame渲染图像,您需要先加载该图像,然后将其在指定坐标处调高到屏幕上。

The coordinates can continue to be the rects you're using for testing (if appropriate), or they can just be a pair of (x, y) coordinates for the topleft corner of the image. 坐标可以继续是您用于测试的矩形(如果适用),也可以只是图像左上角的一对(x, y)坐标。

pygame.Surface.blit(your_image, the_screen, rect_or_coordinates)

There are other optional arguments you can pass, but these are all you need to actually display the image once it has been loaded. 您可以传递其他可选参数,但是这些都是在加载图像后实际显示图像所需的全部。

The PyGame website has some code for a module named spritesheet.py which I use to just take care of the loading boilerplate code. PyGame网站上有一些名为spritesheet.py的模块的代码,我只用它来处理加载样板代码。 It then gives you a spreadsheet that is a PyGame Surface . 然后,它会给您一个电子表格,即PyGame Surface A quick Google search should point you right at the suggested code. 快速的Google搜索应该使您直接找到建议的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM