如何在 pygame 中检测两个矩形物体或图像之间的碰撞

2024-11-22 08:47:00
admin
原创
143
摘要:问题描述:我正在制作一款游戏,玩家必须使用碗来接住掉落的物品。我有一些列表中物品的图像和一个用于接住物品的碗的图像。物品不断下落,如果到达边界(底部边缘),则会重置到屏幕顶部。我完成了允许物品掉落的逻辑,但我不知道如何检测碗和物品之间何时发生碰撞。我的代码:import math import pygame ...

问题描述:

我正在制作一款游戏,玩家必须使用碗来接住掉落的物品。我有一些列表中物品的图像和一个用于接住物品的碗的图像。物品不断下落,如果到达边界(底部边缘),则会重置到屏幕顶部。我完成了允许物品掉落的逻辑,但我不知道如何检测碗和物品之间何时发生碰撞。

我的代码:

import math
import pygame
import random


pygame.init()

display_width = 800
display_height = 600

game_display = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
pygame.display.set_caption("Catch the Ball")

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 255, 0)

player_img = pygame.image.load("Images/soup.png")
thing_imgs = [pygame.image.load('Images/muffin.png'), pygame.image.load('Images/dessert.png'),
              pygame.image.load('Images/cheese.png'), pygame.image.load('Images/fruit.png')]


def player(x, y):
    game_display.blit(player_img, (x, y))


def things(x, y, img):
    game_display.blit(img, (x, y))


def game_loop():
    running = True

    x = display_width * 0.45
    y = display_height * 0.8
    x_change = 0

    player_width = 64
    player_height = 64

    things_cor = [[random.randint(0, display_width), 32]]
    things_added = [random.choice(thing_imgs)]
    thing_height = 32
    thing_width = 32
    y_change = 5

    caught = 0

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5

                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        game_display.fill(white)

        player(x, y)
        x += x_change

        for i in range(len(things_cor)):
            thing_x, thing_y = things_cor[i]
            things(thing_x, thing_y, things_added[i])

        for i in range(len(things_cor)):
            things_cor[i][1] += y_change
            if things_cor[i][1] > display_height:
                things_cor[i][1] = random.randint(-2000, -1000)
                things_cor[i][0] = random.randint(0, display_width)
                things_added[i] = random.choice(thing_imgs)

                things_added.append(random.choice(thing_imgs))

                if len(things_added) < 6:
                    things_cor.append(
                        [random.randint(0, display_width), -10])

        if x < 0:
            x = 0
        elif x > display_width - player_width:
            x = display_width - player_width

        clock.tick(60)
        pygame.display.update()


game_loop()

解决方案 1:

使用pygame.Rect物体和colliderect()检测2个物体或2个图像的边界矩形之间的碰撞:

rect1 = pygame.Rect(x1, y1, w1, h1)
rect2 = pygame.Rect(x2, y2, w2, h2)
if rect1.colliderect(rect2):
    # [...]

如果有图像(pygame.Surface对象),则可以通过获取的边界矩形,其中Surfaceget_rect()的位置必须由关键字参数设置,因为返回的矩形始终从 (0, 0) 开始:(
请参阅为什么我的碰撞测试不起作用以及为什么图像矩形的位置总是错误的 (0, 0)?)

def game_loop():
    # [...]

    while running:
        # [...]

        player_rect = player_img.get_rect(topleft = (x, y))
        for i in range(len(things_cor)):
            thing_rect = things_added[i].get_rect(topleft = things_cor[i])

            if player_rect.colliderect(thing_rect):
                print("hit")

        player(x, y)
        x += x_change

        for i in range(len(things_cor)):
            thing_x, thing_y = things_cor[i]
            things(thing_x, thing_y, things_added[i]) 

用于pygame.time.get_ticks()将游戏的开始延迟一段时间。pygame.time.get_ticks()返回自调用以来的毫秒数pygame.init()。例如:

def game_loop():
    # [...]

    while running:
        passed_time = pygame.time.get_ticks() # passed time in milliseconds
        start_time = 100 * 1000 # start time in milliseconds (100 seconds)
        
        # [...]

        # move player    
        if passed_time >= start_time:
            x += x_change
            if x < 0:
                x = 0
            elif x > display_width - player_width:
                x = display_width - player_width
    
        # move things
        if passed_time >= start_time:
            for i in range(len(things_cor)):
                things_cor[i][1] += y_change
                if things_cor[i][1] > display_height:
                    things_cor[i][1] = random.randint(-2000, -1000)
                    things_cor[i][0] = random.randint(0, display_width)
                    things_added[i] = random.choice(thing_imgs)

                    things_added.append(random.choice(thing_imgs))

                    if len(things_added) < 6:
                        things_cor.append(
                            [random.randint(0, display_width), -10])

        # draw scene and update dispaly
        game_display.fill(white)
        player(x, y)
        for i in range(len(things_cor)):
            thing_x, thing_y = things_cor[i]
            things(thing_x, thing_y, things_added[i])
        pygame.display.update()
        clock.tick(60)
相关推荐
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1265  
  IPD(Integrated Product Development)即集成产品开发,是一套先进的、成熟的产品开发管理理念、模式和方法。随着市场竞争的日益激烈,企业对于提升产品开发效率、降低成本、提高产品质量的需求愈发迫切,IPD 项目管理咨询市场也迎来了广阔的发展空间。深入探讨 IPD 项目管理咨询的市场需求与发展,...
IPD集成产品开发流程   17  
  IPD(Integrated Product Development)产品开发流程是一套先进的、被广泛应用的产品开发管理体系,它涵盖了从产品概念产生到产品推向市场并持续优化的全过程。通过将市场、研发、生产、销售等多个环节紧密整合,IPD旨在提高产品开发的效率、质量,降低成本,增强企业的市场竞争力。深入了解IPD产品开发...
IPD流程中TR   21  
  IPD(Integrated Product Development)测试流程是确保产品质量、提升研发效率的关键环节。它贯穿于产品从概念到上市的整个生命周期,对企业的成功至关重要。深入理解IPD测试流程的核心要点,有助于企业优化研发过程,打造更具竞争力的产品。以下将详细阐述IPD测试流程的三大核心要点。测试策略规划测试...
华为IPD   18  
  华为作为全球知名的科技企业,其成功背后的管理体系备受关注。IPD(集成产品开发)流程作为华为核心的产品开发管理模式,在创新管理与技术突破方面发挥了至关重要的作用。深入剖析华为 IPD 流程中的创新管理与技术突破,对于众多企业探索自身发展路径具有重要的借鉴意义。IPD 流程概述IPD 流程是一种先进的产品开发管理理念和方...
TR评审   16  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用