Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24552 > unrolled thread

Question:Programming a game grid ...

Started byiconoclast011 <iconoclast011@gmail.com>
First post2012-06-27 17:21 -0500
Last post2012-06-27 21:04 -0700
Articles 14 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  Question:Programming a game grid ... iconoclast011 <iconoclast011@gmail.com> - 2012-06-27 17:21 -0500
    Re: Question:Programming a game grid ... David <dwblas@gmail.com> - 2012-06-27 16:24 -0700
      Re: Question:Programming a game grid ... Chris Angelico <rosuav@gmail.com> - 2012-06-28 09:35 +1000
      Re: Question:Programming a game grid ... Chris Angelico <rosuav@gmail.com> - 2012-06-28 09:37 +1000
      Re: Question:Programming a game grid ... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-06-28 00:15 +0000
        Re: Question:Programming a game grid ... woooee@gmail.com - 2012-06-27 19:15 -0700
          Re: Question:Programming a game grid ... alex23 <wuwei23@gmail.com> - 2012-06-27 19:43 -0700
            Re: Question:Programming a game grid ... iconoclast011 <iconoclast011@gmail.com> - 2012-06-27 21:59 -0500
              Re: Question:Programming a game grid ... Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-06-28 02:08 -0400
            Re: Question:Programming a game grid ... Temia Eszteri <lamialily@cleverpun.com> - 2012-06-27 20:03 -0700
          Re: Question:Programming a game grid ... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-06-28 04:39 +0000
    Re: Question:Programming a game grid ... MRAB <python@mrabarnett.plus.com> - 2012-06-28 00:43 +0100
    Re: Question:Programming a game grid ... alex23 <wuwei23@gmail.com> - 2012-06-27 17:31 -0700
    Re: Question:Programming a game grid ... Rick Johnson <rantingrickjohnson@gmail.com> - 2012-06-27 21:04 -0700

#24552 — Question:Programming a game grid ...

Fromiconoclast011 <iconoclast011@gmail.com>
Date2012-06-27 17:21 -0500
SubjectQuestion:Programming a game grid ...
Message-ID<Np-dnS8nJLnEGnbSnZ2dnUVZ_tCdnZ2d@giganews.com>
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()


-- 
--------------------------------- --- -- -
Posted with NewsLeecher v5.0 Beta 15
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

[toc] | [next] | [standalone]


#24561

FromDavid <dwblas@gmail.com>
Date2012-06-27 16:24 -0700
Message-ID<ef9b442a-f4f4-4f86-b3a6-5546254ca327@googlegroups.com>
In reply to#24552
First, you should be getting an error on
vars()[var] = Button(f3, text = "00", bg = "white")
as vars() has not been declared and it does not appear to be valid Python syntax.  I don't see a reason to store a reference to the button since you won't be modifying them.  Also, you can not mix pack() and grid().  It produces unpredictable results.

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

def leftclick(*args):
    print "leftclick called"

def rightclick(*args):
    print "rightclick called"

root = tk.Tk() 

f3 = tk.Frame(root, bg = "white", width = 500) 
f3.grid()

this_row=0
this_column=0
for ctr in range(0, 89):
    b = tk.Button(f3, text = "%0d" % (ctr), bg = "white") 
    b.grid(row=this_row, column=this_column) 
    b.bind('<Button-1>', leftclick)   # bind left mouse click 
    b.bind('<Button-3>', rightclick)   # bind left mouse click
    this_column += 1
    if this_column > 6:
        this_column=0
        this_row += 1
root.title('Puzzle Grid') 
root.mainloop()

[toc] | [prev] | [next] | [standalone]


#24562

FromChris Angelico <rosuav@gmail.com>
Date2012-06-28 09:35 +1000
Message-ID<mailman.1573.1340840162.4697.python-list@python.org>
In reply to#24561
On Thu, Jun 28, 2012 at 9:24 AM, David <dwblas@gmail.com> wrote:
> First, you should be getting an error on
> vars()[var] = Button(f3, text = "00", bg = "white")
> as vars() has not been declared and it does not appear to be valid Python syntax.

It's valid syntax, but highly inadvisable. What it does is call the
vars() function, then dereference its argument for assignment -
perfectly legal when the function returns a dictionary, which vars
does. But check the docs:

http://docs.python.org/library/functions.html#vars

It's intended to be read, NOT written. It's entirely possible that
this works at module level, but should not be relied on. Also, other
Python implementations or even other versions of the same Python could
behave differently.

ChrisA

[toc] | [prev] | [next] | [standalone]


#24563

FromChris Angelico <rosuav@gmail.com>
Date2012-06-28 09:37 +1000
Message-ID<mailman.1574.1340840233.4697.python-list@python.org>
In reply to#24561
On Thu, Jun 28, 2012 at 9:35 AM, Chris Angelico <rosuav@gmail.com> wrote:
> On Thu, Jun 28, 2012 at 9:24 AM, David <dwblas@gmail.com> wrote:
>> First, you should be getting an error on
>> vars()[var] = Button(f3, text = "00", bg = "white")
>> as vars() has not been declared and it does not appear to be valid Python syntax.
>
> It's valid syntax, but highly inadvisable.

