【问题标题】:Pygame won't draw or fill screenPygame 不会绘制或填充屏幕
【发布时间】:2017-08-09 08:48:44
【问题描述】:

我昨天发布了这个问题,但我不小心发布了错误的代码。该程序仍然无法正常工作。我正在尝试使用 pygame 制作游戏,而我的图片来自一个名为 Pixel Art Studio 的应用程序。但是,当我运行代码时,我没有收到任何错误消息,只是一个空白的白屏。我正在关注一本名为 Hello World 的 Python 编程书,但这是一个原始程序。请告诉我我做错了什么,这是我的代码。

import pygame, sys 
guyimages = ['swordsmanguy.pxm','swordsmanstabup.pxm','swordsmanstabstraight.pxm','swordsmanstabdown.pxm']
legsimages = ['swordsman.legs1.pxm','swordsman.legs2.pxm','swordsman.legs3.pxm','swordsman.legs2.3.pxm','swordsman.legs2.2.pxm']
class avatar(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprit.Sprite.__init__(self)
        self.image = pygame.image.load(guyimages["swordsmanguy.pxm"])
        self.rect = self.image.get_rect()
        self.rect.center = [250, 250]
        self.angle = 0

def animate():
    screen.fill([255,255,255])
    screen.blit(avatar.image, avatar.rect)
    pygame.display.flip

pygame.init()
screen = pygame.display.set_mode([960, 640])

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    animate()
pygame.quit()

【问题讨论】:

    标签: python


    【解决方案1】:

    当您设置 self.image 时,您会引用 guyimages 列表,就像它是字典一样。此外,您的代码中似乎缺少一些括号。在运行此脚本的终端/命令提示符窗口中是否出现错误?

    【讨论】:

    • 谢谢我实际上错过了这个错误信息: Traceback (last recent call last): File "C:\Python27\swordsmantester.py", line 25, in animate() File "C :\Python27\swordsmantester.py", line 14, in animate screen.blit(avatar.image, avatar.rect) AttributeError: type object 'avatar' has no attribute 'image' >>>我不知道怎么做代码看起来像代码,所以请告诉我,但我该如何解决这个问题? @马克塞尔
    • 我相信您需要将avatar.imageavatar.rect 更改为self.imageself.rectimagerect 不是属于您的头像类的静态变量。
    • 很抱歉像@maksel 这样打扰您,但这仍然无法解决任何问题。我刚刚收到此错误消息: Traceback(最近一次调用最后一次):文件“C:\Python27\swordsmantester.py”,第 26 行,在 animate() 文件“C:\Python27\swordsmantester.py”,行15、在animate screen.blit(self.image, self.rect) NameError: global name 'self' is not defined
    • 我刚刚注意到您需要缩进整个 animate 函数。现在,它不是avatar 类的方法,导致 self 未定义。
    • 我照你说的做了@maksel,现在我收到一条错误消息,指出未定义 animate() 函数,它仍然不会绘制或填充屏幕。顺便说一句,非常感谢你在这方面帮助我。
    【解决方案2】:

    您发布的代码中的动画后缺少括号。

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        animate #HERE MISSING PARENTHESES
    

    【讨论】:

    • 谢谢@Martin
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多