Całość rysuj na jakiejś wewnętrznej mapie będącą buforem konsoli, wtedy nie będzie problemu z żadnym przesuwaniem kursora.
Rysuj jak na płótnie.
Kopiuj
class Map2D:
def __init__(self, width, height, val):
self._internalMap = [[val]*height for width in range(height)]
def fromArray2D(self, array2D):
self.internalMap = array2D
def at(self, x, y):
return self._internalMap[x][y]
def putAt(self, x, y, val):
self._internalMap[x][y] = val
return self
def putMap2DAt(self, x, y, another):
for ix in range(x, x+another.width()):
for iy in range(y, y+another.height()):
self.putAt(ix, iy, another.at(ix-x, iy-y))
return self
def width(self):
return len(self._internalMap[0])
def height(self):
return len(self._internalMap)
def printPureMap2D(map2d):
for y in range(map2d.height()):
row = []
for x in range(map2d.width()):
row.extend(map2d.at(x, y))
print ''.join(row)
canvas = Map2D(20, 20, ' ')
stars = Map2D(5, 5, '*')
canvas.putMap2DAt(0, 0, stars).putAt(5+3, 0, 'x').putMap2DAt(10, 0, stars)
canvas.putMap2DAt(5, 5, stars)
canvas.putMap2DAt(0, 10, stars).putMap2DAt(10, 10, stars)
printPureMap2D(canvas)
http://ideone.com/uOLCii
Wynik:
Kopiuj
***** x *****
***** *****
***** *****
***** *****
***** *****
*****
*****
*****
*****
*****
***** *****
***** *****
***** *****
***** *****
***** *****