Python Forum
Appropriately delay this PyGame code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appropriately delay this PyGame code
#1



I am a beginner to programming but have been interested in mazes, and I was trying to run this program :
http://thelinuxchronicles.blogspot.in/20...ution.html

But, the output comes all at once, and I want to slow it down so that one can actually see the red line painting the path slowly.

I've tried adding a sleep() delay in the elif and if cases, and i expected it to slow it down, but it just slows down the appearance of the line(it still appears all at once). Where do I have to put it?Thank you.
 56 # paints the solution path in the maze window
 57 def maze_path_window(m,w):
 58   path = m.solutionpath
 59 
 60   # print every cell within the solution path
 61   for index in range(len(path)-1):
 62     actrow = path[index][0]
 63     actcol = path[index][1]
 64     nextrow = path[index+1][0]
 65     nextcol = path[index+1][1]
 66     pygame.draw.rect(w,RED,(actcol*CELLSIZE+(actcol+1)*WALLSIZE,actrow*CELLSIZE+(actrow+
 67     1)*WALLSIZE,CELLSIZE,CELLSIZE))
 68 
 69     # also paint the white spaces between the cells
 70     if actrow == nextrow:
 71       if actcol < nextcol:
 72         pygame.draw.rect(w,RED,((actcol+1)*CELLSIZE+(actcol+1)*WALLSIZE,actrow*CELLSIZE+
 73         (actrow+1)*WALLSIZE,WALLSIZE,CELLSIZE))
 74       else:
 75         pygame.draw.rect(w,RED,(actcol*CELLSIZE+actcol*WALLSIZE,actrow*CELLSIZE+(actrow+
 76         1)*WALLSIZE,WALLSIZE,CELLSIZE))
 77     elif actcol == nextcol:
 78       if actrow < nextrow:
 79         pygame.draw.rect(w,RED,(actcol*CELLSIZE+(actcol+1)*WALLSIZE,(actrow+1)*CELLSIZE+
 80         (actrow+1)*WALLSIZE,CELLSIZE,WALLSIZE))
 81       else:
 82         pygame.draw.rect(w,RED,(actcol*CELLSIZE+(actcol+1)*WALLSIZE,actrow*CELLSIZE+
 83         actrow*WALLSIZE,CELLSIZE,WALLSIZE))
Reply
#2
I can't copy paste that without pasting all the left column numbers too. plus it doesn't look like the full program, so the gist should be this:
you can draw whatever you want to the screen, but the update is only visible to viewer when you call pygame.display.flip()
So instead of:
draw whole maze, then flip.
draw a piece of the maze then flip. also put a time.sleep(0.1) after the flip or it will be too fast to see. (import time at top)
Reply
#3
1. You can't use range or any loops. The event loop is your loop.
Never use time.sleep. time.sleep is stop the world.

2. use pygame.time.get_ticks or timers 
make variables current_tick , next_tick
current_tick = 0
next_tick = 0

#event_loop
# some other stuff
current_tick = pygame.time.get_ticks()
if current_tick > next_tick:
    next_tick += 200 # interval
    # do something
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question about my pygame code for space shooter Than999 4 2,513 Feb-07-2022, 03:55 AM
Last Post: metulburr
  question about my Pygame code Than999 2 1,682 Feb-06-2022, 10:03 PM
Last Post: Than999
  [PyGame] Problems with jump code in pygame Joningstone 4 5,365 Aug-23-2021, 08:23 PM
Last Post: deanhystad
  Distributing Python/Pygame code with CX_Freeze jfng75 2 2,814 Jan-11-2021, 10:23 PM
Last Post: snippsat
  My Pygame Code always says "(Not responding") noodlespinbot 3 8,106 Feb-29-2020, 10:50 PM
Last Post: Windspar
  [PyGame] sound effect delay and program laggy xBlackHeartx 26 12,843 Oct-09-2019, 11:36 AM
Last Post: metulburr
  [pygame] Improvement with code SheeppOSU 1 2,403 Jul-24-2019, 11:09 AM
Last Post: metulburr
  Python Pygame code help Trajme 1 4,026 Dec-07-2017, 04:55 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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