Module PyGame. Object collisions

12.5.3.6 check the relative position of objects on the screen.

Module PyGame. Object collisions

Collisions are an important part of game development. Collision detection implies recognizing when one object in the game is touching another. The response to a collision is what happens at the time of the collision.

Object collision means one of the points of one object is into another object.

Every object in Pygame has a rect attribute that defines its position and size. A rect object in Pygame is represented in the format [x, y, width, height], where x and y represent the top left corner of the rectangle. Another name for this rectangle is the bounding box because it is the boundary of the object.

Checking if a point of one object is in the rectangle of another object.

if obj1.rect.right >= obj2.rect.left and 
   obj1.rect.left <= obj2.rect.right and
   obj1.rect.bottom >= obj2.rect.top and 
   obj1.rect.top <= obj2.rect.bottom:
    collide = True

collide() methods in pygame.Rect

colliderect(Rect) - checking the intersection of two rectangles;

import pygame
# Initialize object1 and object2 rectangles
object1_rect = pygame.Rect(x1, y1, width1, height1)
object2_rect = pygame.Rect(x2, y2, width2, height2)
# Check for collision
if object1_rect.colliderect(object2_rect):
    print("Collision detected!")
else:
    print("No collision.")

In this example, object1_rect and object2_rect are the rectangles of the two objects you want to check for collision. The colliderect() method returns True if the rectangles collide, and False if they do not.

collidepoint(x, y) – check if a point falls into a rectangle

object1_rect = pygame.Rect(x1, y1, width1, height1)
object2_rect = pygame.Rect(x2, y2, width2, height2)
center2_x = x2 + width2 // 2
center2_y = y2 + height2 // 2
if object1_rect.collidepoint(center2_x, center2_y):
    print("Collision detected!")
else:
    print("No collision.")

In this example, object1_rect is the rectangle of the object you want to check for collision with, and center2_x and center2_y are the x and y coordinates of the center point of the second object. The collidepoint() method returns True if the rectangle collides with the point, and False if it does not.

collidelist(list) - check for intersection with at least one rectangle from the list of rectangles list

import pygame
# Initialize object1 rectangle
object1_rect = pygame.Rect(x1, y1, width1, height1)
# Initialize a list of object2 rectangles
object2_rects = [pygame.Rect(x2, y2, width2, height2), 
                pygame.Rect(x3, y3, width3, height3), 
                pygame.Rect(x4, y4, width4, height4)]
# Check for collision with the list of object2 rectangles
collision_index = object1_rect.collidelist(object2_rects)
if collision_index != -1:
    print("Collision detected with object at index", collision_index)
else:
    print("No collision.")

In this example, object1_rect is the rectangle of the object you want to check for collision with, and object2_rects is a list of rectangles representing the other objects. The collidelist() method returns the index of the first rectangle in the list that collides with object1_rect, or -1 if no collision is detected.

You can consider other collide methods here.


Questions:


Exercises:


Tasks:

Категория: Algorithms | Добавил: bzfar77 (29.01.2023)
Просмотров: 5070 | Теги: point, Rectangle, Collide, PyGame, Collision, Python | Рейтинг: 4.0/2
Всего комментариев: 0
avatar