how to read an array from txt.file? Python, curses -



how to read an array from txt.file? Python, curses -

i using curses in python, , trying create game print out characters in terminal window. field supposed saved in text file if press 's' , if press 'o' saved playing field supposed uploaded. manage store characters in array, don't know how upload file back/print on terminal window.

so problem don't know how 'reload' characters have stored. saved txt.file looks this:

[' ', ' ', 'f', 'i', ' '....]

[' ', 'f', ' ', ' ', ' '....]

[...

[...

etc

my program.....

import curses def main(scr): """ draw border around screen, move around using cursor , leave mark of latest pressed character on keyboard. quit using 'q'. """ # clear screen of output scr.clear() # screen dimensions y1, x1 = scr.getmaxyx() y1 -= 1 x1 -= 1 y0, x0 = 0, 0 #min # center position yc, xc = (y1-y0)//2, (x1-x0)//2 # draw border scr.border() # move cursor center scr.move(yc, xc) # refresh draw out scr.refresh() #make re-create of playing field array = [[' ' _ in range(x1-1)] _ in range(y1-1)] # main loop x = xc y = yc ch = 'j' while true: key = scr.getkey() if key == 'q': break elif key == 'key_up' , (y-1) > y0: y -= 1 elif key == 'key_down' , (y+1) < y1: y += 1 elif key == 'key_left' , (x-1) > x0: x -= 1 elif key == 'key_right' , (x+1) < x1: x += 1 elif key == 's': text_file = open("border.txt", "w") char in array: text_file.write("%s\n" % char) text_file.close() elif key == 'o': scr.clear() saved_file = open("border.txt", "r") scr.addstr... # part don't work. scr.refresh() else: if key != 'key_up' , key != 'key_down' , key != 'key_left' , key != 'key_right': ch = key #place key in array posy = y posx = x if y >= y0 , x >= x0 , y <= (y1) , x <= (x1): array[int(posy-1)][int(posx-1)] = ch # draw out char @ cursor positino scr.addstr(ch) # move cursor new position if y < y1 , y > y0 , x > x0 , x < x1: scr.move(y, x) # redraw items on screen scr.refresh()

you can seek store info python pickle module. intended serialization of info structures this. later on can reload construction pickle again

python arrays curses

Comments