2013-11-26 319 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