为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUH函数 2021-05-19 14:10:50
我正在使用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 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

该方法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.

你可以看到,只需要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)


查看完整回答
反对 回复 2021-05-25
?
MM们

TA贡献1886条经验 获得超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-05-25
  • 2 回答
  • 0 关注
  • 657 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号