Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Flipping Tiles (memory game) using Python3
Next article icon

Prerequisites:Turtle Programming in Python

TurtleMovegame is basically a luck-based game. In this game two-players (Red & Blue), using their own turtle (object) play the game.

How to play

The game is played in the predefined grid having some boundaries.

  • Both players move the turtle for a unit distance.
  • Now both players flip the coin:
  • if HEAD, then take a right turn
  • else take a left turn
  • 3) Now repeat 1 & 2, till both turtles lie in the boundary

Implementation in Turtle Python

  • First, a turtle screen object is created for the grid boundary.
  • Now two turtles (Red & Blue) are created, one for each player.
  • Both turtles are moved a unit distance usingturtle_obj.forward(50)method.
  • Turn is decided, Usingrandom.randrange(0, 2) i.e. 0 for left and 1 for the right.
  • After every move, the position of each turtle is checked, if any turtle crosses the boundary, then that turtle loses the game.

Below is the implementation

Python3
importrandomimportturtle# function to check whether turtle# is in Screen or notdefisInScreen(win,turt):# getting the end points of turtle screenleftBound=-win.window_width()/2rightBound=win.window_width()/2topBound=win.window_height()/2bottomBound=-win.window_height()/2# getting the current position of the turtleturtleX=turt.xcor()turtleY=turt.ycor()# variable to store whether in screen or notstillIn=True# condition to check whether in screen or notifturtleX>rightBoundorturtleX<leftBound:stillIn=FalseifturtleY>topBoundorturtleY<bottomBound:stillIn=False# returning the resultreturnstillIn# function to check whether both turtle have# different position or notdefsameposition(Red,Blue):ifRed.pos()==Blue.pos():returnFalseelse:returnTrue#   main functiondefmain():# screen initialization for turtlewn=turtle.Screen()# Turtle Red initialization# instantiate a new turtle object# called 'Red'Red=turtle.Turtle()# set pencolor as redRed.pencolor("red")# set pensize as 5Red.pensize(5)# set turtleshape as turtleRed.shape('turtle')pos=Red.pos()# Turtle Blue initialization# instantiate a new turtle object# called 'Blue'Blue=turtle.Turtle()# set pencolor as blueBlue.pencolor("blue")# set pensize as 5Blue.pensize(5)# set turtleshape as turtleBlue.shape('turtle')# make the turtle invisibleBlue.hideturtle()# don't draw when turtle movesBlue.penup()# move the turtle to a location 50# units away from RedBlue.goto(pos[0]+50,pos[1])# make the turtle visibleBlue.showturtle()# draw when the turtle movesBlue.pendown()# variable to store whether turtles# are in screen or notmT=TruejT=True# loop for the gamewhilemTandjTandsameposition(Red,Blue):# coin flip for RedcoinRed=random.randrange(0,2)# angle for Red# random.randrange(0, 180)angleRed=90# condition for left or right# based on coinifcoinRed==0:Red.left(angleRed)else:Red.right(angleRed)# coin flip for BluecoinBlue=random.randrange(0,2)# angle for Blue# random.randrange(0, 180)angleBlue=90# condition for left or right based# on coinifcoinBlue==0:Blue.left(angleBlue)else:Blue.right(angleBlue)# draw for RedRed.forward(50)# draw for BlueBlue.forward(50)# checking whether turtles are in the# screen or notmT=isInScreen(wn,Blue)jT=isInScreen(wn,Red)# set pencolor for Blue and Red as blackRed.pencolor("black")Blue.pencolor("black")# condition check for draw or winifjT==TrueandmT==False:# writing resultsRed.write("Red Won",True,align="center",font=("arial",15,"bold"))elifmT==TrueandjT==False:# writing resultsBlue.write("Blue Won",True,align="center",font=("arial",15,"bold"))else:# writing resultsRed.write("Draw",True,align="center",font=("arial",15,"bold"))Blue.write("Draw",True,align="center",font=("arial",15,"bold"))# exit on closewn.exitonclick()# Calling main functionmain()

Output: 

Complexity : 

The time complexity of the game is O(n), where n is the number of iterations in the while loop. The while loop continues to run as long as both turtles are in the screen and have different positions.

The space complexity of the game is O(1), as the number of variables and data structures used is constant and does not depend on the size of the input. The variables used are two turtle objects, a screen object, some integer variables, and a few boolean variables.


 


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