【问题标题】:Rectangle collision in pygame? (Bumping rectangles into each other)pygame中的矩形碰撞? (将矩形相互碰撞)
【发布时间】:2023-03-16 05:32:02
【问题描述】:

我决定将那个 Squarey 游戏移到 pygame,现在我有 2 个可以移动并撞到墙壁的矩形。但是,矩形可以相互移动。我怎样才能让他们撞到对方并停下来? 我的代码:

import pygame

pygame.init()
screen = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Squarey")
done = False
is_red = True
x = 30
y = 30
x2 = 100
y2 = 30
clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            is_red = not is_red

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]: y -= 3
    if pressed[pygame.K_DOWN]: y += 3
    if pressed[pygame.K_LEFT]: x -= 3
    if pressed[pygame.K_RIGHT]: x += 3
    if pressed[pygame.K_w]: y2 -= 3
    if pressed[pygame.K_s]: y2 += 3
    if pressed[pygame.K_a]: x2 -= 3
    if pressed[pygame.K_d]: x2 += 3

    if y < 0:
        y += 3
    if x > 943:
        x -= 3
    if y > 743:
        y -= 3
    if x < 0:
        x += 3

    if y2 < 0:
        y2 += 3
    if x2 > 943:
        x2 -= 3
    if y2 > 743:
        y2 -= 3
    if x2 < 0:
        x2 += 3

    screen.fill((0, 0, 0))
    if is_red: color = (252, 117, 80)
    else: color = (168, 3, 253)
    if is_red: color2 = (0, 175, 0)
    else: color2 = (255, 255, 0)
    rect1 = pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
    rect2 = pygame.draw.rect(screen, color2, pygame.Rect(x2, y2, 60, 60))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

【问题讨论】:

标签: python pygame collision rectangles


【解决方案1】:

使用pygame.Rect.colliderect

if rect1.colliderect(rect2):
    print("Collision !!")

顺便说一句:您只能在主循环之前创建一次rect1(和rect2),然后您可以使用rect1.xrect1.y 而不是xy。而且您可以使用pygame.draw.rect(screen, color, rect1),而无需一直创建新的 Rect。

矩形很有用

 # create

 rect1 = pygame.Rect(30, 30, 60, 60)

 # move

 rect1.x += 3

 # check colision with bottom of the screen

 if rect1.bottom > screen.get_rect().bottom:

 # center on the screen

 rect1.center = screen.get_rect().center

【讨论】:

    【解决方案2】:

    要检查冲突,请尝试以下操作:

    def doRectsOverlap(rect1, rect2):
          for a, b in [(rect1, rect2), (rect2, rect1)]:
              # Check if a's corners are inside b
              if ((isPointInsideRect(a.left, a.top, b)) or
                  (isPointInsideRect(a.left, a.bottom, b)) or
                  (isPointInsideRect(a.right, a.top, b)) or
                  (isPointInsideRect(a.right, a.bottom, b))):
                   return True
    
          return False
    
      def isPointInsideRect(x, y, rect):
          if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
              return True
          else:
              return False
    

    然后,在移动它们的同时,你可以调用

    if doRectsOverlap(rect1, rect2):
        x -= 3
        y -= 3
        rect1 = pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
    

    或者类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多