feature: item

- add draw method to Item class with fallback colors
- fix ItemManager class name typo and missing self parameter in _random_position
- add draw and clear methods to ItemManager
- rename ITEM_SPAWN_INTERVAL to ITEM_SHOPPING_TIMER in is_expired
This commit is contained in:
Berger Lena
2026-05-08 16:03:42 +02:00
parent 99d095c761
commit c0a5c12d4e
+34 -4
View File
@@ -29,16 +29,34 @@ class Item:
# 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
return pygame.time.get_ticks() - self.spawn_time >= ITEM_SHOPPING_TIMER
# cam offset to convert world coordinates to screen coordinates
def draw(self, screen, cam_x, cam_y):
draw_x = self.x - cam_x
draw_y = self.y - cam_y
class ItemManger:
if self.image is not None:
screen.blit(self.image, (draw_x, draw_y))
else:
# fallback using settings colors
# coffee brown, beer yellow
if self.item_type == "coffee":
color = (120, 70, 30)
else:
color = COLOR_HP_BAR_YELLOW
draw_rect = (draw_x, draw_y, ITEM_SIZE, ITEM_SIZE)
pygame.draw.rect(screen, color, draw_rect)
pygame.draw.rect(screen, COLOR_HP_BAR_BG, draw_rect, 2)
class ItemManager:
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):
def _random_position(self, room_rect, blocked_rects):
attempts = 0
max_attempts = 50
@@ -66,7 +84,7 @@ class ItemManger:
return None
# remove expired items
# remove expired items and spawn new ones
def update(self, room_rect, blocked_rects):
now = pygame.time.get_ticks()
@@ -78,3 +96,15 @@ class ItemManger:
# replace the old list with the new temp_list
self.items = remaining_items
# todo spawn logic for ned ones with random time? (in settings we can set the time range?)
# draw active items
def draw(self, screen, cam_x, cam_y):
for item in self.items:
item.draw(screen, cam_x, cam_y)
# clear items when changing room
def clear(self):
self.items = []
self.last_spawn_time = pygame.time.get_ticks()