diff --git a/data/inventory.json b/data/inventory.json index ae04b5d..1fa2279 100644 --- a/data/inventory.json +++ b/data/inventory.json @@ -1,43 +1,43 @@ { - "Gold Coin": {"type": "currency", "effect": "collect", "quantity": 1}, - "Silver Coin": {"type": "currency", "effect": "collect", "quantity": 1}, - "Coin Stack (1)": {"type": "currency", "effect": "collect", "quantity": 3}, - "Coin Stack (2)": {"type": "currency", "effect": "collect", "quantity": 5}, - "Circular Gem": {"type": "gem", "effect": "trade", "quantity": 1}, - "Single Gold Bar": {"type": "treasure", "effect": "trade", "quantity": 1}, - "Gold Bar Stack": {"type": "treasure", "effect": "trade", "quantity": 3}, - "Treasure Block": {"type": "treasure", "effect": "open_for_reward", "quantity": 1}, - "Golden Crown": {"type": "artifact", "effect": "boost_status", "quantity": 1}, - "Ornate Cup": {"type": "artifact", "effect": "boost_status", "quantity": 1}, - "Golden Figurine": {"type": "artifact", "effect": "boost_status", "quantity": 1}, - "Simple Sword": {"type": "weapon", "effect": "melee_attack", "quantity": 1}, - "Ornate Sword": {"type": "weapon", "effect": "melee_attack", "quantity": 1}, - "Double-Bladed Axe": {"type": "weapon", "effect": "melee_attack", "quantity": 1}, - "Spear": {"type": "weapon", "effect": "melee_attack", "quantity": 1}, - "Circular Shield": {"type": "armor", "effect": "defense_boost", "quantity": 1}, - "Golden Trophy": {"type": "reward", "effect": "achievement", "quantity": 1}, - "Candelabra": {"type": "decorative", "effect": "none", "quantity": 1}, - "Potion (Red)": {"type": "consumable", "effect": "restore_health", "quantity": 1}, - "Potion (Blue)": {"type": "consumable", "effect": "restore_mana", "quantity": 1}, - "Potion (Green)": {"type": "consumable", "effect": "poison_resistance", "quantity": 1}, - "Square Jar": {"type": "consumable", "effect": "unknown", "quantity": 1}, - "Cake": {"type": "food", "effect": "restore_health", "quantity": 1}, - "Donut": {"type": "food", "effect": "restore_health", "quantity": 1}, - "Bread": {"type": "food", "effect": "restore_health", "quantity": 1}, - "Rug Tile": {"type": "decorative", "effect": "none", "quantity": 1}, - "Geometric Pattern": {"type": "decorative", "effect": "none", "quantity": 1}, - "Glowing Orb (Blue)": {"type": "artifact", "effect": "magic_boost", "quantity": 1}, - "Glowing Orb (Red)": {"type": "artifact", "effect": "fire_boost", "quantity": 1}, - "Glowing Orb (Green)": {"type": "artifact", "effect": "nature_boost", "quantity": 1}, - "Golden Ring": {"type": "artifact", "effect": "magic_resistance", "quantity": 1}, - "Amulet": {"type": "artifact", "effect": "protection", "quantity": 1}, - "Scroll": {"type": "scroll", "effect": "learn_spell", "quantity": 1}, - "Key": {"type": "tool", "effect": "unlock", "quantity": 1}, - "Tool": {"type": "tool", "effect": "repair", "quantity": 1}, - "Dragon (Red)": {"type": "creature", "effect": "fire_attack", "quantity": 1}, - "Dragon (Green)": {"type": "creature", "effect": "nature_attack", "quantity": 1}, - "Dragon (Black)": {"type": "creature", "effect": "dark_attack", "quantity": 1}, - "Dragon (White)": {"type": "creature", "effect": "light_attack", "quantity": 1}, - "Gem Cluster": {"type": "treasure", "effect": "trade", "quantity": 1}, - "Glowing Crystal": {"type": "treasure", "effect": "magic_boost", "quantity": 1} + "Gold Coin": {"type": "currency", "effect": "collect", "quantity": 1, "price": 23}, + "Silver Coin": {"type": "currency", "effect": "collect", "quantity": 1, "price": 23}, + "Coin Stack (1)": {"type": "currency", "effect": "collect", "quantity": 3, "price": 23}, + "Coin Stack (2)": {"type": "currency", "effect": "collect", "quantity": 5, "price": 23}, + "Circular Gem": {"type": "gem", "effect": "trade", "quantity": 1, "price": 23}, + "Single Gold Bar": {"type": "treasure", "effect": "trade", "quantity": 1, "price": 23}, + "Gold Bar Stack": {"type": "treasure", "effect": "trade", "quantity": 3, "price": 23}, + "Treasure Block": {"type": "treasure", "effect": "open_for_reward", "quantity": 1, "price": 23}, + "Golden Crown": {"type": "artifact", "effect": "boost_status", "quantity": 1, "price": 23}, + "Ornate Cup": {"type": "artifact", "effect": "boost_status", "quantity": 1, "price": 23}, + "Golden Figurine": {"type": "artifact", "effect": "boost_status", "quantity": 1, "price": 23}, + "Simple Sword": {"type": "weapon", "effect": "melee_attack", "quantity": 1, "price": 23}, + "Ornate Sword": {"type": "weapon", "effect": "melee_attack", "quantity": 1, "price": 23}, + "Double-Bladed Axe": {"type": "weapon", "effect": "melee_attack", "quantity": 1, "price": 23}, + "Spear": {"type": "weapon", "effect": "melee_attack", "quantity": 1, "price": 23}, + "Circular Shield": {"type": "armor", "effect": "defense_boost", "quantity": 1, "price": 23}, + "Golden Trophy": {"type": "reward", "effect": "achievement", "quantity": 1, "price": 23}, + "Candelabra": {"type": "decorative", "effect": "none", "quantity": 1, "price": 23}, + "Potion (Red)": {"type": "consumable", "effect": "restore_health", "quantity": 1, "price": 23}, + "Potion (Blue)": {"type": "consumable", "effect": "restore_mana", "quantity": 1, "price": 23}, + "Potion (Green)": {"type": "consumable", "effect": "poison_resistance", "quantity": 1, "price": 23}, + "Square Jar": {"type": "consumable", "effect": "unknown", "quantity": 1, "price": 23}, + "Cake": {"type": "food", "effect": "restore_health", "quantity": 1, "price": 23}, + "Donut": {"type": "food", "effect": "restore_health", "quantity": 1, "price": 23}, + "Bread": {"type": "food", "effect": "restore_health", "quantity": 1, "price": 23}, + "Rug Tile": {"type": "decorative", "effect": "none", "quantity": 1, "price": 23}, + "Geometric Pattern": {"type": "decorative", "effect": "none", "quantity": 1, "price": 23}, + "Glowing Orb (Blue)": {"type": "artifact", "effect": "magic_boost", "quantity": 1, "price": 23}, + "Glowing Orb (Red)": {"type": "artifact", "effect": "fire_boost", "quantity": 1, "price": 23}, + "Glowing Orb (Green)": {"type": "artifact", "effect": "nature_boost", "quantity": 1, "price": 23}, + "Golden Ring": {"type": "artifact", "effect": "magic_resistance", "quantity": 1, "price": 23}, + "Amulet": {"type": "artifact", "effect": "protection", "quantity": 1, "price": 23}, + "Scroll": {"type": "scroll", "effect": "learn_spell", "quantity": 1, "price": 23}, + "Key": {"type": "tool", "effect": "unlock", "quantity": 1, "price": 23}, + "Tool": {"type": "tool", "effect": "repair", "quantity": 1, "price": 23}, + "Dragon (Red)": {"type": "creature", "effect": "fire_attack", "quantity": 1, "price": 23}, + "Dragon (Green)": {"type": "creature", "effect": "nature_attack", "quantity": 1, "price": 23}, + "Dragon (Black)": {"type": "creature", "effect": "dark_attack", "quantity": 1, "price": 23}, + "Dragon (White)": {"type": "creature", "effect": "light_attack", "quantity": 1, "price": 23}, + "Gem Cluster": {"type": "treasure", "effect": "trade", "quantity": 1, "price": 23}, + "Glowing Crystal": {"type": "treasure", "effect": "magic_boost", "quantity": 1, "price": 23} } diff --git a/data/wallet.json b/data/wallet.json new file mode 100644 index 0000000..4793d57 --- /dev/null +++ b/data/wallet.json @@ -0,0 +1,5 @@ +{ + "player_wallet": { + "quantity": 100000 + } +} \ No newline at end of file diff --git a/src/inventory.py b/src/inventory.py index ac00c94..84e34ae 100644 --- a/src/inventory.py +++ b/src/inventory.py @@ -3,6 +3,7 @@ this file contain types of items, like Chest """ +from src.utils.currency import load_inventory, load_wallet, save_wallet from src.utils.messaging import get_message @@ -26,6 +27,9 @@ class Inventory: def __init__(self) -> None: # Currency self.money: int = 0 + self.wallet = load_wallet() + self.price = load_inventory() + self.player_wallet = self.wallet["player_wallet"]["quantity"] # Item management self.items: dict[str, int] = {} # name: quantity @@ -58,10 +62,15 @@ def use_item(self, item_name: str) -> str: if self.remove_item(item_name, 1) == get_message("inventory", "remove_success", item=item_name, quantity=1): return get_message("inventory", "use_success", item=item_name) return get_message("inventory", "use_fail", item=item_name) - + def buy_item(self, item_name, quantity): if item_name in self.items: self.items[item_name] += quantity + if "price" in self.price.get(item_name, {}): + self.item_price = self.price[item_name]["price"] + if self.player_wallet >= self.item_price: + self.player_wallet -= self.item_price + self.save_gold = save_wallet(self.player_wallet) return get_message("shop_inventory", "buy_success", item=item_name, quantity=quantity) else: self.items[item_name] = quantity @@ -69,16 +78,23 @@ def buy_item(self, item_name, quantity): def sell_item(self, item_name, quantity): if item_name in self.items and self.items[item_name] >= quantity: - self.items[item_name] -= quantity - if self.items[item_name] == 0: - del self.items[item_name] - return get_message("shop_inventory", "sell_success", item=item_name, quantity=quantity) + if "price" in self.price.get(item_name, {}): + self.item_price = self.price[item_name]["price"] + self.items[item_name] -= quantity + self.player_wallet += self.item_price + self.save_gold = save_wallet(self.player_wallet) + if self.items[item_name] == 0: + del self.items[item_name] + return get_message("shop_inventory", "sell_success", item=item_name, quantity=quantity) return get_message("shop_inventory", "sell_fail", item=item_name, quantity=quantity) def get_items(self) -> dict[str, int]: """Return a copy of the items dictionary.""" return self.items.copy() + def get_money(self): + return self.money + # Methods for Chest and Quest def add_chest(self, chest: Chest) -> None: """Add a chest to the inventory.""" diff --git a/src/shop.py b/src/shop.py index 724e32e..33eb15d 100644 --- a/src/shop.py +++ b/src/shop.py @@ -5,6 +5,7 @@ from src.inventory import Inventory from src.settings import WORLD_LAYERS from src.states.base_state import BaseState +from src.utils.currency import load_inventory class ShowShop(pygame.sprite.Sprite): @@ -40,6 +41,7 @@ def __init__(self, game_state_manager, player, show_shop, inventory: Inventory): self.message = "" self.message_end_time = 0 + self.price_tag = load_inventory() self.sprite_sheet = pygame.image.load("images/tilesets/Treasure+.png").convert_alpha() self.icons = { "Gold Coin": self.extract_icon(0, 0), @@ -120,9 +122,14 @@ def render(self, screen: pygame.Surface): for item, quantity in visible_items: if item in self.icons: self.screen.blit(self.icons[item], (50, y_offset)) - - quantity_text = self.font.render(f"x{quantity}", True, (255, 255, 255)) - self.screen.blit(quantity_text, (100, y_offset + 5)) + + # quantity_text = self.font.render(f"x{quantity}", True, (255, 255, 255)) + # self.screen.blit(quantity_text, (100, y_offset + 5)) + if "price" in self.price_tag.get(item, {}): + item_price = self.price_tag[item]["price"] + + price_text = self.font.render(f"${item_price}", True, (255, 255, 255)) + self.screen.blit(price_text, (100, y_offset + 5)) text = self.font.render(item, True, (255, 255, 255)) self.screen.blit(text, (150, y_offset)) diff --git a/src/states/game_running.py b/src/states/game_running.py index b4993c6..26c31df 100644 --- a/src/states/game_running.py +++ b/src/states/game_running.py @@ -15,6 +15,7 @@ from src.sprites import AnimatedSprites from src.states.base_state import BaseState from src.states.paused import Paused +from src.states.player_gui import Player_gui from src.support import all_character_import, coast_importer, import_folder @@ -151,6 +152,8 @@ def render(self, screen) -> None: """draw sprites to the canvas""" screen.fill("#000000") self.all_sprites.draw(self.player.rect.center) + player_gui = Player_gui(screen) + player_gui.draw_gui(screen) # self.welcome_message = self.font.render("Press 'E' to interact!", True, (100, 100, 100)) # point = self.shop.rect diff --git a/src/states/player_gui.py b/src/states/player_gui.py new file mode 100644 index 0000000..abd04f4 --- /dev/null +++ b/src/states/player_gui.py @@ -0,0 +1,23 @@ +import pygame + +from src.utils.currency import load_wallet + + +class Player_gui: + def __init__(self, screen): + self.font = pygame.font.Font(None, 36) + self.screen = screen + self.margin = 20 + self.wallet = load_wallet() + self.player_wallet = self.wallet["player_wallet"]["quantity"] + + def draw_gui(self, screen: pygame.Surface): + player_money = self.font.render(f"Gold: {self.player_wallet}", True, (255, 255, 255)) + + text_wdith, text_height = player_money.get_size() + x_pos = screen.get_width() - text_wdith - self.margin + y_pos = self.margin + self.screen.blit(player_money, (x_pos, y_pos)) + + screen.blit(self.screen, dest=(0, 0)) + pygame.display.flip() # Update the display diff --git a/src/utils/currency.py b/src/utils/currency.py new file mode 100644 index 0000000..e22c248 --- /dev/null +++ b/src/utils/currency.py @@ -0,0 +1,17 @@ +import json + + +def load_wallet(): + with open("data/wallet.json", "r") as file: + return json.load(file) + + +def save_wallet(player_wallet): + wallet_data = {"player_wallet": {"quantity": player_wallet}} + with open("data/wallet.json", "w") as file: + return json.dump(wallet_data, file, indent=4) + + +def load_inventory(): + with open("data/inventory.json", "r") as file: + return json.load(file)