Pygame TypeError: __init__只接受4个参数(1给定)

时间:2021-02-25 23:18:19

Hello I am getting an error when attempting to run my game (TypeError) I don't have a clue why and so brought me to stack overflow here is the code:

你好,我在试图运行我的游戏(TypeError)时出错了,我不知道为什么,所以把我带到stack overflow是代码:

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

Here is the error that I seem to be getting. I have no clue as to why it is happening (I have never come across this before)

这是我得到的错误。我不知道为什么会这样(我以前从未遇到过)

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]

Any help will be appreciated, thank you.

如有任何帮助,我们将不胜感激,谢谢。

4 个解决方案

#1


2  

Game class expects 3 arguments - x, y, direction (+ self)

游戏类需要3个参数- x, y,方向(+ self)

class Game():

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

but you create object without arguments

但是您创建的对象没有参数

Game().run()

It seems you don't need this arguments so use

看起来你不需要这个参数,所以请使用它

class Game():

    def __init__(self):   

#2


2  

When you call Game() , the __init__ method is called in turn.

调用Game()时,依次调用__init__方法。

Your init method takes 3 parameters (other than self):

你的init方法需要3个参数(除了self):

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

which is the cause of the issue.

这就是问题的原因。

On a side note, you are taking x, y, direction as parameters, but never using it. You might want to remove them.

另一方面,你把x, y,方向作为参数,但是不要使用它。您可能想要删除它们。

#3


1  

Game.__init__ requires 3 additional arguments (x, y, and direction) in addition to the implicit self argument. Your call should be something like

游戏。__init__除了隐含的自辩外,还需要3个额外的参数(x, y,和方向)。你的电话应该是这样的。

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

or whatever makes sense for the 3 required arguments.

或者任何对3个必需参数有意义的东西。

#4


1  

As your code is written, you are doing a

当您的代码被编写时,您正在执行一个

Game().run()

and you constructor is

你构造函数

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

Then you are running the script with just one argument ( self ), that is called at the object creation, but x, y and direction are not been passed, you need something like

然后用一个参数(self)运行这个脚本,这个参数在对象创建时调用,但是x、y和方向没有通过,您需要一些类似的东西。

Game(some_x, some_y, some_direction).run()

I hope this is helpful for you

我希望这对你有帮助

#1


2  

Game class expects 3 arguments - x, y, direction (+ self)

游戏类需要3个参数- x, y,方向(+ self)

class Game():

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

but you create object without arguments

但是您创建的对象没有参数

Game().run()

It seems you don't need this arguments so use

看起来你不需要这个参数,所以请使用它

class Game():

    def __init__(self):   

#2


2  

When you call Game() , the __init__ method is called in turn.

调用Game()时,依次调用__init__方法。

Your init method takes 3 parameters (other than self):

你的init方法需要3个参数(除了self):

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

which is the cause of the issue.

这就是问题的原因。

On a side note, you are taking x, y, direction as parameters, but never using it. You might want to remove them.

另一方面,你把x, y,方向作为参数,但是不要使用它。您可能想要删除它们。

#3


1  

Game.__init__ requires 3 additional arguments (x, y, and direction) in addition to the implicit self argument. Your call should be something like

游戏。__init__除了隐含的自辩外,还需要3个额外的参数(x, y,和方向)。你的电话应该是这样的。

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

or whatever makes sense for the 3 required arguments.

或者任何对3个必需参数有意义的东西。

#4


1  

As your code is written, you are doing a

当您的代码被编写时,您正在执行一个

Game().run()

and you constructor is

你构造函数

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

Then you are running the script with just one argument ( self ), that is called at the object creation, but x, y and direction are not been passed, you need something like

然后用一个参数(self)运行这个脚本,这个参数在对象创建时调用,但是x、y和方向没有通过,您需要一些类似的东西。

Game(some_x, some_y, some_direction).run()

I hope this is helpful for you

我希望这对你有帮助