Image: graphical display vs intended outcome
I am getting the expected outcome in the console, however when I try to convert it to a graphical form, the tiles are not being displayed in the correct location.
I tried adjusting the tile offset and clearing the window before and after drawing to it with almost no change. I have also tried rewriting the graphics code multiple times but always ended up with the same result.
My code:
import numpy as np
import time
from graphics import *
def clear(win):
for item in win.items[:]:
item.undraw()
win.update()
quick_start = int(input("debug:quickstart:"))
if quick_start == 1:
hw = 600
xysize = 5
iLoops = 1000
OnColor = "grey"
OffColor = "white"
startBoard = np.array([[0,0,0,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,0,0,0]])
else:
hw = int(input("Window Size:"))
xysize = int(input("Board Size:"))
iLoops = int(input("Number of loops:"))
OnColor = str(input("On Color:"))
OffColor = str(input("Off Color:"))
startBoard = np.random.randint(0, 2, size=(xysize, xysize))
nextStep = np.full(startBoard.shape, 0)
#print(startBoard)
wHeight = hw
wWidth = hw
window = GraphWin(width = wWidth, height = wHeight, autoflush=False)
squareOrigin = Point(0, 0)
increaseAmountX = 0
newSquareHeight = 0
time.sleep(4)
print(startBoard)
for i in range(iLoops):
update()
#time.sleep(1)
#clear(window)
squareOrigin.y = 0
for r in range(xysize):
for c in range(xysize):
iNei = 0
#cardinal directions
#check left & right
try:
if startBoard[r+1][c] == 1:
iNei += 1
except:
iNei = iNei
try:
if startBoard[r-1][c] == 1:
iNei += 1
except:
iNei = iNei
#check up & down
try:
if startBoard[r][c+1] == 1:
iNei += 1
except:
iNei = iNei
try:
if startBoard[r][c-1] == 1:
iNei += 1
except:
iNei = iNei
#diagonals
try:
if startBoard[r+1][c+1] == 1:
iNei += 1
except:
iNei = iNei
try:
if startBoard[r-1][c-1] == 1:
iNei += 1
except:
iNei = iNei
try:
if startBoard[r+1][c-1] == 1:
iNei += 1
except:
iNei = iNei
try:
if startBoard[r-1][c+1] == 1:
iNei += 1
except:
iNei = iNei
if startBoard[r][c] == 1:
if iNei < 2:
nextStep[r][c] = 0
elif iNei > 3:
nextStep[r][c] = 0
elif iNei == 2 or iNei == 3:
nextStep[r][c] = startBoard[r][c]
else:
if iNei == 3:
nextStep[r][c] = 1
elif iNei == 2:
nextStep[r][c] = startBoard[r][c]
squareOrigin.x += increaseAmountX
newSquare = Rectangle(squareOrigin, Point(squareOrigin.x + (wWidth)/xysize, squareOrigin.y + wHeight/xysize))
if startBoard[r][c] == 1:
newSquare.setFill(OnColor)
else:
newSquare.setFill(OffColor)
if squareOrigin.x < wWidth:
increaseAmountX = wWidth/xysize
elif squareOrigin.x >=wWidth and squareOrigin.y <= -wHeight:
squareOrigin.x = 0
squareOrigin.y = 0
elif squareOrigin.x >= wWidth:
increaseAmountX = 0
squareOrigin.x = 0
squareOrigin.y += wHeight/xysize
addedObj = newSquare
addedObj.draw(window)
print("\n", "-" * 25, "\n")
print(nextStep)
startBoard = nextStep
nextStep = np.full(startBoard.shape, 0)
time.sleep(5)
I find the complication here is createing new graphic objects every time through the main loop. Let's redesign the program to create graphic objects once before the loop, and then simply manipulate their colors during the loop, updating the screen at the end of the loop:
CONSOLE