daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import turtle
  2. import math
  3. import random
  4. import winsound
  5.  
  6.  
  7. wn = turtle.Screen()
  8. wn.bgcolor("black")
  9. wn.title("Space Invaders")
  10. wn.bgpic("space.gif")
  11.  
  12. turtle.register_shape("invader.gif")
  13. turtle.register_shape("tank.gif")
  14. turtle.register_shape("bullet.gif")
  15.  
  16. borderPen = turtle.Turtle()
  17. borderPen.speed(0)
  18. borderPen.penup()
  19. borderPen.setposition(-300, -300)
  20. borderPen.color("white")
  21. borderPen.pendown()
  22. borderPen.pensize(2)
  23. for side in range(4):
  24.     borderPen.fd(600)
  25.     borderPen.lt(90)
  26. borderPen.hideturtle()
  27.  
  28. score = 0
  29. score_pen = turtle.Turtle()
  30. score_pen.speed(0)
  31. score_pen.color("black")
  32. score_pen.penup()
  33. score_pen.setposition(-425, 200)
  34. scorestring = "Score: %s" %score
  35. score_pen.write(scorestring, False, align="left", font=("Arial", 16, "normal"))
  36. score_pen.hideturtle()
  37.  
  38. player = turtle.Turtle()
  39. player.shape("tank.gif")
  40. player.color("purple")
  41. player.penup()
  42. player.setposition(0, -250)
  43. player.setheading(90)
  44. player.speed()
  45.  
  46. playerspeed = 14
  47.  
  48. #Choose the number of enemies
  49. numberOfEnemies = 6
  50. #Create an empty list of enemies
  51. enemies = []
  52.  
  53. #Add enemies to the list
  54. for i in range(numberOfEnemies):
  55.     enemies.append(turtle.Turtle())
  56.  
  57. for enemy in enemies:
  58.     enemy.color("red")
  59.     enemy.shape("invader.gif")
  60.     enemy.penup()
  61.     enemy.speed(0)
  62.     x = random.randint(-200, 200)
  63.     y = random.randint(100, 250)
  64.     enemy.setposition(x, y)
  65.            
  66. enemyspeed = 3
  67.  
  68. bullet = turtle.Turtle()
  69. bullet.shape("bullet.gif")
  70. bullet.color("pink")
  71. bullet.penup()
  72. bullet.speed()
  73. bullet.shapesize(0.5)
  74. bullet.hideturtle()
  75.  
  76. bulletspeed = 20
  77.  
  78. bulletstate = "ready"
  79.  
  80. def moveLeft():
  81.     x = player.xcor()
  82.     x -= playerspeed
  83.     if x < - 280:
  84.         x = - 280
  85.     player.setx(x)
  86.  
  87. def moveRight():
  88.     x = player.xcor()
  89.     x += playerspeed
  90.     if x > 280:
  91.         x = 280
  92.     player.setx(x)
  93.  
  94. def moveUp():
  95.     y = player.ycor()
  96.     y += playerspeed
  97.     player.sety(y)
  98.  
  99. def fireBullet():
  100.     global bulletstate
  101.     if bulletstate == "ready":
  102.         winsound.PlaySound('laser.wav', winsound.SND_ASYNC)
  103.         bulletstate = "fire"
  104.         x = player.xcor()
  105.         y = player.ycor()
  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. boss_speed = 9
  117.  
  118. class Bosses(turtle.Turtle):
  119.         def __init__(self, spriteshape, color, startx, starty):
  120.                 turtle.Turtle.__init__(self, shape = spriteshape)
  121.                 self.speed(0)
  122.                 self.penup()
  123.                 self.color(color)
  124.                 self.goto(startx, starty)
  125.    
  126.         def move(self):
  127.                 global boss_speed
  128.                
  129.                 x = self.xcor()
  130.                 x += boss_speed
  131.                 self.setx(x)
  132.                 if self.xcor() > 280:
  133.                         y = self.ycor()
  134.                         y -= 50
  135.                         self.sety(y)
  136.                         boss_speed *= -1
  137.                 if self.xcor() < -280:
  138.                         y = self.ycor()
  139.                         y -= 50
  140.                         self.sety(y)
  141.                         boss_speed *= -1
  142.                
  143.            
  144. boss = Bosses("circle", "blue", 250, 250)
  145. boss.hideturtle()
  146.  
  147. turtle.listen()
  148. turtle.onkey(moveLeft, "Left")
  149. turtle.onkey(moveUp, "Up")
  150. turtle.onkey(moveRight, "Right")
  151. turtle.onkey(fireBullet, "space")
  152.  
  153. while True:
  154.     for enemy in enemies:
  155.         x = enemy.xcor()
  156.         x += enemyspeed
  157.         enemy.setx(x)
  158.         if enemy.xcor() > 280:
  159.             for e in enemies:
  160.                 y = e.ycor()
  161.                 y -= 40
  162.                 e.sety(y)
  163.             enemyspeed *= -1
  164.         if enemy.xcor() < -280:
  165.             for e in enemies:
  166.                 y = e.ycor()
  167.                 y -= 40
  168.                 e.sety(y)
  169.             enemyspeed *= -1
  170.         if enemy.ycor() < -280:
  171.             for enemy in enemies:
  172.                 enemy.hideturtle()
  173.             player.hideturtle()
  174.             gameover = turtle.Screen()
  175.             gameover.bgpic('gameover.gif')
  176.             bulletstate = "fire"
  177.             winsound.PlaySound('hyenalaugh.wav', winsound.SND_ASYNC)
  178.             break
  179.                          
  180.         if isCollision(bullet, enemy):
  181.             bullet.hideturtle()
  182.             bulletstate = "ready"
  183.             bullet.setposition(0, -400)
  184.             x = random.randint(-200, 200)
  185.             y = random.randint(100, 250)
  186.             enemy.setposition(x, y)
  187.             score += 10
  188.             scorestring = "Score: %s" %score
  189.             score_pen.clear()
  190.             score_pen.write(scorestring, False, align="left", font=("Arial", 16, "normal"))
  191.             winsound.PlaySound('bomb_cut.wav', winsound.SND_ASYNC)
  192.  
  193.         if isCollision(player, enemy):
  194.             for enemy in enemies:
  195.                 enemy.hideturtle()
  196.             player.hideturtle()
  197.             gameover = turtle.Screen()
  198.             gameover.bgpic('gameover.gif')
  199.             bulletstate = "fire"
  200.             winsound.PlaySound('hyenalaugh.wav', winsound.SND_ASYNC)
  201.             break
  202.  
  203.     if score >= 20:
  204.         boss.move()
  205.  
  206.  
  207.     if bulletstate == "fire":
  208.         y = bullet.ycor()
  209.         y += bulletspeed
  210.         bullet.sety(y)
  211.        
  212.     if bullet.ycor() > 275:
  213.         bullet.hideturtle()
  214.         bulletstate = "ready"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top