“TypeError:参数1必须是pygame.Surface,而不是方法”错误Python

时间:2018-03-06 21:00:40

标签: python pygame

我正在制作游戏,似乎在这里遇到了一些问题。这是我第一次使用精灵表,所以如果我的错误很简单,我会道歉。

我提前为大量代码道歉,但不幸的是我相信问题可能出现在这段代码的任何地方。我收到的错误说“TypeError:参数1必须是pygame.Surface,而不是方法 “。这是指all_sprites.draw(屏幕)命令。我知道屏幕格式正确,所以不是那样。我的老师说这可能是任何一个精灵。其中一个格式不正确,或者没有正确地与图像进行交互。如果您找到我的问题的解决方案,请告诉我,并再次为代码的大部分内容道歉。 谢谢!

(我将不同的文件标记为btw,gsettings,gmain和gsprites。你可以看到我在导入中使用它们,应该不是什么大问题,只是想我会澄清)

完全异常`

Traceback (most recent call last):
  File "/Users/AddisonWeatherhead/PycharmProjects/PyGame/Game 2/gmain.py", line 191, in <module>
    game_play()
  File "/Users/AddisonWeatherhead/PycharmProjects/PyGame/Game 2/gmain.py", line 128, in game_play
    all_sprites.draw(screen)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pygame/sprite.py", line 475, in draw
    self.spritedict[spr] = surface_blit(spr.image, spr.rect)
TypeError: argument 1 must be pygame.Surface, not method

Process finished with exit code 1

gsettings

WHITE = (255, 255, 255)
BLACK = (0, 0, 0,)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
GRAY = (180, 180, 180)
MAROON = (128, 0, 0)
NAVY = (0, 0, 128)
YELLOW = (255, 255, 0)
LIGHT_GRAY = (250, 250, 250)
LIGHT_BLUE = (144, 195, 212)

DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 800
FPS=60

gsprites

class sprite_sheet():
    def __init__ (self, filename):
        self.spritesheet = pygame.image.load(filename)

    def get_image(self, x, y, width, height):
        image = pygame.Surface((width, height))
        image.blit(self.spritesheet, (0,0), (x, y, width, height))
        return image



class Player(pygame.sprite.Sprite):
    def __init__(self, hero_left_list, hero_right_list, left_shoot, right_shoot):
        pygame.sprite.Sprite.__init__(self)

        self.hero_left_list=hero_left_list
        self.hero_right_list=hero_right_list
        self.left_shoot=left_shoot
        self.right_shoot=right_shoot

        self.image = self.hero_right_list[0]
        self.rect=self.image.get_rect()
        self.rect.x=self.rect.x
        self.rect.y=self.rect.y

        self.changex=0 #Horizontal movement speed
        self.changey=0 #Vertical movement speed

        self.current_frame=0
        self.delay=50 #In millisecond, this will be the delay BETWEEN each frame of a spritesheet (character walking, explosion exploding, etc)
        self.last=pygame.time.get_ticks() #gets the time in milliseconds since pygame.init() was called

        self.running_right=False
        self.running_left=False

    def update(self):
        self.rect.x+=self.changex
        self.rect.y+=self.changey



        if pygame.key.get_pressed()[pygame.K_RIGHT]:
            self.now=pygame.time.get_ticks() #also gets the time in milliseconds since pygame.init() was called, however this time its LATER than self.delay
            self.running_right=True
            self.changex=5

            if self.now - self.last >self.delay: #Confirming that at least 5 milliseconds has passed since the last call of the function
                self.last=self.now
                self.current_frame=(self.current_frame+1)%(int(len(self.hero_right_list)))
                self.image=self.hero_right_list[self.current_frame]

            self.image=sprite_sheet("hero.png").get_image

        elif pygame.key.get_pressed()[pygame.K_LEFT]:
            self.running_left=True
            self.changey=5

            self.image=sprite_sheet.get_image("hero.png", )

        elif pygame.key.get_pressed()[pygame.K_UP]:
            self.running_left=False
            self.running_right=False
            self.image=sprite_sheet.get_image("hero.png")

戈麦恩

import pygame
from gsettings import *
from gsprites import *
import time

pygame.init()


clock = pygame.time.Clock()
##########################################################################################
def score(score, lives):
    background = sprite_sheet("background.jpg").get_image(0, 0, 800, 800)
    screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
    screen.blit(background, [0,0])

    font = pygame.font.SysFont('Impact', 40, True, False)
    text = font.render(('Score'+str(score)), True, WHITE)
    screen.blit(text, (200, 50))

##########################################################################################

def game_start():
    screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
    text = "Press enter to play"
    font = pygame.font.SysFont('Impact', 50, True, False)
    text_render=font.render(text, True, BLACK)

    background=sprite_sheet("background.jpg").get_image(0, 0, 800, 800)
    screen.blit(background, [0, 0])

    started=False
    while started == False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            quit()

        sprite_sheet("background.jpg").get_image(0, 0, 800, 800)

        screen.blit(text_render, [(DISPLAY_WIDTH / 2) - 150, (DISPLAY_HEIGHT / 2)])
        if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
            screen.blit(background, [0, 0])
            print('last')
            return True


        print(started)
        pygame.display.flip()
        clock.tick(FPS)

##########################################################################################
def game_play():
    pygame.init()
    screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))

    hero_right_list = []
    for x in range(6):
        for y in range(2, 3):
            right_image = sprite_sheet("hero.png").get_image(80 * x, 94 * y, 80, 94)
            hero_right_list.append(right_image)
            # Player is 80 wide 94 tall


    hero_left_list = []
    for image in hero_right_list:
        hero_left_list.append(pygame.transform.flip(image, True, False))



    explosion_list = []
    for x in range(25):
        for y in range(1, 2):
            explosion_image = sprite_sheet("Explosion.png").get_image(192 * x, 195 * y, 192, 195)
            explosion_list.append(explosion_image)



    shoot_right=(sprite_sheet("hero.png").get_image(0, 94*3, 80, 94))
    shoot_left=pygame.transform.flip(shoot_right, True, False)

    #Creating Sprite groups
    all_sprites=pygame.sprite.Group()

    enemy_sprites=pygame.sprite.Group()
    player_sprite=pygame.sprite.Group()
    laser_sprites=pygame.sprite.Group()
    explosion_sprite=pygame.sprite.Group()


    #sprite objects
    player=Player(hero_left_list, hero_right_list, shoot_left, shoot_right)
    all_sprites.add(player)
    player_sprite.add(player)


    for val in range(10):
        enemy_right=Enemy('right')
        enemy_left=Enemy('left')
        enemy_sprites.add(enemy_right)
        enemy_sprites.add(enemy_left)
        all_sprites.add(enemy_right)
        all_sprites.add(enemy_left)

        global game_score
        game_score = 0


    started=True
    while started==True:
        for event in pygame.event.get():
                   # events to end the game
                   if event.type == pygame.QUIT:
                       quit()
        player.update()

        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            player.image=shoot_right

        laser_hits=pygame.sprite.groupcollide(laser_sprites, enemy_sprites, True, True)
        for hit in laser_hits:
            game_score+=5
            explosion=Explosion(explosion_list)
            explosion_sprite.add(explosion)
            all_sprites.add(explosion)
            enemy=Enemy()
            enemy_sprites.add(enemy)
            all_sprites.add(enemy)

        score(score, 5)

        #enemy_sprites.draw(screen) #NO ISSUES
        #explosion_sprite.draw(screen) #NO ISSUES
        #laser_sprites.draw(screen) #NO ISSUES
        player_sprite.draw(screen) #ISSUES


        pygame.display.flip()
        clock.tick(FPS)

##########################################################################################

def game_over(score):
    over_screen=pygame.display.set_mode([DISPLAY_WIDTH, DISPLAY_HEIGHT])
    clock.tick(FPS)

    end_message_1="GAME OVER!"
    end_message_2="Your score was", score

    end_message_3="Replay"
    end_message_4="Quit"

    font = pygame.font.SysFont('Comic Sans', 75, True, False)

    #rendering text

    end_message_1_render=font.render(end_message_1, True, RED)
    end_message_2_render=font.render(end_message_2, True, BLACK)

    end_message_3_render=font.render(end_message_3, True, BLACK)
    end_message_4_render=font.render(end_message_4, True, BLACK)
    started=False
    while started == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

        clicked=pygame.mouse.get_pressed()
        pos=pygame.mouse.get_pos()
        if pos[0]>0 and pos[0]<100 and pos[1]>0 and pos[1]<100:
            if pygame.mouse.get_pressed()[0] ==1:
                started=False

        over_screen.fill(WHITE)

        over_screen.blit(end_message_1, [(DISPLAY_WIDTH//2), (DISPLAY_HEIGHT//2)])
        over_screen.blit(end_message_2, ((DISPLAY_WIDTH//2), ((DISPLAY_HEIGHT//2)+50)))
        over_screen.blit(end_message_3, [((DISPLAY_WIDTH // 2)-20), ((DISPLAY_HEIGHT // 2) + 120)])
        over_screen.blit(end_message_4, [((DISPLAY_WIDTH // 2)+20), ((DISPLAY_HEIGHT // 2) + 120)])

        pygame.display.flip()
        pygame.time.clock.tick(FPS)

    started=True
##########################################################################################








game_start()


while True:
    game_play()
    game_over(game_score)

pygame.quit()

1 个答案:

答案 0 :(得分:1)

在错误行上,spr.image是一种方法,而不是pygame.Surface,这是surface_blit()所期望的。

编辑(回应评论):

错误发生在sprite.py的第475行。