gpt4 book ai didi

python - 类型错误:方法缺少必需的位置参数

转载 作者:太空宇宙 更新时间:2023-11-04 00:23:42 25 4
gpt4 key购买 nike

这是我正在处理的脚本的简化版本,以便在此处发布。

import pyglet as py
import pygame as pg
import os, time

pg.init()

class g:

wMainDisplay = 1024
hMainDiplay = 576
textTitle = "Pokémon Life and Death: ESPLORATORI DEL PROPRIO DESTINO"
flag = pg.DOUBLEBUF | pg.HWSURFACE | pg.NOFRAME
fpsDefault = 60
black = (0, 0, 0)
loopIntro = False
loopWarn = True

def __init__(self):
os.environ["SDL_VIDEO_CENTERED"] = "1"
self.display = pg.display.set_mode((self.wMainDisplay, self.hMainDiplay), self.flag)
self.display.set_alpha(None)
pg.display.set_caption(self.textTitle)
self.alphaSurface = pg.Surface((self.wMainDisplay, self.hMainDiplay))
self.alphaSurface.set_alpha(0)
self.clock = pg.time.Clock()
pg.key.set_repeat(500, 100)
pg.mouse.set_visible(False)
py.options['audio'] = ("openal", "pulse", "directsound", "silent")
self.all_sprites = pg.sprite.Group()

def warnings(self):
alph = 0
img_in = True
img_out = False
warnCount = 0
color = self.black
self.warningGroups = pg.sprite.Group()
_Warning(self, 1)
time.sleep(2)

while self.loopWarn:
self.clock.tick(self.fpsDefault)
self.display.fill(color)

if img_in and img_out is False:
alph += 1
if alph >= 255:
warnCount += 1
img_in = False
time.sleep(0.01)
if img_out and img_in is False:
alph -= 5
if alph <= 0:
warnCount += 1
img_out = False
time.sleep(0.01)

if warnCount == 1:
_Warning.sound(1)
warnCount += 1
time.sleep(0.01)
elif warnCount == 2:
if _Warning.updateAudio() is False:
warnCount += 1
time.sleep(0.01)
elif warnCount == 3:
img_out = True
elif warnCount == 4:
_Warning(2)
img_in = True
elif warnCount == 5:
_Warning.sound(2)
warnCount += 1
time.sleep(0.01)
elif warnCount == 6:
if _Warning.updateAudio() is False:
warnCount += 1
time.sleep(0.01)
elif warnCount == 7:
self.loopWarn = False
self.loopIntro = True
else:
pass

self.alphaSurface.set_alpha(alph)
self.warningGroups.draw(self.alphaSurface)
self.display.blit(self.alphaSurface, (0, 0))
pg.display.flip()

class _Warning(pg.sprite.Sprite):

def __init__(self, game, objectI):
self.groups = game.warningGroups
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.player = py.media.Player()

self.warningImage_1 = pg.image.load("wra.png")
self.warningImage_2 = pg.image.load("wrb.png")
self.warningSound_1 = py.media.load("wra.mp3")
self.warningSound_2 = py.media.load("wra.mp3")

if objectI == 1:
self.image = self.warningImage_1
elif objectI == 2:
self.image = self.warningImage_2
self.image = pg.transform.scale(self.image, (self.game.wMainDisplay, self.game.hMainDiplay))

self.rect = self.image.get_rect()
self.rect.x = 0
self.rect.y = 0

def sound(self, objectS):
if objectS == 1:
self.player.queue(self.warningSound_1)
elif objectS == 2:
self.player.queue(self.warningSound_2)
self.player.play()
py.app.run()

def updateAudio(self):
check = self.player.playing()
return check

g = g()
g.warnings()

当我运行这段 Python 代码时,我得到了这个错误:

Traceback (most recent call last):
File "C:/Users/BlackFenix06/Desktop/Isaac.py/cut.py", line 124, in <module>
g.warnings()
File "C:/Users/BlackFenix06/Desktop/Isaac.py/cut.py", line 57, in warnings
_Warning.sound(1)
TypeError: sound() missing 1 required positional argument: 'objectS'

但是我正在传递 objectS,所以我不明白错误在哪里......

当我写这个问题时,我突然想到,错误可能是因为该类已经被用作 pygame.sprite.Sprite。但是我没有直接使用 __init__ ,而是类的一个不同的函数。你怎么看?

最佳答案

实际上缺少的参数是self。您应该从类实例化而不是类类型本身调用 sound() 方法。

所以不是这个:

_Warning(self, 1)
_Warning.sound(1)

你应该有这样的东西:

warn = _Warning(self, 1)
warn.sound(1)

关于python - 类型错误:方法缺少必需的位置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48234833/

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