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:
+44
-42
@@ -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:
|
||||
def __init__(self, x, y, item_type):
|
||||
self.x = x
|
||||
@@ -69,10 +27,54 @@ class Item:
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user