Search code examples
pythonpython-3.xwindowscrash

os.system('cls') randomly "jamming" cmd console screen


Here is an image of what happens when you run it though python shell:

screenshot of what happens when you run it though python shell

So basically after 50 to 100+ turns my game will crash. It looks like the os.system() is not shutting down properly as it clears the screen. Using the python shell I can easily manually shut down the cmd page and continue with the game with no issues but when you run the game as an exe file or though console then that is not an option and hence crash the system.

I have tried putting a delay before the os.system() clear instructions and used try/except but it has not been able to fix the issue.

def clear_screen():
    playerMap[y][x] = "@"
    time.sleep(.1)
    os.system('cls' if os.name == 'nt' else 'clear')
    displayMap(playerMap)

For example if you click W for movement and end up on a position of "." then clear_screen will activate.

    if movement == "W":
        y = y-1
        position = mapChoice[y][x]
        playerMap[y][x] = "@"

    if position == ".":
        clear_screen()
        intro1=random.choice(intro)
        print (intro1)
        print("which direction will you go ?")

every time a movement has been made it will activate the clear screen function but every 100 so move the os.system() function will "jam" for no reason.


Solution

  • Solve the issue with replacing os.system('cls') with print("\033c", end=""). Got over 500 turns before getting bored. I don't think os.system("cls") was designed for use for such a repetitive usage on every turn, every attack, every action it is getting activated.