Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #kenefick
- #2018 HALLOWEEN 15th
- #program 3.9
- #hit the target game
- import turtle
- #constants
- SCREEN_WIDTH = 600
- SCREEN_HEIGHT = 600
- TARGET_LLEFT_X = 100 #lower left
- TARGET_LLEFT_Y = 250 #lower right
- TARGET_WIDTH = 25 #the target is square ergo this defines its size on all sides
- FORCE_FACTOR = 30
- PROJECTILE_SPEED = 1
- NORTH = 90
- SOUTH = 270
- EAST = 0
- WEST = 180
- #setup
- turtle.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
- turtle.hideturtle()
- turtle.speed(0)
- turtle.penup()
- turtle.goto(TARGET_LLEFT_X, TARGET_LLEFT_Y)
- turtle.pendown()
- #draw the target
- turtle.setheading(EAST)
- turtle.forward(TARGET_WIDTH)
- turtle.setheading(NORTH)
- turtle.forward(TARGET_WIDTH)
- turtle.setheading(WEST)
- turtle.forward(TARGET_WIDTH)
- turtle.setheading(SOUTH)
- turtle.forward(TARGET_WIDTH)
- turtle.penup()
- #get the position of the target center so we can get some angles and distances
- turtle.setheading(NORTH)
- turtle.forward(TARGET_WIDTH / 2)
- turtle.setheading(EAST)
- turtle.forward(TARGET_WIDTH / 2)
- targetPosition = turtle.pos()
- print(targetPosition)
- #center the boy at home base
- turtle.goto(0, 0)
- #get distance and angle from the target to home
- firingDistance = turtle.distance(targetPosition)
- firingAngle = turtle.towards(targetPosition)
- #get some angles from the target position relative to home base
- angleA = firingAngle - 45
- angleB = angleA - 90
- if angleB < 0:
- angleB += 360
- angleC = angleA + 180
- angleD = angleB - 180
- #set up the turtle
- turtle.setheading(EAST)
- turtle.showturtle()
- turtle.speed(PROJECTILE_SPEED)
- #get the angle and force from the user
- angle = float(input("Enter the projectile's angle: "))
- force = float(input("Enter the launch force(1-10): "))
- #calculate the distance
- distance = force * FORCE_FACTOR
- #ready the boy
- turtle.setheading(angle)
- #LAUNCH
- turtle.pendown()
- turtle.forward(distance)
- #check to see if the turtle made it into the box
- #if the turtle made it between the x values of the target...
- if (turtle.xcor() >= TARGET_LLEFT_X and
- turtle.xcor() <= (TARGET_LLEFT_X + TARGET_WIDTH) and
- #... as well as the y values...
- turtle.ycor() >= TARGET_LLEFT_Y and
- turtle.ycor() <= (TARGET_LLEFT_Y + TARGET_WIDTH)):
- #... then it's a win!
- print("Target hit!")
- else:
- #otherwise it's a failure
- print("You failed...")
- #failure handling
- landingSite = turtle.pos()
- differenceAngle = turtle.towards(targetPosition)
- if differenceAngle > 180:
- differenceAngle -= 180
- else:
- differenceAngle += 180
- if differenceAngle > angleB or differenceAngle < angleA:
- print("Try angling your shot higher.")
- elif differenceAngle < angleC and differenceAngle > angleD:
- print("Try angling your shot lower.")
- else:
- if distance > firingDistance:
- print("Try using less force.")
- else:
- print("Try using more force.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement