pygame圈子碰撞? [英] Pygame circle collision?

查看:106
本文介绍了pygame圈子碰撞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pygame制作一个简单的游戏。我遇到圆碰撞的问题。我收到以下错误:

AtributeError:'pygame.Rect'对象没有属性'rect'

I am using pygame to make a simple game. I am having issues with circle collisions. I am getting the following error:
"AtributeError: 'pygame.Rect' object has no attrinute 'rect'"

这是特定的代码我遇到以下问题:

Here is the particular code I am having issues with below:

    if pygame.sprite.collide_circle(hero_circle, enemy_circle):
          gameover()

预先感谢!

推荐答案

使用 pygame.mask 为对象创建碰撞网格,并使用该网格进行碰撞检测。

Use pygame.mask to create a collision mesh for your objects and use the mesh to do collision detections.

更多信息:


  1. 为两个圆圈创建一个图像文件,并将bg颜色设置为您要设置的颜色不会在其他任何地方使用。

  2. 在图像编辑器中将该颜色设置为透明。

  3. 导入图像。

  4. 使用pygame.mask为它们创建一个网格,并将其设置为不透明像素。

  5. 将生成的蒙版用作碰撞检测网格。

  6. 利润

  1. Create an image file for both of your circles and set the bg color to something you will not use anywhere else.
  2. Set that color to "transparent" in your image editor.
  3. Import the images.
  4. Create a mesh for them with pygame.mask and set it to make transparent pixels non-collidable.
  5. Use the generated mask as your collision detection mesh.
  6. PROFIT

(从技术上讲,这只是对矩形上的圆形区域进行碰撞检测,但谁在乎呢!)

(Technically this is just doing collision detection of a circle shaped area on a rectangle, but who cares!)

这篇关于pygame圈子碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