How to Run Multiple While Loops at a Time in Pygame

How to run multiple while loops at a time in Pygame

No, that's not the way it goes. You have an application loop so use it. time.sleep, pygame.time.wait() or pygame.time.delay is not the way to wait or delay something in a typical application. The game does not respond while you wait. Use pygame.time.get_ticks() to measure the time.

In pygame the system time can be obtained by calling pygame.time.get_ticks(), which returns the number of milliseconds since pygame.init() was called. See pygame.time module.

Calculate the point in time when the text needs to be changed. Get a new random text and render the text Surface after the current time is greater than the calculated point of time. Calculate a new random point in time which is greater than the current time:

next_render_time = 0

while run:
current_time = pygame.time.get_ticks()

# [...]

if current_time >= next_render_time:
currentFace = random.choice(face)
faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))
next_render_time = current_time + randint(5, 10) * 1000

screen.fill((0,0,0))
screen.blit(faceDisplay, text_rect)
pygame.display.flip()

The time is multiplied by 1000 (randint(5, 10) * 1000), since the time unit of pygame.time.get_ticks() is milliseconds.

Actually, you can also use a timer event. See How do I use a PyGame timer event?.

In your case, however, the time interval is not constant. It's a bit odd to use a timer event on a dynamically changing interval. I would prefer to use a timer event for an action that needs to be performed at constant intervals. But that's just my opinion.


Complete example:

from random import randint
from time import sleep
import pygame
import pygame.freetype
import time
import random
run = True
pygame.init()

#faces
face = ['^-^', '^v^', '◠◡◠', "'v'", '⁀◡⁀']
talkingFace = ['^o^', '^▽^', '◠▽◠', "'▽'", '⁀ᗢ⁀']
currentFace = random.choice(face)

#background
screen = pygame.display.set_mode((800,600))

#font and size
myFont = pygame.font.Font('unifont.ttf', 100)

#face render
faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))

#center and draw face
text_rect = faceDisplay.get_rect(center=(800/2, 600/2))

next_render_time = 0

#prevent crashes
while run:
current_time = pygame.time.get_ticks()
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False

if current_time >= next_render_time:
currentFace = random.choice(face)
faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))
next_render_time = current_time + randint(5, 10) * 1000

screen.fill((0,0,0))
screen.blit(faceDisplay, text_rect)
pygame.display.flip()

Simple way to run two while loops at the same time using threading?

Assuming your while loops are within the functions you listed, this is the easiest way I can think of.

from threading import Thread

t1 = Thread(target = firstFunction)
t2 = Thread(target = secondFunction)

t1.start()
t2.start()

As pointed out by tdelaney, doing it this way will just kick off each thread and immediately move on. If you need to wait for those threads to complete before running the rest of your program you can use the .join() method.

python run two while loops concurrently

Instead of having two loops that both are dependent on conditions determined within their individual scopes, you can have one loop that you break out of when a condition is met.

while True:
my_var = do_something()
if my_var == 1 or my_var == 2:
do_something_else()
break

How do I run multiple while loops that switch off after each loop round?

Yo can try this, I don't know if it's what you are looking for but it will have your output.

rounds = 0
rounds2= 0
loop=1
while True:
if loop==1:
if rounds == 1:
print('A')
elif rounds == 2:
print('B')
else:
rounds = 0
rounds = rounds + 1
loop=2
if loop==2:
if rounds2 == 1:
print('a')
elif rounds2 == 2:
print('b')
else:
rounds2 = 0
rounds2 = rounds2 + 1
loop=1

how to run two while loops at the same time in Python?

Just use checkMouse() instead of getMouse() inside your animation loop.

Simple as that, I think.

while civiliansSaved < 11:

for horse in horseList:
if horse.leg.getX() > -187
horse.move( -1, 20 )
else:
horse.move( 5, 28 )

for civilian in civiliansList:
if civilian.getX() < 800:
civilian.move( 20, 0 )
else:
civilian.move( -100, 0 )

mouse = win.checkMouse()

if mouse:
princess.move( mouse, civilianCounter)
civilianCounter = princess.move( mouse, civilianCounter)

print( "Game over" )
win.getMouse()
win.close()

Doco:

checkMouse() Similar to getMouse, but does not pause for a user click.
Returns the latest point where the mouse was clicked or None if the
window as not been clicked since the previous call to checkMouse or
getMouse. This is particularly useful for controlling simple animation loops.



Related Topics



Leave a reply



Submit