首页 > 解决方案 > 当只应该有 4 个时,TypeError 获得了太多的位置参数

问题描述

最近尝试完成一个 pygame 分配,但是当我显然只输入了 4 时,我收到了一个 TypeError 有太多的位置参数 (5)。

下面是我的代码:

import pygame
import random
import math
import os
import time
import sys
from os import path
from newsettings import *
from spritesdata import *
class Game:
    def __init__(self):
        pygame.init()
        self.clock = pygame.time.Clock()
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption(TITLE)
        self.time = pygame.time.get_ticks()
        pygame.key.set_repeat(500, 100)
        self.all_sprites = pygame.sprite.Group()
        self.console = Console(self, 0)
        self.player = Player(self, 390, 595)
        self.work = Work(self, 450, 250)
        self.food_station = Food_Station(self, 750, 200)
        self.food = Food(self, 0, 10)
        self.education = Education(self, 300, 10)
        self.school = School(self, 100, 200)
        self.family = Family(self, 600, 10)
        self.money = Money(self, 850, 15)
        self.food_bar = 100
    def run(self):
        self.playing = True
        while self.playing:
            self.dt = self.clock.tick(FPS) / 1000
            self.hunger()
            self.events()
            self.update()
            self.draw()
    def hunger(self):
        HUNGEREVENT = pygame.USEREVENT + 1
        pygame.time.set_timer(HUNGEREVENT, 10000)
    def food_food(self, x, y, cool):
        if cool < 0:
            cool = 0
        BAR_LENGTH = 100
        BAR_HEIGHT = 10
        fill = (cool / 100) * BAR_LENGTH
        outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
        fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
        pygame.draw.rect(self, GREEN, fill_rect)
        pygame.draw.rect(self, WHITE, outline_rect, 2)
    def quit(self):
        pygame.quit()
        sys.exit()
    def update(self):
        self.all_sprites.update()
    def draw(self):
        self.screen.fill(BGCOLOR)
        self.all_sprites.draw(self.screen)
        font = pygame.font.SysFont('Arial', 15, True, False)
        self.food_food(screen, 5, 5, self.food_bar)
        text = font.render("Number of days:" , True, BLACK)
        screen.blit(text, [0, 110])
        font = pygame.font.SysFont('Arial', 30, True, False)
        text = font.render("=" , True, BLACK)
        screen.blit(text, [120, 40])
        font = pygame.font.SysFont('Arial', 30, True, False)
        text = font.render("=" , True, BLACK)
        screen.blit(text, [400, 40])
        font = pygame.font.SysFont('Arial', 30, True, False)
        text = font.render("=" , True, BLACK)
        screen.blit(text, [700, 40])
        font = pygame.font.SysFont('Arial', 30, True, False)
        text = font.render("=" , True, BLACK)
        screen.blit(text, [950, 40])
        self.all_sprites.update()
        pygame.display.flip()
    def events(self):
        # catch all events here
        HUNGEREVENT = pygame.USEREVENT + 1
        pygame.time.set_timer(HUNGEREVENT, 10000)
        if pygame.event.get(HUNGEREVENT):
            self.food_bar = self.food_bar - 10
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.quit()
                if event.key == pygame.K_ESCAPE:
                    self.quit()
    def end_screen(self):
        screen.fill(BLUE)
        font = pygame.font.SysFont('Arial', 30, True, False)
        text = font.render("Life Simulator V2" , True, WHITE)
        screen.blit(text, [400, 100])
        font = pygame.font.SysFont('Arial', 30, True, False)
        text = font.render("Controls: F for food, E for education, W for work", True, WHITE)
        screen.blit(text, [175, 250])
        font = pygame.font.SysFont('Arial', 30, True, False)
        text = font.render("Hard mode: h    Easy mode: e    Normal mode: m", True, WHITE)
        screen.blit(text, [175, 400])
        font = pygame.font.SysFont('Arial', 30, True, False)
        text = font.render("Start by pressing a key corresponding to that mode", True, WHITE)
        screen.blit(text, [150, 600])
        pygame.display.flip()
        waiting = True
        while waiting:
            pygame.init()
            clock = pygame.time.Clock()
            clock.tick(60)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_h:
                        waiting = False
                        initial_money = 50
                        self.food_bar = 100
                    if event.key == pygame.K_m:
                        waiting = False
                        initial_money = 100
                        self.food_bar = 100
                    if event.key == pygame.K_e:
                        waiting = False
                        initial_money = 200
                        self.food_bar = 100
g = Game()
g.end_screen()
g.run()

这是类型错误:

Traceback (most recent call last):
  File "final_project.py", line 135, in <module>
    g.run()
  File "final_project.py", line 37, in run
    self.draw()
  File "final_project.py", line 65, in draw
    self.food_food(screen, 5, 5, self.food_bar)
TypeError: food_food() takes 4 positional arguments but 5 were given

虽然在 self.food_food() 的应用程序中我给出了 4 个参数,但它提示我它收到了 5 个位置参数,这让我非常困惑。任何帮助,将不胜感激。谢谢!

标签: python-3.xpygame

解决方案


在 python 中,方法接收你的参数和self。您正在发送 4 个参数和 self. 就像:所以你是self.food_food(self, screen, 5, 5, self.food_bar),你的函数只接收 4 个参数。(如果 def 是:def food_food(self, x, y, cool)你可以调用它self.food_food(x, y, cool))你可能想要使用food_food(self, screen, x, y, cool):.


推荐阅读