2017-08-07 73 views
0

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

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() 

回答

0

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

+0

谢谢我居然错过了这个错误信息:回溯(最近通话最后一个): 文件 “C:\ Python27 \ swordsmantester.py” 25行,在 动画() 文件“ C:\ Python27 \ swordsmantester.py“,第14行,动画 screen.blit(avatar.image,avatar.rect) AttributeError:type object'avatar'has no attribute'image' >>>我不是确定如何让代码看起来像代码,所以请告诉我,但我该如何解决这个问题? @maksel –

+0

我相信你需要将'avatar.image'和'avatar.rect'改成'self.image'和'self.rect'。 'image'和'rect'不是属于你的头像类的静态变量。 – maksel

+0

我真的很抱歉打扰你这个@maksel,但这仍然不能解决任何问题。我只是得到这个错误信息:Traceback(最近调用最后一次): 文件“C:\ Python27 \ swordsmantester.py”,第26行,在 animate() 文件“C:\ Python27 \ swordsmantester.py”,第15行,动画 screen.blit(self.image,self.rect) NameError:全局名称'self'没有定义 –

0

在发布的代码中生成动画后,您缺少括号。

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

谢谢@马丁 –