0

I was trying to build a T-Rex (The Dinosaur game available offline in chrome) clone in pygame. But now I'm facing an issue that is if the user presses the up arrow key the character stays there at the top all the time until the key is released as opposed to what happens in the T-Rex game.

I have coded it in such a way that when the user presses the up arrow key the character should go up by almost 80 pixels and come back when the key is released. But I need it to reverse back to its original position after a while even if the up arrow key is not released. So I placed a time.sleep() statement but it pauses the whole game along with the obstacles which is not intended.

Github link.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    Please don't post links to your code, create a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) instead and add it to your question. If you want to implement gravity, take a look at [this answer](https://stackoverflow.com/a/46875464/6220679). – skrx Nov 16 '17 at 13:45
  • you can't use `sleep` because it stops mainloop - you have to execute function in mainloop (again and again) and use `time.time()` to check if still display message. You can also use list with functions executed in mainloop and use time.time() to chech if it is time to remove function from list. – furas Nov 16 '17 at 17:14
  • BTW: example using `pygame.time.get_ticks()` to control when to display text - the same way you can control when to stop displaying: https://github.com/furas/python-examples/blob/master/pygame/time-execute-function/example-1.py – furas Nov 16 '17 at 17:19

0 Answers0