Question
modifying a turtle game for python . 1. Ask the user for the initial force and firing angle to launch the turtle which already has
modifying a turtle game for python .
1. Ask the user for the initial force and firing angle to launch the turtle which already has variables instead.''
2. from a for-loop, to a while-loop. This loop shall stop the animation when the turtle hits the mountain, or the target, or falls below the ground line missing the mountain and the target, or goes out of bounds
3. Show "You win" if the turtle falls inside the red rectangle. End the game when the user wins.
4. Show an appropriate message when the turtle hits the mountain, falls below the ground line (y=0), or goes out of bounds (assume the game area is defined by the corners (0,0) and (600,400))
5. Show "You lose" if the player fails to land the turtle on the target after three attempts.
6. Ask the user if s/he wants to play again. Your program shall allow the player to play again (allow three more attempts) if the answer is yes. Otherwise, it should display "Thanks for playing" if the answer is "No."
import math
import turtle
import random
# Resize window to 600x400
turtle.setup(600,400)
# Set coordinate system so bottom-left corner is (-10,-10)
# and top-right corner is (590, 390)
turtle.setworldcoordinates(-10,-10,590,390)
turtle.shape('turtle')
# Constants
GRAVITY = -10
DT = 0.1
# Draw "ground" line (at y=0)
turtle.hideturtle()
turtle.penup()
turtle.pensize(5)
turtle.goto(-10,0)
turtle.pendown()
turtle.goto(640,0)
turtle.penup()
# Draw mountain
mountainX1 = random.randint(1,10) * 10 + 120
mountainY1 = 0
mountainX2 = mountainX1 + random.randint(3,10) * 20
mountainY2 = random.randint(4,10) * 20
turtle.color('green')
turtle.begin_fill()
turtle.goto(mountainX1, mountainY1)
turtle.pendown()
turtle.goto(mountainX2, mountainY1)
turtle.goto(mountainX2, mountainY2)
turtle.goto(mountainX1, mountainY2)
turtle.goto(mountainX1, mountainY1)
turtle.end_fill()
turtle.penup()
# Draw landing target
targetX1 = mountainX2 + random.randint(5,10) * 10
targetY1 = 0
targetX2 = targetX1 + 50
targetY2 = 20
turtle.color('red')
turtle.begin_fill()
turtle.goto(targetX1, targetY1)
turtle.pendown()
turtle.goto(targetX2, targetY1)
turtle.goto(targetX2, targetY2)
turtle.goto(targetX1, targetY2)
turtle.goto(targetX1, targetY1)
turtle.end_fill()
turtle.penup()
turtle.color('black')
# Place turtle at (0,0)
turtle.goto(0,0)
turtle.showturtle()
# Input information: initial force and firing angle
initialForce = 80
firingAngle = 65
# Determine initial velocities
dx = initialForce * math.cos(math.radians(firingAngle))
dy = initialForce * math.sin(math.radians(firingAngle))
# Launch the turtle!
x = 0
y = 0
for i in range(145): # move turtle 145 times
# Calculate new location of turtle
x = x + dx * DT
y = y + dy * DT
turtle.goto(x, y)
# Calculate new vertical velocity
dy = dy + GRAVITY * DT
turtle.done()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started