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:
+36
-34
@@ -6,8 +6,39 @@ from src.settings import (
|
||||
)
|
||||
|
||||
|
||||
# try 50 times to find a free position to spawn item
|
||||
def _random_position(room_rect, blocked_rects):
|
||||
class Item:
|
||||
def __init__(self, x, y, item_type):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.item_type = item_type
|
||||
|
||||
# collision rect for player (2 times item size for width and height)
|
||||
self.rect = pygame.Rect(self.x, self.y, ITEM_SIZE, ITEM_SIZE)
|
||||
|
||||
# timer to store time when item appeared
|
||||
self.spawn_time = pygame.time.get_ticks()
|
||||
|
||||
# try to load sprite
|
||||
try:
|
||||
path = f"assets/items/{item_type}.png"
|
||||
self.image = pygame.image.load(path).convert_alpha()
|
||||
self.image = pygame.transform.scale(self.image, (ITEM_SIZE, ITEM_SIZE))
|
||||
except Exception as e:
|
||||
print(f"Error loading item sprite {item_type}: {e}")
|
||||
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
|
||||
|
||||
@@ -34,8 +65,9 @@ def _random_position(room_rect, blocked_rects):
|
||||
# no spot after 50 attempts -> return
|
||||
return None
|
||||
|
||||
# remove expired items
|
||||
def update(self, room_rect, blocked_rects):
|
||||
|
||||
# remove expired items
|
||||
def update(self, room_rect, blocked_rects):
|
||||
now = pygame.time.get_ticks()
|
||||
|
||||
remaining_items = []
|
||||
@@ -46,33 +78,3 @@ def update(self, room_rect, blocked_rects):
|
||||
|
||||
# replace the old list with the new temp_list
|
||||
self.items = remaining_items
|
||||
|
||||
|
||||
class Item:
|
||||
def __init__(self, x, y, item_type):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.item_type = item_type
|
||||
|
||||
# collision rect for player (2 times item size for width and height)
|
||||
self.rect = pygame.Rect(self.x, self.y, ITEM_SIZE, ITEM_SIZE)
|
||||
|
||||
# timer to store time when item appeared
|
||||
self.spawn_time = pygame.time.get_ticks()
|
||||
|
||||
# try to load sprite
|
||||
try:
|
||||
path = f"assets/items/{item_type}.png"
|
||||
self.image = pygame.image.load(path).convert_alpha()
|
||||
self.image = pygame.transform.scale(self.image, (ITEM_SIZE, ITEM_SIZE))
|
||||
except Exception as e:
|
||||
print(f"Error loading item sprite {item_type}: {e}")
|
||||
self.image = None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -41,10 +41,12 @@ ECTS_UPDATE_INTERVAL = 1000 # time in ms (1000 = 1 sec)
|
||||
BEER_BOTTLE_ECTS_VALUE = 2
|
||||
BEER_COST = 50
|
||||
|
||||
|
||||
# --- BUTTONS ---
|
||||
COLOR_BUTTON = (0, 255, 0)
|
||||
COLOR_BUTTON_HOVER = (200, 255, 0)
|
||||
|
||||
|
||||
# --- STARTSCREEN ---
|
||||
COLOR_STARTSCREEN_BACKGROUND = ((0, 0, 0))
|
||||
COLOR_STARTSCREEN_FONT_TITLE = (0, 255, 0)
|
||||
@@ -55,6 +57,7 @@ COLOR_STARTSCREEN_FONT_LABEL = (0, 255, 255)
|
||||
COFFEE_COST = 30
|
||||
COFFEE_HEAL_VALUE = 30
|
||||
|
||||
|
||||
# --- TOWER ---
|
||||
TOWER_COST = 150
|
||||
PROJECTILE_DMG = 15
|
||||
@@ -62,11 +65,13 @@ PROJECTILE_SPEED = 7
|
||||
COLOR_PROJECTILE = (200, 200, 0)
|
||||
TOWER_TIMER = 30000 # how long the tower stays and attack before it disappears
|
||||
|
||||
|
||||
# --- ITEM SPAWN TIMER ---
|
||||
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_SIZE = 30
|
||||
|
||||
|
||||
# --- ZOMBIE ---
|
||||
ZOMBIE_EASY_HP = 30
|
||||
ZOMBIE_EASY_DMG = 3
|
||||
|
||||
Reference in New Issue
Block a user