'Issues with changing images for players in Pygame using classes
I have run into issues with my generic Player class for my character in a simple RPG game I am developing.
I am trying to make it so that when I press the 'left' key to move left the character not only goes left but also changes its image to look like it is running left by my current code (attached below) keeps crashing with various errors.
import pygame
from pygame import *
class Player(pygame.sprite.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)
self.image = image_normal
self.rect = self.image.get_rect()
def update(self, UP, DOWN, LEFT, RIGHT, WIN_WIDTH, WIN_HEIGHT):
"""Move the player."""
if UP == True:
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
The Error is:
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):
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
