首页 > 解决方案 > 如何从图像创建精灵

问题描述

我有两个代码文件,一个是精灵分类,一个是。我的(基本)pygame 的主要背景。我的目标是加载一个 png 图像以在我的背景中使用。

sprite代码:

class GRASS (pygame.sprite.Sprite):

    def _init_(self):
        super()._init_()

        self.image = pygame.image.load("grass").convert()

        self = self.image.set_colorkey(WHITE)

        self.rect = self.image.get_rect()

主要代码:

from GRASS import GRASS

pygame.init()

screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
FPS = 60

screen.fill(WHITE)
pygame.draw.rect(screen, SKY_BLUE, [0, 0, 20000, 400],0)
pygame.draw.rect(screen, GREEN, [0, 400, 20000, 200],0)
cloud()
grassblock1 = GRASS
grassblock1.x = 10
grassblock1.y = 50

我的目标是让它显示在指定空间的屏幕上,但它根本不显示。我不确定是否需要我使用的所有代码。

标签: pythonpygame

解决方案


首先,将草文件重命名为Grass.png

然后,将扩展添加.png到图像草中

self.image = pygame.image.load("grass").convert()

推荐阅读