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

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


HUH函数
浏览 658回答 2
2回答

慕尼黑5688855

该方法rect仅接受4个参数,但是您要向其传递6个参数,这就是为什么会出现错误的原因。这里是文档:pygame.draw.rect()draw a rectangle shaperect(Surface, color, Rect, width=0) -> RectDraws 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.你可以看到,只需要Surface,color,Rect和width它有默认值为0。你是做合格6 pygame.draw.rect(window, Color, xpos, ypos, particle_size, particle_size)。您需要传入pygame.Rect(xpos, ypos, particle_size, particle_size)第三个参数,并删除除window和以外的所有其他参数Color。所以它应该看起来像这样:pygame.draw.rect(window, Color, pygame.Rect(xpos, ypos, particle_size, particle_size)

MM们

如果您查看docs,那么最后一个参数应该是一个Rectangle对象。在Particle构造函数中,将第一行更改为:class Particle(object):&nbsp; &nbsp; def __init__(self, Color, xpos, ypos):&nbsp; &nbsp; &nbsp; &nbsp; pygame.draw.rect(window, Color, pygame.Rect(xpos, ypos, particle_size, particle_size))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python