Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time based loops
#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


Messages In This Thread
Time based loops - by Owenix - Sep-20-2018, 03:34 PM
RE: Time based loops - by nilamo - Sep-20-2018, 03:37 PM
RE: Time based loops - by Owenix - Sep-20-2018, 03:43 PM
RE: Time based loops - by nilamo - Sep-20-2018, 05:25 PM
RE: Time based loops - by Owenix - Sep-20-2018, 05:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,281 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Update Date based on Time/String stevezemlicka 1 2,040 Jan-08-2021, 06:54 PM
Last Post: Gribouillis
  Split gps files based on time (text splitting) dervast 0 1,897 Nov-09-2020, 09:19 AM
Last Post: dervast
  running 2 loops at the same time julio2000 7 4,574 Mar-21-2020, 05:21 PM
Last Post: ndc85430
  Time based control of phillips hue Kimzer 3 3,470 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