【问题标题】:TypeError: function takes at most 4 arguments (6 given)TypeError:函数最多接受 4 个参数(给定 6 个)
【发布时间】:2019-01-20 18:18:47
【问题描述】:

我正在使用 Pygame 模块在 python 2.7 中制作粒子模拟器。而且我在涉及某些课程时遇到错误,我无法修复,请帮忙。这是代码:

 import pygame, sys
 from colors import *
 from random import randint
 import particles

 pygame.init()

 #background = pygame.image.load("graphics//background.jpg")
 #Background = pygame.Surface(background.get_size(), pygame.HWSURFACE)
 #Background.blit(background, (0, 0))


 global window, window_height, window_width, window_title
 window_width, window_height = 800, 600
 window_title = "particle game"
 title_icon = "graphics//icons//icon_title.jpg"
 pygame.display.set_caption(window_title)
 window = pygame.display.set_mode((window_width, 
 window_height), pygame.HWSURFACE|pygame.DOUBLEBUF)


 particle_size = 2


 class Particle(object):
      def __init__(self, Color, xpos, ypos):
          pygame.draw.rect(window, Color, xpos, ypos, particle_size, particle_size)



 class Hydrogen(Particle):
      def __init__(self, Color, xpos, ypos):
          Particle.__init__(self, Color, xpos, ypos)
          pygame.draw.rect(window, Color, xpos, ypos, particle_size, particle_size)

          window.fill(Color.LightGray)

          particle_num = 12
          isRunning = True
          #for particle in range(particle_num):
               #Hydrogen(Color.Green)
               #print"hello"
 while isRunning:

     for event in pygame.event.get():
          if event.type == pygame.QUIT:
              isRunning = False
          elif event.type == pygame.MOUSEBUTTONDOWN:
              mx, my = pygame.mouse.get_pos()
              Hydrogen(Color.Orange, mx, my)

 pygame.display.update()

pygame.quit()
sys.exit()

缩进是正确的,缩进可能是复制的时候弄乱了。

以下是所有错误:

line 53, in <module>
      Hydrogen(Color.Orange, mx, my)

line 36, in __init__
      Particle.__init__(self, Color, xpos, ypos)

line 30, in __init__
      pygame.draw.rect(window, Color, xpos, ypos, particle_size, particle_size)
TypeError: function takes at most 4 arguments (6 given)

【问题讨论】:

  • 我对PyGame几乎一无所知,但我认为应该是pygame.draw.rect(window, Color, (xpos, ypos, particle_size, particle_size))
  • 请修复代码缩进。
  • “缩进是正确的,缩进可能在复制时搞砸了”没有帮助。如果我想运行你的代码来调试你的问题,我的解释器会给我一个IndentationError 或者默默地做错事。我不能只说:“抱歉,Python,狼蛛家伙说它就在他的机器上,所以无论如何请运行它”。
  • 错误很明显,接下来的方法是阅读您错误使用的功能的文档。

标签: python python-2.7 pygame


【解决方案1】:

rect 方法只接受 4 个参数,但您传递给它 6 个参数,这就是您收到错误的原因。这是文档:

pygame.draw.rect()
draw a rectangle shape
rect(Surface, color, Rect, width=0) -> Rect
Draws a rectangular shape on the Surface. The given Rect is the area of the rectangle. The width argument is the thickness to draw the outer edge. If width is zero then the rectangle will be filled.

Keep in mind the Surface.fill() method works just as well for drawing filled rectangles. In fact the Surface.fill() can be hardware accelerated on some platforms with both software and hardware display modes.

如您所见,它只需要 SurfacecolorRectwidth,默认值为 0。您通过执行 pygame.draw.rect(window, Color, xpos, ypos, particle_size, particle_size) 传递了 6。您需要将pygame.Rect(xpos, ypos, particle_size, particle_size) 作为第三个参数传入,并删除除windowColor 之外的所有其他参数。所以它应该是这样的:

pygame.draw.rect(window, Color, pygame.Rect(xpos, ypos, particle_size, particle_size)

【讨论】:

  • @Tarantulaguy 如果有帮助,请接受答案(我的答案旁边有一个白色复选标记)。谢谢队友:)
【解决方案2】:

如果您查看docs,最后一个参数应该是Rectangle 对象。

在您的Particle 构造函数中,将第一行更改为:

class Particle(object):
    def __init__(self, Color, xpos, ypos):
        pygame.draw.rect(window, Color, pygame.Rect(xpos, ypos, particle_size, particle_size))

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 2016-02-03
    • 2017-06-30
    • 2019-09-12
    • 2018-12-23
    • 2013-01-13
    相关资源
    最近更新 更多