鱼C论坛

 找回密码
 立即注册
查看: 1255|回复: 6

求助一下pygame的碰撞检测问题

[复制链接]
发表于 2020-5-3 21:48:11 | 显示全部楼层 |阅读模式

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

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

x
自己写的一个打砖块游戏 碰撞检测部分如下 我的球只有遇到边框会发生反弹 在需要到mask碰撞检测的地方就会直接穿过砖块跟板子是怎么回事呢 file:///C:/Users/Folly/Desktop/L%7BJKU(GS)5_T55VZUH1E$%7B6.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-3 21:49:42 | 显示全部楼层
  1.         #检测球的碰撞
  2.             for each in brick_list:
  3.                 ball_hit_brick = pygame.sprite.collide_mask(ball,each)
  4.                 if ball_hit_brick == True:
  5.                     if ball.rect.centerx < each.rect.left:
  6.                         if ball.centery < each.rect.top or ball.rect.centery > each.rect.bottom:
  7.                             if ball.centery < each.rect.top and ball.speed_y > 0:
  8.                                 ball.speed_x = - ball.speed_x
  9.                                 ball.speed_y = - ball.speed_y
  10.                             elif ball.rect.centery > each.rect.bottom and ball.speed_y < 0:
  11.                                 ball.speed_x = - ball.speed_x
  12.                                 ball.speed_y = - ball.speed_y
  13.                            
  14.                         else:
  15.                             ball.speed_x = - ball.speed_x
  16.                     elif ball.rect.centerx > each.rect.right:
  17.                         if ball.rect.centery < each.rect.top or ball.rect.centery > each.rect.bottom:
  18.                             if ball.rect.centery < each.rect.top and ball.speed_y > 0:
  19.                                 ball.speed_x = - ball.speed_x
  20.                                 ball.speed_y = - ball.speed_y
  21.                             elif ball.rect.centery > each.rect.bottom and ball.speed_y < 0:
  22.                                 ball.speed_x = - ball.speed_x
  23.                                 ball.speed_y = - ball.speed_y
  24.                                 
  25.                         else:
  26.                             ball.speed_x = - ball_speed_x
  27.                     else:
  28.                         ball.speed_y = - ball_speed_y

  29.             ball_hit_board = pygame.sprite.collide_mask(ball,board)
  30.             if ball_hit_board == True:
  31.                 if ball.rect.centerx < board.rect.left:
  32.                     if ball.rect.centert < board.rect.top or ball.rect.centery > board.rect.bottom:
  33.                         if ball.rect.centert < board.rect.top and ball.speed_y > 0:
  34.                             ball.speed_x = - ball.speed_x
  35.                             ball.speed_y = - ball.speed_y
  36.                         elif ball.rect.centery > board.rect.bottom and ball.speed_y < 0:
  37.                             ball.speed_x = - ball.speed_x
  38.                             ball.speed_y = - ball.speed_y
  39.                         
  40.                     else:
  41.                         ball.speed_x = - ball.speed_x
  42.                 elif ball.rect.centerx > board.rect.right:
  43.                     if ball.rect.centery < board.rect.top or ball.rect.centery > board.rect.bottom:
  44.                             if ball.rect.centery < board.rect.top and ball.speed_y > 0:
  45.                                 ball.speed_x = - ball.speed_x
  46.                                 ball.speed_y = - ball.speed_y
  47.                             elif ball.rect.centery > board.rect.bottom and ball.speed_y < 0:
  48.                                 ball.speed_x = - ball.speed_x
  49.                                 ball.speed_y = - ball.speed_y
  50.                     else:
  51.                         ball.speed_x = - ball_speed_x
  52.                 else:
  53.                     ball.speed_y = - ball.speed_y

  54.             if ball.rect.left < 0:
  55.                 ball.speed_x = - ball.speed_x
  56.             if ball.rect.right > background_width:
  57.                 ball.speed_x = - ball.speed_x
  58.             if ball.rect.top < 0:
  59.                 ball.speed_y = - ball.speed_y
  60.                     
  61.                         
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-3 22:01:53 | 显示全部楼层
  1. # 导入模块
  2. import pygame
  3. from pygame.locals import *
  4. import sys, random, time, math


  5. class GameWindow(object):
  6.     """创建游戏窗口类"""

  7.     def __init__(self, *args, **kw):
  8.         self.window_length = 600
  9.         self.window_wide = 500
  10.         # 绘制游戏窗口,设置窗口尺寸
  11.         self.game_window = pygame.display.set_mode((self.window_length, self.window_wide))
  12.         # 设置游戏窗口标题
  13.         pygame.display.set_caption("CatchBallGame")
  14.         # 定义游戏窗口背景颜色参数
  15.         self.window_color = (135, 206, 250)

  16.     def backgroud(self):
  17.         # 绘制游戏窗口背景颜色
  18.         self.game_window.fill(self.window_color)


  19. class Ball(object):
  20.     '''创建球类'''

  21.     def __init__(self, *args, **kw):
  22.         # 设置球的半径、颜色、移动速度参数
  23.         self.ball_color = (255, 215, 0)
  24.         self.move_x = 1
  25.         self.move_y = 1
  26.         self.radius = 10

  27.     def ballready(self):
  28.         # 设置球的初始位置、
  29.         self.ball_x = self.mouse_x
  30.         self.ball_y = self.window_wide - self.rect_wide - self.radius
  31.         # 绘制球,设置反弹触发条件
  32.         pygame.draw.circle(self.game_window, self.ball_color, (self.ball_x, self.ball_y), self.radius)

  33.     def ballmove(self):
  34.         # 绘制球,设置反弹触发条件
  35.         pygame.draw.circle(self.game_window, self.ball_color, (self.ball_x, self.ball_y), self.radius)
  36.         self.ball_x += self.move_x
  37.         self.ball_y -= self.move_y
  38.         # 调用碰撞检测函数
  39.         self.ball_window()
  40.         self.ball_rect()
  41.         # 每接5次球球速增加一倍
  42.         if self.distance < self.radius:
  43.             self.frequency += 1
  44.             if self.frequency == 5:
  45.                 self.frequency = 0
  46.                 self.move_x += self.move_x
  47.                 self.move_y += self.move_y
  48.                 self.point += self.point
  49.         # 设置游戏失败条件
  50.         if self.ball_y > 520:
  51.             self.gameover = self.over_font.render("Game Over", False, (0, 0, 0))
  52.             self.game_window.blit(self.gameover, (100, 130))
  53.             self.over_sign = 1


  54. class Rect(object):
  55.     '''创建球拍类'''

  56.     def __init__(self, *args, **kw):
  57.         # 设置球拍颜色参数
  58.         self.rect_color = (255, 0, 0)
  59.         self.rect_length = 100
  60.         self.rect_wide = 10

  61.     def rectmove(self):
  62.         # 获取鼠标位置参数
  63.         self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
  64.         # 绘制球拍,限定横向边界
  65.         if self.mouse_x >= self.window_length - self.rect_length // 2:
  66.             self.mouse_x = self.window_length - self.rect_length // 2
  67.         if self.mouse_x <= self.rect_length // 2:
  68.             self.mouse_x = self.rect_length // 2
  69.         pygame.draw.rect(self.game_window, self.rect_color, (
  70.         (self.mouse_x - self.rect_length // 2), (self.window_wide - self.rect_wide), self.rect_length, self.rect_wide))


  71. class Brick(object):
  72.     def __init__(self, *args, **kw):
  73.         # 设置砖块颜色参数
  74.         self.brick_color = (139, 126, 102)
  75.         self.brick_list = [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1],
  76.                            [1, 1, 1, 1, 1, 1]]
  77.         self.brick_length = 80
  78.         self.brick_wide = 20

  79.     def brickarrange(self):
  80.         for i in range(5):
  81.             for j in range(6):
  82.                 self.brick_x = j * (self.brick_length + 24)
  83.                 self.brick_y = i * (self.brick_wide + 20) + 40
  84.                 if self.brick_list[i][j] == 1:
  85.                     # 绘制砖块
  86.                     pygame.draw.rect(self.game_window, self.brick_color,
  87.                                      (self.brick_x, self.brick_y, self.brick_length, self.brick_wide))
  88.                     # 调用碰撞检测函数
  89.                     self.ball_brick()
  90.                     if self.distanceb < self.radius:
  91.                         self.brick_list[i][j] = 0
  92.                         self.score += self.point
  93.         # 设置游戏胜利条件
  94.         if self.brick_list == [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
  95.                                [0, 0, 0, 0, 0, 0]]:
  96.             self.win = self.win_font.render("You Win", False, (0, 0, 0))
  97.             self.game_window.blit(self.win, (100, 130))
  98.             self.win_sign = 1


  99. class Score(object):
  100.     '''创建分数类'''

  101.     def __init__(self, *args, **kw):
  102.         # 设置初始分数
  103.         self.score = 0
  104.         # 设置分数字体
  105.         self.score_font = pygame.font.SysFont('arial', 20)
  106.         # 设置初始加分点数
  107.         self.point = 1
  108.         # 设置初始接球次数
  109.         self.frequency = 0

  110.     def countscore(self):
  111.         # 绘制玩家分数
  112.         my_score = self.score_font.render(str(self.score), False, (255, 255, 255))
  113.         self.game_window.blit(my_score, (555, 15))


  114. class GameOver(object):
  115.     '''创建游戏结束类'''

  116.     def __init__(self, *args, **kw):
  117.         # 设置Game Over字体
  118.         self.over_font = pygame.font.SysFont('arial', 80)
  119.         # 定义GameOver标识
  120.         self.over_sign = 0


  121. class Win(object):
  122.     '''创建游戏胜利类'''

  123.     def __init__(self, *args, **kw):
  124.         # 设置You Win字体
  125.         self.win_font = pygame.font.SysFont('arial', 80)
  126.         # 定义Win标识
  127.         self.win_sign = 0


  128. class Collision(object):
  129.     '''碰撞检测类'''

  130.     # 球与窗口边框的碰撞检测
  131.     def ball_window(self):
  132.         if self.ball_x <= self.radius or self.ball_x >= (self.window_length - self.radius):
  133.             self.move_x = -self.move_x
  134.         if self.ball_y <= self.radius:
  135.             self.move_y = -self.move_y

  136.     # 球与球拍的碰撞检测
  137.     def ball_rect(self):
  138.         # 定义碰撞标识
  139.         self.collision_sign_x = 0
  140.         self.collision_sign_y = 0

  141.         if self.ball_x < (self.mouse_x - self.rect_length // 2):
  142.             self.closestpoint_x = self.mouse_x - self.rect_length // 2
  143.             self.collision_sign_x = 1
  144.         elif self.ball_x > (self.mouse_x + self.rect_length // 2):
  145.             self.closestpoint_x = self.mouse_x + self.rect_length // 2
  146.             self.collision_sign_x = 2
  147.         else:
  148.             self.closestpoint_x = self.ball_x
  149.             self.collision_sign_x = 3

  150.         if self.ball_y < (self.window_wide - self.rect_wide):
  151.             self.closestpoint_y = (self.window_wide - self.rect_wide)
  152.             self.collision_sign_y = 1
  153.         elif self.ball_y > self.window_wide:
  154.             self.closestpoint_y = self.window_wide
  155.             self.collision_sign_y = 2
  156.         else:
  157.             self.closestpoint_y = self.ball_y
  158.             self.collision_sign_y = 3
  159.         # 定义球拍到圆心最近点与圆心的距离
  160.         self.distance = math.sqrt(
  161.             math.pow(self.closestpoint_x - self.ball_x, 2) + math.pow(self.closestpoint_y - self.ball_y, 2))
  162.         # 球在球拍上左、上中、上右3种情况的碰撞检测
  163.         if self.distance < self.radius and self.collision_sign_y == 1 and (
  164.                 self.collision_sign_x == 1 or self.collision_sign_x == 2):
  165.             if self.collision_sign_x == 1 and self.move_x > 0:
  166.                 self.move_x = - self.move_x
  167.                 self.move_y = - self.move_y
  168.             if self.collision_sign_x == 1 and self.move_x < 0:
  169.                 self.move_y = - self.move_y
  170.             if self.collision_sign_x == 2 and self.move_x < 0:
  171.                 self.move_x = - self.move_x
  172.                 self.move_y = - self.move_y
  173.             if self.collision_sign_x == 2 and self.move_x > 0:
  174.                 self.move_y = - self.move_y
  175.         if self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3:
  176.             self.move_y = - self.move_y
  177.         # 球在球拍左、右两侧中间的碰撞检测
  178.         if self.distance < self.radius and self.collision_sign_y == 3:
  179.             self.move_x = - self.move_x

  180.     # 球与砖块的碰撞检测
  181.     def ball_brick(self):
  182.         # 定义碰撞标识
  183.         self.collision_sign_bx = 0
  184.         self.collision_sign_by = 0

  185.         if self.ball_x < self.brick_x:
  186.             self.closestpoint_bx = self.brick_x
  187.             self.collision_sign_bx = 1
  188.         elif self.ball_x > self.brick_x + self.brick_length:
  189.             self.closestpoint_bx = self.brick_x + self.brick_length
  190.             self.collision_sign_bx = 2
  191.         else:
  192.             self.closestpoint_bx = self.ball_x
  193.             self.collision_sign_bx = 3

  194.         if self.ball_y < self.brick_y:
  195.             self.closestpoint_by = self.brick_y
  196.             self.collision_sign_by = 1
  197.         elif self.ball_y > self.brick_y + self.brick_wide:
  198.             self.closestpoint_by = self.brick_y + self.brick_wide
  199.             self.collision_sign_by = 2
  200.         else:
  201.             self.closestpoint_by = self.ball_y
  202.             self.collision_sign_by = 3
  203.         # 定义砖块到圆心最近点与圆心的距离
  204.         self.distanceb = math.sqrt(
  205.             math.pow(self.closestpoint_bx - self.ball_x, 2) + math.pow(self.closestpoint_by - self.ball_y, 2))
  206.         # 球在砖块上左、上中、上右3种情况的碰撞检测
  207.         if self.distanceb < self.radius and self.collision_sign_by == 1 and (
  208.                 self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
  209.             if self.collision_sign_bx == 1 and self.move_x > 0:
  210.                 self.move_x = - self.move_x
  211.                 self.move_y = - self.move_y
  212.             if self.collision_sign_bx == 1 and self.move_x < 0:
  213.                 self.move_y = - self.move_y
  214.             if self.collision_sign_bx == 2 and self.move_x < 0:
  215.                 self.move_x = - self.move_x
  216.                 self.move_y = - self.move_y
  217.             if self.collision_sign_bx == 2 and self.move_x > 0:
  218.                 self.move_y = - self.move_y
  219.         if self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3:
  220.             self.move_y = - self.move_y
  221.         # 球在砖块下左、下中、下右3种情况的碰撞检测
  222.         if self.distanceb < self.radius and self.collision_sign_by == 2 and (
  223.                 self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
  224.             if self.collision_sign_bx == 1 and self.move_x > 0:
  225.                 self.move_x = - self.move_x
  226.                 self.move_y = - self.move_y
  227.             if self.collision_sign_bx == 1 and self.move_x < 0:
  228.                 self.move_y = - self.move_y
  229.             if self.collision_sign_bx == 2 and self.move_x < 0:
  230.                 self.move_x = - self.move_x
  231.                 self.move_y = - self.move_y
  232.             if self.collision_sign_bx == 2 and self.move_x > 0:
  233.                 self.move_y = - self.move_y
  234.         if self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3:
  235.             self.move_y = - self.move_y
  236.         # 球在砖块左、右两侧中间的碰撞检测
  237.         if self.distanceb < self.radius and self.collision_sign_by == 3:
  238.             self.move_x = - self.move_x


  239. class Main(GameWindow, Rect, Ball, Brick, Collision, Score, Win, GameOver):
  240.     '''创建主程序类'''

  241.     def __init__(self, *args, **kw):
  242.         super(Main, self).__init__(*args, **kw)
  243.         super(GameWindow, self).__init__(*args, **kw)
  244.         super(Rect, self).__init__(*args, **kw)
  245.         super(Ball, self).__init__(*args, **kw)
  246.         super(Brick, self).__init__(*args, **kw)
  247.         super(Collision, self).__init__(*args, **kw)
  248.         super(Score, self).__init__(*args, **kw)
  249.         super(Win, self).__init__(*args, **kw)
  250.         # 定义游戏开始标识
  251.         start_sign = 0

  252.         while True:
  253.             self.backgroud()
  254.             self.rectmove()
  255.             self.countscore()

  256.             if self.over_sign == 1 or self.win_sign == 1:
  257.                 break
  258.             # 获取游戏窗口状态
  259.             for event in pygame.event.get():
  260.                 if event.type == pygame.QUIT:
  261.                     sys.exit()
  262.                 if event.type == MOUSEBUTTONDOWN:
  263.                     pressed_array = pygame.mouse.get_pressed()
  264.                     if pressed_array[0]:
  265.                         start_sign = 1
  266.             if start_sign == 0:
  267.                 self.ballready()
  268.             else:
  269.                 self.ballmove()

  270.             self.brickarrange()

  271.             # 更新游戏窗口
  272.             pygame.display.update()
  273.             # 控制游戏窗口刷新频率
  274.             time.sleep(0.010)


  275. if __name__ == '__main__':
  276.     pygame.init()
  277.     pygame.font.init()
  278.     catchball = Main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-3 22:04:25 | 显示全部楼层

您这是百度的代码答案啊 我想要知道的是能不能偷懒直接用碰撞检测函数来完成
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-3 22:06:04 | 显示全部楼层
Folly 发表于 2020-5-3 22:04
您这是百度的代码答案啊 我想要知道的是能不能偷懒直接用碰撞检测函数来完成

我没学到这。。。代码是哪的我忘了,两个月前弄的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-3 22:07:52 | 显示全部楼层
乘号 发表于 2020-5-3 22:06
我没学到这。。。代码是哪的我忘了,两个月前弄的

我是看到小甲鱼视频里面可以直接用一个内置的碰撞检测函数来判断是否发生碰撞 而不是像这样用数学方法重新算一遍
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-3 22:08:38 | 显示全部楼层
Folly 发表于 2020-5-3 22:07
我是看到小甲鱼视频里面可以直接用一个内置的碰撞检测函数来判断是否发生碰撞 而不是像这样用数学方法重 ...

ummmmmmm
@zltzlt
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 17:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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