1

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.

0

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.