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()
what problem do you have?
My key press isnt recognised
did you try to debug? what you get when print Move1?
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)
.
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 :/
(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
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