gpt4 book ai didi

python - Pygame 类型错误 : __init__ takes exactly 4 arguments (1 given)

转载 作者:太空宇宙 更新时间:2023-11-04 08:12:10 24 4
gpt4 key购买 nike

你好,我在尝试运行我的游戏时遇到错误 (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]

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

最佳答案

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

class Game():

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

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

Game().run()

看来你不需要这个参数所以使用

class Game():

def __init__(self):

关于python - Pygame 类型错误 : __init__ takes exactly 4 arguments (1 given),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20405664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com