【问题标题】:PyGame Collisions does only work on one side of the rectangle [duplicate]PyGame Collisions 仅适用于矩形的一侧[重复]
【发布时间】:2020-10-20 10:43:31
【问题描述】:

我只是在尝试一些碰撞,并找到了检查矩形一侧与另一侧的方法。

我有以下问题:

如果我将我的游戏角色(粉红色框)从左侧移到对象上,我的游戏角色就会穿过它:

如果我来自另一边,一切正常,我的游戏角色停止。

我的意思是说我需要为双方提供相同的代码,但必须将双方从if not player_rect.left == other_rect.right: 更改为 if not player_rect.right == other_rect.left:。但这不适用于一方。

import pygame
import sys

pygame.init()

clock = pygame.time.Clock()
window = pygame.display.set_mode([1200, 800])
pygame.display.set_caption("Collision Test")

x = 300
y = 300
width = 48
height = 96
velocity = 5

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

        is_pressed = pygame.key.get_pressed()
        player_rect = pygame.Rect(x, y, width, height)
        other_rect = pygame.Rect(400, 300, 50, 50)

        if is_pressed[pygame.K_d]:
            if not player_rect.right == other_rect.left:
                x += velocity
        if is_pressed[pygame.K_a]:
            if not player_rect.left == other_rect.right:
                x -= velocity

    window.fill((100, 150, 50))
    pygame.draw.rect(window, (255, 50, 100), player_rect)
    pygame.draw.rect(window, (255, 100, 50), other_rect)
    pygame.display.update()
    clock.tick(60)

【问题讨论】:

    标签: python pygame collision-detection


    【解决方案1】:

    使用collideRect()

    移动对象并测试矩形是否发生碰撞。当检测到碰撞时,根据移动方向改变物体的位置:

    is_pressed = pygame.key.get_pressed()
    move_right = is_pressed[pygame.K_d]
    move_left = is_pressed[pygame.K_a]
    
    if move_right:
        x += velocity
    if move_left:
        x -= velocity
    
    player_rect = pygame.Rect(x, y, width, height)
    other_rect = pygame.Rect(400, 300, 50, 50)
    
    if player_rect.colliderect(other_rect):
        if move_right:
            player_rect.right = other_rect.left
            x = player_rect.left
        if move_left:
            player_rect.left = other_rect.right
            x = player_rect.left
    

    为了平稳移动,您必须在应用程序循环而不是事件循环中评估 pygame.key.get_pressed()
    另见What all things happens inside pygame when I press a key? When to use pygame.event==KEYDOWN

    import pygame
    import sys
    
    pygame.init()
    
    clock = pygame.time.Clock()
    window = pygame.display.set_mode([1200, 800])
    pygame.display.set_caption("Collision Test")
    
    x = 300
    y = 300
    width = 48
    height = 96
    velocity = 5
    
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
    
        is_pressed = pygame.key.get_pressed()
        move_right = is_pressed[pygame.K_d]
        move_left = is_pressed[pygame.K_a]
    
        if move_right:
            x += velocity
        if move_left:
            x -= velocity
    
        player_rect = pygame.Rect(x, y, width, height)
        other_rect = pygame.Rect(400, 300, 50, 50)
    
        if player_rect.colliderect(other_rect):
            if move_right:
                player_rect.right = other_rect.left
                x = player_rect.left
            if move_left:
                player_rect.left = other_rect.right
                x = player_rect.left
    
        window.fill((100, 150, 50))
        pygame.draw.rect(window, (255, 50, 100), player_rect)
        pygame.draw.rect(window, (255, 100, 50), other_rect)
        pygame.display.update()
        clock.tick(60)
    

    【讨论】:

    • 现在我如何实现跳跃或重力。当然这应该适用于上方和下方的碰撞。我已经尝试了很多东西,但我找不到解决方案。我添加了一个跳跃,但后来我在矩形上,当我试图向下跑时,我不再摔倒了。请帮帮我
    • @Max 如果您有新问题,请Ask a public question。 cmets 部分不用于提出其他问题。也不要扩展这个问题,你将不得不提出一个全新的问题。请在新问题中分享您到目前为止所尝试的内容。
    • 好的,抱歉。但它说我需要再等 2 天才能提出一个新问题。
    • @Max 你确定吗。即使这个问题被赞成并且有一个接受的答案,你是否需要等待?
    • "您的问题已达到上限 看来您可能需要休息一下了 - 稍作休息,稍后再回来!您最近提出了 2 个问题,其中一些问题没有得到很好的答复……”好像是这样:/
    猜你喜欢
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多