Advertisement
Guest User

shoot shelid

a guest
Mar 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. import random
  2. import turtle
  3. import math
  4. #background color
  5. wn = turtle.Screen()
  6. wn.bgcolor("black")
  7. wn.title("sheild")
  8. #border
  9. border_pen = turtle.Turtle()
  10. border_pen.speed(0)
  11. border_pen.color("white")
  12. border_pen.penup()
  13. border_pen.setposition(-300,-300)
  14. border_pen.pendown()
  15. border_pen.pensize(3)
  16. for side in range(4):
  17. border_pen.forward(600)
  18. border_pen.left(90)
  19. border_pen.hideturtle()
  20. #set the score 0
  21. score = 0
  22. #draw score
  23. score_pen = turtle.Turtle()
  24. score_pen.speed(0)
  25. score_pen.color("white")
  26. score_pen.penup()
  27. score_pen.setposition(-290, 280)
  28. scorestring = "Score: %s"%score
  29. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  30. score_pen.hideturtle()
  31. #player
  32. player = turtle.Turtle()
  33. player.color("blue")
  34. player.shape("circle")
  35. player.penup()
  36. player.speed(0)
  37. player.setposition(0, -0)
  38. player.setheading(90)
  39. #enemy
  40. enemy= turtle.Turtle()
  41. enemy.color("red")
  42. enemy.shape("square")
  43. enemy.penup()
  44. enemy.speed(0)
  45. x = (0)
  46. y = (250)
  47. enemy.setposition(x, y)
  48. #enemy2
  49. enemy2= turtle.Turtle()
  50. enemy2.color("red")
  51. enemy2.shape("square")
  52. enemy2.penup()
  53. enemy2.speed(0)
  54. x = (0)
  55. y = (-250)
  56. enemy2.setposition(x, y)
  57. #create the players sheild
  58. sheild = turtle.Turtle()
  59. sheild.color("red")
  60. sheild.shape("square")
  61. sheild.penup()
  62. sheild.speed(0)
  63. sheild.setposition(0, 20)
  64. sheild.setheading(90)
  65. sheild.shapesize(2, 0.5)
  66. sheildspeed = 40
  67. #create the players bullet
  68. bullet = turtle.Turtle()
  69. bullet.color("yellow")
  70. bullet.shape("triangle")
  71. bullet.penup()
  72. bullet.speed(0)
  73. bullet.setheading(90)
  74. bullet.shapesize(0.5, 0.5)
  75. bullet.hideturtle()
  76. bulletspeed = 10
  77. #define bullet state
  78. bulletstate = "ready"
  79. #create the enemys bullet
  80. ebullet = turtle.Turtle()
  81. ebullet.color("yellow")
  82. ebullet.shape("triangle")
  83. ebullet.penup()
  84. ebullet.speed(0)
  85. ebullet.setheading(-90)
  86. ebullet.shapesize(0.5, 0.5)
  87. ebullet.hideturtle()
  88. ebulletspeed = 5
  89. #define bullet state
  90. ebulletstate = "ready"
  91. #create the players wbullet
  92. wbullet = turtle.Turtle()
  93. wbullet.color("yellow")
  94. wbullet.shape("triangle")
  95. wbullet.penup()
  96. wbullet.speed(0)
  97. wbullet.setheading(90)
  98. wbullet.shapesize(0.5, 0.5)
  99. wbullet.hideturtle()
  100. wbulletspeed = 5
  101. #define bullet state
  102. wbulletstate = "ready"
  103. #create the players bullet 2
  104. qbullet = turtle.Turtle()
  105. qbullet.color("yellow")
  106. qbullet.shape("triangle")
  107. qbullet.penup()
  108. qbullet.speed(0)
  109. qbullet.setheading(-90)
  110. qbullet.shapesize(0.5, 0.5)
  111. qbullet.hideturtle()
  112. qbulletspeed = 10
  113. #define bullet state
  114. qbulletstate = "ready"
  115. #sheild movement down
  116. def move_down():
  117. y = sheild.ycor()
  118. y -= sheildspeed
  119. if y < -20:
  120. y = - 20
  121. sheild.sety(y)
  122. #sheild movement up
  123. def move_up():
  124. y = sheild.ycor()
  125. y += sheildspeed
  126. if y > 20:
  127. y = 20
  128. sheild.sety(y)
  129. #bullet movement
  130. def fire_bullet():
  131. global bulletstate
  132. if bulletstate == "ready":
  133. bulletstate = "fire"
  134. #set bullet position
  135. x = player.xcor()
  136. y = player.ycor() +10
  137. bullet.setposition(x, y)
  138. bullet.showturtle()
  139. #wbullet movement
  140. def fire_wbullet():
  141. global wbulletstate
  142. if wbulletstate == "ready":
  143. wbulletstate = "fire"
  144. #set bullet position
  145. x = enemy2.xcor()
  146. y = enemy2.ycor() +10
  147. wbullet.setposition(x, y)
  148. wbullet.showturtle()
  149. #ebullet movement
  150. def fire_ebullet():
  151. global ebulletstate
  152. if ebulletstate == "ready":
  153. ebulletstate = "fire"
  154. #set bullet position
  155. x = enemy.xcor()
  156. y = enemy.ycor() +10
  157. ebullet.setposition(x, y)
  158. ebullet.showturtle()
  159. #qbullet movement
  160. def fire_qbullet():
  161. global qbulletstate
  162. if qbulletstate == "ready":
  163. qbulletstate = "fire"
  164. #set bullet position
  165. x = player.xcor()
  166. y = player.ycor() +10
  167. qbullet.setposition(x, y)
  168. qbullet.showturtle()
  169. def isCollision(t1, t2):
  170. distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
  171. if distance < 15:
  172. return True
  173. else:
  174. return False
  175. #bindings
  176. turtle.listen()
  177. turtle.onkey(fire_bullet, "Left")
  178. turtle.onkey(fire_ebullet, "w")
  179. turtle.onkey(move_down, "Down")
  180. turtle.onkey(move_up, "Up")
  181. turtle.onkey(fire_qbullet, "Right")
  182. turtle.onkey(fire_wbullet, "s")
  183. #main game loop
  184. while True:
  185. player.fd(0)
  186. r = random.randint(0, 1000)
  187. if r > 995:
  188. fire_ebullet()
  189. elif r < 5:
  190. fire_wbullet()
  191. #move the bullet
  192. if bulletstate == "fire":
  193. y = bullet.ycor()
  194. y += bulletspeed
  195. bullet.sety(y)
  196. #move the bullet
  197. if wbulletstate == "fire":
  198. y = wbullet.ycor()
  199. y += wbulletspeed
  200. wbullet.sety(y)
  201. #move the bullet
  202. if ebulletstate == "fire":
  203. y = ebullet.ycor()
  204. y -= ebulletspeed
  205. ebullet.sety(y)
  206. #move the qbullet
  207. if qbulletstate == "fire":
  208. y = qbullet.ycor()
  209. y -= qbulletspeed
  210. qbullet.sety(y)
  211. #check for a collision with enemy
  212. if isCollision(bullet, enemy):
  213. #reset bullet
  214. bullet.hideturtle()
  215. bulletstate = "ready"
  216. bullet.setposition(0, -400)
  217. #update the score
  218. score += 10
  219. scorestring = "Score: %s" %score
  220. score_pen.clear()
  221. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  222. #check for a collision with enemy2
  223. if isCollision(wbullet, player):
  224. #reset wbullet
  225. wbullet.hideturtle()
  226. wbulletstate = "ready"
  227. wbullet.setposition(0, -400)
  228. #update the score
  229. score -= 10
  230. scorestring = "Score: %s" %score
  231. score_pen.clear()
  232. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  233. #check for a collision with enemy
  234. if isCollision(qbullet, enemy2):
  235. #reset qbullet
  236. qbullet.hideturtle()
  237. qbulletstate = "ready"
  238. qbullet.setposition(0, -400)
  239. #update the score
  240. score += 10
  241. scorestring = "Score: %s" %score
  242. score_pen.clear()
  243. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  244. #player collision
  245. if isCollision(ebullet, player):
  246. #reset bullet
  247. ebullet.hideturtle()
  248. ebulletstate = "ready"
  249. ebullet.setposition(0, 250)
  250. #update the score
  251. score -= 10
  252. scorestring = "Score: %s" %score
  253. score_pen.clear()
  254. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  255. #sheild collision
  256. if isCollision(ebullet, sheild):
  257. #reset bullet
  258. ebullet.hideturtle()
  259. ebulletstate = "ready"
  260. ebullet.setposition(0, 250)
  261. if isCollision(wbullet, sheild):
  262. #reset wbullet
  263. wbullet.hideturtle()
  264. wbulletstate = "ready"
  265. wbullet.setposition(0, -250)
  266.  
  267.  
  268.  
  269. delay = input("Press enter to finish.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement