欢迎光临散文网 会员登陆 & 注册

python模块调用练习(I)

2020-03-25 16:47 作者:一心想当网红的李老师  | 我要投稿

还是两个不同文件种模块调用的问题。

1、算法模块。model.py

height=100

width=100


grid_model=[0]*height

next_grid_model=[0]*height

for i in range(height):

    grid_model[i]=[0]*width

    next_grid_model[i]=[0]*width

    

def next_gen():

    global grid_model,next_grid_model

    

    for i in range(0,height):

        for j in range (0,width):

            cell=0

            print('check cell',i,j)

            count=count_neighbours(grid_model,i,j)

            

            if grid_model[i][j]==0:

                if count==3:

                    cell=1

            elif grid_model[i][j]==1:

                if count==2 or count==3:

                    cell=1

            next_grid_model[i][j]=cell


temp=grid_model

grid_model=next_grid_model

next_grid_model=temp

                

def count_neighbours(grid,row,col):

    count=0

    if (row-1>=0):

        count=count+grid[row-1][col]

    if (row-1>=0) and (col-1>=0):

        count=count+grid[row-1][col-1]

    if (row-1>=0) and (col+1<width):

        count=count+grid[row-1][col+1]

    if (col-1>=0):

        count=count+grid[row][col-1]

    if  (col+1<width):

        count=count+grid[row][col+1]

    if (row+1<height):

        count=count+grid[row+1][col]

    if (row+1<height) and (col-1>=0):

        count=count+grid[row+1][col-1]

    if (row+1<height) and (col+1<width):

        count=count+grid[row+1][col+1]

    return count

 

if __name__=='__main__':

    next_gen()


2、视窗文件。view.py

from tkinter import *

import model


cell_size=5



def setup():

    global root,grid_view,cell_size,start_btn,clear_btn,choice

    

    root=Tk()

    root.title('game screen')

    grid_view=Canvas(root,width=model.width*cell_size,  

                     height=model.height*cell_size,

                     borderwidth=0,

                     bg='white')

####这个部分开始引用model里面的信息

    start_btn=Button(root,text="start",width=12)

    clear_btn=Button(root,text="clear",width=12)

    choice=StringVar(root)

    choice.set('choose a pattern')

    option=OptionMenu(root,choice,"'choose a pattern","grider","grider gun","random")

    option.config(width=25)

    ####学习了怎么设置选择项

    grid_view.pack()

    start_btn.pack()

    clear_btn.pack()

    option.pack()


if __name__=='__main__':

    setup()

    mainloop()

虽然没有显示怎么实现功能,但是调用成功。

####第一次根据书本尝试。

python模块调用练习(I)的评论 (共 条)

分享到微博请遵守国家法律