Question:Programming a game grid ...

Rick Johnson rantingrickjohnson at gmail.com
Thu Jun 28 00:04:01 EDT 2012


On Jun 27, 5:21 pm, iconoclast011 <iconoclast... at gmail.com> wrote:
> Fairly new to Python ... Is there a way to efficiently (different from my brute
> force code shown below) to set up a game grid of buttons (ie with pygame)
> responding to mouse clicks ?   I would want to vary the size of the grid ...
>
> Thanks
>
> Brute force code:
> from Tkinter import *
>
> root = Tk()
>
> f = Frame(root, bg = "blue", width = 500, height = 500)
> f.pack(side=LEFT, expand = 1)
>
> f3 = Frame(f, bg = "white", width = 500)
> f3.pack(side=LEFT, expand = 1, pady = 50, padx = 50)
>
> #f2 = Frame(root, bg = "black", height=100, width = 100)
> #f2.pack(side=LEFT, fill = Y)
>
> #b = Button(f2, text = "test")
> #b.pack()
>
> var = 'b00'
> vars()[var] = Button(f3, text = "00", bg = "white")
> b00.grid(row=0, column=0)
> b00.bind('<Button-1>', leftclick)   # bind left mouse click
> b00.bind('<Button-3>', rightclick)   # bind left mouse click
>
> var = 'b01'
> vars()[var] = Button(f3, text = "01", bg = "white")
> b01.grid(row=0, column=1)
> b01.bind('<Button-1>', leftclick)   # bind left mouse click
> b01.bind('<Button-3>', rightclick)   # bind left mouse click
>
> b02 = Button(f3, text = "02", bg = "white")
> b02.grid(row=0, column=2)
> b02.bind('<Button-1>', leftclick)   # bind left mouse click
> b02.bind('<Button-3>', rightclick)   # bind left mouse click
>
> b03 = Button(f3, text = "03", bg = "white")
> b03.grid(row=0, column=3)
> b03.bind('<Button-1>', leftclick)   # bind left mouse click
> b03.bind('<Button-3>', rightclick)   # bind left mouse click
>
> b04 = Button(f3, text = "04", bg = "white")
> b04.grid(row=0, column=4)
> b04.bind('<Button-1>', leftclick)   # bind left mouse click
> b04.bind('<Button-3>', rightclick)   # bind left mouse click
>
> b05 = Button(f3, text = "05", bg = "white")
> b05.grid(row=0, column=5)
> b05.bind('<Button-1>', leftclick)   # bind left mouse click
> b05.bind('<Button-3>', rightclick)   # bind left mouse click
>
> b06 = Button(f3, text = "06", bg = "white")
> b06.grid(row=0, column=6)
> b07 = Button(f3, text = "07", bg = "white")
> b07.grid(row=0, column=7)
> b08 = Button(f3, text = "08", bg = "white")
> b08.grid(row=0, column=8)
>
> b10 = Button(f3, text = "10", bg = "white")
> b10.grid(row=1, column=0)
> b11 = Button(f3, text = "11", bg = "white")
> b11.grid(row=1, column=1)
> b12 = Button(f3, text = "12", bg = "white")
> b12.grid(row=1, column=2)
>
> b13 = Button(f3, text = "13", bg = "white")
> b13.grid(row=1, column=3)
> b14 = Button(f3, text = "14", bg = "white")
> b14.grid(row=1, column=4)
> b15 = Button(f3, text = "15", bg = "white")
> b15.grid(row=1, column=5)
>
> b16 = Button(f3, text = "16", bg = "white")
> b16.grid(row=1, column=6)
> b17 = Button(f3, text = "17", bg = "white")
> b17.grid(row=1, column=7)
> b18 = Button(f3, text = "18", bg = "white")
> b18.grid(row=1, column=8)
>
> b20 = Button(f3, text = "20", bg = "white")
> b20.grid(row=2, column=0)
> b21 = Button(f3, text = "21", bg = "white")
> b21.grid(row=2, column=1)
> b22 = Button(f3, text = "22", bg = "white")
> b22.grid(row=2, column=2)
>
> b23 = Button(f3, text = "23", bg = "white")
> b23.grid(row=2, column=3)
> b24 = Button(f3, text = "24", bg = "white")
> b24.grid(row=2, column=4)
> b25 = Button(f3, text = "25", bg = "white")
> b25.grid(row=2, column=5)
>
> b26 = Button(f3, text = "26", bg = "white")
> b26.grid(row=2, column=6)
> b27 = Button(f3, text = "27", bg = "white")
> b27.grid(row=2, column=7)
> b28 = Button(f3, text = "28", bg = "white")
> b28.grid(row=2, column=8)
>
> b30 = Button(f3, text = "30", bg = "white")
> b30.grid(row=3, column=0)
> b31 = Button(f3, text = "31", bg = "white")
> b31.grid(row=3, column=1)
> b32 = Button(f3, text = "32", bg = "white")
> b32.grid(row=3, column=2)
>
> b36 = Button(f3, text = "36", bg = "white")
> b36.grid(row=3, column=6)
> b37 = Button(f3, text = "37", bg = "white")
> b37.grid(row=3, column=7)
> b38 = Button(f3, text = "38", bg = "white")
> b38.grid(row=3, column=8)
>
> b33 = Button(f3, text = "33", bg = "white")
> b33.grid(row=3, column=3)
> b34 = Button(f3, text = "34", bg = "white")
> b34.grid(row=3, column=4)
> b35 = Button(f3, text = "35", bg = "white")
> b35.grid(row=3, column=5)
>
> b40 = Button(f3, text = "40", bg = "white")
> b40.grid(row=4, column=0)
> b41 = Button(f3, text = "41", bg = "white")
> b41.grid(row=4, column=1)
> b42 = Button(f3, text = "42", bg = "white")
> b42.grid(row=4, column=2)
>
> b43 = Button(f3, text = "43", bg = "white")
> b43.grid(row=4, column=3)
> b44 = Button(f3, text = "44", bg = "white")
> b44.grid(row=4, column=4)
> b45 = Button(f3, text = "45", bg = "white")
> b45.grid(row=4, column=5)
>
> b46 = Button(f3, text = "46", bg = "white")
> b46.grid(row=4, column=6)
> b47 = Button(f3, text = "47", bg = "white")
> b47.grid(row=4, column=7)
> b48 = Button(f3, text = "48", bg = "white")
> b48.grid(row=4, column=8)
>
> b50 = Button(f3, text = "50", bg = "white")
> b50.grid(row=5, column=0)
> b51 = Button(f3, text = "51", bg = "white")
> b51.grid(row=5, column=1)
> b52 = Button(f3, text = "52", bg = "white")
> b52.grid(row=5, column=2)
>
> b53 = Button(f3, text = "53", bg = "white")
> b53.grid(row=5, column=3)
> b54 = Button(f3, text = "54", bg = "white")
> b54.grid(row=5, column=4)
> b55 = Button(f3, text = "55", bg = "white")
> b55.grid(row=5, column=5)
>
> b56 = Button(f3, text = "56", bg = "white")
> b56.grid(row=5, column=6)
> b57 = Button(f3, text = "57", bg = "white")
> b57.grid(row=5, column=7)
> b58 = Button(f3, text = "58", bg = "white")
> b58.grid(row=5, column=8)
>
> b60 = Button(f3, text = "60", bg = "white")
> b60.grid(row=6, column=0)
> b61 = Button(f3, text = "61", bg = "white")
> b61.grid(row=6, column=1)
> b62 = Button(f3, text = "62", bg = "white")
> b62.grid(row=6, column=2)
>
> b63 = Button(f3, text = "63", bg = "white")
> b63.grid(row=6, column=3)
> b64 = Button(f3, text = "64", bg = "white")
> b64.grid(row=6, column=4)
> b65 = Button(f3, text = "65", bg = "white")
> b65.grid(row=6, column=5)
>
> b66 = Button(f3, text = "66", bg = "white")
> b66.grid(row=6, column=6)
> b67 = Button(f3, text = "67", bg = "white")
> b67.grid(row=6, column=7)
> b68 = Button(f3, text = "68", bg = "white")
> b68.grid(row=6, column=8)
>
> b70 = Button(f3, text = "70", bg = "white")
> b70.grid(row=7, column=0)
> b71 = Button(f3, text = "71", bg = "white")
> b71.grid(row=7, column=1)
> b72 = Button(f3, text = "72", bg = "white")
> b72.grid(row=7, column=2)
>
> b73 = Button(f3, text = "73", bg = "white")
> b73.grid(row=7, column=3)
> b74 = Button(f3, text = "74", bg = "white")
> b74.grid(row=7, column=4)
> b75 = Button(f3, text = "75", bg = "white")
> b75.grid(row=7, column=5)
>
> b76 = Button(f3, text = "76", bg = "white")
> b76.grid(row=7, column=6)
> b77 = Button(f3, text = "77", bg = "white")
> b77.grid(row=7, column=7)
> b78 = Button(f3, text = "78", bg = "white")
> b78.grid(row=7, column=8)
>
> b80 = Button(f3, text = "80", bg = "white")
> b80.grid(row=8, column=0)
> b81 = Button(f3, text = "81", bg = "white")
> b81.grid(row=8, column=1)
> b82 = Button(f3, text = "82", bg = "white")
> b82.grid(row=8, column=2)
>
> b83 = Button(f3, text = "83", bg = "white")
> b83.grid(row=8, column=3)
> b84 = Button(f3, text = "84", bg = "white")
> b84.grid(row=8, column=4)
> b85 = Button(f3, text = "85", bg = "white")
> b85.grid(row=8, column=5)
>
> b86 = Button(f3, text = "86", bg = "white")
> b86.grid(row=8, column=6)
> b87 = Button(f3, text = "87", bg = "white")
> b87.grid(row=8, column=7)
> b88 = Button(f3, text = "88", bg = "white")
> b88.grid(row=8, column=8)
> b88.bind('<Button-1>', leftclick)   # bind left mouse click
> b88.bind('<Button-3>', rightclick)   # bind left mouse click
>
> #b86.configure(text = "X")
> b86.configure(bg = "black")
> b86.configure(fg = "white")
>
> root.title('Puzzle Grid')
> root.mainloop()

And on the sixth day the code god created loops and iterators; and his
fingers thanked him; and he suddenly found he had developed an excess
of free time... and so he rested.



More information about the Python-list mailing list