产卵的敌人精灵

时间:2016-12-03 15:10:18

标签: python pygame

对于我的计算项目,我需要制作某种应用或游戏。我决定让太空入侵者。我已经设法使基础工作,玩家精灵会产生我可以移动它,背景和音乐工作正常,但现在当我试图创建敌人的精灵时,我一直收到这个错误:

import pygame
import random

# --- 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):
                                     # <-- empty line for readabelity
    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

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 40))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(0, DISPLAY_WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.speedy = random.randrange(1, 8)

    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.speedy = random.randrange(1, 8)


# --- 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 -

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()

player = Player()
player.rect.center = ((DISPLAY_WIDTH / 2), 650)

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


background = pygame.image.load("C:\\Users\\Harry\\Desktop\\Computing Project\\images\\background.jpg")
background = pygame.transform.scale(background, (DISPLAY_WIDTH, DISPLAY_HEIGHT))

# - other -

pygame.mixer.music.load("C:\\Users\\Harry\\Desktop\\Computing Project\\Audio\\music\\soundtrack.mp3")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.4)

# - mainloop -

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

while not crashed:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        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()

如果有人可以提供帮助,我会非常感激!这是我的代码:

{{1}}

1 个答案:

答案 0 :(得分:0)

TypeError: unbound method add() must be called with Mob instance as first argument (got type instance instead)

问题是您已调用add方法并传入了类Mob名称,但未传递Mob的实例。

all_sprites.add(player)
for z in range(8):
    mobs = Mob()
    all_sprites.add(mobs)
    mobs.add(Mob)   # This is the class name

取决于add()您可能想要做的界面:

mob = Mob()
mobs.add(mob)

但这里有点不清楚你正在努力实现的目标