简体   繁体   English

如何在(pygame)中绘制图像

[英]How to draw an image in (pygame)

I want to replace this draw rectangle function, and instead let it produce an image.我想替换这个绘制矩形函数,而是让它生成一个图像。

Rectangle Function:矩形函数:

pygame.draw.rect(pygame.display.set_mode((800,600)), black, [0,0, 100, 100])

Looking for something on the lines of this:寻找与此相关的内容:

pygame.draw.image(**coordinatesX**, **coordinatesY**, **imagefile.jpg**)

To load an image, you need the file stored, and you load it using:要加载图像,您需要存储该文件,然后使用以下命令加载它:

pygame.image.load('example.png')

Now your code should look like:现在你的代码应该是这样的:

pygame.image.load('example.png')

pygame.display.set_mode((800, 600))

so first you have to load the image using image = pygame.image.load() and then use screen.blit(image) to draw it on the screen.所以首先你必须使用image = pygame.image.load() screen.blit(image)加载图像,然后使用screen.blit(image)在屏幕上绘制它。 For this to work you should define the screen at the beginning not in the draw function like you have done above.为此,您应该在开始时定义屏幕,而不是像上面那样在绘图函数中定义。 Just screen = pygame.display.set_mode((800,600)) should work.只是screen = pygame.display.set_mode((800,600))应该可以工作。

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

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