0

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)

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Please provide a [mre], not your whole program. Try to make it not require user input if possile. Otherwise explain what that should be to reproduce the problem. – martineau Jul 06 '21 at 00:52

1 Answers1

0

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:

import time
import numpy as np
from graphics import *

quick_start = int(input("debug:quickstart:"))

if quick_start:
    wHeightWidth = 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:
    wHeightWidth = 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)

wHeight = wWidth = wHeightWidth
window = GraphWin(width=wWidth, height=wHeight, autoflush=False)

squareOrigin = Point(0, 0)

increaseAmountX = wWidth/xysize
increaseAmountY = wHeight/xysize

print(startBoard)

screenBoard = []

for r in range(xysize):
    screenBoard.append(list())

    for c in range(xysize):
        newSquare = Rectangle(squareOrigin, Point(squareOrigin.x + increaseAmountX, squareOrigin.y + increaseAmountY))

        if startBoard[r][c] == 1:
            newSquare.setFill(OnColor)
        else:
            newSquare.setFill(OffColor)

        screenBoard[r].append(newSquare)

        newSquare.draw(window)

        squareOrigin.x += increaseAmountX

    squareOrigin.x = 0
    squareOrigin.y += increaseAmountY

window.update()

for i in range(iLoops):

    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 IndexError:
                pass
            try:
                if startBoard[r-1][c] == 1:
                    iNei += 1
            except IndexError:
                pass

            # check up & down
            try:
                if startBoard[r][c+1] == 1:
                    iNei += 1
            except IndexError:
                pass
            try:
                if startBoard[r][c-1] == 1:
                    iNei += 1
            except IndexError:
                pass

            # diagonals
            try:
                if startBoard[r+1][c+1] == 1:
                    iNei += 1
            except IndexError:
                pass
            try:
                if startBoard[r-1][c-1] == 1:
                    iNei += 1
            except IndexError:
                pass
            try:
                if startBoard[r+1][c-1] == 1:
                    iNei += 1
            except IndexError:
                pass
            try:
                if startBoard[r-1][c+1] == 1:
                    iNei += 1
            except IndexError:
                pass

            if startBoard[r][c] == 1:
                if iNei < 2:
                    nextStep[r][c] = 0
                elif iNei > 3:
                    nextStep[r][c] = 0
                else:
                    nextStep[r][c] = startBoard[r][c]
            else:
                if iNei == 3:
                    nextStep[r][c] = 1
                elif iNei == 2:
                    nextStep[r][c] = startBoard[r][c]

            if startBoard[r][c] == 1:
                screenBoard[r][c].setFill(OnColor)
            else:
                screenBoard[r][c].setFill(OffColor)

    window.update()

    print("\n", "-" * 25, "\n")
    print(nextStep)
    startBoard = nextStep
    nextStep = np.full(startBoard.shape, 0)
    time.sleep(5)

enter image description here

CONSOLE

> python3 test.py
debug:quickstart:1
[[1 0 0 0 0]
 [0 0 1 1 0]
 [1 0 0 0 0]
 [1 0 0 0 1]
 [0 1 1 0 1]]

 ------------------------- 
cdlane
  • 40,441
  • 5
  • 32
  • 81