gpt4 book ai didi

python - 使用 Pygame 检测碰撞点

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:43 25 4
gpt4 key购买 nike

我正在使用 Pygame 制作游戏。对于这个游戏,我不仅需要能够检测到两个矩形发生了碰撞,还需要能够检测到它们之间的碰撞点。我查看了文档,但似乎找不到任何答案。

这样的事情可能吗?

最佳答案

您可以使用 Rect.clip :

crops a rectangle inside another

clip(Rect) -> Rect

Returns a new rectangle that is cropped to be completely inside the argument Rect. If the two rectangles do not overlap to begin with, a Rect with 0 size is returned.

这是一个例子:

import pygame
import random

class Stuff(pygame.sprite.Sprite):
def __init__(self, pos, color, *args):
super().__init__(*args)
self.image = pygame.Surface((30, 30))
self.image.fill(color)
self.rect = self.image.get_rect(center=pos)
self.pos = pygame.Vector2(pos)

def update(self):
self.rect.center = self.pos

def main():
pygame.init()
screen = pygame.display.set_mode((500, 500))
screen_rect = screen.get_rect()
font = pygame.font.SysFont(None, 26)
clock = pygame.time.Clock()
sprites = pygame.sprite.Group()
blocks = pygame.sprite.Group()

movement = {
pygame.K_UP: ( 0, -1),
pygame.K_DOWN: ( 0, 1),
pygame.K_LEFT: (-1, 0),
pygame.K_RIGHT: ( 1, 0)
}

for _ in range(15):
x, y = random.randint(0, 500), random.randint(0, 500)
color = random.choice(['green', 'yellow'])
Stuff((x, y), pygame.Color(color), sprites, blocks)

player = Stuff(screen_rect.center, pygame.Color('dodgerblue'))
sprites.add(player)

dt = 0
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return

pressed = pygame.key.get_pressed()
move = pygame.Vector2()
for dir in (movement[key] for key in movement if pressed[key]):
move += dir
if move.length() > 0: move.normalize_ip()
player.pos += move * dt/5

sprites.update()
screen.fill(pygame.Color('black'))
sprites.draw(screen)

for block in pygame.sprite.spritecollide(player, blocks, False):
clip = player.rect.clip(block.rect)
pygame.draw.rect(screen, pygame.Color('red'), clip)
hits = [edge for edge in ['bottom', 'top', 'left', 'right'] if getattr(clip, edge) == getattr(player.rect, edge)]
text = font.render(f'Collision at {", ".join(hits)}', True, pygame.Color('white'))
screen.blit(text, (20, 20))

pygame.display.flip()
dt = clock.tick(60)

if __name__ == '__main__':
main()

enter image description here

关于python - 使用 Pygame 检测碰撞点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55410128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com