我正在尝试检测 pygame 中两个移动 Rect 之间的碰撞

I'm trying to detect the collision between two moving Rects in pygame

所以我一直试图检测两个不同的移动矩形 pygame 碰撞,但它只是不断地记录碰撞。正如在控制台中不断打印失败,但敌人甚至没有碰到球。我想要的效果只是一个简单的游戏,球必须避开大头钉,这样球才不会弹出。

import pygame
import sys
import math
import random
pygame.init()



size = width, height = 800, 600
white = 255, 255, 255
red = 255, 0, 0
clock = pygame.time.Clock()

screen = pygame.display.set_mode(size)

character = pygame.image.load("intro_ball.gif")
charrect = character.get_rect()
x = 340
y = 480
enemy = pygame.image.load("ho.png")
enrect = enemy.get_rect()
ex = random.randint(0, 690)
ey = 0
lose = False




while 1:
     clock.tick(60)
     for event in pygame.event.get():
          if event.type == pygame.QUIT: sys.exit()


     if ey >= 600 and lose != True:
          ey = 0
          ex = random.randint(0, 690)

     collide = pygame.Rect.colliderect(charrect, enrect)


     if event.type == pygame.KEYDOWN:
          if event.key == pygame.K_RIGHT and x < 690:
          x += 4
     if event.key == pygame.K_LEFT and x > 0:
        x -= 4

     if collide:
        lose = True
     else: lose = False

     if lose == True: print("lose")




    ey += 2        
    screen.fill(white)
    screen.blit(enemy, (ex, ey))
    screen.blit(character, (x, y))
    pygame.display.update()

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the top lelft of the rectangle can be specified with the keyword argument topleft. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect 关键字参数的完整列表):

while 1:
    # [...]

    charrect = character.get_rect(topleft = (x, y))
    enrect = enemy.get_rect(topleft = (ex, ey))
    collide = pygame.Rect.colliderect(charrect, enrect)

    # [...]

另一方面,您根本不需要变量 xyexey。使用 pygame.Rect 对象的功能。
另外阅读 How can I make a sprite move when key is held down

最小示例:

import pygame, sys, math, random
pygame.init()

size = width, height = 800, 600
white = 255, 255, 255
red = 255, 0, 0
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)

#character = pygame.image.load("intro_ball.gif")
character = pygame.Surface((20, 20))
character.fill((0, 255, 0))
charrect = character.get_rect(topleft = (340, 480))
#enemy = pygame.image.load("ho.png")
enemy = pygame.Surface((20, 20))
enemy.fill((255, 0, 0))
enrect = enemy.get_rect(topleft = (random.randint(0, 690), 0))
lose = False

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    keys = pygame.key.get_pressed()
    charrect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 4
    charrect.clamp_ip(screen.get_rect())

    enrect.y += 2     
    if enrect.y >= 600 and lose != True:
        enrect.y = 0
        enrect.x = random.randint(0, 690)

    collide = pygame.Rect.colliderect(charrect, enrect)
    if collide:
        lose = True
    else: 
        lose = False
    if lose == True: 
        print("lose")
   
    screen.fill(white)
    screen.blit(enemy, enrect)
    screen.blit(character, charrect)
    pygame.display.update()

pygame.quit()
exit()