Create a GUI for a matching game. Initially, buttons should cover pairs of images. When the user clicks a button, the image displays. If the user finds a matching pair, disable the buttons and dis- play their images. If the user’s choices do not match, hide the images.

What will be an ideal response?


```
# Matching game

from Tkinter import *
import random
from tkMessageBox import *

class MatchingGame( Frame ):
"""Matching game"""

def __init__( self ):
"""Create GUI and play game"""

Frame.__init__( self )
self.grid( sticky = W+E+N+S )
self.master.title( "Matching Game" )

self.board = [] # list of buttons
self.hidden = [] # list of hidden images
self.displayedImages = [] # list of displayed images
self.moves = [] # list of moves

# create PhotoImage objects for game board
gifs = [ "bug1.gif", "bug2.gif", "buganim.gif",
"travelbug.gif" ]
self.images = [] # list of images to be hidden

for gif in gifs:

self.images.append( PhotoImage( file = gif ) )

self.images = self.images * 4 # 16 images to assign
self.randomImages() # images assigned to buttons

for i in range( 16 ):

newButton = Button( self, height = 75, width = 80,
name = str( i ), bitmap = "question", bg = "gray" )
newButton.bind( "", self.play )
self.board.append( newButton )

current = 0

# display buttons in a 4x4 grid
for x in range( 0, 4 ):

for y in range( 0, 4 ):
self.board[ current ].grid( row = x, column = y,
padx = 10, pady = 10 )
current += 1

def randomImages( self ):
"""Randomly assign images to list hidden"""

for index in range( 16 ):

assigned = 0 # image not assigned to button yet

while not assigned:

randomIndex = random.randrange( 16 )

if self.images[ randomIndex ] != None:

self.hidden.append( self.images[ randomIndex ] )
assigned = 1
self.images[ randomIndex ] = None

def play( self, event ):
"""Handle playing of the game"""

# get currently selected square
square = self.currentButton( event )

self.moves.append( square ) # add move

# number of moves made
numberMoves = len( self.moves )

self.displayImage( square ) # display selected image

if numberMoves % 2 == 0:

# selections match and are not the same location
if self.displayedImages[ numberMoves - 1 ] == \
self.displayedImages[ numberMoves - 2 ] \
and self.moves[ numberMoves - 1 ] != \
self.moves[ numberMoves - 2 ]:
showinfo( "Game Information", "A MATCH!" )
self.board[ square ].config( state = DISABLED )
self.board[ self.moves[ -2 ] ].config( state =
DISABLED )
return

# selections do not match
elif self.displayedImages[ numberMoves - 1 ] != \
self.displayedImages[ numberMoves - 2 ]:
showinfo( "Game Information", "NOT A MATCH!" )

# hide incorrect choices
self.board[ square ].config( image = "", bg =
"gray", bitmap = "question" )
self.board[ self.moves[ -2 ] ].config( image =
"", bitmap = "question", bg = "gray" )

def displayImage( self, square ):
"""Display image hidden under button"""

self.board[ square ].config( image =
self.hidden[ square ], bg = "white" )
self.displayedImages.append( self.hidden[ square ] )

def currentButton( self, event ):
"""Return number of button associated with event"""

square = ( str( event.widget ) ).split( "." )
return int( square[ 2 ] )

def main():
MatchingGame().mainloop()

if __name__ == "__main__":
main()
```
![15066|410x208](upload://8ekH3K4wSTm7Mgfe4H2MRQmz9uh.png)

Computer Science & Information Technology

You might also like to view...

A series of nested __________ statements can be used to process menu selections instead of using a case statement.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

One might need to make small adjustments in a graphic's position if the text does not wrap as expected.?

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

What is the shortcut key for the Bucket tool?

What will be an ideal response?

Computer Science & Information Technology

What is the meaning of the "10" in 10BaseT?

A. 10 meters maximum distance B. cabling with 10 pairs C. cabling with 10 wires D. maximum speed of 10 Mbps

Computer Science & Information Technology