Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. #kenefick
  2. #2018 HALLOWEEN 15th
  3. #program 3.9
  4.  
  5. #hit the target game
  6. import turtle
  7.  
  8. #constants
  9. SCREEN_WIDTH = 600
  10. SCREEN_HEIGHT = 600
  11. TARGET_LLEFT_X = 100 #lower left
  12. TARGET_LLEFT_Y = 250 #lower right
  13. TARGET_WIDTH = 25 #the target is square ergo this defines its size on all sides
  14. FORCE_FACTOR = 30
  15. PROJECTILE_SPEED = 1
  16. NORTH = 90
  17. SOUTH = 270
  18. EAST = 0
  19. WEST = 180
  20.  
  21. #setup
  22. turtle.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
  23. turtle.hideturtle()
  24. turtle.speed(0)
  25. turtle.penup()
  26. turtle.goto(TARGET_LLEFT_X, TARGET_LLEFT_Y)
  27. turtle.pendown()
  28. #draw the target
  29. turtle.setheading(EAST)
  30. turtle.forward(TARGET_WIDTH)
  31. turtle.setheading(NORTH)
  32. turtle.forward(TARGET_WIDTH)
  33. turtle.setheading(WEST)
  34. turtle.forward(TARGET_WIDTH)
  35. turtle.setheading(SOUTH)
  36. turtle.forward(TARGET_WIDTH)
  37. turtle.penup()
  38.  
  39. #get the position of the target center so we can get some angles and distances
  40. turtle.setheading(NORTH)
  41. turtle.forward(TARGET_WIDTH / 2)
  42. turtle.setheading(EAST)
  43. turtle.forward(TARGET_WIDTH / 2)
  44. targetPosition = turtle.pos()
  45. print(targetPosition)
  46.  
  47. #center the boy at home base
  48. turtle.goto(0, 0)
  49.  
  50. #get distance and angle from the target to home
  51. firingDistance = turtle.distance(targetPosition)
  52. firingAngle = turtle.towards(targetPosition)
  53.  
  54. #get some angles from the target position relative to home base
  55. angleA = firingAngle - 45
  56. angleB = angleA - 90
  57. if angleB < 0:
  58.     angleB += 360
  59. angleC = angleA + 180
  60. angleD = angleB - 180
  61.  
  62. #set up the turtle
  63. turtle.setheading(EAST)
  64. turtle.showturtle()
  65. turtle.speed(PROJECTILE_SPEED)
  66.  
  67. #get the angle and force from the user
  68. angle = float(input("Enter the projectile's angle: "))
  69. force = float(input("Enter the launch force(1-10): "))
  70.  
  71. #calculate the distance
  72. distance = force * FORCE_FACTOR
  73.  
  74. #ready the boy
  75. turtle.setheading(angle)
  76.  
  77. #LAUNCH
  78. turtle.pendown()
  79. turtle.forward(distance)
  80.  
  81. #check to see if the turtle made it into the box
  82. #if the turtle made it between the x values of the target...
  83. if (turtle.xcor() >= TARGET_LLEFT_X and
  84.     turtle.xcor() <= (TARGET_LLEFT_X + TARGET_WIDTH) and
  85.     #... as well as the y values...
  86.     turtle.ycor() >= TARGET_LLEFT_Y and
  87.     turtle.ycor() <= (TARGET_LLEFT_Y + TARGET_WIDTH)):
  88.     #... then it's a win!
  89.     print("Target hit!")
  90. else:
  91.     #otherwise it's a failure
  92.     print("You failed...")
  93.  
  94.     #failure handling
  95.  
  96.     landingSite = turtle.pos()
  97.     differenceAngle = turtle.towards(targetPosition)
  98.     if differenceAngle > 180:
  99.         differenceAngle -= 180
  100.     else:
  101.         differenceAngle += 180
  102.        
  103.     if differenceAngle > angleB or differenceAngle < angleA:
  104.         print("Try angling your shot higher.")
  105.     elif differenceAngle < angleC and differenceAngle > angleD:
  106.         print("Try angling your shot lower.")
  107.     else:
  108.         if distance > firingDistance:
  109.             print("Try using less force.")
  110.         else:
  111.             print("Try using more force.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement