Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/paulledemon/pycollision
Quickly makes hitboxes given an image, useful for collision detection. view demo images: https://github.com/PaulleDemon/PyCollision/blob/main/DemoImages.md
https://github.com/paulledemon/pycollision
2d-game collision collision-detection pycollision pygame
Last synced: about 2 months ago
JSON representation
Quickly makes hitboxes given an image, useful for collision detection. view demo images: https://github.com/PaulleDemon/PyCollision/blob/main/DemoImages.md
- Host: GitHub
- URL: https://github.com/paulledemon/pycollision
- Owner: PaulleDemon
- License: mit
- Created: 2021-08-09T14:04:43.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-04T14:15:46.000Z (about 1 year ago)
- Last Synced: 2024-04-26T16:42:04.214Z (9 months ago)
- Topics: 2d-game, collision, collision-detection, pycollision, pygame
- Language: Python
- Homepage: https://browsedocs.com/LBImCEvuwGje
- Size: 1.95 MB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# PyCollision
This is a collision detection program that makes use of rectangles to detect collision.
Quick example using pygame:
```python
import pygame
import random
from pycollision import Collisionpygame.init()
screen = pygame.display.set_mode((1000, 800))
player_rect = pygame.Rect(0, 0, 50, 50)
collision_check = Collision(r"sample.png", (15, 15), wall_collision=False) # set wall collision to True if you want to check the collision only at the walls, this will be much faster
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 = Falsepos_x, pos_y = pygame.mouse.get_pos()
colliding, pos = collision_check.smart_check((pos_x, pos_y)) # checks if the point is first inside the outer rectangle then checks if it is inside the image
# rect = (player_rect.x, player_rect.y, player_rect.x+player_rect.width, player_rect.height+player_rect.y)
# colliding, pos = collision_check.rect_collide(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.![Sample Image](https://github.com/PaulleDemon/PyCollision/blob/main/Examples/TestImages/sample.png?raw=True)
Check out more examples [here](https://github.com/PaulleDemon/PyCollision/tree/main/Examples).
Refer documentation [here](https://browsedocs.com/LBImCEvuwGje)