python - 如何从图像创建精灵
问题描述
我有两个代码文件,一个是精灵分类,一个是。我的(基本)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
我的目标是让它显示在指定空间的屏幕上,但它根本不显示。我不确定是否需要我使用的所有代码。
解决方案
首先,将草文件重命名为Grass.png。
然后,将扩展添加.png
到图像草中
self.image = pygame.image.load("grass").convert()
推荐阅读
- safari - 在移动设备上使用 Safari 的传入 MediaStream 将无法播放
- ionic-framework - 带有 JavaScript 的 ionic 5 警报按钮
- c# - 像多维数组一样填充 C# 数组
- python - 基于数据的拐点研究
- android - 如何在设置栏中添加隐私政策部分 android studio
- td-engine - 在指定或可用的数据库注释中使用 tdengine restful 错误
- linux - 如果使用 shell 脚本在先前运行的进程文件(nohup.out)中发生任何异常,我如何重新启动进程?
- android - 是否有可以通过 React Native/Expo 访问的每个设备的唯一编号/代码/ID
- c++ - c++ Eigen:当我必须遍历矩阵的每一行时如何让代码更优雅更快
- git - 如何访问和比较在 git pre receive 钩子中更改了哪个文件