Clarification: There's nothing inadvisable about the syntax, just
about writing to vars(). Dereferencing a function's return value is
perfectly alright.

ChrisA

[toc] | [prev] | [next] | [standalone]


#24567

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-06-28 00:15 +0000
Message-ID<4feba20d$0$29866$c3e8da3$5496439d@news.astraweb.com>
In reply to#24561
On Wed, 27 Jun 2012 16:24:30 -0700, David wrote:

> First, you should be getting an error on 
> vars()[var] = Button(f3, text = "00", bg = "white")
> as vars() has not been declared

The Fine Manual says differently:

Python 2:
http://docs.python.org/library/functions.html#vars

Python 3:
http://docs.python.org/py3k/library/functions.html#vars


> and it does not appear to be valid Python syntax.

It's perfectly fine syntax, no different from:

my_dict['spam'] = 'a yummy ham-like substance'

or similar.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#24573

Fromwoooee@gmail.com
Date2012-06-27 19:15 -0700
Message-ID<5f203d38-5595-46af-85aa-ba8aeeb46c25@googlegroups.com>
In reply to#24567
On Wednesday, June 27, 2012 5:15:09 PM UTC-7, Steven D&#39;Aprano wrote:
> On Wed, 27 Jun 2012 16:24:30 -0700, David wrote:
> 
> > First, you should be getting an error on 
> > vars()[var] = Button(f3, text = "00", bg = "white")
> > as vars() has not been declared
> 
> The Fine Manual says differently:
> 
> Python 2:
> http://docs.python.org/library/functions.html#vars
> 
> Python 3:
> http://docs.python.org/py3k/library/functions.html#vars
> 
> 
> > and it does not appear to be valid Python syntax.
> 
> It's perfectly fine syntax, no different from:
> 
> my_dict['spam'] = 'a yummy ham-like substance'
> 
> or similar.
> 
> 
> -- 
> Steven

"as vars() has not been declared and it does not appear to be valid Python syntax"

You assume too much IMHO.  Vars() was not declared in the code provided and I do not think that we should be assuming that it is a function returning a dictionary instead of an error.  Just my opinion.

[toc] | [prev] | [next] | [standalone]


#24575

Fromalex23 <wuwei23@gmail.com>
Date2012-06-27 19:43 -0700
Message-ID<8ecd09f5-6b94-45aa-a354-adeb11aab000@d6g2000pbt.googlegroups.com>
In reply to#24573
On Jun 28, 12:15 pm, woo...@gmail.com wrote:
> You assume too much IMHO.  Vars() was not declared in
> the code provided and I do not think that we should be
> assuming that it is a function returning a dictionary instead
> of an error.

http://docs.python.org/library/functions.html#vars

Do you have the same objection to every built-in?

> Just my opinion.

There are two ways to help people: by trying to understand what
they're doing, or by submitting them to endless pedantry. Only one of
those is actually helpful.

[toc] | [prev] | [next] | [standalone]


#24576

Fromiconoclast011 <iconoclast011@gmail.com>
Date2012-06-27 21:59 -0500
Message-ID<8uSdna9xetkAVXbSnZ2dnUVZ_qSdnZ2d@giganews.com>
In reply to#24575
In reply to "alex23" who wrote the following:

> On Jun 28, 12:15=A0pm, woo...@gmail.com wrote:
> > You assume too much IMHO. =A0Vars() was not declared in
> > the code provided and I do not think that we should be
> > assuming that it is a function returning a dictionary instead
> > of an error.
> 
> http://docs.python.org/library/functions.html#vars
> 
> Do you have the same objection to every built-in?
> 
> > Just my opinion.
> 
> There are two ways to help people: by trying to understand what
> they're doing, or by submitting them to endless pedantry. Only one of
> those is actually helpful.
        
Thanks to all ...      I think I'll plunge into a book on Tkinter ...

KDA



-- 
--------------------------------- --- -- -
Posted with NewsLeecher v5.0 Beta 15
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

[toc] | [prev] | [next] | [standalone]


#24594

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-06-28 02:08 -0400
Message-ID<mailman.1589.1340863748.4697.python-list@python.org>
In reply to#24576
On Wed, 27 Jun 2012 21:59:41 -0500, iconoclast011
<iconoclast011@gmail.com> declaimed the following in
gmane.comp.python.general:

>         
> Thanks to all ...      I think I'll plunge into a book on Tkinter ...
>

	There aren't enough such to take a plunge...

	There's the PDF format Tkinter reference guide (which I find
practically unusable unless one already knows the terminology of TCL/Tk)

	There's an old Manning "Tkinter in Action" (which goes back to when
PMW was the great add-on to Tkinter, before Tix; ie, a decade ago).

	There are Tkinter chapters in various Python textbooks (like
Programming Python)

	Though to be honest, your initial question and sample code indicates
