如何在 Pygame 中显示精灵? [英] How to Display Sprites in Pygame?

查看:125
本文介绍了如何在 Pygame 中显示精灵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个关于 PyGame 中精灵的快速问题,我按照下面的代码加载了我的图像,我只是想知道如何在 PyGame 中显示精灵,比如绘制矩形或圆形.无论如何,我不想让它表现得如此.我想我使用了 blit 命令,但我不确定,而且我在网上找不到太多.

This is just a quick question regarding sprites in PyGame, I have my image loaded as in the code below, and I'm just wondering how to display the sprite in PyGame like drawing a rectangle or circle. I don't want to have it behave in anyway. I think I use a blit command, but I'm not sure and I'm not finding much online.

这是我加载它的图像代码.

Here's my image code for loading it.

Star = pygame.image.load('WhiteStar.png').convert_alpha()

你可以只提供一个加载精灵的大纲.我只是想显示它.

You could just provide an outline for loading a sprite. I simply want to display it.

推荐答案

使用 blit 绘制图像.实际上 blit 绘制了一个 表面到另一个.因此,您需要将图像blit 到与显示器关联的Surface 上.
您需要指定图像在目标上blit 的位置.位置可以由定义左上角位置的一对坐标指定.或者也可以用矩形指定,只考虑矩形的左上点:

Use blit to draw an image. Actually blit draws one Surface onto another. Hence you need to blit the image onto the Surface associated to the display.
You need to specify the position where the image is blit on the target. The position can be specified by a pair of coordinates that define the top left position. Or it can be specified by a rectangle, only taking into account the top left point of the rectangle:

screen = pygame.dispaly.set_mode((width, height))
star = pygame.image.load('WhiteStar.png').convert_alpha()

# [...]

while run:
    # [...]

    screen.blit(star, (x, y))

    # [...]


在以下情况下使用 pygame.Rect您想将曲面的中心放置在特定点上.pygame.Surface.get_rect.get_rect() 返回一个具有 Surface 对象大小的矩形,它总是从 (0, 0) 开始,因为 Surface 对象没有位置.矩形的位置可以通过关键字参数指定.例如,矩形的中心可以用关键字参数 center 指定.这些关键字参数应用于 pygame.Rect 的属性 在返回之前(参见 pygame.Rect 获取关键字参数的完整列表):


Use a pygame.Rect when you want to place the center of a surface at a specific point. pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. The position of the rectangle can be specified by a keyword argument. For example, the center of the rectangle can be specified with the keyword argument center. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect for a full list of the keyword arguments):

screen.blit(star, star.get_rect(center = (x, y)))

这篇关于如何在 Pygame 中显示精灵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