Pygame, Python 2.7, AttributeError: объект типа «newPlayer» не имеет атрибута «прямой», но я объявил прямоточный атрибут

Итак, я создаю объект игрока в pygame, и у меня есть загрузчик спрайтов, загружающий изображение, устанавливающий объект для этого изображения и получающий от него прямоугольный объект.

Однако, когда я пытаюсь проверить это в основном, я получаю эту ошибку

AttributeError: type object 'newPlayer' has no attribute 'rect'

Но у меня есть эта строка в моем классе newPlayer

self.rect = self.image.get_rect()

Что происходит? У меня был почти идентичный код с другими объектами в игре, и они работали без проблем.

Полный код:

import pygame

# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
red      = ( 255,   0,   0)

#initialize pygame
pygame.init()

#set the height and width of the screen
width = 800
height = 480

mainScreen = pygame.display.set_mode([width,height])

#A list of all of the sprites in the game
all_sprites_list = pygame.sprite.Group()


def sprite_sheet_load(colorKey, spriteLocX, spriteLocY, spriteSizeX, spriteSizeY, fileName):
    '''Purpose: to extract a sprite from a sprite sheet at the chosen location'''
    '''credit to SO user hammyThePig for original code'''

    sheet = pygame.image.load(fileName).convert() #loads up the sprite sheet. convert makes sure the pixel format is coherent
    sheet.set_colorkey(colorKey) #sets the color key

    sprite = sheet.subsurface(pygame.Rect(spriteLocX, spriteLocY, spriteSizeX, spriteSizeY)) #grabs the sprite at this location

    return sprite

class newPlayer(pygame.sprite.Sprite):
    '''class that builds up the player'''
     #constructor function
    def __init__(self): #create a self variable to do stuff

        #call up the parent's constructor
        pygame.sprite.Sprite.__init__(self)

        img = "mainCharacterFinal.png"

        #size of each sprite
        sprite_sizeX = 35
        sprite_sizeY = 37

        #List of images for different types of movement
        self.imagesLeft = []

        self.imagesRight = []

        self.imagesUp = []

        self.imagesDown = []

        #these two variables go and help reset the position variables of the sprites
        xInit = 35
        yInit = 37

        #inital positions of sprites on the sheet
        positionX = 0
        positionY = 0

        colorKey = white #colorKey to pass to the function

        self.imagesUp.append(sprite_sheet_load(black, positionX, positionY, sprite_sizeX, sprite_sizeY, img))


        #the best image to use by default is the one that has the player facing the screen.
        self.image=self.imagesUp[0]


        self.rect = self.image.get_rect()

newplayer = newPlayer()
all_sprites_list.add(newplayer)
newPlayer.rect.x = 300
newPlayer.rect.y = 300

#a conditional for the loop that keeps the game running until the user Xes out
done = False

#clock for the screen updates
clock = pygame.time.Clock()


while done==False:
    for event in pygame.event.get(): #user did something
        if event.type == pygame.QUIT: #if the user hit the close button
                done=True

    mainScreen.fill(white)#makes the background white, and thus, the white part of the images will be invisible

    #draw the sprites
    all_sprites_list.draw(mainScreen)

    #limit the game to 20 fps
    clock.tick(20)

    #update the screen on the regular
    pygame.display.flip()

pygame.quit()

person user1768884    schedule 23.10.2013    source источник
comment
Пожалуйста, опубликуйте полную трассировку. Мы должны знать, какая из примерно 100 строк вызывает ошибку.   -  person    schedule 23.10.2013


Ответы (1)


Вы должны написать

    newplayer 

вместо newPlayer в соответствующем месте. Это связано с тем, что rect является атрибутом экземпляра класса, а не самого класса, когда вы пытаетесь написать

    newPlayer.rect 

вы пытаетесь получить доступ к атрибуту rect класса newPlayer, которого там нет, поэтому вам нужно написать

    newplayer.rect

пытаться

    newplayer.rect.x = 300
    newplayer.rect.y = 300

Проверьте следующую ссылку: - Python: разница между атрибутами класса и экземпляра

person Pratik Singhal    schedule 23.10.2013
comment
Фу!!! Я знал, что это будет какая-то глупая опечатка/оплошность, но по какой-то причине я этого не видел, и мне нужна была вторая пара глаз. - person user1768884; 23.10.2013
comment
Это может случиться даже с лучшими программистами. Вы можете принять ответ, если он решил проблему для вас. - person Pratik Singhal; 23.10.2013
comment
Я собирался раньше, но он хотел, чтобы я подождал несколько минут, и в то время у меня было другое обязательство. Взят под опеку. - person user1768884; 23.10.2013
comment
@ user1768884 Нет проблем - person Pratik Singhal; 24.10.2013