Pygame TypeError:__ init__正好占用4个参数(给定1个)

时间:2013-12-05 16:41:34

标签: python pygame typeerror

你好我在尝试运行我的游戏时遇到错误(TypeError)我不知道为什么这样带来堆栈溢出这里是代码:

import pygame
import random
import pygame.mixer
import Funk
from player import *
from zombie import *
from level import *
from bullet import *

class Game():

    def __init__(self, x, y, direction):
        pygame.init()
        pygame.mixer.init()

        #pygame.mixer.music.load('sounds/menugame.ogg')
        #pygame.mixer.music.play(-1)

        #sound = pygame.mixer.Sound('sounds/jump.ogg')

        # A few variables
        self.gravity = .50
        self.ground = pygame.Rect(0, 640, 1280, 80)

        # Bullets
        self.bullets = []

        # Screen
        size = (1280, 720)
        self.screen = pygame.display.set_mode(size)
        pygame.display.set_caption('Moon Survival!')

        # Moon / Background
        self.moon = Background()

        # Zombies
        self.zombies = []
        for i in range(10):
            self.zombies.append( Zombie(random.randint(0,1280), random.randint(0,720)) )

        # Player
        self.player = Player(25, 320, self.gravity)

        # Font for text
        self.font = pygame.font.SysFont(None, 72)

        # Pause - center on screen
        self.pause_text = self.font.render("PAUSE", -1, (255,0,0))
        self.pause_rect = self.pause_text.get_rect(center = self.screen.get_rect().center)

    def run(self):

        clock = pygame.time.Clock()

        # "state machine" 
        RUNNING   = True
        PAUSED    = False 
        GAME_OVER = False

        # Game loop
        while RUNNING:

            # (all) Events

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    RUNNING = False

                elif event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_x:
                        self.bullets.append(Bullet(self.player.rect.x, self.player.rect.y))

                    if event.key == pygame.K_ESCAPE:
                        RUNNING = False

                    elif event.key == pygame.K_p:
                        PAUSED = not PAUSED

                # Player/Zomies events  

                if not PAUSED and not GAME_OVER:
                    self.player.handle_events(event)

            # (all) Movements / Updates

            if not PAUSED and not GAME_OVER:
                self.player_move()
                self.player.update()

                for z in self.zombies:
                    self.zombie_move(z)
                    z.update(self.screen.get_rect())

            # (all) Display updating

            self.moon.render(self.screen)

            for z in self.zombies:
                z.render(self.screen)

            for b in self.bullets:
                b.render(self.screen)

            self.player.render(self.screen)

            if PAUSED:
                self.screen.blit(self.pause_text, self.pause_rect)

            Funk.text_to_screen(self.screen, 'Level 1', 5, 675)
            Funk.text_to_screen(self.screen, 'Health: {0}'.format(self.player.health), 5, 0)

            pygame.display.update()

            # FTP

            clock.tick(100)

        # --- the end ---
        pygame.quit()

    def player_move(self):
        # add gravity
        self.player.do_jump()

        # simulate gravity
        self.player.on_ground = False
        if not self.player.on_ground and not self.player.jumping:
            self.player.velY = 4

        # Health
        for zombie in self.zombies:
            if pygame.sprite.collide_rect(self.player, zombie):
                self.player.health -= 5

        # move player and check for collision at the same time
        self.player.rect.x += self.player.velX
        self.check_collision(self.player, self.player.velX, 0)
        self.player.rect.y += self.player.velY
        self.check_collision(self.player, 0, self.player.velY)

    def zombie_move(self, zombie_sprite):
        # add gravity
        zombie_sprite.do_jump()

        # simualte gravity
        zombie_sprite.on_ground = False
        if not zombie_sprite.on_ground and not zombie_sprite.jumping:
            zombie_sprite.velY = 4

        # move zombie and check for collision
        zombie_sprite.rect.x += zombie_sprite.velX
        self.check_collision(zombie_sprite, zombie_sprite.velX, 0)
        zombie_sprite.rect.y += zombie_sprite.velY
        self.check_collision(zombie_sprite, 0, zombie_sprite.velY)


    def check_collision(self, sprite, x_vel, y_vel):
        # for every tile in Background.levelStructure, check for collision
        for block in self.moon.get_surrounding_blocks(sprite):
            if block is not None:
                if pygame.sprite.collide_rect(sprite, block):
                    # we've collided! now we must move the collided sprite a step back
                    if x_vel < 0:
                        sprite.rect.x = block.rect.x + block.rect.w

                        if sprite is Zombie:
                            print "wohoo"

                    if x_vel > 0:
                        sprite.rect.x = block.rect.x - sprite.rect.w

                    if y_vel < 0:
                        sprite.rect.y = block.rect.y + block.rect.h

                    if y_vel > 0 and not sprite.on_ground:
                        sprite.on_ground = True
                        sprite.rect.y = block.rect.y - sprite.rect.h

#---------------------------------------------------------------------

Game().run()

这是我似乎得到的错误。我不清楚它为什么会发生(我之前从未遇到过这种情况)

Traceback (most recent call last):
  File "F:\My Moon Survival Game\Moon Survival.py", line 183, in <module>
    Game().run()
TypeError: __init__() takes exactly 4 arguments (1 given)
[Finished in 0.2s with exit code 1]

任何帮助将不胜感激,谢谢。

4 个答案:

答案 0 :(得分:2)

当您致电Game()时,会依次调用__init__方法。

您的init方法需要3个参数(self除外):

def __init__( self, x, y, direction ) :

这是问题的原因。

在旁注中,您将xydirection作为参数,但从不使用它。您可能想要删除它们。

答案 1 :(得分:2)

游戏类需要3个参数 - xydirection(+ self

class Game():

    def __init__(self, x, y, direction):   

但你创建没有参数的对象

Game().run()

您似乎不需要这些参数,所以请使用

class Game():

    def __init__(self):   

答案 2 :(得分:1)

除隐式Game.__init__参数外,

x还需要3个附加参数(ydirectionself)。你的电话应该是

Game(100, 100, "north").run()

或对3个必要参数有意义的任何内容。

答案 3 :(得分:1)

在编写代码时,您正在执行

Game().run()

你的构造函数是

def __init__(self, x, y, direction):

然后你运行的脚本只有一个参数(self),在创建对象时被调用,但x,y和direction没有被传递,你需要像

这样的东西
Game(some_x, some_y, some_direction).run()

我希望这对你有所帮助