Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MSVCRT Help
#1
I want my distance variable to increase by 1 every time i click a / d.

import msvcrt
import time
import random

def Start():
    input("Press ENTER to start")
    print("On your marks...")
    time.sleep(random.uniform(2, 5))
    print("Set...")
    time.sleep(random.uniform(0.5, 2))
    print("GO!")
    Distance = 100
    Race(Distance)
    
def Race(Distance):
    Travelled = 0
    while Travelled != Distance:
        Move1 = msvcrt.getwch()
        if Move1 == chr(97):
            Move2 = msvcrt.getwch()
            if Move2 == chr(100):
                Distance = Distance + 1
                print(Distance)

Start()
Reply
#2
what problem do you have?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
My key press isnt recognised
Reply
#4
did you try to debug? what you get when print Move1?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Look at this post where i explain how msvcrt work.
So on first call get only info back if it's Scan code or Function keys,next call after that get keyboard input.
See how i quit b'q' so work in bytes,for you use eg b'a' instead of chr(97).
Reply
#6
Thanks for the reply's but its still not working...

Im on 3.7.4 if that helps. I did a bit of code a couple of years ago on 3 and it seemed to work doing the same thing :/
Reply
#7
(Aug-22-2019, 09:52 AM)TomJS Wrote: Thanks for the reply's but its still not working...
Is this answer ever gone be useful,do you show what you tried Dodgy

Okay here a little more help using your code code.
Do not capitalize every variable,function,..ect PEP-8.
import msvcrt
import time
import random
import sys

def start():
    input("Press ENTER to start")
    print("On your marks...")
    time.sleep(random.uniform(2, 5))
    print("Set...")
    time.sleep(random.uniform(0.5, 2))
    print("GO!")
    distance = 100
    race(distance)

def race(distance):
    travelled = 0
    while travelled != distance:
        move1 = msvcrt.getch()
        if move1 in b'\x00':
            move1 = msvcrt.getch()
        if move1 == b'a':
            travelled += 20
            print(travelled)
        if move1 == b'q':
            print('You quit the race')
            sys.exit()
        else:
           print(f'Key Pressed: {move1}')
    print(f'You reach the end distance: <{distance}>')

start()
Test pushing a:
Output:
E:\div_code\foo λ python -V Python 3.7.3 E:\div_code\foo λ python key_in.py Press ENTER to start On your marks... Set... GO! 20 Key Pressed: b'a' 40 Key Pressed: b'a' 60 Key Pressed: b'a' 80 Key Pressed: b'a' 100 Key Pressed: b'a' You reach the end distance: <100>
Test pushing q:
Output:
E:\div_code\foo λ python key_in.py Press ENTER to start On your marks... Set... GO! 20 Key Pressed: b'a' 40 Key Pressed: b'a' You quit the race
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  msvcrt.getch prints twice inside loop ReallyBigTeeth 5 6,906 Sep-04-2018, 06:54 AM
Last Post: ReallyBigTeeth
  can import msvcrt and click on windows but not on Linux sylas 4 13,254 Aug-06-2018, 08:28 AM
Last Post: Gribouillis
  " No module named 'msvcrt' " for mac harmanjotuppal 12 30,246 Oct-12-2016, 01:12 AM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020