TypeError:未绑定方法sprite()

时间:2017-01-17 15:44:32

标签: python pygame

对于我的高级计算机课程,我需要做一个项目,地雷正在制造太空入侵者,我以前从未使用过python而且我对我迄今为止的进展感到非常满意,但是我最近遇到了一个我不知道的错误怎么处理。代码如下。

import pygame
import random
import sys
# --- constants --- (UPPER_CASE names)

WHITE  = (255, 255, 255)
BLACK  = (  0,   0,   0)
RED    = (255,   0,   0)
GREEN  = (  0, 255,   0)
BLUE   = (  0,   0, 255)
ORANGE = (255, 255,   0)
YELLOW = (  0, 255, 255)

DISPLAY_WIDTH = 720
DISPLAY_HEIGHT = 720

FPS = 60

# --- classes --- (CamelCase names)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images\\user1.gif").convert()
        self.image = pygame.transform.scale(self.image, (50, 50))
        self.rect = self.image.get_rect()
        self.speed_x = 0

    def update(self):
        self.speed_x = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speed_x = -7
        if keystate[pygame.K_RIGHT]:
            self.speed_x = 7
        self.rect.x += self.speed_x
        if self.rect.right > DISPLAY_WIDTH:
            self.rect.right = DISPLAY_WIDTH
        if self.rect.left < 0:
            self.rect.left = 0

    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.top)
        all_sprites.add(bullet)

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images\\enemy1.gif").convert()
        self.image = pygame.transform.scale(self.image, (50, 50))
        self.rect = self.image.get_rect()
        self.speed_x = 0
        self.rect.x = random.randrange(0, DISPLAY_WIDTH - self.rect.width)
        self.rect.y = random.randrange(-500, -40)
        self.speedy = random.randrange(5, 11)

    def update(self):
            self.rect.y +=self.speedy
            if self.rect.top > DISPLAY_HEIGHT + 10:
                self.rect.x = random.randrange(0, DISPLAY_WIDTH - self.rect.width)
                self.rect.y = random.randrange(-100, -40)
                self.speed_y = random.randrange(5, 11)

class Bullet(pygame.sprite.Sprite):

    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images\\laser1.gif").convert()
        self.image = pygame.transform.scale(self.image, (15, 25))
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speed_y = -20

    def update(self):
        self.rect.y += self.speed_y
        if self.rect.bottom < 0:
            self.kill()

# --- functions --- (lower_case names)

# empty

# --- main --- (lower_case names)

# - init -

pygame.init()
pygame.mixer.init()

display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
display_rect = display.get_rect()

# - objects and groups -

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullet_group = pygame.sprite.Group
player = Player()
player.rect.center = ((DISPLAY_WIDTH / 2), DISPLAY_HEIGHT/1.2)

all_sprites.add(player)

for z in range(8):
    mob = Mob()
    mobs.add(mob)
    all_sprites.add(mob)

background = pygame.image.load("images\\background.jpg")
background = pygame.transform.scale(background, (DISPLAY_WIDTH, DISPLAY_HEIGHT))

# - other -

pygame.mixer.music.load("audio\\soundtrack.mp3")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.4)

# - mainloop -

crashed = False
clock = pygame.time.Clock()



while not crashed:

    # - checks for a hit -
    col = pygame.sprite.spritecollideany(player, mobs)
    if col:
        sys.exit()

    col = pygame.sprite.groupcollide(mobs, bullet_group, True, True)

    # - events -
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.shoot()

        print(event)

# - updates (without draws) -

    all_sprites.update()

# - draws (without updates) -

    display.blit(background, (0, 0))

    all_sprites.draw(display)

    pygame.display.update()

# - FPS -

    clock.tick(FPS)

# - end -
pygame.quit()

这是我收到的错误消息,任何帮助都会很棒,谢谢。

C:\Python27\python.exe "C:/Users/Iain/Desktop/Computing Project/Galaxian.py"
Traceback (most recent call last):
  File "C:/Users/Iain/Desktop/Computing Project/Galaxian.py", line 132, in <module>
    col = pygame.sprite.groupcollide(mobs, bullet_group, True, True)
  File "C:\Python27\lib\site-packages\pygame\sprite.py", line 1382, in groupcollide
    c = SC(s, groupb, dokillb, collided)
  File "C:\Python27\lib\site-packages\pygame\sprite.py", line 1339, in spritecollide
    for s in group.sprites():
TypeError: unbound method sprites() must be called with Group instance as first argument (got nothing instead)

Process finished with exit code 1

1 个答案:

答案 0 :(得分:1)

“未绑定方法”是从类中获取的方法对象。调用时,它需要传递给它的类的实例。

“绑定方法”是绑定到类实例的方法。它可以被称为“正常”,因为它已经有了实例。

在你的情况下,你有一个简单的拼写错误。你有一个未绑定的方法,但它应该是一个绑定方法。该消息具有误导性,因为真正的问题是您传递给pygame的参数不正确。

在您的计划的第132行,您致电groupcollide。您提供的参数之一是名为bullet_group的变量。

问题的根本原因是这一行:

bullet_group = pygame.sprite.Group

您错过了括号,因此名称bullet_group引用了 pygame.sprite.Group。您只需添加括号即可调用该类以创建它的实例

bullet_group = pygame.sprite.Group()

您在mobsall_sprites的前一行中正确执行了此操作。