Agregar colisiones usando pygame.Rect.colliderect en Pygame

Prerrequisito: Dibujo de formas en Pygame , Introducción a Pygame

En este artículo, usaremos pygame.Rect.colliderect para agregar colisión en una forma usando Pygame en Python.

Podemos agregar fácilmente colisiones en las formas de Pygame usando colliderect(). Para esto, vamos a dibujar dos rectángulos y luego verificaremos si los rectángulos chocan o no.

Sintaxis: pygame.Rect.colliderect(rect1, rect2)

Parámetros: Tomará dos rects como sus parámetros.

Devuelve: Devuelve verdadero si alguna parte de cualquiera de los rectángulos se superpone.

Ejemplo 1: ejemplo simple para agregar colisión en forma de una manera

Python3

# Importing the pygame module
import pygame
from pygame.locals import *
 
# Initiate pygame and give permission
# to use pygame's functionality
pygame.init()
 
# Create a display surface object
# of specific dimension
window = pygame.display.set_mode((600, 600))
 
# Creating a new clock object to
# track the amount of time
clock = pygame.time.Clock()
 
 
# Creating a new rect for first object
player_rect = Rect(200, 500, 50, 50)
 
# Creating a new rect for second object
player_rect2 = Rect(200, 0, 50, 50)
 
# Creating variable for gravity
gravity = 4
 
# Creating a boolean variable that
# we will use to run the while loop
run = True
 
# Creating an infinite loop
# to run our game
while run:
 
    # Setting the framerate to 60fps
    clock.tick(60)
 
    # Adding gravity in player_rect2
    player_rect2.bottom += gravity
 
    # Checking if player is colliding
    # with platform or not using the
    # colliderect() method.
    # It will return a boolean value
    collide = pygame.Rect.colliderect(player_rect, p
                                      layer_rect2)
 
    # If the objects are colliding
    # then changing the y coordinate
    if collide:
        player_rect2.bottom = player_rect.top
 
    # Drawing player rect
    pygame.draw.rect(window, (0,   255,   0),
                     player_rect)
    # Drawing player rect2
    pygame.draw.rect(window, (0,   0,   255),
                     player_rect2)
 
    # Updating the display surface
    pygame.display.update()
 
    # Filling the window with white color
    window.fill((255, 255, 255))

Producción:

Ejemplo 2: Agregar colisión en ambos lados

Python3

# Importing the pygame module
import pygame
from pygame.locals import *
 
# Initiate pygame and give permission
# to use pygame's functionality
pygame.init()
 
# Create a display surface object
# of specific dimension
window = pygame.display.set_mode((600, 600))
 
# Creating a new clock object to
# track the amount of time
clock = pygame.time.Clock()
 
 
# Creating a new rect for first object
player_rect = Rect(200, 500, 50, 50)
 
# Creating a new rect for second object
player_rect2 = Rect(200, 0, 50, 50)
 
 
# Creating a boolean variable that
# we will use to run the while loop
run = True
 
# Speed for the objects
speed_a = 8
speed_b = -7
 
# Creating an infinite loop
# to run our game
while run:
 
    # Setting the framerate to 60fps
    clock.tick(60)
 
    # Adding speed in player rects
    player_rect.bottom += speed_a
    player_rect2.top += speed_b
 
    # Checking if player is colliding
    # with platform or not using the
    # colliderect() method.
    # It will return a boolean value
    collide = pygame.Rect.colliderect(player_rect,
                                      player_rect2)
 
    # If the objects are colliding
    # then changing the speed direction
    if collide:
        speed_a *= -1
        speed_b *= -1
 
    # Changing the direction if the objects
    # goes outside the window
    if player_rect.top<0 or player_rect.bottom > 600:
        speed_a *= -1
    if player_rect2.bottom > 600 or player_rect2.top<0:
        speed_b *= -1
     
    # Drawing player rect
    pygame.draw.rect(window, (0,   255,   0),
                     player_rect)
    # Drawing player rect2
    pygame.draw.rect(window, (0,   0,   255),
                     player_rect2)
 
    # Updating the display surface
    pygame.display.update()
 
    # Filling the window with white color
    window.fill((255, 255, 255))

Producción:

Publicación traducida automáticamente

Artículo escrito por imranalam21510 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *