Building the Game HUD | CodeSignal Learn
Building the Game HUD
Building the Heads-Up Display and Derived State
Reducer Update: Toggle Pause Join the 1M+ learners on CodeSignal Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
Start learning today!
GameHUD: Reading State and Computing Derived Values
Now create the HUD that consumes the global state and derives UI values on the fly (no extra component state needed). You read from Context, compute display values, and conditionally style elements.
// src/components/GameHUD.jsx
import React from 'react';
import { useGameContext } from '../context/GameContext';
import './GameHUD.css';
export default function GameHUD() {
const { state, dispatch } = useGameContext();
const formatGameTime = () => {
const hour = state.timers.gameHour;
const minute = Math.floor(state.timers.gameMinute);
const ampm = hour >= 12 ? 'PM' : 'AM';
const displayHour = hour > 12 ? hour - 12 : hour;
return `${displayHour}:${minute.toString().padStart(2, '0')} ${ampm}`;
};
const getQueueColor = () => {
const percentage = (state.shop.queueLength / state.config.maxQueue) * 100;
if (percentage < 30) return '#4CAF50';
if (percentage < 60) return '#FFC107';
if (percentage < 80) return '#FF9800';
return '#F44336';
};
const isRushHour = state.timers.gameHour === 7;
return (
<div className={`game-hud${isRushHour ? ' rush-hour' : ''}`}>
<div className="hud-section time-section">
<h2 className={isRushHour ? 'pulse-fast' : ''}>
🕐 {formatGameTime()}
</h2>
<p className="hud-label">{isRushHour ? '⚡ RUSH HOUR ⚡' : 'Morning Shift'}</p>
<button
className="neutral-btn small-btn"
onClick={() => dispatch({ type: 'TOGGLE_PAUSE' })}
>
{state.timers.isPaused ? '▶️ Resume' : '⏸️ Pause'}
</button>
</div>
<div className="hud-section resources-section">
<div className="resource-item">
<span className="resource-icon">💰</span>
<div>
<div className="resource-value">${state.resources.money}</div>
<div className="resource-label">Cash</div>
</div>
</div>
<div className="resource-item">
<span className="resource-icon">☕</span>
<div>
<div className="resource-value">{state.resources.coffeeBeans}</div>
<div className="resource-label">Beans</div>
</div>
</div>
<div className="resource-item">
<span className="resource-icon">🥛</span>
<div>
<div className="resource-value">{state.resources.milk}</div>
<div className="resource-label">Milk</div>
</div>
</div>
<div className="resource-item">
<span className="resource-icon">⚡</span>
<div>
<div className="resource-value">{state.resources.energy}</div>
<div className="resource-label">Energy</div>
</div>
</div>
<div className="resource-item">
<span className="resource-icon">😊</span>
<div>
<div className="resource-value">{state.resources.satisfaction}</div>
<div className="resource-label">Satisfaction</div>
</div>
</div>
</div>
<div className="hud-section queue-section">
<div className="queue-header">
<span>👥 Customer Queue</span>
<span style={{ color: getQueueColor() }}>
{state.shop.queueLength} / {state.config.maxQueue}
</span>
</div>
<div className="queue-bar" style={{ background: '#eee', borderRadius: 8, height: 16, marginTop: 4 }}>
<div
className="queue-fill"
style={{
width: `${(state.shop.queueLength / state.config.maxQueue) * 100}%`,
backgroundColor: getQueueColor(),
height: '100%',
borderRadius: 8,
transition: 'width 0.3s'
}}
/>
</div>
</div>
</div>
);
}
Explanation:
useGameContext gives you state and dispatch from the global store (no prop drilling).
formatGameTime derives a human-readable clock from timers.gameHour/minute during render.
getQueueColor derives a color from the queue percentage; the same function feeds the text color and the bar color.
isRushHour toggles visual emphasis when the hour hits 7.
The Pause/Resume button dispatches TOGGLE_PAUSE. The label depends on timers.isPaused.
The queue bar width is computed from queueLength / maxQueue — no extra state means fewer re-renders and no sync bugs.
Note: The Pause/Resume button in the HUD currently only toggles the isPaused flag in state; it does not affect the actual game logic or timers yet. Implementing a real game loop that responds to pause/resume will be covered in a future course. For now, focus on wiring up the UI and state.
// src/components/GameHUD.jsx
import React from 'react';
import { useGameContext } from '../context/GameContext';
import './GameHUD.css';
export default function GameHUD() {
const { state, dispatch } = useGameContext();
const formatGameTime = () => {
const hour = state.timers.gameHour;
const minute = Math.floor(state.timers.gameMinute);
const ampm = hour >= 12 ? 'PM' : 'AM';
const displayHour = hour > 12 ? hour - 12 : hour;
return `${displayHour}:${minute.toString().padStart(2, '0')} ${ampm}`;
};
const getQueueColor = () => {
const percentage = (state.shop.queueLength / state.config.maxQueue) * 100;
if (percentage < 30) return '#4CAF50';
if (percentage < 60) return '#FFC107';
if (percentage < 80) return '#FF9800';
return '#F44336';
};
const isRushHour = state.timers.gameHour === 7;
return (
<div className={`game-hud${isRushHour ? ' rush-hour' : ''}`}>
<div className="hud-section time-section">
<h2 className={isRushHour ? 'pulse-fast' : ''}>
🕐 {formatGameTime()}
</h2>
<p className="hud-label">{isRushHour ? '⚡ RUSH HOUR ⚡' : 'Morning Shift'}</p>
<button
className="neutral-btn small-btn"
onClick={() => dispatch({ type: 'TOGGLE_PAUSE' })}
>
{state.timers.isPaused ? '▶️ Resume' : '⏸️ Pause'}
</button>
</div>
<div className="hud-section resources-section">
<div className="resource-item">
<span className="resource-icon">💰</span>
<div>
<div className="resource-value">${state.resources.money}</div>
<div className="resource-label">Cash</div>
</div>
</div>
<div className="resource-item">
<span className="resource-icon">☕</span>
<div>
<div className="resource-value">{state.resources.coffeeBeans}</div>
<div className="resource-label">Beans</div>
</div>
</div>
<div className="resource-item">
<span className="resource-icon">🥛</span>
<div>
<div className="resource-value">{state.resources.milk}</div>
<div className="resource-label">Milk</div>
</div>
</div>
<div className="resource-item">
<span className="resource-icon">⚡</span>
<div>
<div className="resource-value">{state.resources.energy}</div>
<div className="resource-label">Energy</div>
</div>
</div>
<div className="resource-item">
<span className="resource-icon">😊</span>
<div>
<div className="resource-value">{state.resources.satisfaction}</div>
<div className="resource-label">Satisfaction</div>
</div>
</div>
</div>
<div className="hud-section queue-section">
<div className="queue-header">
<span>👥 Customer Queue</span>
<span style={{ color: getQueueColor() }}>
{state.shop.queueLength} / {state.config.maxQueue}
</span>
</div>
<div className="queue-bar" style={{ background: '#eee', borderRadius: 8, height: 16, marginTop: 4 }}>
<div
className="queue-fill"
style={{
width: `${(state.shop.queueLength / state.config.maxQueue) * 100}%`,
backgroundColor: getQueueColor(),
height: '100%',
borderRadius: 8,
transition: 'width 0.3s'
}}
/>
</div>
</div>
</div>
);
} Copyright © 2026 CodeSignal