0

I'm writing a simple script for a slot machine game just after a tutorial on codeacademy.com I would like to print things in the same place of the first print not everything under the previous. Is this possible?

In my script i print out a board like this:

slot = []

for row in range(9):
    slot.append(['[]'] * 9)

def print_slot(slot):
    for element in slot:
        print(" ".join(element))
print_slot(slot)

after this board i don't want another board for every action but i would like actions to happen in the board or, even better something like the output you get when you rerun the code.

how can achieve this? something different from print? or maybe is the python console thet work like this, a complete program can be different. (real beginner here)

this is my output before the user input:

##- simple slotmachine -##
##- five identical win -##
##- u start with 100cr -##
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
##- simple slotmachine -##
##- five identical win -##
##- space enter 2 play -##

here after the user input:

##- simple slotmachine -##
##- five identical win -##
##- u start with 100cr -##
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
##- simple slotmachine -##
##- five identical win -##
##- space enter 2 play -## 
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] 0 4 5 4 5 [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []

as you can see the previous board is still visible, i would like to substitute a new board at every change.

Mat
  • 35
  • 5

1 Answers1

0

This is an example how you can emulate a rewrite of an old line in stdout (actually you're going back to the start of the line using \r and writing over the old input):

#!/usr/bin/env python

import sys
import time

sys.stdout.write("This is in a line")
# force flush as stdout is flushed by default on '\n'
sys.stdout.flush()
time.sleep(2)
# rewrite line and move to next
sys.stdout.write("\rThis is still in the same line\n")

You can use these VT100 codes to overwrite previous lines as suggest in this answer:

CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'

print "these"
print "are"
print "four"
print "lines"
time.sleep(2)
# go back two lines and overwrite with two new lines
sys.stdout.write(CURSOR_UP_ONE)
sys.stdout.write(CURSOR_UP_ONE)
print "five"
print "lines"
print "now"
Community
  • 1
  • 1
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • i don't know if this will work too with a board made with a list and a loop, but hey thanks for the quick reply! – Mat Dec 24 '14 at 13:37
  • Why not? If the board size is fixed you can make an `erase_board` function and call it before you re-draw the board. And always re-draw the board (instead of tracking small changes). You should also take a look at string formatting in python to better sync board cells' width. Good luck! – Reut Sharabani Dec 24 '14 at 13:38