2013-11-26 49 views
0

我在我正在开发的简单RPG游戏中遇到了我的角色'Player()'类别的问题。使用类别改变Pygame中玩家影像的问题

我想使它当我按下'左'键向左移动时,角色不仅向左移动,而且还改变其图像,使其看起来像我现在的代码左边运行与各种错误崩溃。

任何帮助将非常感激,

####################################################### 
#StoD - player.py 
#By James Walker (trading as Ilmiont Software). 
#Copyright ©Ilmiont Software 2013. All rights reserved. 
# 
#This file contains the class, method and function 
#definitions for the player character in the Ilmiont 
#Software game 'StoD'. The player class itself is an 
#instance of a Pygame sprite class. 
####################################################### 

import pygame 
from pygame import * 

class Player(pygame.sprite.Sprite): #create the player class as an instance of a Pygame sprite 
    image_normal = pygame.image.load('images/player/normal.png').convert() 
    image_left = pygame.image.load('images/player/left.png').convert() 
    image_right = pygame.image.load('images/player/right.png').convert() 

    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) #init the Pygame sprite 
     self.image = image_normal #load the player image 
     self.rect = self.image.get_rect() #get a rect for the player 

    def update(self, UP, DOWN, LEFT, RIGHT, WIN_WIDTH, WIN_HEIGHT):  #move the player 
     if UP == True: #if true and not touching screen edge... 
      if self.rect.y > 0: 
       self.rect.y -= 5 
     if DOWN == True: 
      if self.rect.bottom < WIN_HEIGHT: 
       self.rect.y += 5 
     if LEFT == True: 
      self.image = image_left 
      if self.rect.x > 0: 
       self.rect.x -= 5 
     if RIGHT == True: 
      self.image = image_right 
      if self.rect.right < WIN_WIDTH: 
       self.rect.x += 5 
     else: 
      self.image = image_normal 

的错误是:

Traceback (most recent call last): File "D:\StoD\main.py", line 11, in <module> import player File "D:\StoD\player.py", 
line 15, in <module> class Player(pygame.sprite.Sprite): #create the player class as an instance of a Pygame sprite File "D:\StoD\player.py", 
line 16, in Player image_normal = pygame.image.load('images/player/normal.png').convert() 
pygame.error: cannot convert without pygame.display initialized 
+0

请张贴错误信息,包括完整的协议栈跟踪。 – Mailerdaimon

+0

回溯(最近通话最后一个): 文件 “d:\ STOD \ main.py”,第11行,在 进口玩家 文件 “d:\ STOD \ player.py”,第15行,在 类Player(pygame.sprite.Sprite):#作为Pygame精灵的一个实例创建玩家类 玩家 中的第16行文件“D:\ StoD \ player.py”image_normal = pygame.image.load('images /player/normal.png')。convert() pygame.error:不能转换没有pygame.display初始化 – Ilmiont

+0

你能否用你在评论中提供的错误信息更新你的问题? –

回答

2

编辑:
好像你还没有初始化pygame.display因为这个错误告诉你

pygame.error: cannot convert without pygame.display initialized 

我将加载在播放器类在相似图片:

class Player(pygame.sprite.Sprite): #create the player class as an instance of a Pygame sprite 
    image_normal = [] 
    image_left = [] 
    image_right = [] 

    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) #init the Pygame sprite 
     #load all images 
     self.image_normal = pygame.image.load('images/player/normal.png').convert() 
     self.image_left = pygame.image.load('images/player/left.png').convert() 
     self.image_right = pygame.image.load('images/player/right.png').convert() 
     self.image = self.image_normal #load the player image 

     self.rect = self.image.get_rect() #get a rect for the player 

,如果你需要一个不同的图像执行下列操作之一:

self.image = self.image_left 
self.image = self.image_right 
self.image = self.image_normal 
+0

它现在说全局变量'image_normal'还没有被定义......但是在__init__之外定义意味着什么都不起作用...... [编辑]这个错误实际上来自更新函数,它改变图像并移动字符,我现在应该加载图像吗? – Ilmiont

+0

我用pygame.init()初始化main.py中的显示,然后调用player = Player()来创建我的播放器。 – Ilmiont

+0

我得到'自我'没有定义错误的前四行的初始声明... – Ilmiont