Time based loops - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Time based loops (/thread-12959.html) |
Time based loops - Owenix - Sep-20-2018 Having a hard time figuring out how to make timeloops. The goal is having a text flicker on screen every even number of seconds (2,4,6 etc..) This is the code i have so far: intro = True x = time.time() while intro: y = int(time.time() - x) if y % 2 == 0: message_to_screen(font2,"Keys",white,(width/4,height/2)) else: x = time.time() win.blit(background,(0,0))The problem is that the screen is just freezing when running this. I tried also setting time.sleep(1) after the loop, not working and even if so it kinda ruins the other code. * Text is appearing on screen, and blit deletes everything off the screen EXCEPT "Keys". Help will be much appreciated :) RE: Time based loops - nilamo - Sep-20-2018 Could you show a runnable code snippet? It looks fine, but it's also an infinite while loop. Do you ever leave the loop? Or call win.flip() ? Also, is win a pygame.display ?
RE: Time based loops - Owenix - Sep-20-2018 (Sep-20-2018, 03:37 PM)nilamo Wrote: Could you show a runnable code snippet? It looks fine, but it's also an infinite while loop. Do you ever leave the loop? Or call Win is a pygame.display yes, and I am calling .update on the message_to_screen def Even setting intro == False (on the 'else' statment) doesn't seem to help the screen-freeze full code below: import pygame import random import time import sys pygame.init() #### Colors white = 255,255,255 red = 255,0,0 blue = 0,0,255 green = 0,255,0 yellow = 255,255,0 black = 0,0,0 grey = 128,128,128 orange = 255,231,60 #### Screen width = 800 height = 800 #### GVars win = pygame.display.set_mode([width,height]) pygame.display.set_caption("Snake") clock = pygame.time.Clock() game = 0 game_start = False #### Background bg = black background = pygame.Surface(win.get_size()) background = background.convert() background.fill(bg) ### Snake snake_dir = "up" #### Fonts font1 = pygame.font.SysFont('Comic Sans MS', 45) font2 = pygame.font.SysFont('Comic Sans MS', 35) font3 = pygame.font.SysFont('Comic Sans MS', 20) font4 = pygame.font.SysFont('membra', 72) def message_to_screen(font,msg,color,where): screen_text = font.render(msg, True, color) win.blit(screen_text, where) pygame.display.update() def intro_game(): message_to_screen(font4,"SNAKE",white,(width/4, height/8)) message_to_screen(font2,"Version 1.0",white,(width/4, height/5)) message_to_screen(font2,"Keys are your controls",white,(width/4, height/4)) intro = True x = time.time() while intro: y = int(time.time() - x) if y % 2 == 0: message_to_screen(font2,"Keys",white,(width/4,height/2)) else: x = time.time() win.blit(background,(0,0)) intro_game() RE: Time based loops - nilamo - Sep-20-2018 1) you never flip the display. Blitting doesn't make something visible, it just writes it to screen that's in memory, which doesn't actually get displayed on the screen until you call win.flip() . This is for performance reasons, because pushing something to the screen is very slow, so you want to do it all at once after you have the image ready.2) you have a clock, but never use it. So instead of 30 fps, or 60 fps, you're running as fast as you possibly can, which eats cpu. 3) you never check pygame events, which is why it locks. Events are how an operating system talks to an application, so if you don't poll events, the OS thinks your app is hanging, and will pop up the "unresponsive application" dialog. The pygame.QUIT event is hit if the OS tells your app it should close (if the user hits the "x", etc).4) you only write "Keys" when there's a two second gap between time.time() and x . But you also update x every loop (so 9001 times per second). So there's never 2 seconds, because it's always 0.001 seconds.5) there's no character limits, so there's no reason not to use variable names that describe what they're for, instead of x or y .With all that in mind, I made a few changes to your intro_game() function (I didn't touch anything outside the function):def intro_game(): message_to_screen(font4,"SNAKE",white,(width/4, height/8)) message_to_screen(font2,"Version 1.0",white,(width/4, height/5)) message_to_screen(font2,"Keys are your controls",white,(width/4, height/4)) intro = True last_update = time.time() while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: intro = False seconds_since_last_update = (time.time() - last_update) if seconds_since_last_update >= 2: message_to_screen(font2,"Keys",white,(width/4,height/2)) last_update = time.time() else: win.blit(background,(0,0)) pygame.display.flip() clock.tick(60) # desired fps RE: Time based loops - Owenix - Sep-20-2018 Thanks a lot! really helpful |