如何检测pygame中两个矩形对象或图像之间的碰撞 [英] How to detect collisions between two rectangular objects or images in pygame

查看:66
本文介绍了如何检测pygame中两个矩形对象或图像之间的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a game in which the player has to use a bowl to catch falling items. I have some images of items in a list and an image of a bowl that is used to catch the items. The items keep on falling and reset to the top of the screen if they reach the boundary (bottom edge). I got this logic done which allows the items to fall but I do not know how to detect when there is a collision between the bowl and item.

我的代码:

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()

推荐答案

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

Use pygame.Rect objects and colliderect() to detect the collision between the bounding rectangles of 2 objects or 2 images:

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

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

If you have to images (pygame.Surface objects), the bounding rectangle of can be get by get_rect(), where the location of the Surface has to be set by an keyword argument, since the returned rectangle always starts at (0, 0):
(see Why is my collision test not working and why is the position of the rectangle of the image always wrong (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() 被调用以来的毫秒数.例如:


Use pygame.time.get_ticks() to delay the start of the game for a certain time. pygame.time.get_ticks() return the number of milliseconds since pygame.init() was called. For instance:

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)

这篇关于如何检测pygame中两个矩形对象或图像之间的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