Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Create pong game using Python - Turtle
Next article icon

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 theturtleand randommodules.

Approach:

  1. 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.
  2. Set the screen and also choose the background color of your output screen window.
  3. Define a function for making a square for the base of your game.
  4. Define a function to keep a check of the index number.
  5. Define a function to make your game user-friendly i.e user click.
  6. Write a function to draw tiles on the square base defined in step 3.
  7. Finally use the shuffle() function to shuffle the numbers placed on the square tiles in the square box.
Python3
# import modulesfromrandomimport*fromturtleimport*# set the screenscreen=Screen()#choose background colorscreen.bgcolor("yellow")# define the function# for creating a square section# for the gamedefSquare(x,y):up()goto(x,y)down()color('white','green')begin_fill()forcountinrange(4):forward(50)left(90)end_fill()# define function to# keep a check of index numberdefNumbering(x,y):returnint((x+200)//50+((y+200)//50)*8)# define functiondefCoordinates(count):return(count%8)*50-200,(count//8)*50-200# define function# to make it interactive# user clickdefclick(x,y):spot=Numbering(x,y)mark=state['mark']ifmarkisNoneormark==spotortiles[mark]!=tiles[spot]:state['mark']=spotelse:hide[spot]=Falsehide[mark]=Falsestate['mark']=Nonedefdraw():clear()goto(0,0)stamp()forcountinrange(64):ifhide[count]:x,y=Coordinates(count)Square(x,y)mark=state['mark']ifmarkisnotNoneandhide[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))*2state={'mark':None}hide=[True]*64# for shuffling the# numbers placed inside# the square tilesshuffle(tiles)tracer(False)onscreenclick(click)draw()done()

Output:


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp