Pygame and Time module clash?

This is the place for queries that don't fit in any of the other categories.

Pygame and Time module clash?

Postby BlueBeachMonster » Sat Apr 05, 2014 10:15 pm

Basically, I've put:
Code: Select all
In = True
while True:
#Lots of code
    if In == True:
        time.sleep(2)
        EntC = (random.choice(EntP))

Then basically I have some code set up to display the randomized moves such as left right up & down (randomized moving entity), but because the time.sleep is in the while loop, it renders the game at 0.5 fps (freezes for 2 seconds then a single/what seems like a single frame), as this function was obviously not meant for pygame, so is there a way for delays that does not involve freezing the rest of the code & only affecting the indented ones?

Any help is appreciated! Thanks,
Tom.
Last edited by stranac on Sat Apr 05, 2014 10:43 pm, edited 1 time in total.
Reason: Added code tags.
BlueBeachMonster
 
Posts: 3
Joined: Fri Apr 04, 2014 8:33 pm

Re: Pygame and Time module clash?

Postby DrakeMagi » Sat Apr 05, 2014 10:32 pm

use pygame.time.get_ticks() . It in milliseconds.

example
Code: Select all
import pygame
# pygame.init() , needed for get_ticks to start and for other modules too.
pygame.init()

def main():
    screen = pygame.display.set_mode((800,600))
    pygame.display.set_caption("get_tick example")
    clock = pygame.time.Clock()
   
    next_tick = 2000 # in milliseconds
    colors = [(0,0,200), (200,0,0), (0,200,0), (0,100,200)]
    color_number = 0
   
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
               
        get_tick = pygame.time.get_ticks()
        if get_tick > next_tick:
            color_number += 1
            color_number %= len(colors)
            next_tick += 2000
           
        screen.fill((0,0,0))
        pygame.draw.rect(screen, colors[color_number], pygame.Rect(10,10,50,50))
        pygame.display.flip()
        clock.tick(60)
       
    pygame.quit()
   
main()
DrakeMagi
 
Posts: 89
Joined: Sun May 12, 2013 8:36 pm

Re: Pygame and Time module clash?

Postby stranac » Sat Apr 05, 2014 10:44 pm

Please read this to learn how to post code properly: viewtopic.php?t=145
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests