How do I increment an variable in python every 10 seconds?

Question:

So im trying to create an game where the day increase every 10 seconds and the player loses some of his stats, ive tried everything i could think of but im an beginner coder in python.

I tried to understand the time/clock library but i couldn’t find anything useful.

Asked By: Vishaal

||

Answers:

To implement a game that increases the day every 10 seconds and decreases the player’s stats, you can use the time module in Python. The time module provides functions for working with time and dates, including the sleep() function, which can be used to pause the execution of the program for a specific number of seconds.

Here is an example of how you can use the time module to implement a game that increases the day every 10 seconds and decreases the player’s stats:

import time

# Initialize the day and player stats
day = 1
player_stats = { "health": 100, "hunger": 50, "thirst": 50 }

while True:
    # Print the current day and player stats
    print("Day:", day)
    print("Player stats:", player_stats)

    # Decrease the player's stats
    player_stats["health"] -= 5
    player_stats["hunger"] -= 10
    player_stats["thirst"] -= 15

    # Check if the player is still alive
    if player_stats["health"] <= 0:
        print("Game over! You have died.")
        break

    # Pause the program for 10 seconds
    time.sleep(10)

    # Increase the day
    day += 1

This code will run an infinite loop that increases the day and decreases the player’s stats every 10 seconds. It uses the sleep() function to pause the program for 10 seconds, and it checks if the player’s health is less than or equal to 0 to determine if the game is over.

I hope this helps! Let me know if you have any questions.

Answered By: Clause
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.