This recipe demonstrates some keyboard input functions and screen output "magic" that some people may be interested (such as for an ASCII game). It is meant as a very simple demonstration only, and does not contain any error checking whatsoever. The style of the code is not the best, but it is meant to get a point across.<br>NOTE: This code was written to work in windows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from msvcrt import kbhit, getch
from os import system
from time import sleep
x = 0
y = 0
def show(ch):
system('cls')
for z in range(y):
print
print ' ' * x + ch
while True:
sleep(0.03)
if kbhit():
if getch() == '\xe0':
ch = getch()
if ch == 'H':
y -= 1
show('^')
elif ch == 'M':
x += 1
show('>')
elif ch == 'P':
y += 1
show('V')
elif ch == 'K':
x -= 1
show('<')
|
Trying to figure out how to play around with IO in windows? Try this code if what you have to do involves working with text (ASCII).