Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. #Space Invaders
  2.  
  3. #Set up the screen
  4. import turtle
  5. import os
  6. import math
  7. import random
  8.  
  9. #Set up the screen
  10. wn = turtle.Screen()
  11. wn.bgcolor("black")
  12. wn.title("Space invaders")
  13.  
  14. #Draw border
  15. border_pen = turtle.Turtle()
  16. border_pen.speed(0)
  17. border_pen.color("white")
  18. border_pen.penup()
  19. border_pen.setposition(-300,-300)
  20. border_pen.pendown()
  21. border_pen.pensize(3)
  22. for side in range(4):
  23. border_pen.fd(600)
  24. border_pen.lt(90)
  25. border_pen.hideturtle()
  26.  
  27. #Set the score to 0
  28. score = 0
  29.  
  30. #Draw the score
  31. score_pen = turtle.Turtle()
  32. score_pen.speed(0)
  33. score_pen.color("white")
  34. score_pen.penup()
  35. score_pen.setposition(-290, 280)
  36. scorestring = "Score: %s" %score
  37. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  38. score_pen.hideturtle()
  39.  
  40. #Create the player turtle
  41. player = turtle.Turtle()
  42. player.color("blue")
  43. player.shape("triangle")
  44. player.penup()
  45. player.speed(0)
  46. player.setposition(0, -250)
  47. player.setheading(90)
  48.  
  49. playerspeed = 15
  50.  
  51. #Choose a number of enemies
  52. number_of_enemies = 5
  53. #Create an empty list of enemies
  54. enemies = []
  55.  
  56. #Add enemies to the list
  57. for i in range(number_of_enemies):
  58. #Create the enemy
  59. enemies.append(turtle.Turtle())
  60.  
  61. for enemy in enemies:
  62. enemy.color("red")
  63. enemy.shape("circle")
  64. enemy.penup()
  65. enemy.speed(0)
  66. x = random.randint(-200, 200)
  67. y = random.randint(100, 250)
  68. enemy.setposition(x, y)
  69.  
  70. enemyspeed = 2
  71.  
  72. #Create the player's bullet
  73. bullet = turtle.Turtle()
  74. bullet.color("yellow")
  75. bullet.shape("triangle")
  76. bullet.penup()
  77. bullet.speed(0)
  78. bullet.setheading(90)
  79. bullet.shapesize(0.5, 0.5)
  80. bullet.hideturtle()
  81.  
  82. bulletspeed = 20
  83.  
  84. #Define bullet state
  85. #ready - ready to fire
  86. #fire - bullet is firing
  87. bulletstate = "ready"
  88.  
  89. #Move the player left and right
  90. def move_left():
  91. x = player.xcor()
  92. x -= playerspeed # takes value of X, subtracts player speed and assigns it to X
  93. if x < -280:
  94. x = -280
  95. player.setx(x)
  96.  
  97. def move_right():
  98. x = player.xcor()
  99. x += playerspeed # takes value of X, adds player speed and assigns it to X
  100. if x > 280:
  101. x = 280
  102. player.setx(x)
  103.  
  104. def fire_bullet():
  105. #Declare bulletstate as a global if it needs to be changed
  106. global bulletstate
  107. if bulletstate == "ready":
  108. bulletstate = "fire"
  109. #Move the bullet to the just above the player
  110. x = player.xcor()
  111. y = player.ycor() +10
  112. bullet.setposition(x, y)
  113. bullet.showturtle()
  114.  
  115. def isCollision(t1, t2):
  116. distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
  117. if distance < 15:
  118. return True
  119. else:
  120. return False
  121.  
  122. #Create keyboard bindings
  123. turtle.listen()
  124. turtle.onkey(move_left, "Left") #when left key is pushed, it uses move_left function
  125. turtle.onkey(move_right, "Right") #when right key is pushed, it uses move_right function
  126. turtle.onkey(fire_bullet, "space") #press spacebar = fire_bullet function
  127.  
  128. #Main game loop
  129. while True:
  130.  
  131. for enemy in enemies:
  132. #Move the enemy
  133. x = enemy.xcor()
  134. x += enemyspeed
  135. enemy.setx(x)
  136.  
  137. #Move the enemy back and down
  138. if enemy.xcor() > 280:
  139. #Move all enemies down
  140. for e in enemies:
  141. y = e.ycor()
  142. y -= 40
  143. e.sety(y)
  144. #Change enemy direction
  145. enemyspeed *= -1
  146.  
  147. if enemy.xcor() < -280:
  148. #Move all enemies down
  149. for e in ememies:
  150. y = e.ycor()
  151. y -= 40
  152. e.sety(y)
  153. #Change enemy direction
  154. enemyspeed *= -1
  155.  
  156. #Check for collision between bullet and enemy
  157. if isCollision(bullet, enemy):
  158. #Reset the bullet
  159. bullet.hideturtle()
  160. bulletstate = "ready"
  161. bullet.setposition(0, -400)
  162. #Reset the enemy
  163. x = random.randint(-200, 200)
  164. y = random.randint(100, 250)
  165. enemy.setposition(x, y)
  166. #Update the score
  167. score += 10
  168. scorestring = "Score: %s" %score
  169. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  170.  
  171. if isCollision(player, enemy):
  172. player.hideturtle()
  173. enemy.hideturtle()
  174. print ("Game Over")
  175. break
  176.  
  177. #Move the bullet
  178. if bulletstate == "fire":
  179. y = bullet.ycor()
  180. y += bulletspeed
  181. bullet.sety(y)
  182.  
  183. #Check to see if bullet has gone to the top
  184. if bullet.ycor() > 275:
  185. bullet.hideturtle()
  186. bulletstate = "ready"
  187.  
  188.  
  189.  
  190. delay = raw_input("Press enter to finish.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement