如何在 Pygame 模块中碰撞两个图像

How to collide two images in Pygame module

我是编程新手,知道 Python 的基础知识,想问一下如果两个图像稍微重叠一点,然后在 [=21= 中按下特定按钮,我该如何执行操作]. 游戏如下所示:

import pygame
import random

pygame.init()
window = pygame.display.set_mode((1000, 600))
caption = pygame.display.set_caption(
    'Test your reaction speed. Shoot the target game!')  # sets a caption for the window
game_running = True  # this is the gameloop so the window stays open

PlayerImg = pygame.image.load('F:\PythonPortable\oscn.png')
PlayerX = 370
PlayerY = 420
PlayerX_change = 0
PlayerY_change = 0


def player():
    window.blit(PlayerImg, (PlayerX, PlayerY))


aim_sight = pygame.image.load('F:\PythonPortable\ktarget.png')
aim_sightX = 460
aim_sightY = 300
aim_sight_changeX = 0
aim_sight_changeY = 0


def aim_sight_function(x, y):
    window.blit(aim_sight, (x, y))


targetedImg = pygame.image.load('F:\PythonPortable\ktargetedperson.png')
targetedX = random.randint(0, 872)
targetedY = random.randint(0, 200)


def random_target():
    window.blit(targetedImg, (targetedX, targetedY))


while game_running:
    window.fill((255, 255, 255))
    random_target()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                aim_sight_changeX = -2
                PlayerX_change = -2
            elif event.key == pygame.K_RIGHT:
                aim_sight_changeX = 2
                PlayerX_change = 2
            elif event.key == pygame.K_UP:
                aim_sight_changeY = -2
                PlayerY_change = -2
            elif event.key == pygame.K_DOWN:
                aim_sight_changeY = 2
                PlayerY_change = 2
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                aim_sight_changeX = 0
                PlayerX_change = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                aim_sight_changeY = 0
                PlayerY_change = 0

    aim_sightX += aim_sight_changeX
    if aim_sightX <= 46.5:
        aim_sight_changeX = 0
    elif aim_sightX >= 936:
        aim_sight_changeX = 0
    aim_sightY += aim_sight_changeY
    if aim_sightY <= 0:
        aim_sight_changeY = 0
    elif aim_sightY >= 400:
        aim_sight_changeY = 0

    PlayerX += PlayerX_change
    if PlayerX <= -50:
        PlayerX_change = 0
    elif PlayerX >= 850:
        PlayerX_change = 0
    player()
    aim_sight_function(aim_sightX, aim_sightY)
    pygame.display.update()

我想知道如何做我想做的事。我想也许:

if event.type == pygame.K_SPACE and targetedX == targetedX in range(aim_sightX - 100, aim_sightY + 100) or targetedY == targetedY in range (aim_sightY - 100, aim_sightY + 100):
...

我观看的教程中似乎 none 涵盖了这个主题,并且希望收到有关涵盖该问题的教程的推荐或直接答案。

我建议使用 pygame.Rect objects and colliderect() to find a collision between two Surface objects. pygame.Surface.get_rect.get_rect() returns 大小为 Surface 对象的矩形,它始终从 (0, 0) 开始,因为Surface 对象没有位置。矩形的位置可以通过关键字参数指定:

PlayerImg_rect   = PlayerImg.get_rect(topleft = (PlayerX, PlayerY))
targetedImg_rect = targetedImg .get_rect(topleft = (targetedX, targetedX))

if PlayerImg_rect.colliderect(targetedImg_rect):
    print("hit")