Pygame TypeError: missing 1 required positional ar

2019-07-27 04:49发布

问题:

I am programming a game in Pygame. I kept running up on this error and I don't understand how to fix it.It runs the pauseMenu() init and prints what is in it so it can run it but after that I am stuck. Any help would be a appreciated.

Exact error:

Traceback (most recent call last):
  File "C:/Users/derek/Desktop/current workspace/Red_Fox/main.py", line 85, in <module>
    start()
  File "C:/Users/derek/Desktop/current workspace/Red_Fox/main.py", line 37, in start
60
    mainLoop()
  File "C:/Users/derek/Desktop/current workspace/Red_Fox/main.py", line 67, in mainLoop
    pauseMenu.pauseMenuFunct(DISPLAYSURF,(WINWIDTH,WINHEIGHT))
TypeError: pauseMenuFunct() missing 1 required positional argument: 'WINDIM'

The main is here and the file I am calling is under it.

import pygame,sys
from Player import *
from Block import *
from menu import *
from pauseMenu import *
from pygame.locals import *
goToMenu = True
def start():
    pygame.init()
    menuScreen = MenuScreen()
    pMenu = pauseMenu()

global  DISPLAYSURF,BRIGHTBLUE,FPSCLOCK, \
        FPS,player,objects, \
        WINHEIGHT,WINWIDTH,goToMenu, \
        objectsFP,WINDIM
FPS =  60 # frames per second to upate the screen
WINWIDTH = 800 # width of the program's window, in pixels
WINHEIGHT = 600 # height in pixels
WINDIM = WINWIDTH,WINHEIGHT
#WINWIDTH = 1920 # width of the program's window, in pixels
#WINHEIGHT = 1080 # height in pixels
FPSCLOCK = pygame.time.Clock()
BRIGHTBLUE = (  0, 170, 255)
DISPLAYSURF = pygame.display.set_mode((WINWIDTH, WINHEIGHT))
FPSCLOCK = pygame.time.Clock()
#player = Player(100,100)
objects = []
objectsFP = []
if goToMenu:
    goToMenu = False
    menuState = menuScreen.menuFunct(DISPLAYSURF,WINDIM,BRIGHTBLUE)

if menuState == 0:
    initGame()
    worldGen()
    mainLoop()
elif menuState == 1:
    print('Will have options here')


#global DISPLAYSURF,BRIGHTBLUE,FPSCLOCK,FPS

def worldGen():
    for i in range(int(WINHEIGHT/2),WINHEIGHT,10):
        for j in range(0,WINWIDTH,10):
            objects.append(Block(j,i))
            if(i == int(WINHEIGHT/2)):
                objectsFP.append(Block(j,i))
                print('happened dawg')
    for o in objectsFP:
        o.draw(DISPLAYSURF)
def initGame():
    print('in initGame()')
    objects.append(Player(100,100))

def mainLoop():
    while True:
        DISPLAYSURF.fill(BRIGHTBLUE)
        for event in pygame.event.get():
            if event.type == QUIT:
                quitCleanUp()
            elif event.type == KEYDOWN:
                objects[0].keyDownUpdate(event)
                if event.key == K_ESCAPE:
                    print(FPS)
                    pauseMenu.pauseMenuFunct(DISPLAYSURF,(WINWIDTH,WINHEIGHT))
            elif event.type == KEYUP:
                objects[0].keyUpUpdate(event)
        for i in objects:
            i.update()
            i.draw(DISPLAYSURF)
        #objectsFP = objects.copy()
        objects[0].objUpdate(objectsFP)
        #player.update()
        #player.draw(DISPLAYSURF)
        pygame.display.update()
        FPSCLOCK.tick(FPS)

def quitCleanUp():
    pygame.quit()
    sys.exit()

if __name__ == '__main__':
    start()

This is the other class that I am trying to call.

import pygame,time,sys
from pygame import *



