gpt4 book ai didi

python - 多边形与圆的碰撞检测

转载 作者:行者123 更新时间:2023-12-01 01:42:44 25 4
gpt4 key购买 nike

我想知道足球比赛中多边形和圆形之间的碰撞检测在 pygame 中是如何工作的。我想做的是让一个球朝汽车撞击的方向移动。请有人修复我的代码,我被困住了!如果运行以下代码,两辆车将会移动,但如果它们击中球,则不会发生任何事情,并且不会出现错误消息(第 26 行是我尝试使其工作的尝试)。对于初学者来说,我只想打印(“Hello”)。如果有人可以提供帮助,我将非常感激。谢谢您

import pygame
from pygame.math import Vector2


pygame.init()
screen = pygame.display.set_mode((1150, 800))

redx = 50
redy = 30
bluex = 100
bluey = 30
clock = pygame.time.Clock()
BLUECAR_ORIGINAL = pygame.Surface((bluex, bluey), pygame.SRCALPHA)
pygame.draw.polygon(
BLUECAR_ORIGINAL, (0, 0, 255), [(0, 30), (50, 20), (50, 10), (0, 0)])
bluecar = BLUECAR_ORIGINAL

REDCAR_ORIGINAL = pygame.Surface((redx,redy), pygame.SRCALPHA)
pygame.draw.polygon(
REDCAR_ORIGINAL, (255, 0, 0), [(0, 0), (50, 10), (50, 20), (0, 30)])
redcar = REDCAR_ORIGINAL



def paddle_hit():
if pygame.sprite.collide_rect(ball, bluerect):
print("HI")
elif pygame.sprite.collide_rect(ball, redrect):
print("hello")









pos = Vector2(70, 70)
vel = Vector2(7, 0)

poss = Vector2(70,70)
vell = Vector2(7,0)

redrect = redcar.get_rect(center=pos)
redangle = 0

bluerect = bluecar.get_rect(center=pos)
blueangle = 0

ballx = 575
bally = 400




run = True
while run:
ball = pygame.draw.circle(screen, [0,0,0],[ballx,bally],30)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False






keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
redangle += 5
vel.rotate_ip(-5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)

redrect = redcar.get_rect(center=pos)
elif keys[pygame.K_RIGHT]:
redangle -= 5
vel.rotate_ip(5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=pos)


if keys[pygame.K_a]:
blueangle += 5
vell.rotate_ip(-5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=pos)
elif keys[pygame.K_d]:
blueangle -= 5
vell.rotate_ip(5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=poss)




pos += vel
redrect.center = pos

poss += vell
bluerect.center = poss

bgImg = pygame.image.load("Football_pitch.png")
screen.blit(bgImg, (0,0))

screen.blit(redcar, redrect)

screen.blit(bluecar, bluerect)






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

pygame.quit()

最佳答案

您可以使用 mask 进行碰撞检测,并将球的速度设置为碰撞玩家的速度。

创建pygame.mask.Mask球、红色和蓝色汽车的对象(使用 pygame.mask.from_surface )(当汽车旋转时,您必须创建新的蒙版)。

while 循环中,计算汽车和球之间的偏移,然后调用 overlap方法来查看它们是否重叠。如果 mask 发生碰撞,则返回碰撞点,否则None。因此,如果 overlap 不返回 None,您可以将球的速度设置为玩家的速度,它会朝相同的方向移动。缩放速度矢量来踢球。

import pygame
from pygame.math import Vector2


pygame.init()
screen = pygame.display.set_mode((1150, 800))
clock = pygame.time.Clock()
# Images.
BG_IMG = pygame.Surface((1150, 800))
BG_IMG.fill((30, 120, 30))
BLUECAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
BLUECAR_ORIGINAL, (0, 0, 255), [(0, 30), (50, 20), (50, 10), (0, 0)])
bluecar = BLUECAR_ORIGINAL
REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
REDCAR_ORIGINAL, (255, 0, 0), [(0, 0), (50, 10), (50, 20), (0, 30)])
redcar = REDCAR_ORIGINAL

BALL = pygame.Surface((30, 30), pygame.SRCALPHA)
pygame.draw.circle(BALL, [250,250,250], [15, 15], 15)
# Ball variables.
ball_pos = Vector2(575, 400)
ballrect = BALL.get_rect(center=ball_pos)
ball_vel = Vector2(0, 0)
# Car variables.
pos_red = Vector2(470, 370)
vel_red = Vector2(3, 0)
redrect = redcar.get_rect(center=pos_red)
redangle = 0
pos_blue = Vector2(70,70)
vel_blue = Vector2(3,0)
bluerect = bluecar.get_rect(center=pos_red)
blueangle = 0
# Masks.
mask_blue = pygame.mask.from_surface(bluecar)
mask_red = pygame.mask.from_surface(redcar)
mask_ball = pygame.mask.from_surface(BALL)

run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
redangle += 5
vel_red.rotate_ip(-5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
# We need a new mask after the rotation.
mask_red = pygame.mask.from_surface(redcar)
elif keys[pygame.K_RIGHT]:
redangle -= 5
vel_red.rotate_ip(5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
mask_red = pygame.mask.from_surface(redcar)

if keys[pygame.K_a]:
blueangle += 5
vel_blue.rotate_ip(-5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=bluerect.center)
mask_blue = pygame.mask.from_surface(bluecar)
elif keys[pygame.K_d]:
blueangle -= 5
vel_blue.rotate_ip(5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=bluerect.center)
mask_blue = pygame.mask.from_surface(bluecar)

# Move the cars.
pos_red += vel_red
redrect.center = pos_red
pos_blue += vel_blue
bluerect.center = pos_blue
# Move the ball.
ball_vel *= .99 # Friction.
ball_pos += ball_vel
ballrect.center = ball_pos

# Red car collision.
# We need the offset between the redrect and the ballrect.
offset_red = redrect[0] - ballrect[0], redrect[1] - ballrect[1]
# Pass the offset to the `overlap` method. If the masks collide,
# overlap will return a single point, otherwise `None`.
overlap_red = mask_ball.overlap(mask_red, offset_red)
# Blue car collision.
offset_blue = bluerect[0] - ballrect[0], bluerect[1] - ballrect[1]
overlap_blue = mask_ball.overlap(mask_blue, offset_blue)

if overlap_red and overlap_blue: # Both collide with the ball.
# Not sure what should happen here.
ball_vel = vel_red + vel_blue * 1.4
elif overlap_red: # Red collides with the ball.
ball_vel = Vector2(vel_red) * 1.4
elif overlap_blue: # Blue collides with the ball.
ball_vel = Vector2(vel_blue) * 1.4

# Drawing.
screen.blit(BG_IMG, (0, 0))
screen.blit(BALL, ballrect)
screen.blit(redcar, redrect)
screen.blit(bluecar, bluerect)
pygame.display.flip()
clock.tick(60)

pygame.quit()

关于python - 多边形与圆的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51705239/

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