Compare commits

...

2 commits

Author SHA1 Message Date
Benjamin
0476dadfa4 ENDE: 18.09.2026 2026-09-18 18:37:17 +02:00
Benjamin
f71dae0af4 5 Minuten Pause vpm 19.09.2026 2026-09-18 18:06:44 +02:00

57
game.py
View file

@ -136,7 +136,7 @@ class Game:
for wall in self.walls: for wall in self.walls:
pygame.draw.rect(self.display, "grey", wall) pygame.draw.rect(self.display, "grey", wall)
def calculate_collsions(self) -> None: def calculate_collsions(self) -> None:
if self.item_rect.colliderect(self.flashlight_rect): if self.item_rect.colliderect(self.flashlight_rect):
@ -151,17 +151,15 @@ class Player:
self.game = game self.game = game
self.pos = pos self.pos = pos
self.player_width = width self.width = width
self.player_height = height self.height = height
self.speed = 150 self.speed = 150
self.rect = pygame.Rect(self.pos[0]-self.player_width/2, self.pos[1]-self.player_height/2, width, height) self.rect = pygame.Rect(self.pos[0]-self.width/2, self.pos[1]-self.height/2, width, height)
def update(self, walls, dt): def update(self, walls, dt):
self.player_rect = pygame.Rect(self.pos[0]-10, self.pos[1]-10, self.player_width, self.player_height)
# Smooth Movement # Smooth Movement
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
@ -177,57 +175,60 @@ class Player:
movement: pygame.Vector2 = direction * self.speed * dt movement: pygame.Vector2 = direction * self.speed * dt
self.pos.x += movement.x self.pos.x += movement.x
if self.pos.x - self.width/2 < 0:
self.pos.x = self.width/2
if self.pos.x + self.width/2 > SCREEN_WIDTH/2:
self.pos.x = SCREEN_WIDTH/2 - self.width/2
# self.pos += movement # self.pos += movement
# player_rect.center = round(player_pos) # player_rect.center = round(player_pos)
self.player_rect.centerx = round(self.pos.x) self.rect.centerx = round(self.pos.x)
for wall in walls: for wall in walls:
if self.player_rect.colliderect(wall): if self.rect.colliderect(wall):
if movement.x > 0: if movement.x > 0:
self.player_rect.right = wall.left self.rect.right = wall.left
elif movement.x < 0: elif movement.x < 0:
self.player_rect.left = wall.right self.rect.left = wall.right
self.pos.x = self.player_rect.centerx self.pos.x = self.rect.centerx
# if self.pos.x < self.player_rect.width/2 or self.pos.x + self.player_rect.width > SCREEN_WIDTH:
# self.pos.x = old_position_x
if self.pos.x - self.player_rect.width/2 < 0:
self.pos.x = self.player_rect.width/2
if self.pos.x + self.player_rect.width/2 > SCREEN_WIDTH:
self.pos.x = SCREEN_WIDTH - self.player_rect.width/2
self.pos.y += movement.y self.pos.y += movement.y
self.player_rect.centery = round(self.pos.y) if self.pos.y - self.height/2 < 0:
self.pos.y = self.height/2
if self.pos.y + self.height/2 > SCREEN_HEIGHT/2:
self.pos.y = SCREEN_HEIGHT/2 - self.height/2
self.rect.centery = round(self.pos.y)
for wall in walls: for wall in walls:
if self.player_rect.colliderect(wall): if self.rect.colliderect(wall):
if movement.y > 0: if movement.y > 0:
self.player_rect.bottom = wall.top self.rect.bottom = wall.top
elif movement.y < 0: elif movement.y < 0:
self.player_rect.top = wall.bottom self.rect.top = wall.bottom
self.pos.y = self.rect.centery
self.pos.y = self.player_rect.centery
if self.pos.y - self.player_rect.height/2 < 0:
self.pos.y = self.player_rect.height/2
if self.pos.y + self.player_rect.height/2 > SCREEN_HEIGHT:
self.pos.y = SCREEN_HEIGHT - self.player_rect.height/2
def render(self, surface): def render(self, surface):
pygame.draw.rect(surface, (255, 0, 0), self.player_rect) pygame.draw.rect(surface, (255, 0, 0), self.rect)