class pauseMenu():

    goToMenu = False
    imageDict = {}
    textDict = {}
    rectDict = {}
    Clicked_Button = ''
    DarkGrey =      (134, 134, 134)
    volume = 10
    FPSCLOCK = pygame.time.Clock()


    def pauseMenuFunct(self,screen,WINDIM):
        print("made it here")
        def check_collisions(self,pos,list):
        # iterate dict, check for collisions in systems
        #     print('Made it to teh da function')
            for k,v in list.items():
                # print('Made into to the for loop')
                if v.collidepoint(pos):
                    # print(v)
                    # print("clicked button:", k)
                    self.Clicked_Button = k
                    # print(self.Clicked_Button)
                    return True,k

        WINWIDTH,WINHEIGHT = WINDIM
        # print('Made it to the function')
        # screen.fill(background)
        imageDict = {'buttons': pygame.image.load('Resources/Pics/PauseOptions.png')
        }

        imageDict['buttons']=pygame.transform.smoothscale(imageDict['buttons'],(int(WINWIDTH*.2),(int(WINHEIGHT*.1))))

        buttonX,buttonY= imageDict['buttons'].get_size()
        # print(buttonX , buttonY)
        textSize = int(buttonY/2)
        # print(textSize)
        self.font = pygame.font.Font('Resources/chunkfont.ttf', textSize)
        textOptions  = self.font.render("Options", 1, self.DarkGrey)
        textSave     = self.font.render("Save", 1, self.DarkGrey)
        textQuit     = self.font.render("Quit", 1, self.DarkGrey)
        textVolume   = self.font.render("Volume", 1, self.DarkGrey)
        textBackToGame = self.font.render('Return', 1, self.DarkGrey)
        VolumePopUp  = self.font.render(str(self.volume), 1, self.DarkGrey)
        textBackToMenu = self.font.render('Menu', 1 , self.DarkGrey)


        self.rectDict['Options'] =           screen.blit(imageDict['buttons'], (WINWIDTH/4,(WINHEIGHT/3)))
        self.rectDict['Save'] =              screen.blit(imageDict['buttons'], (WINWIDTH/2+60.5,WINHEIGHT/3))
        self.rectDict['Quit'] =              screen.blit(imageDict['buttons'], (WINWIDTH/4,(WINHEIGHT/2)))
        self.rectDict['Volume'] =            screen.blit(imageDict['buttons'], ((WINWIDTH/2+60.5),(WINHEIGHT/2)))
        self.rectDict['Return'] =            screen.blit(imageDict['buttons'], (WINWIDTH/4,(WINHEIGHT/1.5)))
        self.rectDict['Menu'] =              screen.blit(imageDict['buttons'], (WINWIDTH/2+60.5,WINHEIGHT/1.5))
        self.textDict['textOptions'] =       screen.blit(textOptions,(WINWIDTH/4 + (buttonX/4),(WINHEIGHT/3+textSize/2)))
        self.textDict['textQuit'] =          screen.blit(textQuit,(WINWIDTH/4 + (buttonX/4),(WINHEIGHT/2+textSize/2)))
        self.textDict['textSave'] =          screen.blit(textSave,(WINWIDTH/2+60.5 + (buttonX/3),(WINHEIGHT/3+textSize/2)))
        self.textDict['textVolume'] =        screen.blit(textVolume,(WINWIDTH/2+50.5 + (buttonX/4.5),(WINHEIGHT/2+textSize/2)))
        self.textDict['textBacktoGame']=     screen.blit(textBackToGame,(WINWIDTH/4+ (buttonX/4),(WINHEIGHT/1.5+textSize/2)))
        self.textDict['textMenu']=           screen.blit(textBackToMenu,(WINWIDTH/2+60.5+ (buttonX/4),(WINHEIGHT/1.5+textSize/2)))
        self.textDict['volumeNum']=          screen.blit(VolumePopUp,(WINWIDTH/2+55.5 + buttonX/1.25,(WINHEIGHT/2+textSize/2)))
        pygame.display.update()
        print('made it to the update')


        while True:
            self.FPSCLOCK.tick(60)
            for event in pygame.event.get():
                if event.type == QUIT:
                    sys.exit()
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        sys.exit()

                elif event.type == MOUSEBUTTONDOWN:
                    if event.button == 1:
                        if check_collisions(self,event.pos,self.rectDict):

                            # print('Button Clicked from Event Loop is: '+self.Clicked_Button)

                            if self.Clicked_Button == 'Quit':
                                sys.exit()
                            elif self.Clicked_Button == 'Save':
                                print('It will be saved later')
                            elif self.Clicked_Button == 'Volume' and self.volume>0:
                                self.volume -= 1
                                print(self.volume)

                                VolumePopUp  = self.font.render(str(self.volume), 1, self.DarkGrey)
                                screen.blit(imageDict['buttons'], ((WINWIDTH/2+60.5),(WINHEIGHT/2)))
                                screen.blit(VolumePopUp,(WINWIDTH/2+55.5 + buttonX/1.25,(WINHEIGHT/2+textSize/2)))

                                pygame.display.update(self.textDict['volumeNum'])


                            elif self.Clicked_Button == 'Return':
                                print('This will close the menu and you return you to the game.')
                            elif self.Clicked_Button == 'Options':
                                print('This will take you to an options screen')
                                #TODO Return values
                            elif self.Clicked_Button == 'Menu':
                                print('Will take you to main menu sooner or later')
                    elif event.button == 3:
                        if check_collisions(self,event.pos,self.rectDict):
                            if self.Clicked_Button == 'Volume' and self.volume<10:
                                self.volume += 1
                                print(self.volume)

                                VolumePopUp  = self.font.render(str(self.volume), 1, self.DarkGrey)
                                screen.blit(imageDict['buttons'], ((WINWIDTH/2+60.5),(WINHEIGHT/2)))
                                screen.blit(VolumePopUp,(WINWIDTH/2+55.5 + buttonX/1.25,(WINHEIGHT/2+textSize/2)))

                                pygame.display.update(self.textDict['volumeNum'])

                                #TODO Return volume values


    def __init__(self):
        print('PauseMenu _init_ ran')

回答1:

You are calling pauseMenu on the class itself:

pauseMenu.pauseMenuFunct(DISPLAYSURF,(WINWIDTH,WINHEIGHT))

This is an unbound function, so self will not be filled in for you.

You also create an instance somewhere:

def start():
    pygame.init()
    menuScreen = MenuScreen()
    pMenu = pauseMenu()

Perhaps you meant to call pMenu.pauseMenuFunct() instead?