我在我正在开发的简单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
你能否用你在评论中提供的错误信息更新你的问题? –