PyGame 碰撞? [英] PyGame Collision?

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

问题描述

如何在 PyGame 中找到角色和图像之间的冲突?我从图像中绘制了一个玩家,并从瓷砖中绘制了墙壁,那么我将如何检测这些碰撞?

How do I find collisions between characters and images within PyGame? I have drawn a player from an image, and have drawn the walls from tiles, so how would I detect these collisions?

推荐答案

如果你使用 pygame Rect 类来表示您的对象的边界,您可以使用 Rect.colliderect 函数检测两个是否发生碰撞.例如:

If you use the pygame Rect class to represent the boundaries of your object, you can detect whether two are colliding by using the Rect.colliderect function. For example:

import pygame

a = pygame.Rect((1, 1), (2, 2))
b = pygame.Rect((0, 0), (2, 2))
c = pygame.Rect((0, 0), (1, 1))
a.colliderect(b)
# 1
a.colliderect(c)
# 0
b.colliderect(c)
# 1

a 与 b 碰撞,b 与 c 碰撞,但 a 未与 c 碰撞.请注意,共享边界的矩形不会发生冲突.

a is colliding with b, and b is colliding with c, but a is not colliding with c. Note that rects that share a boundary are not colliding.

Pygame 还支持让您在想要将图像blit"到屏幕上时使用 Rect 作为图像的位置.

Pygame also supports letting you use a Rect as the position for an image when you want to 'blit' it onto the screen.

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

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