0

I want my python program to wait before continuing the loop. But the other functions, loops usw. should work. If I use the sleep-function the whole program would sleep. Example:

import pygame

def function():
   print("You moved forward")

turtle.listen()
turtle.onkey(function, "f")

print("use f to move forward")
(Wait 5 seconds, so the player can try pressing f and sees what happens)

Thats a simple version of my original code. I want the player to try WASD cause the character is moved with WASD.

If I'd use time.sleep() it won't work if the player presses f.

Thanks for any answer :D

-> btw sry for my bad english...

MF714
  • 153
  • 7
  • I want make a tutorial for a game. The player is moved with WASD so there should be a text "press WASD to move". Then the player has some seconds time to press WASD and move around. – MF714 Apr 04 '20 at 07:00
  • what about ```input()```. this will wait until the player presses Enter – Aven Desta Apr 04 '20 at 07:02
  • Sorry, it should be a comment, but I can't comment :( Try this : https://stackoverflow.com/a/48111882/12658348 – Malcador Apr 04 '20 at 07:04

1 Answers1

1

If you want to wait for 5 seconds but not sleep then you can maybe try with a loop and measure the running time of the loop. Something like this maybe:

First make sure pytictoc is installed by running pip install pytictoc

Then run a loop like this maybe:

from pytictoc import TicToc
t = TicToc() #create instance of class
t.tic() #Start timer
while (t.tocvalue()<5):
    pass
t.toc()
amm98d
  • 1,997
  • 2
  • 9
  • 15
  • There is an error: SyntaxError: Non-ASCII character '\xc3' in file test.py on line 141, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details MyNames-MacBook-Pro:rpg username$ – MF714 Apr 04 '20 at 07:24
  • I'll need to see more of your code to see if this error is from this code or not. It is possible that it is related to something else perhaps. – amm98d Apr 04 '20 at 08:08
  • I tried it in a other document and there was no error. But it works just like time.sleep(), the screen gets paused. Functions aren't working. Here's my code: import turtle from pytictoc import TicToc #Add a function def testfunc(): print("Key pressed") turtle.listen() #First message print("test") #timer t = TicToc() #create instance of class t.tic() #Start timer while (t.tocvalue()<5): pass t.toc() #Second message print("test2") #Keyboard turtle.onkey(testfunc, "x") delay = raw_input("Press enter to finish") I want that the function still works – MF714 Apr 08 '20 at 16:43