Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #Space Invaders - Part 5
  2.  
  3. import turtle
  4. import os
  5. import math
  6. import random
  7.  
  8. #Set up the screen
  9. wn = turtle.Screen()
  10. wn.bgcolor("black")
  11. wn.title("Space Invaders")
  12.  
  13. #Draw border
  14. border_pen = turtle.Turtle()
  15. border_pen.speed(0)
  16. border_pen.color("white")
  17. border_pen.penup()
  18. border_pen.setposition(-300,-300)
  19. border_pen.pendown()
  20. border_pen.pensize(3)
  21. for side in range(4):
  22. border_pen.fd(600)
  23. border_pen.lt(90)
  24. border_pen.hideturtle()
  25.  
  26. #Create the player turtle
  27. player = turtle.Turtle()
  28. player.color("blue")
  29. player.shape("triangle")
  30. player.penup()
  31. player.speed(0)
  32. player.setposition(0, -250)
  33. player.setheading(90)
  34. playerspeed = 15
  35.  
  36. number_of_enemies = 5
  37. enemies = []
  38.  
  39. for i in range(number_of_enemies):
  40. enemies.append(turtle.Turtle())
  41.  
  42. for enemy in enemies:
  43. # Create an enemy player turtle
  44. enemy.color("yellow")
  45. enemy.shape("circle")
  46. enemy.penup()
  47. enemy.speed(0)
  48.  
  49. x = random.randint(-200, 200)
  50. y = random.randint(100, 250)
  51.  
  52. enemy.setposition(x, y)
  53.  
  54. enemyspeed = 2
  55.  
  56. #Create the player's bullet
  57. bullet = turtle.Turtle()
  58. bullet.color("yellow")
  59. bullet.shape("triangle")
  60. bullet.penup()
  61. bullet.speed(0)
  62. bullet.setheading(90)
  63. bullet.shapesize(0.5, 0.5)
  64. bullet.hideturtle()
  65.  
  66. bulletspeed = 20
  67.  
  68. #Define bullet state
  69. #ready - ready to fire
  70. #fire - bullet is firing
  71. bulletstate = "ready"
  72.  
  73.  
  74. #Move the player left and right
  75. def move_left():
  76. x = player.xcor()
  77. x -= playerspeed
  78. if x < -280:
  79. x = - 280
  80. player.setx(x)
  81.  
  82. def move_right():
  83. x = player.xcor()
  84. x += playerspeed
  85. if x > 280:
  86. x = 280
  87. player.setx(x)
  88.  
  89. def fire_bullet():
  90. #Declare bulletstate as a global if it needs changed
  91. global bulletstate
  92. if not bullet.isvisible():
  93. bulletstate = "fire"
  94. #Move the bullet to the just above the player
  95. x = player.xcor()
  96. y = player.ycor() + 10
  97. bullet.setposition(x, y)
  98. bullet.showturtle()
  99.  
  100. def isCollision(t1, t2):
  101. distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
  102. if distance < 15:
  103. return True
  104. else:
  105. return False
  106. #Create keyboard bindings
  107. wn.listen()
  108. turtle.onkey(move_left, "Left")
  109. turtle.onkey(move_right, "Right")
  110. turtle.onkey(fire_bullet, "space")
  111.  
  112. #Main game loop
  113. while True:
  114. for enemy in enemies:
  115. #Move the enemy
  116. x = enemy.xcor()
  117. x += enemyspeed
  118. enemy.setx(x)
  119.  
  120. #Move the enemy back and down
  121. if enemy.xcor() > 280:
  122. y = enemy.ycor()
  123. y -= 40
  124. enemyspeed *= -1
  125. enemy.sety(y)
  126.  
  127. if enemy.xcor() < -280:
  128. y = enemy.ycor()
  129. y -= 40
  130. enemyspeed *= -1
  131. enemy.sety(y)
  132.  
  133. #Move the bullet
  134. if bullet.isvisible():
  135. y = bullet.ycor()
  136. y += bulletspeed
  137. bullet.sety(y)
  138.  
  139. #Check to see if the bullet has gone to the top
  140. if bullet.ycor() > 275:
  141. bullet.hideturtle()
  142. bulletstate = "ready"
  143.  
  144. #Check for a collision between the bullet and the enemy
  145. if isCollision(bullet, enemy):
  146. #Reset the bullet
  147. bullet.hideturtle()
  148. bulletstate = "ready"
  149. bullet.setposition(0, -400)
  150. #Reset the enemy
  151. enemy.setposition(0, 250)
  152.  
  153. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement