TypeError:函数最多接受4个参数(给定6个)

时间:2018-08-14 00:18:18

标签: python python-2.7 pygame

我正在使用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)

2 个答案:

答案 0 :(得分:3)

方法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)。您通过执行{{1 }}。您需要传递pygame.draw.rect(window, Color, xpos, ypos, particle_size, particle_size)作为第三个参数,并删除pygame.Rect(xpos, ypos, particle_size, particle_size)window以外的所有其他参数。所以它应该像这样:

Color

答案 1 :(得分:1)

如果您查看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))