PyGame碰撞? [英] PyGame Collision?
问题描述
如何在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还支持让您将Rect用作将图像 blit到图像上的位置。屏幕。
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屋!