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

53
game.py
View file

@ -151,17 +151,15 @@ class Player:
self.game = game
self.pos = pos
self.player_width = width
self.player_height = height
self.width = width
self.height = height
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):
self.player_rect = pygame.Rect(self.pos[0]-10, self.pos[1]-10, self.player_width, self.player_height)
# Smooth Movement
keys = pygame.key.get_pressed()
@ -180,54 +178,57 @@ class Player:
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
# 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:
if self.player_rect.colliderect(wall):
if self.rect.colliderect(wall):
if movement.x > 0:
self.player_rect.right = wall.left
self.rect.right = wall.left
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.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:
if self.player_rect.colliderect(wall):
if self.rect.colliderect(wall):
if movement.y > 0:
self.player_rect.bottom = wall.top
self.rect.bottom = wall.top
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):
pygame.draw.rect(surface, (255, 0, 0), self.player_rect)
pygame.draw.rect(surface, (255, 0, 0), self.rect)