我在我正在開發的簡單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
請張貼錯誤信息,包括完整的協議棧跟蹤。 – Mailerdaimon
回溯(最近通話最後一個): 文件 「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
你能否用你在評論中提供的錯誤信息更新你的問題? –