如果我在Pygame中有两个大小不同的矩形(10 x 10和20 x 20),那么如何确定两个矩形发生碰撞?

时间:2019-10-14 02:31:01

标签: python pygame

我无法检测到两个大小不同的矩形何时发生碰撞。

我尝试过“如果x == obj_x和y == obj_y:”,其中x是一个矩形的x值,obj_x是另一个矩形的x值,并且y值相同。

import pygame
import time
import random

pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)

display_width = 500
display_height = 500

screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Avoid')

x_change = 0
y_change = 0

FPS = 30
block_size = 10

font = pygame.font.SysFont(None, 25)
smallfont = pygame.font.SysFont("comicsansms",25)


def showLives(lives):
    text = smallfont.render("Lives: "+str(lives), True, white)
    screen.blit(text, [0,0])
def message_to_screen(msg,color):
    screen_text = font.render(msg, True, color)
    screen.blit(screen_text, [100,250])

clock = pygame.time.Clock()

def gameLoop():
    gameExit = False
    gameOver = False

    x = 250
    y = 425

    x_change = 0
    y_change = 0

    obj_speed = 5


    obj_y = 0
    obj_x = 0

    obj2_y = 0
    obj2_x = 0
    obj2_speed = 3

    lives = 3
    while not gameExit:
        while gameOver == True:
            screen.fill(white)
            message_to_screen("Game Over, Press C to play again or Q to quit", red)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameLoop()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -block_size
                    y_change = 0
                elif event.key == pygame.K_RIGHT:
                    x_change = block_size
                    y_change = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    x_change = 0
                    y_change = 0
        if x > 500-block_size:
            x-=block_size
        if x < 0+block_size:
            x+=block_size
 #       if x >= display_width-block_size-block_size or x < 0:
 #           gameOver = True
        obj_y = obj_y + obj_speed
        if obj_y > display_height:
            obj_x = random.randrange(0, display_width-block_size)
            obj_y = -25
        obj2_y = obj2_y + obj2_speed
        if obj2_y > display_height:
            obj2_x = random.randrange(1, display_width-block_size)
            obj2_y = -27

        x += x_change
        y += y_change
        screen.fill(black)

        pygame.draw.rect(screen,white, [obj_x,obj_y,20,20])
        pygame.draw.rect(screen,white, [obj2_x,obj2_y,20,20])
        pygame.draw.rect(screen, red, [x,y,block_size,block_size])
        showLives(lives)
        pygame.display.update()
        if x == obj_x and y == obj_y:
            lives -= 1
        if lives == 0:
            gameOver = True
    clock.tick(FPS)
    pygame.quit()
    quit()

gameLoop()

我希望程序检测矩形的任何部分何时碰撞,而不仅仅是检测每个矩形上的一个点何时碰撞。

1 个答案:

答案 0 :(得分:1)

PyGame具有pygame.Rect()类,用于保持矩形的位置和大小。它用于绘制图像/精灵,并检查精灵之间的碰撞。

x = 250
y = 425

obj_y = 0
obj_x = 0

rect_1 = pygame.Rect(x, y, 10, 10)
rect_2 = pygame.Rect(obj_x, obj_y, 20, 20)

然后您可以检查碰撞

rect_1.colliderect(rect_2)

您也可以使用它在屏幕上绘制矩形

pygame.draw.rect(screen, white, rect_2)
pygame.draw.rect(screen, red, rect_1)

您还可以使用它来检查矩形和点之间的碰撞-即。检查鼠标是否单击了按钮

button_rect.collidepoint(event.pos)

要更改矩形中的值,您可以使用rect_1.xrect_1.yrect_1.widthrect_1.height

x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h

其中有些人接受了(x, y)的元组

例如:屏幕上的中心矩形

rect_1.center = (display_width//2, display_height//2)

或使用screen

的事件
rect_1.center = screen.get_rect().center

或在屏幕上显示中心文字

screen_text_rect = screen_text.get_rect()
screen_text_rect.center = screen.get_rect().center

screen.blit(screen_text, screen_text_rect)