Flipping tiles game can be played to test our memory. In this, we have a certain even number of tiles, in which each number/figure has a pair. The tiles are facing downwards, and we have to flip them to see them. In a turn, one flips 2 tiles, if the tiles match then they are removed. If not then they are flipped and placed back in the position. We keep on doing this until all the tiles have been matched and removed.
To simulate this game in Python, we will be using the turtle and random modules.
Approach:
- Import turtle and random module. Python offers the random module that can generate random numbers, and turtle module is being used in making different objects.
- Set the screen and also choose the background color of your output screen window.
- Define a function for making a square for the base of your game.
- Define a function to keep a check of the index number.
- Define a function to make your game user-friendly i.e user click.
- Write a function to draw tiles on the square base defined in step 3.
- Finally use the shuffle() function to shuffle the numbers placed on the square tiles in the square box.
Python3
from random import *
from turtle import *
screen = Screen()
screen.bgcolor( "yellow" )
def Square(x, y):
up()
goto(x, y)
down()
color( 'white' , 'green' )
begin_fill()
for count in range ( 4 ):
forward( 50 )
left( 90 )
end_fill()
def Numbering(x, y):
return int ((x + 200 ) / / 50 + ((y + 200 ) / / 50 ) * 8 )
def Coordinates(count):
return (count % 8 ) * 50 - 200 , (count / / 8 ) * 50 - 200
def click(x, y):
spot = Numbering(x, y)
mark = state[ 'mark' ]
if mark is None or mark = = spot or tiles[mark] ! = tiles[spot]:
state[ 'mark' ] = spot
else :
hide[spot] = False
hide[mark] = False
state[ 'mark' ] = None
def draw():
clear()
goto( 0 , 0 )
stamp()
for count in range ( 64 ):
if hide[count]:
x, y = Coordinates(count)
Square(x, y)
mark = state[ 'mark' ]
if mark is not None and hide[mark]:
x, y = Coordinates(mark)
up()
goto(x + 2 , y)
color( 'black' )
write(tiles[mark], font = ( 'Arial' , 30 , 'normal' ))
update()
ontimer(draw, 10 )
tiles = list ( range ( 32 )) * 2
state = { 'mark' : None }
hide = [ True ] * 64
shuffle(tiles)
tracer( False )
onscreenclick(click)
draw()
done()
|
Output:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Sep, 2021
Like Article
Save Article