refactor: create ItemManger class

- add itemManager class and moved randomPosition and update into it
- add is_expired to check if time ran out to buy spawned item
This commit is contained in:
Berger Lena
2026-05-08 15:47:29 +02:00
parent 3ca2ef1332
commit 2a6c93998b
2 changed files with 49 additions and 42 deletions
+44 -42
View File
@@ -6,48 +6,6 @@ from src.settings import (
) )
# try 50 times to find a free position to spawn item
def _random_position(room_rect, blocked_rects):
attempts = 0
max_attempts = 50
while attempts < max_attempts:
# generate random x/y coordinates with room boundaries
# * 2 to have a margin to walls
x = random.randint(room_rect.left + ITEM_SIZE, room_rect.right - ITEM_SIZE * 2)
y = random.randint(room_rect.top + ITEM_SIZE, room_rect.bottom - ITEM_SIZE * 2)
# create a rect at this position for testing collision
test_rect = pygame.Rect(x, y, ITEM_SIZE, ITEM_SIZE)
blocked = False
for b in blocked_rects:
if test_rect.colliderect(b):
blocked = True
break
# if position is not blocked, return the position
if not blocked:
return x, y
attempts += 1
# no spot after 50 attempts -> return
return None
# remove expired items
def update(self, room_rect, blocked_rects):
now = pygame.time.get_ticks()
remaining_items = []
for item in self.items:
if item.is_expired() == False:
remaining_items.append(item)
# replace the old list with the new temp_list
self.items = remaining_items
class Item: class Item:
def __init__(self, x, y, item_type): def __init__(self, x, y, item_type):
self.x = x self.x = x
@@ -69,10 +27,54 @@ class Item:
print(f"Error loading item sprite {item_type}: {e}") print(f"Error loading item sprite {item_type}: {e}")
self.image = None self.image = None
# to check if the time ran out to buy the item
def is_expired(self):
return pygame.time.get_ticks() - self.spawn_time >= ITEM_SPAWN_INTERVAL
class ItemManger:
def __init__(self):
self.items = []
self.last_spawn_time = pygame.time.get_ticks()
# try 50 times to find a free position to spawn item
def _random_position(room_rect, blocked_rects):
attempts = 0
max_attempts = 50
while attempts < max_attempts:
# generate random x/y coordinates with room boundaries
# * 2 to have a margin to walls
x = random.randint(room_rect.left + ITEM_SIZE, room_rect.right - ITEM_SIZE * 2)
y = random.randint(room_rect.top + ITEM_SIZE, room_rect.bottom - ITEM_SIZE * 2)
# create a rect at this position for testing collision
test_rect = pygame.Rect(x, y, ITEM_SIZE, ITEM_SIZE)
blocked = False
for b in blocked_rects:
if test_rect.colliderect(b):
blocked = True
break
# if position is not blocked, return the position
if not blocked:
return x, y
attempts += 1
# no spot after 50 attempts -> return
return None
# remove expired items
def update(self, room_rect, blocked_rects):
now = pygame.time.get_ticks()
remaining_items = []
for item in self.items:
if item.is_expired() == False:
remaining_items.append(item)
# replace the old list with the new temp_list
self.items = remaining_items
+5
View File
@@ -41,10 +41,12 @@ ECTS_UPDATE_INTERVAL = 1000 # time in ms (1000 = 1 sec)
BEER_BOTTLE_ECTS_VALUE = 2 BEER_BOTTLE_ECTS_VALUE = 2
BEER_COST = 50 BEER_COST = 50
# --- BUTTONS --- # --- BUTTONS ---
COLOR_BUTTON = (0, 255, 0) COLOR_BUTTON = (0, 255, 0)
COLOR_BUTTON_HOVER = (200, 255, 0) COLOR_BUTTON_HOVER = (200, 255, 0)
# --- STARTSCREEN --- # --- STARTSCREEN ---
COLOR_STARTSCREEN_BACKGROUND = ((0, 0, 0)) COLOR_STARTSCREEN_BACKGROUND = ((0, 0, 0))
COLOR_STARTSCREEN_FONT_TITLE = (0, 255, 0) COLOR_STARTSCREEN_FONT_TITLE = (0, 255, 0)
@@ -55,6 +57,7 @@ COLOR_STARTSCREEN_FONT_LABEL = (0, 255, 255)
COFFEE_COST = 30 COFFEE_COST = 30
COFFEE_HEAL_VALUE = 30 COFFEE_HEAL_VALUE = 30
# --- TOWER --- # --- TOWER ---
TOWER_COST = 150 TOWER_COST = 150
PROJECTILE_DMG = 15 PROJECTILE_DMG = 15
@@ -62,11 +65,13 @@ PROJECTILE_SPEED = 7
COLOR_PROJECTILE = (200, 200, 0) COLOR_PROJECTILE = (200, 200, 0)
TOWER_TIMER = 30000 # how long the tower stays and attack before it disappears TOWER_TIMER = 30000 # how long the tower stays and attack before it disappears
# --- ITEM SPAWN TIMER --- # --- ITEM SPAWN TIMER ---
ITEM_SPAWN_INTERVAL = 5000 # ms until a new item spawns ITEM_SPAWN_INTERVAL = 5000 # ms until a new item spawns
ITEM_SHOPPING_TIMER = 10000 # how long you have time to buy items before they despawn ITEM_SHOPPING_TIMER = 10000 # how long you have time to buy items before they despawn
ITEM_SIZE = 30 ITEM_SIZE = 30
# --- ZOMBIE --- # --- ZOMBIE ---
ZOMBIE_EASY_HP = 30 ZOMBIE_EASY_HP = 30
ZOMBIE_EASY_DMG = 3 ZOMBIE_EASY_DMG = 3