鱼C论坛

 找回密码
 立即注册
查看: 1720|回复: 1

[已解决]python86讲碰撞检测

[复制链接]
发表于 2020-12-17 21:47:57 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 gaowei1 于 2020-12-17 22:09 编辑

大佬求解,为什么我照着视频敲的代码,碰撞检测时只有一个小球在动,其他4个都在原地鬼畜啊

  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *

  5. class Ball(pygame.sprite.Sprite):
  6.     def __init__(self,image,position,speed,bg_size):
  7.         super().__init__()
  8.         self.image = pygame.image.load(image).convert_alpha()
  9.         self.rect  = self.image.get_rect()
  10.         self.rect.left ,self.rect.top = position
  11.         self.speed = speed
  12.         self.width,self.height = bg_size
  13.         self.radius = self.rect.width / 2

  14.     def move(self):
  15.         self.rect = self.rect.move(self.speed)

  16.         if self.rect.right < 0:
  17.             self.rect.left = self.width

  18.         elif self.rect.left > self.width:
  19.             self.rect.right = 0

  20.         elif self.rect.bottom < 0:
  21.             self.rect.top = self.height

  22.         elif self.rect.top > self.height:
  23.             self.rect.bottom = 0



  24. def main():
  25.     pygame.init()
  26.     ball_image = '1.png'
  27.     bg_imagre = '2.png'

  28.     running = True
  29.     bg_size = width,height = 1024,681
  30.     screen = pygame.display.set_mode(bg_size)
  31.     pygame.display.set_caption('play the ball')
  32.     background = pygame.image.load(bg_imagre).convert_alpha()

  33.     balls = []
  34.     group = pygame.sprite.Group()

  35.     for i in range(5):
  36.         position = randint(0,width-100),randint(0,height-100)
  37.         speed = [randint(-10,10),randint(-10,10)]
  38.         ball = Ball(ball_image,position,speed,bg_size)
  39.         #检测球生成时是否发生碰撞
  40.         while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
  41.             ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
  42.         balls.append(ball)
  43.         group.add(ball)

  44.     clock = pygame.time.Clock()

  45.     while True:
  46.         for event in pygame.event.get():
  47.             if event.type == QUIT:
  48.                 sys.exit()


  49.         screen.blit(background,(0,0))

  50.         for each in balls:
  51.             each.move()
  52.             screen.blit(each.image,each.rect)

  53.         for each in group:
  54.             group.remove(each)
  55.             if pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
  56.                 each.speed[0] = - each.speed[0]
  57.                 each.speed[1] = - each.speed[1]

  58.             group.add(each)

  59.         pygame.display.flip()
  60.         clock.tick(30)

  61. if __name__ == '__main__':
  62.     main()
复制代码
最佳答案
2020-12-30 15:44:37
第74行应该改为each
  1. if pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
复制代码
  1. if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-30 15:44:37 | 显示全部楼层    本楼为最佳答案   
第74行应该改为each
  1. if pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
复制代码
  1. if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 14:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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