more a need to study algorithms and data structures in general, and
Python in specific (a simple loop, a dictionary for handling dynamically
generated "variable names", etc.) -- so Programming Python might be a
recommendation (if that's too deep, start with "Learning Python")

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#24577

FromTemia Eszteri <lamialily@cleverpun.com>
Date2012-06-27 20:03 -0700
Message-ID<i8inu75dmavvf1p2rokqm8sh3qlpbnf9ji@4ax.com>
In reply to#24575
On Wed, 27 Jun 2012 19:43:07 -0700 (PDT), alex23 <wuwei23@gmail.com>
wrote:

>There are two ways to help people: by trying to understand what
>they're doing, or by submitting them to endless pedantry. Only one of
>those is actually helpful.

Is it alright if I use that as a quote? Properly attributed, of
course.
-- 
The amazing programming device: fuelled entirely by coffee, it codes while
awake and tests while asleep!

[toc] | [prev] | [next] | [standalone]


#24589

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-06-28 04:39 +0000
Message-ID<4febe00a$0$29866$c3e8da3$5496439d@news.astraweb.com>
In reply to#24573
On Wed, 27 Jun 2012 19:15:54 -0700, woooee wrote:

> "as vars() has not been declared and it does not appear to be valid
> Python syntax"
> 
> You assume too much IMHO.  Vars() was not declared in the code provided
> and I do not think that we should be assuming that it is a function
> returning a dictionary instead of an error.  Just my opinion.

Thanks for sharing.

It's an ignorant opinion. vars is a built-in function, just like len, chr, 
map, and many others.

Ignorance is not a sin, but willful ignorance is. I already gave the 
links showing that vars is a built-in function, and you chose to ignore 
them. Here they are again. Please take a few seconds to read at least one 
of them, so that you will be a better programmer in the future and your 
opinion will be less ignorant:

Python 2:
http://docs.python.org/library/functions.html#vars
 
Python 3:
http://docs.python.org/py3k/library/functions.html#vars



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#24564

FromMRAB <python@mrabarnett.plus.com>
Date2012-06-28 00:43 +0100
Message-ID<mailman.1575.1340840605.4697.python-list@python.org>
In reply to#24552
On 27/06/2012 23:21, iconoclast011 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 ...
>
[code snipped]

Something based around this, perhaps:


buttons = {}

for row in range(10):
     for column in range(9):
         b = Button(f3, text = "{0}{1}".format(row, column), bg = "white")
         b.grid(row=0, column=0)
         b.bind('<Button-1>', leftclick)   # bind left mouse click
         b.bind('<Button-3>', rightclick)   # bind left mouse click

         buttons['{0}{1}'.format(row, column)] = b

[toc] | [prev] | [next] | [standalone]


#24568

Fromalex23 <wuwei23@gmail.com>
Date2012-06-27 17:31 -0700
Message-ID<3dfaba22-9fe6-4f79-b74a-11d37e81cfc5@m2g2000pbv.googlegroups.com>
In reply to#24552
On Jun 28, 8:21 am, iconoclast011 <iconoclast...@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 ...

It hasn't been updated for a few years, but I was always impressed by
Richard Jones' use of context managers in his withgui, especially his
minesweeper example:

    import random

    class Cell(object):
        def __init__(self, i, j, has_bomb):
            self.i, self.j = i, j
            self.has_bomb = has_bomb
    class Board(list):
        def __init__(self, size, chance=.2):
            self.size = size
            self[:] = [[Cell(i, j, random.random() < chance)
                for i in range(size)] for j in range(size)]
        def count(self, cell):
            '''Count the number of bombs near the cell.'''
            return sum(self[j][i].has_bomb
                for i in range(max(0, cell.i-1), min(self.size, cell.i
+2))
                    for j in range(max(0, cell.j-1), min(self.size,
cell.j+2)))

    board = Board(20)

    with gui.canvas(width=320, height=320) as canvas:
        for column in board:
            for cell in column:
                @canvas.image('cover.png', x=cell.i*16, y=cell.j*16)
                def on_mouse(image, mouse, cell=cell):
                    count = board.count(cell)
                    if cell.has_bomb:
                        image.value = 'bomb.png'
                        print 'GAME OVER!'
                    elif count:
                        image.destroy()
                        canvas.label(str(count), x=cell.i*16+8,
y=cell.j*16+8,
                            anchor=center)
                    else:
                        image.destroy()

http://www.mechanicalcat.net/richard/log/Python/Something_I_m_working_on.7
https://code.launchpad.net/withgui

[toc] | [prev] | [next] | [standalone]


#24586

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2012-06-27 21:04 -0700
Message-ID<a6ee296e-6d5e-4167-9267-dbcaf65ea7eb@w6g2000yqg.googlegroups.com>
In reply to#24552
On Jun 27, 5:21 pm, iconoclast011 <iconoclast...@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web