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:
@@ -2,6 +2,7 @@ import pygame
|
||||
|
||||
from src.settings import START_ECTS_VALUE, ECTS_UPDATE_INTERVAL
|
||||
|
||||
|
||||
class ECTS:
|
||||
def __init__(self):
|
||||
# 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
|
||||
def upgrade_rate(self, 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
|
||||
|
||||
@@ -74,7 +74,6 @@ class Game:
|
||||
self.update(dt)
|
||||
self.draw()
|
||||
|
||||
|
||||
pygame.display.flip()
|
||||
frame_count += 1
|
||||
|
||||
@@ -144,8 +143,6 @@ class Game:
|
||||
self.screen.fill(color_void)
|
||||
self.map.draw_map(self.screen, self.font)
|
||||
|
||||
|
||||
# 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):
|
||||
actor.draw(self.screen)
|
||||
|
||||
|
||||
+5
-1
@@ -11,6 +11,7 @@ class Item:
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.item_type = item_type
|
||||
self.cost = random.randint(5, 10)
|
||||
|
||||
# collision rect for player (2 times item size for width and height)
|
||||
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_HP_BAR_BG, draw_rect, 2)
|
||||
|
||||
def get_cost(self):
|
||||
return self.cost
|
||||
|
||||
|
||||
# try 50 times to find a free position to spawn item
|
||||
def _random_position(room_rect, blocked_rects):
|
||||
@@ -130,4 +134,4 @@ class ItemManager:
|
||||
# clear items when changing room
|
||||
def clear(self):
|
||||
self.items = []
|
||||
self.last_spawn_time = pygame.time.get_ticks()
|
||||
self.last_spawn_time = pygame.time.get_ticks()
|
||||
|
||||
+2
-3
@@ -1,9 +1,8 @@
|
||||
import pygame
|
||||
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)
|
||||
tile_size = 30
|
||||
# Fenstergröße
|
||||
screen_w = 1900
|
||||
screen_h = 900
|
||||
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
from typing import Mapping, Literal, Tuple
|
||||
|
||||
from src.map import tile_size
|
||||
# Fenstergröße und Tile Größe (Felder)
|
||||
tile_size = 30
|
||||
|
||||
# --- DESK ---
|
||||
DESK_WIDTH = 70
|
||||
|
||||
+30
@@ -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
|
||||
Reference in New Issue
Block a user