使用类更改Pygame中的玩家图像的问题

时间:2013-11-26 09:21:59

标签: python image class character pygame

在我正在开发的一个简单的RPG游戏中,我的角色遇到了我的通用'Player()'课程的问题。

我试图让它成为当我按下“向左”键向左移动时,角色不仅向左移动而且还将其图像更改为看起来它正在由我当前的代码向左运行(附在下方)不断崩溃有各种错误。

非常感谢任何帮助,

#######################################################
#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

1 个答案:

答案 0 :(得分:2)

修改
看起来您没有初始化pygame.display,因为此错误告诉您

pygame.error: cannot convert without pygame.display initialized

我会像这样在Player类中加载图像:

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