Advertisement
scormier416

Space Invaders

May 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. # space Invaders
  2. # Set up screen
  3. import turtle
  4. import math
  5. import random
  6.  
  7. # set up the screen
  8. wn = turtle.Screen()
  9. wn.bgcolor("black")
  10. wn.title("Space Invaders")
  11.  
  12. # Draw border
  13. border_pen = turtle.Turtle()
  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. for side in range(4):
  21.     border_pen.fd(600)
  22.     border_pen.lt(90)
  23. border_pen.hideturtle()
  24.  
  25. # Create player
  26. player = turtle.Turtle()
  27. player.color("blue")
  28. player.shape("triangle")
  29. player.penup()
  30. player.speed(0)
  31. player.setposition(0,-250)
  32. player.setheading(90)
  33.  
  34. playerspeed = 15
  35.  
  36.  
  37.  
  38. # choose number of enemies
  39. number_of_enemies = 5
  40. # create an empty list
  41. enemies = []
  42.  
  43. # add enemies to the list
  44. for i in range(number_of_enemies):
  45.     enemies.append(turtle.Turtle())
  46.  
  47.  
  48. for enemy in enemies:
  49.     enemy.color("red")
  50.     enemy.shape("circle")
  51.     enemy.penup()
  52.     enemy.speed(0)
  53.     x = random.randint(-200, 200)
  54.     y = random.randint(100,250)
  55.     enemy.setposition(x, y)
  56.  
  57. enemyspeed = 2
  58.  
  59.  
  60.  
  61.  
  62. # Create bullet
  63. bullet = turtle.Turtle()
  64. bullet.color("yellow")
  65. bullet.shape("triangle")
  66. bullet.penup()
  67. bullet.speed(0)
  68. bullet.setheading(90)
  69. bullet.shapesize(0.5, 0.5)
  70. bullet.hideturtle()
  71.  
  72. bulletspeed = 20
  73.  
  74. # Define bullet state
  75. # Ready - ready to fire
  76. # Fire - bullet is firing
  77.  
  78. bulletstate = "ready"
  79.  
  80.  
  81.  
  82.  
  83. # Control player
  84. def move_left():
  85.     x = player.xcor()
  86.     x -= playerspeed
  87.     if x < -280:
  88.         x = -280
  89.     player.setx(x)
  90.  
  91. def move_right():
  92.     x = player.xcor()
  93.     x += playerspeed
  94.     if x > 280:
  95.         x = 280
  96.     player.setx(x)
  97.  
  98. def fire_bullet():
  99.     # Declare bulletstate as a global if it needs to be changed
  100.     global bulletstate
  101.     if bulletstate == "ready":
  102.         bulletstate = "fire"
  103.     # Move bullet to just above the player
  104.     x = player.xcor()
  105.     y = player.ycor() + 10
  106.     bullet.setposition(x, y)
  107.     bullet.showturtle()
  108.  
  109. def isCollision(t1, t2):
  110.     distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
  111.     if distance < 15:
  112.         return True
  113.     else:
  114.         return False
  115.  
  116.  
  117.  
  118. # Keyboard Bindings
  119. wn.listen()
  120. wn.onkey(move_left, "Left")
  121. wn.onkey(move_right, "Right")
  122. wn.onkey(fire_bullet, "space")
  123.  
  124.  
  125.  
  126.  
  127. # Main game loop
  128. while True:
  129.  
  130.     for enemy in enemies:
  131.         # Move the enemy
  132.         x = enemy.xcor()
  133.         x += enemyspeed
  134.         enemy.setx(x)
  135.  
  136.         # Move the enemy back and down
  137.         if enemy.xcor() > 280:
  138.             y = enemy.ycor()
  139.             y -= 40
  140.             enemyspeed *= -1
  141.             enemy.sety(y)
  142.  
  143.  
  144.         if enemy.xcor() < -280:
  145.             y = enemy.ycor()
  146.             y -= 40
  147.             enemyspeed *= -1
  148.             enemy.sety(y)
  149.  
  150.  
  151.         # check for a collision between the bullet and the enemy
  152.         if isCollision(bullet, enemy):
  153.             # Reset the bullet
  154.             bullet.hideturtle()
  155.             bulletstate = "ready"
  156.             bullet.setposition(0, -400)
  157.             # Reset the enemy
  158.             enemy.setposition(-200, 250)
  159.  
  160.         if isCollision(player, enemy):
  161.             player.hideturtle()
  162.             enemy.hideturtle()
  163.  
  164.             print("Game Over")
  165.             break
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.         # Move the bullet
  174.         if bulletstate == "fire":
  175.             y = bullet.ycor()
  176.             y += bulletspeed
  177.             bullet.sety(y)
  178.  
  179.  
  180.         # Check to see if the bullet has gone to the top
  181.         if bullet.ycor() > 275:
  182.             bullet.hideturtle()
  183.             bulletstate = "ready"
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. turtle.done()
  205. input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement