diff --git a/game.py b/game.py index 61e6758..a1810bb 100644 --- a/game.py +++ b/game.py @@ -30,6 +30,8 @@ class Game: self.item_rect = pygame.Rect(60, 40, 10, 10) + self.flashlight_rect = (self.player_pos, 30) + self.flashlight = 0 self.color_item = (0, 0, 0) @@ -54,7 +56,7 @@ class Game: self.running = False # Player-Draw - self.flashlight = pygame.draw.circle(self.display, (255, 255, 255, 0), self.player_pos, 30) + self.flashlight = pygame.draw.circle(self.display, (255, 255, 255, 0), self.flashlight_rect[0], self.flashlight_rect[1]) pygame.draw.rect(self.display, self.color_item, self.item_rect) @@ -71,10 +73,11 @@ class Game: if keys[pygame.K_d]: self.player_pos.x += 300 * self.dt - if not self.flashlight.colliderect(self.item_rect): - self.color_item = (0,0,0) - else: + + if self.rect_circle_collision(self.item_rect, self.flashlight_rect): self.color_item = (0, 0, 255) + else: + self.color_item = (0, 0, 0) self.screen.blit( pygame.transform.scale(self.display, self.screen.get_size()) @@ -83,6 +86,26 @@ class Game: pygame.display.update() self.dt = self.clock.tick(60) / 3000 + def rect_circle_collision(self, rect, circle): + rect_x, rect_y, rect_w, rect_h = rect + + circle_x = circle[0][0] + circle_y = circle[0][1] + circle_r = circle[1] + + + # Calculate the closest point on the rectangle to the center of the circle + closest_x = max(rect_x, min(circle_x, rect_x + rect_w)) + closest_y = max(rect_y, min(circle_y, rect_y + rect_h)) + + # Calculate the distance between the center of the circle and the closest point on the rectangle + dx = circle_x - closest_x + dy = circle_y - closest_y + distance = (dx ** 2 + dy ** 2) ** 0.5 + + return distance <= circle_r + + game = Game() game.run() diff --git a/test.py b/test.py index e69de29..17987f1 100644 --- a/test.py +++ b/test.py @@ -0,0 +1,30 @@ +import pygame + + +def rect_circle_collision(rect, circle): + rect_x, rect_y, rect_w, rect_h = rect + circle_x, circle_y, circle_r = circle + + # Calculate the closest point on the rectangle to the center of the circle + closest_x = max(rect_x, min(circle_x, rect_x + rect_w)) + closest_y = max(rect_y, min(circle_y, rect_y + rect_h)) + + # Calculate the distance between the center of the circle and the closest point on the rectangle + dx = circle_x - closest_x + dy = circle_y - closest_y + distance = (dx ** 2 + dy ** 2) ** 0.5 + + return distance <= circle_r + + +# Example usage +pygame.init() +rect = (100, 100, 200, 200) # (x, y, width, height) +circle = (150, 150, 50) # (x, y, radius) + +if rect_circle_collision(rect, circle): + print("Collision detected!") +else: + print("No collision.") + +pygame.quit() \ No newline at end of file diff --git a/versuche-python-hausaugabe.txt b/versuche-python-hausaugabe.txt new file mode 100644 index 0000000..bf0a840 --- /dev/null +++ b/versuche-python-hausaugabe.txt @@ -0,0 +1,2 @@ +1. "pygame.sprite.collide_mask(self.flashlight ,self.item_rect)..." = Ergebnis das selbe wie "self.flashlight.colliderect(self.item_rect)..." +2. Neue Methode "rect_circle_collision" mit den Parametern "rect, circle" womit das mathematisch berechnet wird was ich nicht verstehe + die prüfung mit self.item_rect und self.flashlight_rect, welches neu hinzugefügt wurde. Quelle: https://coderivers.org/blog/see-if-rect-collides-with-circle-python/ \ No newline at end of file