0

I am trying to make a text based rpg game in python. I just started and this is what my code looks so far:

class Game:
    def __init__(self):
        self.map = [[" " for i in range(50)]for i in range(30)]

    def draw_map(self):
        for i in range(len(self.map)):
            print(self.map[i])


    def add_stuff(self, thing, x, y):
        self.map[y][x] = thing

game = Game()

class Player:
    def __init__(self):
        self.x = 1
        self.y = 1
        self.player = "ME"

    def draw(self):
        game.add_stuff(self.player, self.x, self.y)

    def move(self):
        pass

player = Player()
player.draw()
game.draw_map()

I was trying to find a way to implement a game loop in some way. To do this i was thinking of running the draw_map() and clearing the screen right away and printing the map again, a bit like real game loops. However i am having problems doing this. Based on other answers to other similar questions, i managed to produce the following code(it just shows the main loop and subprocess is imported as sp).

while True:
    game.draw_map()
    sp.call("cls", shell = True)

However this is not working for me. It simply dosent do anything. I also tried using clear function from clear_screen` module in a similar way and i cant figure out why this wouldnt work. Thanks for any help

  • Your print-clear loop works fine for me (albeit a bit choppy). This might be OS specific since I'm using `subprocess.call('clear')` on Linux. – jfaccioni Feb 13 '20 at 19:56
  • Hmm, thats really weird. Maybe the problem is with "cls" command ? –  Feb 13 '20 at 20:01
  • But i am using windows and the previous answer i looked at said its "cls" for windows –  Feb 13 '20 at 20:03
  • Yes `cls` is the correct command for clearing the screen in Windows. My (rather weak) hypothesis is that, internally, Windows deals with subprocess calls differently than what I tested on Linux. – jfaccioni Feb 13 '20 at 20:05
  • I think this is a duplicate of https://stackoverflow.com/questions/517970/how-to-clear-the-interpreter-console. – AMC Feb 13 '20 at 20:13
  • @AMC I guess you could argue it is, but the answers there arent working for me for some strange reason –  Feb 13 '20 at 20:15
  • @ AMC sorry, i confused clearing interpreter with clearing the console. I was trying to clear the python interpreter –  Feb 13 '20 at 20:29

2 Answers2

1

so based on your previous comments you want to clear the screen in a Python interpreter. There is no "command" to clear the screen just like this. But you can use a loop to print some new lines until your screen is blank again.

def clear(lines):
    for l in range(lines):
        print()

while True:
    game.draw_map()
    clear(35)
    time.sleep(0.05)

You may need to increase or decrease the amount of lines cleared. I hope this works for you. P.S.: I would use a normal console.

https://en.wikipedia.org/wiki/Interpreter_(computing)#Efficiency:

Efficiency: The main disadvantage of interpreters is that an interpreted program typically runs slower than if it had been compiled. The difference in speeds could be tiny or great; often an order of magnitude and sometimes more. It generally takes longer to run a program under an interpreter than to run the compiled code but it can take less time to interpret it than the total time required to compile and run it. This is especially important when prototyping and testing code when an edit-interpret-debug cycle can often be much shorter than an edit-compile-run-debug cycle.

sp4c38
  • 455
  • 3
  • 14
0

If your problem is clearing the console window, you can just use

import os

os.system('cls')

in place of

sp.call("cls", shell = True)

(Assuming you are working on windows)

EDIT for clarity:

This is the new loop after the change:

while True:
game.draw_map()
os.system('cls')
Elijah B.
  • 33
  • 1
  • 8
  • I am working on windows, but i get this error if i remove shell = True: FileNotFoundError: [WinError 2] The system cannot find the file specified –  Feb 13 '20 at 20:05
  • Yes, my solution was not to remove shell=True from sp.call("cls", shell = True), but to replace the entire line with os.system('cls'). – Elijah B. Feb 13 '20 at 20:09
  • Ahh, my bad. But still dosent solve the problem. It just kinda pauses the printing for a few miliseconds then continues –  Feb 13 '20 at 20:12
  • are you running the script in an interpreter or in the console? This wont clear text from an interpreter – Elijah B. Feb 13 '20 at 20:17
  • Ohh, i was using an interpreter. Is there a way to clear an interpreter? because it was kinda difficult for me to scale the map on the console –  Feb 13 '20 at 20:18
  • @hippozhipos Aren’t you just using the interpreter for development/debugging? – AMC Feb 13 '20 at 20:31
  • @ No, it was a challenge set by my teacher and the entire game has to be interpreter based –  Feb 13 '20 at 20:34