I'm trying to make a program that makes you lose "energy" (Python 3)

Viewed 178

I'm trying to make a program that makes you lose "energy" for a game.

I made it so when you press enter you gain "energy":

if question == '':
    print("press enter to gain energy")



    while True:

     energyup = input()

     if energyup == "":
         energy += energygain



     print(energy)

I also want to make it so you lose energy every 0.5 seconds. I don't know how to do this.

I tried adding:

while True:
     energy -= energyloss
 print(energy)

     time.sleep(1)

to the bottom of the program, but that makes you lose energy WAY too fast and I don't know if there is a way to make 1 line of code sleep while everything else continues (I'm using time.sleep which affects the whole program) and on top of that it makes the energy gaining system just not work.

Sorry if this is a dumb question I just started learning programing and this is my first question on this site.

3 Answers

Instead of time.sleep, just check if it has been 0.5 seconds past the last time energy was lost:

prev_time = time.time()
while True:
    # rest of the game loop
    curr_time = time.time()
    if curr_time - prev_time > 0.5:
        energy -= energyloss
        prev_time = curr_time

This lets the rest of the game loop code to run concurrently.

You need to use threads. Thread A is responsible for consumption, and thread B listens for input


energy = 0
energyloss = 1
energygain = 1
lock = threading.Lock()


def consumption():
    global energy
    while 1:
        with lock:
            energy -= energyloss
            time.sleep(0.5)
            print(energy)


threading.Thread(target=consumption).start()

Use threading.Lock to ensure thread safety

You also need to call with lock when increasing energy

I see solution with threading which might be helpful but from your problem statement, I believe you only need to have a function that will provide the diff between the total energy accumulated and the one lost with time.

To do so, you may start a timer at the beginning and create a function that will use the current time minus the execution time to calculate the energy lost each time.

The below might be enough for you case:

import time

energy_gain_constant = 5
energy_loss_constant = -2
total_energy_gain = 0
start_time = time.time()

def print_total_energy():
    print(total_energy_gain + int((time.time() - start_time) / 0.5) * energy_loss_constant)


print("press enter to gain energy")
while True:
    energyup = input()
    if energyup == "":
        total_energy_gain += energy_gain_constant

    print_total_energy()

Adding debug logs into the method that prints how it will behave:

def print_total_energy():
    total_energy_lost = int((time.time() - start_time) / 0.5) * energy_loss_constant
    print(f'Total execution time: {time.time() - start_time}')
    print(f'Total energy lost: {total_energy_lost}')
    print(f'Total energy gained: {total_energy_gain}')
    print(f'Total Energy: {total_energy_gain + total_energy_lost}')

Output:

Total execution time: 12.982820272445679
Total energy lost: -50
Total energy gained: 65
Total Energy: 15
Related