Creating an Inventory Display System in Pygame for a Stardew Valley-Style Game
Creating an Inventory Display System in Pygame for a Stardew Valley-Style Game
In this guide, we'll implement an inventory overlay that displays the player's current selected tool and seed at the bottom of the game screen, similar to the inventory system in Stardew Valley.
Implementation Approach
First, we'll create a new file named inventory_display.py that will contain an InventoryDisplay class. This class will be responsible for loading all possible tool and seed graphics and displaying the currently selected items on the screen.
Code Implementation
Let's start by creating the inventory_display.py file with the following content:
import pygame
from game_settings import *
class InventoryDisplay:
def __init__(self, player):
# Get the game display surface
self.screen = pygame.display.get_surface()
# Store reference to player object
self.player = player
# Dictionary to store tool graphics
self.tool_graphics = {}
# Dictionary to store seed graphics
self.seed_graphics = {}
# Path to overlay graphics
graphics_path = '../assets/inventory/'
# Load all tool graphics
for tool in player.available_tools:
# Load and convert the image
tool_image = pygame.image.load(graphics_path + tool + '.png').convert_alpha()
self.tool_graphics[tool] = tool_image
# Load all seed graphics
for seed in player.available_seeds:
# Load and convert the image
seed_image = pygame.image.load(graphics_path + seed + '.png').convert_alpha()
self.seed_graphics[seed] = seed_image
def render(self):
# Get the currently selected tool's image
current_tool = self.tool_graphics[self.player.active_tool]
# Set position for tool display (bottom center)
tool_position = current_tool.get_rect(midbottom=INVENTORY_POSITIONS['tool'])
# Draw the tool on screen
self.screen.blit(current_tool, tool_position)
# Get the currently selected seed's image
current_seed = self.seed_graphics[self.player.active_seed]
# Set position for seed display (bottom center)
seed_position = current_seed.get_rect(midbottom=INVENTORY_POSITIONS['seed'])
# Draw the seed on screen
self.screen.blit(current_seed, seed_position)
The key new method here is screen.blit(), which is used to draw images onto the screen at specified positions. It takes two parameters: the image to draw and the position coordinates. For example, self.screen.blit(image, (0, 0)) would draw the image at the top-left corner of the screen.
Next, we need to modify the game_level.py file to integrate this inventory display system:
import pygame
from game_settings import *
from player import Player
from inventory_display import InventoryDisplay
class GameLevel:
def __init__(self):
# Get the game display surface (same as in main.py)
self.screen = pygame.display.get_surface()
# Create sprite group
self.all_sprites = pygame.sprite.Group()
# Initialize game elements
self.setup()
# Create inventory display instance
self.inventory_display = InventoryDisplay(self.player)
def setup(self):
# Create player instance
self.player = Player((640, 360), self.all_sprites)
def update(self, time_delta):
# Clear screen with black background
self.screen.fill('black')
# Draw all sprites
self.all_sprites.draw(self.screen)
# Update all sprites
self.all_sprites.update(time_delta)
# Render inventory display
self.inventory_display.render()
The changes involve importing the InventoryDisplay class, creating an instence of it after the player is set up, and calling its render() method in the update loop. Since the inventory needs to be drawn every frame, it's included in the update function.