Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time based loops
#1
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 :)
Reply
#2
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?
Reply
#3
(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.flip()? Also, is win a pygame.display?

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()
Reply
#4
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
Reply
#5
Thanks a lot! really helpful
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Update Date based on Time/String stevezemlicka 1 249 Jan-08-2021, 06:54 PM
Last Post: Gribouillis
  Split gps files based on time (text splitting) dervast 0 198 Nov-09-2020, 09:19 AM
Last Post: dervast
  running 2 loops at the same time julio2000 7 1,179 Mar-21-2020, 05:21 PM
Last Post: ndc85430
  Time based control of phillips hue Kimzer 3 1,738 Dec-22-2017, 07:35 PM
Last Post: Kimzer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020