Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9052339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:08:40+00:00 2026-06-16T13:08:40+00:00

I am making a snake game which requires the player to press the WASD

  • 0

I am making a snake game which requires the player to press the WASD keys without stopping the game process to to get input from player. So I can’t use input() for this situation because then the game stops ticking to get input.

I found a getch() function which immediately gives input without pressing enter, but this function also stops game ticking to get input like input(). I decided to use threading module to get input via getch() in different thread. The problem is that getch() isn’t working while in different thread and I’m not sure why.

import threading, time
from msvcrt import getch

key = "lol" #it never changes because getch() in thread1 is useless

def thread1():
    while True:
        key = getch() #this simply is almost ignored by interpreter, the only thing it
        #gives is that delays print() unless you press any key
        print("this is thread1()")

threading.Thread(target = thread1).start()

while True:
    time.sleep(1)
    print(key)

So why getch() is useless when it is in thread1()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:08:42+00:00Added an answer on June 16, 2026 at 1:08 pm

    The problem was that you’re creating a local variable key inside thread1 instead of overwriting the existing one. The quick-and-easy solution would be to declare key to be global inside thread1.

    Finally, you should consider using locks. I don’t know if it’s necessary or not, but I’d imagine weird things could happen if you try and write a value to key in the thread while printing it out at the same time.

    The working code:

    import threading, time
    from msvcrt import getch
    
    key = "lol"
    
    def thread1():
        global key
        lock = threading.Lock()
        while True:
            with lock:
                key = getch()
    
    threading.Thread(target = thread1).start()
    
    while True:
        time.sleep(1)
        print(key)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently in the process of making a snake game using VB.NET... I
I'm making a snake game like player and I have the body moving fine
I'm new to C# so I'm making a Snake Game to learn. I created
I'm making a snake game in C# and I have a pause and play
I'm making a Snake type game using Pygame. Everything's pretty much working, but below
I am making a snake game in python with pygame and for moving the
I'm making my first game in allegro 5, it's a snake game. In order
It's my first time working with python sockets. I'm making a snake game, to
I'm making a really simple snake game, and I have an object called Apple
I am making a snake game these days as an assignment...and hope it will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.