1

I'm a new programmer and I'm developing a little Snake Game. In this project I'd like to use Threads, but I literally don't know HOW. I (obviously) looking on the internet for first, but all the codes I found were "already done" code, ready to be used. But what I want is to learn HOW to use the threads.

In my Snake Game I need Threads to make my Snake move: the player press key W, I register the key on a list and then I increase by 50 the playerpos. But, if I just use functions, the Snake moves super fast and it becomes hard to play. For this reason, I'd like to put my "increase by 50 the playerpos" into the Thread and then set a "time.sleep(1)". In this way I'll be able to slow down my snake without influencing the entire program with the "time.sleep".

And here's where my problem begin: I create a Class

import threading, time
class Thread():

now I can add all my functions

    def mov_up(self):
        playerpos[1]-=50
        time.sleep(1)
    def mov_sx(self):
        playerpos[0]-=50
        time.sleep(1)
    def mov_down(self):
        playerpos[1]+=50
        time.sleep(1)
    def mov_dx(self):
        playerpos[0]+=50
        time.sleep(1)

ok, now, what should I do to make this class a thread? Please give me some help >.<

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Jacopo
  • 123
  • 1
  • 10
  • 1
    Maybe you could give more information about your base code, such as the other classes that you have, to provide better context on how to use threads to solve your problem – applecider Sep 12 '19 at 15:01
  • 2
    Threads are nice in a few cases, but in many they cause problems. One of those cases is in pygame, as pygame expect certain code to be executed in certain threads. Which is why pygame handles threading for you. If you want to learn about threads, then start with a project that doesn't use a library, until you are comfortable enough with them. – Ted Klein Bergman Sep 12 '19 at 15:54
  • 1
    There a simple answer on how to make a thread here - https://stackoverflow.com/questions/2905965/creating-threads-in-python . But for this application you *don't* want a thread. Move the snake less, and/or use a `pygame.time.Clock()`, with a `tick_busy_loop( 60 )` to clamp the frames per second to a reasonable speed. – Kingsley Sep 12 '19 at 23:11
  • Ok, well, for first: thanks so much guys :D – Jacopo Sep 13 '19 at 17:25
  • Then: 1. Just that: I have a Snake (that is actually an orange square). The game starts and the player press key W. What happen? The Snake has to start moving up. How does the Snake do that? Easy, I keep adding +50 to the playerpos variable and then the code print the orange square 50pixel up than before. But this happens too fast and it becomes hard to control the Snake (he covers something like 10 places in 1s) – Jacopo Sep 13 '19 at 17:30
  • 2. Ok, thx for this explanation. At the beginning I thought I only had to use a time.sleep() or something like that. But, unluckily, this code stops the whole program and, in that case, I won't be able to control the key press anymore when the program is "sleeping". So I looked on the internet for another kind of clock and I found threads. But I don't know how to use them and I tried to learn that. In my last question I asked "how i could restart a thread" to use him like a perma-clock, but someone told me "yeah, it would be better if you put all the snake moving into a thread" and so I tried. – Jacopo Sep 13 '19 at 17:38
  • 3. OK, I tried that, but it's not what I really want to do. I mean, I've been able to use tick_busy_loop, but in this way the Snake lose some "reflex" and when I press W, it takes 1 more place to turn app... I'm going to think about a better way. If I won't find anything nice, I'll keep this option. thank you for the idea by the way ^ - ^ – Jacopo Sep 13 '19 at 19:35

0 Answers0