平台游戏有一个' str'对象没有属性' get_image'精灵错误,我不明白为什么

时间:2018-05-10 19:49:25

标签: python pygame

所以,我正在制作一种声音类型的游戏,我希望他只是在表面上显示有点漂浮,直到我知道如何让他做碰撞和跑来跑去(之后)这使它成为一个很好的声音类型的游戏,速度奖励,水平等)我不知道我的代码有什么问题,我已经尝试改变我的Player类和精灵类的序列。我已经玩了好两个小时,并想知道它的语法化原因。

  

' STR'对象没有属性' get_image'

     

文件,第103行,在main,playerOne =播放器(3,20,29,39)

import pygame
import random
import sys

WINDOWWIDTH = 800
WINDOWHEIGHT = 600
WINDOWSIZE = [WINDOWWIDTH,WINDOWHEIGHT]

WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)

COLORS = [WHITE,RED,GREEN,BLUE]

FPS = 30

#Classes Go Here
class SpriteSheet(object):

   def __init__(self, file_name):

       self.sprite_sheet = pygame.image.load(file_name).convert()

   def get_image(self, x, y, width, height, colorkey):

       image = pygame.Surface([width, height]).convert()
       image.blit(self.sprite_sheet, (0,0), (x,y, width, height))
       image.set_colorkey(colorkey)
       return image

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):

        super().__init__()

        #Sprite
        self.color_key = (72, 98, 78)
        sprite_sheet = 'spritesheet.png'
        self.image = sprite_sheet.get_image(x, y, width, height, self.color_key)
        self.rect = self.image.get_rect()
        # Location
        self.rect.x = 10
        self.rect.y = 100
        # Velocity
        self.change_x = 0
        self.change_y = 0

    def update(self):
        self.rect.x += self.change_x
        self.rect.y += self.change_y

def main():
    global COLORS, WINDOWSIZE, DISPLAYSURF, FPSCLOCK
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode(WINDOWSIZE)
    FPSCLOCK = pygame.time.Clock()
    #Initializations go here
    playerOne = Player(3, 20, 29, 39)
    active_sprite_list = pygame.sprite.Group()
    active_sprite_list.add(playerOne)
    while True:
        #Controls go here
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    playerOne.change_x += 50
                if event.key == pygame.K_LEFT:
                    playerOne.change_x -= 50
                if event.key == pygame.K_UP:
                    playerOne.change_y -= 50
                if event.key == pygame.K_DOWN:
                    playerOne.change_y += 50
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    playerOne.change_x = 0
                if event.key == pygame.K_LEFT:
                    playerOne.change_x = 0
                if event.key == pygame.K_UP:
                    playerOne.change_y = 0
                if event.key == pygame.K_DOWN:
                    playerOne.change_y = 0
            if event.type == pygame.QUIT:
                quit()



        DISPLAYSURF.fill(BLACK)

        #Game Logic goes here
        #Drawing new objects go here
        active_sprite_list.draw(DISPLAYSURF)
        FPSCLOCK.tick(FPS)
        pygame.display.flip()


#Additional Modules go here

def quit():
    pygame.quit()
    sys.exit()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

有关详细信息,请参阅我的评论。

变化:

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):

        super().__init__()

        #Sprite
        self.color_key = (72, 98, 78)
        sprite_sheet = 'spritesheet.png'
        self.image = sprite_sheet.get_image(x, y, width, height, self.color_key)

要:

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):

        super().__init__()

        #Sprite
        self.color_key = (72, 98, 78)
        sprite_sheet = SpriteSheet('spritesheet.png') # instantiate the class!
        self.image = sprite_sheet.get_image(x, y, width, height, self.color_key)