refactor: replace static door dimensions with dynamic sizing and add rect property

This commit is contained in:
Niklas Haiden
2026-05-08 19:34:10 +02:00
parent 4faad9dd4b
commit 3fcbd8ce51
2 changed files with 15 additions and 11 deletions
+9 -5
View File
@@ -1,8 +1,6 @@
import pygame import pygame
from src.settings import ( from src.settings import (
DOOR_WIDTH,
DOOR_HEIGHT,
DOOR_BORDER_WIDTH, DOOR_BORDER_WIDTH,
COLOR_DOOR_BEIGE, COLOR_DOOR_BEIGE,
COLOR_DOOR_BORDER COLOR_DOOR_BORDER
@@ -11,10 +9,12 @@ from src.settings import (
# using class to be able to reuse it # using class to be able to reuse it
# as we will have multiple rooms to go in # as we will have multiple rooms to go in
class Door: class Door:
def __init__(self, x, y, hp_value): def __init__(self, x, y, width, height, hp_value):
# for each door we can set our own x/y position # for each door we can set our own x/y position
self.x = x self.x = x
self.y = y self.y = y
self.width = width
self.height = height
self.max_hp = hp_value self.max_hp = hp_value
self.current_hp = hp_value self.current_hp = hp_value
@@ -25,10 +25,14 @@ class Door:
def draw(self, screen): def draw(self, screen):
# door # door
pygame.draw.rect(screen, self.door_color, (self.x, self.y, DOOR_WIDTH, DOOR_HEIGHT)) pygame.draw.rect(screen, self.door_color, self.rect)
# doorframe # doorframe
pygame.draw.rect(screen, self.door_border_color, (self.x, self.y, DOOR_WIDTH, DOOR_HEIGHT), DOOR_BORDER_WIDTH) pygame.draw.rect(screen, self.door_border_color, self.rect, DOOR_BORDER_WIDTH)
@property
def rect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
# when attacked and gets dmg reduces the health (if closed), door "breaks" when health = 0 # when attacked and gets dmg reduces the health (if closed), door "breaks" when health = 0
def take_damage(self, amount): def take_damage(self, amount):
+6 -6
View File
@@ -25,9 +25,7 @@ from src.settings import (
BEER_BOTTLE_ECTS_VALUE, BEER_BOTTLE_ECTS_VALUE,
COFFEE_HEAL_VALUE, COFFEE_HEAL_VALUE,
DEMO_ACTOR_HEIGHT, DEMO_ACTOR_HEIGHT,
DOOR_HEIGHT,
DOOR_HP, DOOR_HP,
DOOR_WIDTH,
FPS, FPS,
PLAYER_SPEED, PLAYER_SPEED,
) )
@@ -338,15 +336,17 @@ class Game:
door_tiles.append(tile_rect) door_tiles.append(tile_rect)
if not door_tiles: if not door_tiles:
return Door(room_rect.centerx - DOOR_WIDTH // 2, room_rect.bottom, DOOR_HP) return Door(room_rect.centerx - tile_size, room_rect.bottom, tile_size * 2, 10, DOOR_HP)
door_area = door_tiles[0].copy() door_area = door_tiles[0].copy()
for tile_rect in door_tiles[1:]: for tile_rect in door_tiles[1:]:
door_area.union_ip(tile_rect) door_area.union_ip(tile_rect)
return Door( return Door(
door_area.centerx - DOOR_WIDTH // 2, door_area.x,
door_area.centery - DOOR_HEIGHT // 2, door_area.y,
door_area.width,
door_area.height,
DOOR_HP, DOOR_HP,
) )
@@ -363,7 +363,7 @@ class Game:
blockers = self.get_monster_spawn_blockers() blockers = self.get_monster_spawn_blockers()
if self.active_door is not None: if self.active_door is not None:
blockers.append(pygame.Rect(self.active_door.x, self.active_door.y, DOOR_WIDTH, DOOR_HEIGHT)) blockers.append(self.active_door.rect)
for monster in self.monsters.monsters: for monster in self.monsters.monsters:
blockers.append(monster.rect) blockers.append(monster.rect)
blockers.extend(item.rect for item in self.item_manager.items) blockers.extend(item.rect for item in self.item_manager.items)