Advertisement
here2share

# turtle_Space_Invaders.py

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