Merge branch 'feat/shop-system' into 'main'

feat: implement a basic shop system

See merge request swd25-bootcamp/silent-fh!22
This commit is contained in:
Haiden Niklas
2026-05-08 15:29:35 +00:00
6 changed files with 46 additions and 8 deletions
+7
View File
@@ -2,6 +2,7 @@ import pygame
from src.settings import START_ECTS_VALUE, ECTS_UPDATE_INTERVAL from src.settings import START_ECTS_VALUE, ECTS_UPDATE_INTERVAL
class ECTS: class ECTS:
def __init__(self): def __init__(self):
# total amount of ECTS the player has at the beginning # total amount of ECTS the player has at the beginning
@@ -24,3 +25,9 @@ class ECTS:
# beer increases amount of how much ECTS are earned per second # beer increases amount of how much ECTS are earned per second
def upgrade_rate(self, extra_ects): def upgrade_rate(self, extra_ects):
self.ects_per_second += extra_ects self.ects_per_second += extra_ects
def is_over_balance(self, amount):
return self.ects_balance >= amount
def decrease_balance(self, amount):
self.ects_balance -= amount
-3
View File
@@ -74,7 +74,6 @@ class Game:
self.update(dt) self.update(dt)
self.draw() self.draw()
pygame.display.flip() pygame.display.flip()
frame_count += 1 frame_count += 1
@@ -144,8 +143,6 @@ class Game:
self.screen.fill(color_void) self.screen.fill(color_void)
self.map.draw_map(self.screen, self.font) self.map.draw_map(self.screen, self.font)
# Draw by Y position so actors lower on the map appear in front. # Draw by Y position so actors lower on the map appear in front.
for actor in sorted([self.player], key=lambda item: item.position.y): for actor in sorted([self.player], key=lambda item: item.position.y):
actor.draw(self.screen) actor.draw(self.screen)
+4
View File
@@ -11,6 +11,7 @@ class Item:
self.x = x self.x = x
self.y = y self.y = y
self.item_type = item_type self.item_type = item_type
self.cost = random.randint(5, 10)
# collision rect for player (2 times item size for width and height) # collision rect for player (2 times item size for width and height)
self.rect = pygame.Rect(self.x, self.y, ITEM_SIZE, ITEM_SIZE) self.rect = pygame.Rect(self.x, self.y, ITEM_SIZE, ITEM_SIZE)
@@ -50,6 +51,9 @@ class Item:
pygame.draw.rect(screen, color, draw_rect) pygame.draw.rect(screen, color, draw_rect)
pygame.draw.rect(screen, COLOR_HP_BAR_BG, draw_rect, 2) pygame.draw.rect(screen, COLOR_HP_BAR_BG, draw_rect, 2)
def get_cost(self):
return self.cost
# try 50 times to find a free position to spawn item # try 50 times to find a free position to spawn item
def _random_position(room_rect, blocked_rects): def _random_position(room_rect, blocked_rects):
+2 -3
View File
@@ -1,9 +1,8 @@
import pygame import pygame
from src.desk import Desk from src.desk import Desk
from src.settings import DESK_WIDTH, DESK_HEIGHT from src.settings import DESK_WIDTH, DESK_HEIGHT, tile_size
# Fenstergröße und Tile Größe (Felder) # Fenstergröße
tile_size = 30
screen_w = 1900 screen_w = 1900
screen_h = 900 screen_h = 900
+2 -1
View File
@@ -1,6 +1,7 @@
from typing import Mapping, Literal, Tuple from typing import Mapping, Literal, Tuple
from src.map import tile_size # Fenstergröße und Tile Größe (Felder)
tile_size = 30
# --- DESK --- # --- DESK ---
DESK_WIDTH = 70 DESK_WIDTH = 70
+30
View File
@@ -0,0 +1,30 @@
from src.item import Item
class Shop:
def __init__(self, ects, buyable_items):
self.ects = ects # give the ects object to the shop
self.buyable_items: list[Item] = buyable_items
def buy(self, item_name):
item_to_buy = self.get_item(item_name)
if item_to_buy is None:
return False
cost = item_to_buy.get_cost()
# is_over_balance() means "has enough balance"
if not self.ects.is_over_balance(cost):
return False
self.ects.decrease_balance(cost)
return True
def get_item(self, item_name):
for item in self.buyable_items:
if item.item_type == item_name:
return item
return None
def get_items(self):
return self.buyable_items