Appropriately delay this PyGame code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Appropriately delay this PyGame code (/thread-6163.html) |
Appropriately delay this PyGame code - kleynah22 - Nov-09-2017 I am a beginner to programming but have been interested in mazes, and I was trying to run this program : http://thelinuxchronicles.blogspot.in/2012/07/python-maze-generation-and-solution.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)) RE: Appropriately delay this PyGame code - ineedastupidusername - Nov-09-2017 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) RE: Appropriately delay this PyGame code - Windspar - Nov-09-2017 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 |