如何解决我们必须使用出现图案来更改网格点的颜色
这是我的项目代码。我必须编写updateemergence函数。我们需要使用假设将点的颜色更改为蓝色。我的观点是,当一个红点被超过40%的蓝点包围时,它将变为蓝色。但是我怎么写呢?
导入tkinter 来自随机进口randint
班级涌现:
def __init__(self,gridsize = 10,gridtype = 0):
""" initialize the emergence grid
"""
if gridsize > 10:
print("grid size is reduced to the max size 10")
self.gridsize = 10
else:
self.gridsize = gridsize # number of grid points in each dimension
self.dotsize = 10 # the diameter for each dot
self.xspace = 40 # the space between two adjacent dots
self.yspace = self.xspace
self.numberofcolors = 2 # Could be more than 2
self.TOTALGRIdratIO = 0.8
self.gridcolors = [[0 for _ in range(self.gridsize)] for _ in range(self.gridsize)] # to save the colors for each point
self.gridwidth = 10
self.gridheight = 10
self.canvaswidth = 0
# at this point,we only have one example 10x10 grid
# if user requires an example grid with size smaller than 10,# we will use the top left subsquare of the 10x10 example
if gridtype == 0 : # use the example grid
examplegridcolors = [[0,1,1],#row 1
[1,#row 2
[0,#row 3
[1,#row 4
[1,#row 5
[0,#row 6
[0,#row 7
[0,[0,0],1]]
self.gridcolors = examplegridcolors
else:
# initialize the grid using random numbers
for i in range(self.gridsize):
for j in range(self.gridsize):
self.gridcolors[i][j] = randint(0,self.numberofcolors - 1)
# initialize the GUI window here.
# create the main window
self.mainwindow = tkinter.Tk()
# initialize the canvas to None.
self.canvas = None
# use this function for debugging purposes
def printGrid(self):
""" prints the gridcolors in text
"""
for i in range(self.gridsize):
for j in range(self.gridsize):
print(self.gridcolors[i][j] + "\t",end="")
print()
# this function make the GUI window
def showindow(self):
self.mainwindow.title("Emergence Grid")
self.mainwindow.geometry("500x500") # default size of the window
self.mainwindow.resizable(0,0)
self.mainwindow.bind('<Button-1>',self.onMouseClick) # bind the onMouseClick to the left mouse click <Button-1>
# make the application ready to run. mainloop() is an infinite loop used to run the application,wait for an event to occur and process the event till the window is not closed.
self.mainwindow.mainloop()
self.drawGrid()
def computeGridLayout(self):
""" The computeGridLayout method computes the coordinates of the dots
"""
# compute width and height of the grid
self.gridwidth = self.gridsize * (self.dotsize + self.xspace)
self.canvaswidth = self.gridwidth / self.TOTALGRIdratIO
# create canvas
if self.canvas == None :
self.canvas = tkinter.Canvas(self.mainwindow,height = self.canvaswidth,width = self.canvaswidth)
else:
self.canvas.delete("all") # clear the canvas
self.canvas.pack()
def drawGrid(self):
""" draw the grid dots on canvas
"""
# put your code here.
r = self.dotsize //2
topLeftCorner_x = (self.canvaswidth - self.gridwidth) //2 # make the grid to be on center of canvas
topLeftCorner_y = topLeftCorner_x
for i in range(self.gridsize):
for j in range(self.gridsize):
# compute the coordinates of grid
x = topLeftCorner_x + j * self.xspace
y = topLeftCorner_y + i * self.yspace
if self.gridcolors[i][j] == 0 :
color = "blue"
else:
color = "red"
self.canvas.create_oval(x-r,y-r,x+r,y+r,fill=color) # draw a circle
# self.canvas.create_oval(50,50,80,fill="green") # draw a circle
def updateEmergenceGrid(self):
""" The updateEmergenceGrid function updates grid based on emergence rules
"""
# put your code here
same_color = 0
for i in range(self.gridsize):
for j in range(self.gridsize):
# while self.gridcolors[i][j] == 1:
if self.gridcolors[i][j] == 1:
count = chck_neighbor[i][j]
for r in range(count):
for c in range(count):
if
pass # placeholder for body of the method
def onMouseClick(self,event):
""" The onMouseClick event handler will call everytime user clicks on main form.
"""
self.computeGridLayout()
self.drawGrid()
self.updateEmergenceGrid()
def in_bounds(self,row,col):
""" Checks if the row and column are in bounds of the grid
returns True when in bounds and False when not
"""
if 0 < row < self.gridsize and 0 < col < self.gridsize:
return True
else:
return False
def chck_neighbor(self,col):
""" Checks the neighbors of a dot with a particular row and col
"""
for dr in [-1,1]:
for dc in [-1,1]:
num_ngbr = 0
neighbor_row = row + dr
neighbor_col = col + dc
if self.in_bounds(neighbor_row,neighbor_col):
# neighbor = self.gridsize[neighbor_row][neighbor_col]
num_ngbr += 1
#return neighbor
return num_ngbr
如果名称 ==“ 主要”:
e = Emergence(10,0)
e.showindow()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。