鱼C论坛

 找回密码
 立即注册
查看: 15836|回复: 2

[已解决]86讲,

[复制链接]
发表于 2020-4-25 12:16:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 猪猪虾 于 2020-4-25 12:20 编辑

报错:TypeError: collide_check() takes 2 positional arguments but 5 were given


  1. #cartoon
  2. import pygame
  3. import sys   #退出程序时要用
  4. from pygame.locals import *
  5. from random import *
  6. import math
  7. #继承本身自带的类即可,本身定义了精灵
  8. class Ball(pygame.sprite.Sprite):
  9.     #实例化球对象
  10.     def __init__(self,image,position,speed,bg_size):
  11.         pygame.sprite.Sprite.__init__(self)#调用该类

  12.         self.image = pygame.image.load(image).convert_alpha()
  13.         self.rect = self.image.get_rect()#获取球的大小
  14.         self.rect.left,self.rect.top = position   #希望球在position的位置上显示
  15.         self.speed = speed    #赋值速度
  16.         self.width,self.height = bg_size[0],bg_size[1]

  17.     def move(self):
  18.         self.rect = self.rect.move(self.speed)

  19.         #实现小球从左边出去,从右边进来,从上边出去,从下面进来
  20.         if self.rect.right <0: #球出右边
  21.             self.rect.left = self.width
  22.             
  23.         if self.rect.left >self.width:
  24.             self.rect.right = 0

  25.         if self.rect.bottom <0:
  26.             self.rect.top = self.height

  27.         if self.rect.top > self.height:
  28.             self.rect.bottom = 0

  29.         

  30. #target是除了Item(1个球),的四个球
  31. def collide_check(item,target):
  32.     collide_ball = []
  33.     for each in target:        
  34.         distance = math.sqrt(\
  35.             math.pow((item.rect.center[0] - each.rect.center[0]), 2) + \
  36.             math.pow((item.rect.center[1] - each.rect.center[1]), 2))
  37.         if distance <= (item.rect.width + each.rect.width) / 2:
  38.             collide_ball.append(each)
  39.     return collide_ball
  40.                
  41. def main():
  42.     pygame.init()
  43.     ball_image = "gray_ball.png"
  44.     bg_image = "background_ball.png"

  45.     running = True

  46.     bg_size = width,height = 1024,681
  47.     screen = pygame.display.set_mode(bg_size)
  48.     pygame.display.set_caption("play the ball")

  49.     balls= []

  50.     #产生五个随机大小相同的球
  51.     for i in range(5):
  52.         position = randint(0,width-100),randint(0,height-100)  #球宽是100,避免球越界
  53.         speed = [randint(-10,10),randint(-10,10)]
  54.         ball = Ball(ball_image,position,speed,bg_size)
  55.         #如果产生的球发生碰撞,重新再设置球的位置
  56.         while collide_check(ball,balls):
  57.             ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
  58.         balls.append(ball)
  59.         
  60.     clock = pygame.time.Clock()
  61.     background =pygame.image.load(bg_image).convert_alpha()

  62.     while running:
  63.         for event in pygame.event.get():
  64.             if event.type == QUIT:
  65.                 sys.exit()
  66.                
  67.         screen.blit(background,(0,0))

  68.         for each in balls:
  69.             each.move()
  70.             screen.blit(each.image,each.rect)

  71.         #移动后开始检测小球的碰撞情况
  72.         for i in range(5):
  73.             item = balls.pop(i)
  74.             if collide_check(item,balls):  #collide_ball不为空
  75.                 item.speed[0] = -item.speed[0]
  76.                 item.speed[1] = -item.speed[1]
  77.             balls.insert(i,item)
  78.                
  79.             

  80.         collide_check(item,balls)
  81.         pygame.display.flip()
  82.         clock.tick(30)
  83.         
  84.         
  85.         
  86. if __name__ =="__main__":
  87.     main()
  88.    
复制代码
  
        
最佳答案
2020-4-25 12:19:53
96行,为啥要解包?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-25 12:19:53 | 显示全部楼层    本楼为最佳答案   
96行,为啥要解包?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-25 12:22:25 | 显示全部楼层
qiuyouzhi 发表于 2020-4-25 12:19
96行,为啥要解包?

原来是他,谢啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 16:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表