Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Question:Programming a game grid ...

Reply
Thread Tools

Re: Question:Programming a game grid ...

 
 
alex23
Guest
Posts: n/a
 
      06-28-2012
On Jun 28, 8:21*am, iconoclast011 <(E-Mail Removed)> wrote:
> Fairly new to Python ... Is there a way to efficiently (different from mybrute
> 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...m_working_on.7
https://code.launchpad.net/withgui
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Game programming for kids: looking for open source 2D game developmentkit Max Kubierschky Python 10 03-31-2007 07:18 PM
I need help with card game game of war in GUI enviroment judith Java 0 11-01-2006 06:11 AM
Java Game- Game can't find it's graphics jar file BlackHawke Java 12 01-26-2004 06:14 AM



Advertisments