Get the App
SLTechnology News&Howtos  ›  Development  › 

How does python detect collisions in pygame

Shulou Source: shulou.com Published: 2022-06-03 15:34:04 10月04日 Update

This article will explain in detail how python detects collisions in pygame. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Description

1. In PyGame, you can use pygame.Rect objects to complete basic collision detection.

2. The Rect object provides many methods to detect collisions between objects.

Note that even in the Pong game, the collision between a rectangular object and a circular object, such as a racket and a ball, can be roughly detected by a collision between two rectangular objects (the bounding rectangle of the racket and the ball).

Example

Pygame.Rect.collidepoint:

Is the test point in the rectangle?

Import pygame pygame.init () window = pygame.display.set_mode ((250,250)) rect = pygame.Rect (* window.get_rect () .center, 0,0) .consumate (100,100) run = Truewhile run: for event in pygame.event.get (): if event.type = = pygame.QUIT: run = pygame.mouse.get_pos () collide = rect.collidepoint (point) color = (255,0,0) if collide else 255,255) window.fill (0) pygame.draw.rect (window, color, rect) pygame.display.flip () pygame.quit () exit () about "how python detects collisions in pygame" is here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Collisions detection rectangles objects objects articles between more rackets good practical rough two content can pass circle multiple examples articles methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB macOS OPPO Reno Huawei