2016-12-03 4 views
0

私のコンピューティングプロジェクトでは、何らかのアプリやゲームを作成する必要があります。私は宇宙侵略者を作ることに決めました。私はプレーヤーのスプライトが、私はそれを移動することができ生むだろうと背景と音楽が正常に働いていた、基本は作業を取得するために管理しているが、私は敵のスプライトを作成しようとしていたときに、今、私はこのエラーを取得しておいてください。敵のスプライトを生成する

C:\Python27\python.exe "C:/Users/Harry/Desktop/Computing Project/Galaxian.py" 
Traceback (most recent call last): 
    File "C:/Users/Harry/Desktop/Computing Project/Galaxian.py", line 87, in <module> 
    mobs.add(Mob) 
TypeError: unbound method add() must be called with Mob instance as first argument (got type instance instead) 

Process finished with exit code 1 

場合誰でも私が本当にそれを感謝するのを助けることができた!ここに私のコードは次のとおりです。

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

答えて

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) 

をしかし、それはあなたが

を達成しようとしているものをここで少し不明です
関連する問題