I'm pretty new to Python, and programming in general and I'm creating a virtual pet style game for my little sister.
Is it possible to run 2 while loops parallel to each other in python? eg:
while 1:
input_event_1 = gui.buttonbox(
msg = 'Hello, what would you like to do with your Potato Head?',
title = 'Main Screen',
choices = ('Check Stats', 'Feed', 'Exercise', 'Teach', 'Play', 'Go to Doctor', 'Sleep', 'Change Favourite Thing', 'Get New Toy', 'Quit'))
if input_event_1 == 'Check Stats':
myPotatoHead.check_p_h_stats()
elif input_event_1 == 'Feed':
myPotatoHead.feed_potato_head()
elif input_event_1 == 'Exercise':
myPotatoHead.exercise_potato_head()
elif input_event_1 == 'Teach':
myPotatoHead.teach_potato_head(myPotatoHead)
elif input_event_1 == 'Play':
myPotatoHead.play_with_toy()
elif input_event_1 == 'Sleep':
myPotatoHead.put_p_h_asleep()
elif input_event_1 == 'Go to Doctor':
myPotatoHead.doctor_check_up()
elif input_event_1 == 'Change Favourite Thing':
myPotatoHead.change_favourite_thing()
elif input_event_1 == 'Quit':
input_quit = gui.ynbox(
msg = 'Are you sure you want to quit?',
title = 'Confirm quit',
choices = ('Quit', 'Cancel'))
if input_quit == 1:
sys.exit(0)
while 1:
time.sleep(20)
myPotatoHead.hunger = str(float(myPotatoHead.hunger) + 1.0)
myPotatoHead.happiness = str(float(myPotatoHead.happiness) - 1.0)
myPotatoHead.tiredness = str(float(myPotatoHead.tiredness) + 1.0)
If not, is there some way that I can turn this into one loop? I want the stuff in the second loop to happen every 20 seconds, but the stuff in the first loop to by constantly happening.
Thanks for any help
Essentially to have processing to happen in parallel you have several solutions
1- Separate processes (ie: programs) running independently that speak to one another through a specific protocol (eg: Sockets)
2- Or you can have the one process spawn off multiple threads
3- Build an event queue internally and process them one by one
That is the general picture.
As for the specific answer to your question, you said "the stuff in the first loop to b[e] constantly happening". The reality is you never want this to happen all the time, because all that will do is use up 100% of the CPU and nothing else will ever get done
The simplest solution is probably number 3.
The way I would implement it is in my main loop have a thread that goes through an event queue and sets a timer for each event. Once all the timers have been sent the main loop then goes to sleep.
When a timer times out, an other function will then run the corresponding function for the event that triggered that timer.
In your case, you have two events. One for displaying the selection menu (first loop) and the second for changing myPotatoHead. The timer associated with the first one, I would set to 0.5sec, making it larger reduces CPU usage but slows down responsivness, increasing it usses up more CPU, for the second event I would set a 20 second timer.
Ofcourse when the timer expires, you would not do
while 1
but you will just go through yourwhile
loop body once (ie get rid of while).Have a look at Threading.Timer.
There is a code recipe here to schedule a function to run every 5 seconds.
i think they cannot be coupled in to one while loop. maybe you need to check the threading or multiprocessing library.
put one of them into a function, the threading.Thread class supports a target attribute:
Will start yourFunc() running in the background.
There is also a package called SimPy that you could also look at. The threading and multiprocessing libraries may also help.
You should use State Machines for this (see the Apress pygame book - downloads here: http://apress.com/book/downloadfile/3765 ), see chapter 7.
A simplified state machine: