This library helps to detect collision for image with transparency. Useful in game development
Project description
PyCollision
This is a collision detection program that makes use of rectangles to detect collision.
Quick example using pygame:
import pygame
import random
from pycollision import Collision
pygame.init()
screen = pygame.display.set_mode((1000, 800))
player_rect = pygame.Rect(0, 0, 50, 50)
collision_check = Collision(r"sample.png", (15, 15), optimize=False)
collision_object = pygame.image.load(r"sample.png").convert_alpha()
colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for x in
range(len(collision_check.collision_points()))]
running = True
speed = 1.5
pos_x, pos_y = (10, 10)
coll_font = pygame.font.SysFont('Consolas', 50)
while running:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pos_x, pos_y = pygame.mouse.get_pos()
colliding, pos = collision_check.smart_check((pos_x, pos_y)) # collision_check.smart_check(player_rect)
# rect = (player_rect.x, player_rect.y, player_rect.x+player_rect.width, player_rect.height+player_rect.y)
# colliding, pos = collision_check.check_rect_collision(rect)
if colliding:
screen.fill((255, 16, 8))
screen.blit(coll_font.render("Collision", True, (255, 255, 255)), (50, 50))
screen.blit(collision_object, (0, 0))
# for color, x in zip(colors, collision_check.collision_points()): # uncomment this to get colourful rectangles
# x = (x[0], x[1], x[2] - x[0], x[3] - x[1])
# pygame.draw.rect(screen, color, pygame.Rect(x), width=3)
player_rect = pygame.Rect(pos_x, pos_y, 50, 50)
pygame.draw.rect(screen, (0, 0, 0), player_rect)
pygame.display.update()
save the below sample image in the same directory as the program and give a test run.
Check out more examples here.
Refer documentation here
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
PyCollision-0.0.1.tar.gz
(4.6 kB
view hashes)
Built Distribution
Close
Hashes for PyCollision-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 296c080001518661d1c38c73ce8d506562940145e88cdbf44268851ed00ca33d |
|
MD5 | 3011807e81fe9a07220fb29620487a08 |
|
BLAKE2b-256 | f811f5a981287abc8474bc7be68873fb1042cccf13eded224adb7793e4861af3 |