TypeError:方法缺少必需的位置参数

2024-04-29 07:38:15 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我正在编写的脚本的简化版本,以便在这里发布。在

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代码时,我得到一个错误:

^{pr2}$

但是我要传递objectS,所以我不知道错误在哪里。。。在

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


Tags: pyimageselffalseimgiftimedisplay
1条回答
网友
1楼 · 发布于 2024-04-29 07:38:15

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

所以不是这样:

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

你应该有这样的东西:

^{pr2}$

相关问题 更多 >