Home / Widgets / fundamentals
Duplicate Snippet

Embed Snippet on Your Site

fundamentals

Code Preview
html
</head>
<body>
    <div id="gameState">
        <h1>Game State</h1>
        <p>Round: <span id="round">0</span>/10</p>
        <p>Cash: $<span id="cash">0</span></p>
        <p>Automobile Production: <span id="production">0</span> units</p>
        <p>Industry Level: <span id="level">0</span></p>
    </div>
    <div class="wp-block-button is-style-outline">
        <button id="startButton" class="wp-block-button__link wp-element-button">Start New Game</button>
    </div>
    <div class="wp-block-button is-style-outline">
        <button id="saveButton" class="wp-block-button__link wp-element-button">Save Game</button>
    </div>
    <div class="wp-block-button is-style-outline">
        <button id="loadButton" class="wp-block-button__link wp-element-button">Load Game</button>
    </div>
    <div class="wp-block-button is-style-outline">
        <button id="restartButton" class="wp-block-button__link wp-element-button">Restart Game</button>
    </div>
    <div class="wp-block-button is-style-outline">
        <button id="progressButton" class="wp-block-button__link wp-element-button">Progress to Next Round</button>
    </div>
    <script>
        // Game state
        let gameData = {
            round: 0,
            cash: 0,
            production: 0,
            level: 0,
        };
        // Update the display
        function updateDisplay() {
            document.getElementById('round').textContent = gameData.round;
            document.getElementById('cash').textContent = gameData.cash;
            document.getElementById('production').textContent = gameData.production;
            document.getElementById('level').textContent = gameData.level;
        }
        // Start a new game
        function startGame() {
            gameData = {
                round: 1,
                cash: 10000,
                production: 10,
                level: 1,
            };
            updateDisplay();
            alert('New game started!');
        }
        // Save the game
        function saveGame() {
            localStorage.setItem('gameState', JSON.stringify(gameData));
            alert('Game saved!');
        }
        // Load the game
        function loadGame() {
            const savedGame = localStorage.getItem('gameState');
            if (savedGame) {
                gameData = JSON.parse(savedGame);
                updateDisplay();
                alert('Game loaded!');
            } else {
                alert('No saved game found.');
            }
        }
        // Restart the game
        function restartGame() {
            startGame();
            alert('Game restarted!');
        }
        // Progress to the next round
        function progressRound() {
            if (gameData.round < 10) {
                gameData.round += 1;
                gameData.cash += gameData.production * 5000; // Add income for the round
                updateDisplay();
                alert(`Round ${gameData.round - 1} ended. Starting round ${gameData.round}.`);
            } else {
                alert('Game over!');
            }
        }
        // Attach event listeners to buttons
        document.getElementById('startButton').addEventListener('click', startGame);
        document.getElementById('saveButton').addEventListener('click', saveGame);
        document.getElementById('loadButton').addEventListener('click', loadGame);
        document.getElementById('restartButton').addEventListener('click', restartGame);
        document.getElementById('progressButton').addEventListener('click', progressRound);
        // Initialize the game display
        updateDisplay();
    </script>
</body>

Comments

Add a Comment