gpt4 book ai didi

python - 碰撞Pygame后穿过墙壁

转载 作者:太空宇宙 更新时间:2023-11-04 05:50:40 24 4
gpt4 key购买 nike

大家好,我需要帮助解决 pygame 上的碰撞问题。我有一个边界图,可以通过使用蒙版重叠来设置像素完美碰撞。检测到碰撞没有任何问题,但是当涉及到阻止玩家穿过墙壁时,总是有一些方法可以穿过它。我已经做了几次尝试(真的很多),包括当你一次按下三个按钮时甚至卡住播放器以避免它发出嘶嘶声,但无济于事。

现在,我检查用户按下哪个按钮来设置方向:

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key = pygame.key.get_pressed()
if key[pygame.K_w]:
COLLISION_DIRECTION = "UP"
if key[pygame.K_s]:
COLLISION_DIRECTION = "DOWN"
if key[pygame.K_d]:
COLLISION_DIRECTION = "RIGHT"
if key[pygame.K_a]:
COLLISION_DIRECTION = "LEFT"

然后根据方向检查碰撞。在我最后一次孤注一掷的尝试中,我求助于将最后一个非碰撞位置保存在数组上,将世界(我正在移动世界,而不是玩家)平移到该位置并减去或增加移动速度:

if boundariesMap_mask.overlap(player.mask, (offset_x, offset_y)) is None:
lastPos = [world.x, world.y]
else:
while COLLISION_DIRECTION == "UP":
world.y = (lastPos[1]-DIST_WORLD)
break
while COLLISION_DIRECTION == "DOWN":
world.y = (lastPos[1]+DIST_WORLD)
break
while COLLISION_DIRECTION == "LEFT":
world.x = (lastPos[0]-DIST_WORLD)
break
while COLLISION_DIRECTION == "RIGHT":
world.x = (lastPos[0]+DIST_WORLD)
break

我最好的猜测是,我应该在返回安全位置时阻止所有额外的输入,以避免更改 COLLISION_DIRECTION。你们能帮帮我吗?

world.x和world.y是世界的可变坐标

编辑:我忘了添加世界构造函数类

class World(pygame.sprite.Sprite):
def __init__(self):
self.sprite = pygame.image.load('bin\\assets\\test.png')
self.x = 220
self.y = 45

def draw(self, surface):
surface.blit(self.sprite, (self.x, self.y))

def handle_keys(self):

key = pygame.key.get_pressed()
if pygame.joystick.get_count():
joystick_x = round(my_joystick.get_axis(0))
joystick_y = round(my_joystick.get_axis(1))

if joystick_x < 0:
self.x += DIST_WORLD
if joystick_x > 0:
self.x -= DIST_WORLD
if joystick_y < 0:
self.y += DIST_WORLD
if joystick_y > 0:
self.y -= DIST_WORLD

if key[pygame.K_w]:
self.y += DIST_WORLD
if key[pygame.K_s]:
self.y -= DIST_WORLD
if key[pygame.K_a]:
self.x += DIST_WORLD
if key[pygame.K_d]:
self.x -= DIST_WORLD

最佳答案

从问题和您在评论中的回复来看,似乎每当注册三个或更多按键时就会触发故障,导致 COLLISION_DIRECTIONWorld.yWorld.x更新不当。

考虑这样一个事实,即如果玩家沿 X 轴或 Y 轴的某个方向移动,他们不能同时沿相反方向移动。还要考虑一个事实,即主游戏循环中的代码允许 COLLISION_DIRECTION 的值。如果玩家同时按下多个键,则被覆盖;例如,如果同时按下 W、A 和 S,最后的 COLLISION_DIRECTION将是 "LEFT" .

我假设您允许对角线运动,所以我们在这里要做的是制作 COLLISION_DETECTION同时对 x 轴和 y 轴有意义。然后,我们必须将按键检查的次数限制为恰好两次(一次用于 x 轴,一次用于 y 轴),这样三键问题就不会发生。

在你的主游戏循环中:

## Instead of one COLLISION_DIRECTION, we now have two separate variables:
## COLLISION_DIRECTION_X and COLLISION_DIRECTION_Y

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key = pygame.key.get_pressed()

## Using elif so we don't mess up COLLISION_DIRECTION_Y
if key[pygame.K_w]:
COLLISION_DIRECTION_Y = "UP"
elif key[pygame.K_s]:
COLLISION_DIRECTION_Y = "DOWN"

## Using elif so we don't mess up COLLISION_DIRECTION_X
if key[pygame.K_d]:
COLLISION_DIRECTION_X = "RIGHT"
elif key[pygame.K_a]:
COLLISION_DIRECTION_X = "LEFT"

相应地更新您的碰撞检测:

 if boundariesMap_mask.overlap(player.mask, (offset_x, offset_y)) is None:
lastPos = [world.x, world.y]
else:
## Redundant while-break replaced with simple if-statements;
## Using while is not exactly necessary because we carry out
## these statements each frame

if COLLISION_DIRECTION_Y == "UP":
world.y = (lastPos[1]-DIST_WORLD)
elif COLLISION_DIRECTION_Y == "DOWN":
world.y = (lastPos[1]+DIST_WORLD)

if COLLISION_DIRECTION_X == "LEFT":
world.x = (lastPos[0]-DIST_WORLD)
elif COLLISION_DIRECTION_X == "RIGHT":
world.x = (lastPos[0]+DIST_WORLD)

然后,在您的 handle_keys 中功能:

 def handle_keys(self):

key = pygame.key.get_pressed()
if pygame.joystick.get_count():
joystick_x = round(my_joystick.get_axis(0))
joystick_y = round(my_joystick.get_axis(1))

## Restrict joystick x-input checking, because we logically
## cannot be both greater and less than zero

if joystick_x < 0:
self.x += DIST_WORLD
elif joystick_x > 0:
self.x -= DIST_WORLD

## Restrict joystick y-input checking, because we logically
## cannot be both greater and less than zero

if joystick_y < 0:
self.y += DIST_WORLD
elif joystick_y > 0:
self.y -= DIST_WORLD

## Restrict keypress vertical axis checking for the same reason
if key[pygame.K_w]:
self.y += DIST_WORLD
elif key[pygame.K_s]:
self.y -= DIST_WORLD

## Restrict keypress horizontal axis checking for same reason
if key[pygame.K_a]:
self.x += DIST_WORLD
elif key[pygame.K_d]:
self.x -= DIST_WORLD

希望对您有所帮助!如果代码没有做到这一点,我深表歉意,但至少,我希望我在代码之前的评论能提供一些见解。

关于python - 碰撞Pygame后穿过墙壁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30387081/

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