Du kannst nun rennen
This commit is contained in:
parent
a2de58f2ce
commit
3e00e6d0e5
3 changed files with 21 additions and 37 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -218,3 +218,7 @@ __marimo__/
|
|||
|
||||
# Streamlit
|
||||
.streamlit/secrets.toml
|
||||
|
||||
|
||||
der_test.py
|
||||
rechner.py
|
||||
24
game.py
24
game.py
|
|
@ -1,8 +1,8 @@
|
|||
import pygame
|
||||
|
||||
|
||||
SCREEN_WIDTH = 640
|
||||
SCREEN_HEIGHT = 480
|
||||
SCREEN_WIDTH = 1280
|
||||
SCREEN_HEIGHT = 720
|
||||
|
||||
|
||||
class Game:
|
||||
|
|
@ -30,12 +30,14 @@ class Game:
|
|||
|
||||
self.item_rect = pygame.Rect(60, 40, 10, 10)
|
||||
|
||||
self.flashlight_rect = (self.player_pos, 30)
|
||||
self.flashlight_rect = (self.player_pos, 50)
|
||||
|
||||
self.flashlight = 0
|
||||
|
||||
self.color_item = (0, 0, 0)
|
||||
|
||||
self.speed = 300
|
||||
|
||||
#self.circle = pygame.geometry.Circle(self.player_pos[0], self.player_pos[1], 30)
|
||||
|
||||
def run(self) -> None:
|
||||
|
|
@ -55,6 +57,13 @@ class Game:
|
|||
if event.key == pygame.K_ESCAPE:
|
||||
self.running = False
|
||||
|
||||
if event.key == pygame.K_LSHIFT:
|
||||
self.speed = 600
|
||||
|
||||
if event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_LSHIFT:
|
||||
self.speed = 300
|
||||
|
||||
# Player-Draw
|
||||
self.flashlight = pygame.draw.circle(self.display, (255, 255, 255, 0), self.flashlight_rect[0], self.flashlight_rect[1])
|
||||
|
||||
|
|
@ -65,13 +74,14 @@ class Game:
|
|||
# Smooth Movement
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_w]:
|
||||
self.player_pos.y -= 300 * self.dt
|
||||
self.player_pos.y -= self.speed * self.dt
|
||||
if keys[pygame.K_s]:
|
||||
self.player_pos.y += 300 * self.dt
|
||||
self.player_pos.y += self.speed * self.dt
|
||||
if keys[pygame.K_a]:
|
||||
self.player_pos.x -= 300 * self.dt
|
||||
self.player_pos.x -= self.speed * self.dt
|
||||
if keys[pygame.K_d]:
|
||||
self.player_pos.x += 300 * self.dt
|
||||
self.player_pos.x += self.speed * self.dt
|
||||
|
||||
|
||||
|
||||
if self.rect_circle_collision(self.item_rect, self.flashlight_rect):
|
||||
|
|
|
|||
30
test.py
30
test.py
|
|
@ -1,30 +0,0 @@
|
|||
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()
|
||||
Loading…
Reference in a new issue