Advertisement
Guest User

Space Invaders code

a guest
Jul 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. import turtle
  2. import os
  3. import time
  4. import math
  5.  
  6. #Set up the screen
  7. wn = turtle.Screen()
  8. wn.bgcolor("black")
  9. wn.title("Space Invaders")
  10.  
  11. #Draw border
  12. border_pen = turtle.Turtle()
  13. border_pen.hideturtle()
  14. border_pen.speed(0)
  15. border_pen.color("white")
  16. border_pen.penup()
  17. border_pen.setposition(-300,-300)
  18. border_pen.pendown()
  19. border_pen.pensize(3)
  20. #Loop for drawing
  21. for side in range(4):
  22. #Forward 600 steps
  23. border_pen.fd(600)
  24. #Turn 90 degrees
  25. border_pen.lt(90)
  26.  
  27.  
  28. #Create the player
  29. player = turtle.Turtle()
  30. player.color("blue")
  31. player.shape("triangle")
  32. player.penup()
  33. player.speed(0)
  34. player.setposition(0, -250)
  35. player.setheading(90)
  36.  
  37. playerspeed = 15
  38.  
  39. #Create the enemy
  40. enemy = turtle.Turtle()
  41. enemy.color("Red")
  42. enemy.shape ("circle")
  43. enemy.penup()
  44. enemy.speed(0)
  45. enemy.setposition(-200, 250)
  46.  
  47. enemyspeed = 20
  48.  
  49. #Create the weapon
  50. bullet = turtle.Turtle()
  51. bullet.color("yellow")
  52. bullet.shape("triangle")
  53. bullet.penup()
  54. bullet.speed(0)
  55. bullet.setheading (90)
  56. bullet.shapesize(0.5, 0.5)
  57.  
  58.  
  59.  
  60. bulletspeed = 20
  61.  
  62.  
  63.  
  64. #Define bullet states
  65. #ready - ready to fire
  66. #fire - bullet if firing
  67. bulletstate = "ready"
  68.  
  69.  
  70. #Move the player left and right
  71. def move_left():
  72. #Get the player's x coords
  73. x = player.xcor()
  74. x-= playerspeed
  75. #Setting left boundary
  76. if x < -280:
  77. x=-280
  78. player.setx(x)
  79.  
  80. def move_right():
  81. x = player.xcor()
  82. x+= playerspeed
  83. if x > 280:
  84. x = 280
  85. player.setx(x)
  86.  
  87. def fire_bullet():
  88. #Declare bulletstate as a global if it needs changed
  89. global bulletstate
  90. if bulletstate == "ready":
  91. bulletstate = "fire"
  92.  
  93.  
  94.  
  95. def isCollision(t1, t2):
  96. distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
  97. if distance < 15:
  98. return True
  99. else:
  100. return False
  101.  
  102.  
  103. #Create keyboard bindings
  104. turtle.listen()
  105. #When pressed left, call function move left
  106. turtle.onkey(move_left, "Left")
  107. turtle.onkey(move_right, "Right")
  108. turtle.onkey(fire_bullet, "space")
  109.  
  110. #Main game loop
  111. while True:
  112. if bulletstate == "ready":
  113. bullet.hideturtle()
  114. #Move the bullet to above player
  115. x = player.xcor()
  116. y = player.ycor()
  117. bullet.setposition(x, y+10)
  118.  
  119. #Move the enemy
  120. x = enemy.xcor()
  121. x += enemyspeed
  122. enemy.setx(x)
  123.  
  124. #Move the enemy back and down
  125. if enemy.xcor() > 280:
  126. y = enemy.ycor()
  127. #Bring enemy closer to the player
  128. y -= 50
  129. enemy.sety(y)
  130. #When the enemy is touching the right wall, change enemyspeed to
  131. #enemyspeed * -1, (2*-1 is -2) so that it will go the other way.
  132. enemyspeed *= -1
  133.  
  134. if enemy.xcor() < -280:
  135. y = enemy.ycor()
  136. y-= 40
  137. enemy.sety(y)
  138. enemyspeed *= -1
  139.  
  140. #Shoot the bullet
  141. if bulletstate == "fire":
  142. bullet.showturtle()
  143. y = bullet.ycor()
  144. y += bulletspeed
  145. bullet.sety(y)
  146.  
  147. #Check bullet if touching top
  148. if bullet.ycor() > 275:
  149. bullet.hideturtle()
  150. #Move the bullet to above player
  151. x = player.xcor()
  152. y = player.ycor()
  153. bullet.setposition(x, y+10)
  154. bulletstate = "ready"
  155.  
  156. #Check for contact with enemy
  157. if isCollision(bullet, enemy):
  158. #Reset the bullet
  159. bullet.hideturtle()
  160. bulletstate = "ready"
  161. x = player.xcor()
  162. y = player.ycor()
  163. bullet.setposition (x, y+10)
  164. #Reset the enemy
  165. enemy.setposition (-200, 250)
  166.  
  167. #If enemy touches player
  168. if isCollision(player, enemy):
  169. player.hideturtle()
  170. enemy.hideturtle()
  171. print("Game over :(")
  172. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement