Py学习  »  Python

python元胞自动机图形库中的图形表现异常

PhantomDevelopment • 3 年前 • 1347 次点击  

Image: graphical display vs intended outcome

我在控制台中得到了预期的结果,但是当我试图将其转换为图形形式时,这些分幅没有显示在正确的位置。

我试着调整瓷砖偏移,并在绘制之前和之后清理窗口,几乎没有任何变化。 我也尝试过多次重写图形代码,但结果总是一样的。

我的代码:

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)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128771
 
1347 次点击  
文章 [ 1 ]  |  最新文章 3 年前
cdlane
Reply   •   1 楼
cdlane    4 年前

我发现这里的复杂性在于每次通过主循环创建新的图形对象。让我们重新设计程序,在循环之前创建一次图形对象,然后在循环过程中简单地操纵它们的颜色,在循环结束时更新屏幕:

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

安慰

> 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]]

 -------------------------