简体   繁体   中英

chessboard and list for program

I have to create a python program for my exam, this program is Langton's Ant and for this I need to create a chessboard with coordinate like this:

[x1y1, x2y1, ..., XnY1
 X1Y2, X2Y2, ..., XnY2
 ...
 X1Yn, X2Yn, ..., XnYn]

I would do like to draw all rectangle in can1 but I can't succeed!!

This is my program so far:

from Tkinter import *

# Création du widget principal:

fen1 = Tk()
fen1.title('Fourmi de Langton')



# création des widgets:

can1 = Canvas(fen1,bg='white',height=500,width=600)
can1.pack(side=TOP,padx=10,pady=10)    
can1.create_rectangle(1,1,10,10, fill="black")

bou1 = Button(fen1,text='Quitter',width=25,command=fen1.quit)
bou1.pack(side=RIGHT,padx=10,pady=10)

bou2 = Button(fen1,text='Start/Stop',width=25,)
bou2.pack(side=LEFT,padx=10,pady=10)

bou3 = Button(fen1,text='Step',width=25)
bou3.pack(padx=10,pady=10)

fen1.mainloop()              # récupération des instructions
fen1.destroy()  

Generally, a loop(s) is used to handle repetitive coding. Some things that are necessary before creating the board, as this may or may not be the way to start coding, depending on: 1) How will you identify which square to move from and to, and 2) how will you identify which piece is moved and if the new square is occupied. After all, chess is about moving pieces, not the board used.

from Tkinter import *

fen1 = Tk()
fen1.title('Fourmi de Langton')

can1 = Canvas(fen1,bg='white',height=500,width=600)
can1.pack(side=TOP,padx=10,pady=10)    

board_rows=5
board_cols=5
color_black=True
x=1
y=1
square_size=10
for rows in range(board_rows):
    for columns in range(board_cols):
        color="lightgray"
        if not color_black:
            color="red"
        x=columns*square_size
        y=rows*square_size
        can1.create_rectangle(x, y,
                   x+square_size, y+square_size, fill=color)
        color_black= not color_black

bou1 = Button(fen1,text='Quitter',width=25,command=fen1.quit)
bou1.pack(side=RIGHT,padx=10,pady=10)

fen1.mainloop()          

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM