GhostFrag1 GhostFrag1 - 3 months ago 18
Python Question

Pygame TypeError: __init__ takes exactly 4 arguments (1 given)

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:

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.

Answer

Game class expects 3 arguments - x, y, direction (+ 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):   
Comments