I am trying to write a text based game in Python, but have reached a problem. I have my character move from one position to another in the Console, but every time the user presses a key, the character disappears. In order to see the character again, the user must press a key. Here is my code:
import os
import msvcrt
class Frog:
X = 0
Y = 0
def __init__(self, x, y):
self.X = x
self.Y = y
def Draw(self):
for y in range(self.Y):
print ""
print ' ' * self.X + '#'
def Update(self):
if msvcrt.kbhit() == True:
if msvcrt.getch() == 'a':
if self.X > 0:
self.X = self.X - 1
if msvcrt.getch() == 'd':
self.X = self.X + 1
if msvcrt.getch() == 'w':
self.Y = self.Y - 1
if msvcrt.getch() == 's':
self.Y = self.Y + 1
frog = Frog(0,0)
def Draw():
frog.Draw()
os.system('cls')
def Loop():
while 1:
frog.Update()
Draw()
Loop()
Does anyone know what is causing this? All help would be greatly appreciated.
You clear the screen immediately after drawing, rather than before drawing. Thus, the thing you just drew is erased.
def Draw():
frog.Draw()
os.system('cls')
Try switching the order:
def Draw():
os.system('cls')
frog.Draw()