I am trying to animate the below diamond pattern in such a way that, It will first print the centre "star" and then its 4 neighbours and so on.
*
*
* * *
*
*
* * *
* * * * *
* * *
*
I tried clearing the console but it did not work. Is there any other way to do it?
here is the code:
import os
import sys
import numpy as np
def get_string(pattern, num):
string = ""
for i in range(num):
for j in range(num):
if pattern[i][j] == 0:
string += " "
else:
string += " * "
string += "\n"
return string
num = 5
pattern = np.zeros((num, num))
loc = (num // 2)
pattern[loc, loc] = 1
string = get_string(pattern, num)
os.system("clear")
sys.stdout.write("\r" + string)
sys.stdout.flush()
for k in range(1, loc+1):
for i in range(num):
for j in range(num):
eclu = abs(i - loc) + abs(j - loc)
if eclu == k:
pattern[i, j] = 1
string = get_string(pattern, num)
os.system("clear")
sys.stdout.write("\r" + string)
sys.stdout.flush()