1

I'm working on a Snake Game in Pygame, but I bumped into a little problem: my Snake moves too fast. I thought I could use a time.sleep(), but in this way the program stop itself and it doesn't register keys pressing anymore. So I looked on the internet for other options and I found out I could use thread. That was a good solution, but I actually can't restart my timer after I've used it.

import threading
def mythread():
    print("hello")
t=threading.Timer(3.0,mythread)

I run the test code and I then I try to run the command

t.start()

but I receive this error message:

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    t.start()
RuntimeError: threads can only be started once

I'd just like to be able to start the timer again, is there a way to do this?

Jacopo
  • 123
  • 1
  • 10
  • Sounds like an XY problem. I suggest you move the snake in its own thread, where you can also delay the movement with sleep(), and change the direction in a different (main) thread, that reacts to user input and is not influenced by the sleep() of the snake's movement. – Mike Scotty Sep 11 '19 at 15:09
  • Thank you, I'll try out this – Jacopo Sep 11 '19 at 16:10
  • No you can't, but you can pause them. See [How to start and stop thread?](https://stackoverflow.com/questions/15729498/how-to-start-and-stop-thread) – martineau Sep 11 '19 at 16:12
  • ok, thank you for the answer :) – Jacopo Sep 11 '19 at 17:36

0 Answers0