'How do I make python give an output when my turtle is in a specific range on the canvas?
I'm pretty new to programming and I was making a simple turtle game for a programming class. I wanted to expand my current program which uses turtle graphics, so that when the turtle that the user moves gets to the 'treasure' or the crate, python outputs a "You win!" command. Here is my code: import turtle
def theTreasure(t):
# move to position
t.penup()
x = 150
y = 200
t.setpos(x, y) # Sets the position of the turtle
t.setheading(270)
t.pendown()
t.color("maroon")
t.begin_fill()
for n in range(4):
t.forward(100)
t.left(90)
t.end_fill()
t.penup()
x = 160
y = 190
t.setpos(x, y) # Sets the position of the turtle
t.setheading(270)
t.pendown()
t.color("peru")
t.begin_fill()
for n in range(4):
t.forward(80)
t.left(90)
t.end_fill()
t.color("black")
t.pensize(10)
t.left(45)
t.forward(113)
t.penup()
t.right(220)
t.forward(80)
t.pendown()
t.left(130)
t.forward(113)
def player(s):
s.setpos(0, 0)
s.shape("triangle")
s.color("orange")
s.left(90)
def printControls():
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("~ Player Controls ")
print("~ W: Forward")
print("~ A: Right")
print("~ S: Backward")
print("~ D: Left")
print("~ R: Quit")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Remember, you must get to the crate!")
def forwardControl(s):
s.forward(20)
def backwardControl(s):
s.right(180)
s.forward(20)
def rightControl(s):
s.right(45)
def leftControl(s):
s.left(45)
def endGame():
print("Thanks for playing!")
def main():
t = turtle.Turtle()
s = turtle.Turtle()
t.speed(40)
theTreasure(t)
player(s)
printControls()
movement = "L"
while (movement != "R"):
movement = input("Look at the controls. Which way would you like to go? Select W, A, S, or D.")
if (movement == "W"):
forwardControl(s)
elif (movement == "S"):
backwardControl(s)
elif (movement == "A"):
rightControl(s)
elif (movement == "D"):
leftControl(s)
elif (movement == "R"):
endGame()
else:
print("Not a control option!")
main()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
