Advertisement
dronkowitz

Space Invaders.py

Apr 20th, 2022 (edited)
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.84 KB | None | 0 0
  1. import turtle
  2. import random
  3. import winsound
  4.  
  5. # variables
  6. playing = True
  7. state = "play"
  8. level = 0
  9. playerSpeed = 25
  10. bulletSpeed = 25
  11. bulletDelay = 3
  12. alienDX = 25
  13. alienDY = 25
  14. playerHealth = 50
  15. currentBulletDelay = bulletDelay
  16. enemyBulletDelay = 10
  17. gameTimerDelay = 20
  18. playerpic = "Player.gif"
  19. alien1 = "Alien 1.gif"
  20. alien2 = "Alien 2.gif"
  21. alien3 = "Alien 3.gif"
  22. alien4 = "Alien 4.gif"
  23. alien5 = "Alien 5.gif"
  24. alien6 = "Alien 6.gif"
  25. playerBullets = []
  26. spawnedEnemies = []
  27. enemyBullets = []
  28.  
  29. levelenemies = [
  30.     [2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # level 1
  31.     [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1], # level 2
  32.     [3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1], # level 3
  33.     [4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1], # level 4
  34.     [5, 5, 5, 4, 4, 5, 5, 3, 3, 4, 4, 5, 5, 3, 4], # level 5
  35.     [6, 6, 6, 5, 5, 5, 5, 4, 3, 4, 4, 5, 6 ,2 ,1] # level 6
  36. ]
  37.  
  38.  
  39.  
  40. # screen
  41. wn = turtle.Screen()
  42. wn.title("Space Invaders")
  43. wn.bgcolor("black")
  44. wn.setup(width=600, height=800)
  45. wn.addshape(playerpic)
  46. wn.addshape(alien1)
  47. wn.addshape(alien2)
  48. wn.addshape(alien3)
  49. wn.addshape(alien4)
  50. wn.addshape(alien5)
  51. wn.addshape(alien6)
  52.  
  53. # Player
  54. player = turtle.Turtle()
  55. player.speed(0)
  56. player.shape(playerpic)
  57. player.color("red")
  58. player.penup()
  59. player.sety(-350)
  60.  
  61. # pen
  62. pen = turtle.Turtle()
  63. pen.speed(0)
  64. pen.penup()
  65. pen.hideturtle()
  66. pen.goto(0, 0)
  67.  
  68.  
  69. # Functions
  70. def PlayerMoveLeft():
  71.     if state == "play":
  72.         x = player.xcor()
  73.         x -= playerSpeed
  74.         if x < -300:
  75.             x = -300
  76.         player.setx(x)
  77.  
  78.  
  79. def PlayerMoveRight():
  80.     if state == "play":
  81.         x = player.xcor()
  82.         x += playerSpeed
  83.         if x > 300:
  84.             x = 300
  85.         player.setx(x)
  86.  
  87.  
  88. def SpawnPlayerBullet():
  89.     global currentBulletDelay
  90.     if state == "play":
  91.         if currentBulletDelay > bulletDelay:
  92.             winsound.PlaySound("playerLaser.wav", winsound.SND_ASYNC)
  93.             currentBulletDelay = 0
  94.             newbullet = turtle.Turtle()
  95.             newbullet.penup()
  96.             newbullet.setx(player.xcor())
  97.             newbullet.sety(player.ycor() + 10)
  98.             newbullet.speed(0)
  99.             newbullet.shape("circle")
  100.             newbullet.color("white")
  101.             newbullet.shapesize(stretch_wid=0.2, stretch_len=0.2)
  102.             playerBullets.append(newbullet)
  103.  
  104.  
  105. def SpawnLevelEnemies(levelNumber):
  106.     aliensToSpawn = levelenemies[levelNumber]
  107.     i = 0
  108.     for y in range(3):
  109.         for x in range(5):
  110.             newalien = turtle.Turtle()
  111.             newalien.penup()
  112.             newalien.setpos(x * 50 - 100, y * -50 + 350)
  113.             newalien.speed(0)
  114.             newalien.shape("Alien {}.gif".format(aliensToSpawn[i]))
  115.             newalien.color("red")
  116.             newalien.hp = aliensToSpawn[i]
  117.             spawnedEnemies.append(newalien)
  118.             i += 1
  119.  
  120.  
  121. def SpawnEnemyBullet(x, y):
  122.     global enemyBulletDelay
  123.     if enemyBulletDelay > 5:
  124.         winsound.PlaySound("alienLasers.wav", winsound.SND_ASYNC)
  125.         enemyBulletDelay = 0
  126.         newbullet = turtle.Turtle()
  127.         newbullet.penup()
  128.         newbullet.setx(x)
  129.         newbullet.sety(y)
  130.         newbullet.speed(0)
  131.         newbullet.shape("circle")
  132.         newbullet.color("red")
  133.         newbullet.shapesize(stretch_wid=0.2, stretch_len=0.2)
  134.         enemyBullets.append(newbullet)
  135.  
  136.  
  137.  
  138. wn.listen()
  139. wn.onkeypress(PlayerMoveLeft, "a")
  140. wn.onkeypress(PlayerMoveRight, "s")
  141. wn.onkeypress(SpawnPlayerBullet, "space")
  142. SpawnLevelEnemies(level)
  143. while playing:
  144.     wn.update()
  145.     currentBulletDelay += 1
  146.     enemyBulletDelay += 1
  147.  
  148.     # if the game is in play mode
  149.     if state == "play":
  150.         # move bullets
  151.         for bullet in playerBullets:
  152.             y = bullet.ycor()
  153.             y += bulletSpeed
  154.             bullet.sety(y)
  155.             if y >= 400:
  156.                 # remove bullet
  157.                 playerBullets.remove(bullet)
  158.                 bullet.reset()
  159.  
  160.         for bullet in enemyBullets:
  161.             y = bullet.ycor()
  162.             y -= bulletSpeed
  163.             bullet.sety(y)
  164.             if y <= -400:
  165.                 # remove bullet
  166.                 enemyBullets.remove(bullet)
  167.                 bullet.reset()
  168.  
  169.         i = random.randrange(0, len(spawnedEnemies))
  170.         selectedEnemy = spawnedEnemies[i]
  171.         SpawnEnemyBullet(selectedEnemy.xcor(), selectedEnemy.ycor())
  172.         # move all aliens to the side
  173.         edge = False
  174.         for alien in spawnedEnemies:
  175.             x = alien.xcor()
  176.             x += alienDX
  177.             if x <= -250 or x >= 250:
  178.                 edge = True
  179.  
  180.             alien.setx(x)
  181.         # if an alien hits the edge. Move all aliens down and reverse direction
  182.         if edge:
  183.             alienDX *= -1
  184.             for alien in spawnedEnemies:
  185.                 y = alien.ycor()
  186.                 y -= alienDY
  187.                 alien.sety(y)
  188.                 # if an alien reaches the bottom of the screen then game over
  189.                 if y <= -380:
  190.                     state = "lose"
  191.                     pen.color("red")
  192.  
  193.                     pen.write("GAMEOVER", align="center", font=("Courier", 50, "normal"))
  194.  
  195.         # compare bullet position to enemies
  196.         for alien in spawnedEnemies:
  197.             for bullet in playerBullets:
  198.                 if alien.xcor() - 20 < bullet.xcor() < alien.xcor() +20 and alien.ycor() - 20 < bullet.ycor() < alien.ycor() + 20:
  199.  
  200.                     playerBullets.remove(bullet)
  201.                     bullet.reset()
  202.                     bullet.clear()
  203.                     bullet.hideturtle()
  204.                     alien.hp -= 1
  205.                     if alien.hp == 0:
  206.                         winsound.PlaySound("alienExplosion.wav", winsound.SND_ASYNC)
  207.                         if spawnedEnemies.__contains__(alien):
  208.                             spawnedEnemies.remove(alien)
  209.                         alien.reset()
  210.                         alien.clear()
  211.                         alien.hideturtle()
  212.                     else:
  213.                         winsound.PlaySound("hitHurt.wav", winsound.SND_ASYNC)
  214.  
  215.         # compare enemy bullet to player
  216.         for bullet in enemyBullets:
  217.             if player.xcor()-20 < bullet.xcor() < player.xcor() + 20 and -350 < bullet.ycor() < -340:
  218.                 winsound.PlaySound("playerExplosion.wav", winsound.SND_NOWAIT)
  219.                 state = "lose"
  220.  
  221.                 pen.color("red")
  222.  
  223.                 pen.write("YOU DIED", align="center", font=("Courier", 30, "normal"))
  224.                 winsound.PlaySound("You Lose.wav", winsound.SND_NOWAIT)
  225.         # if all enemies are dead
  226.         if len(spawnedEnemies) == 0:
  227.             state = "win"
  228.             pen.color("green")
  229.             pen.write("YOU WIN", align="center", font=("Courier", 30, "normal"))
  230.  
  231.     if state == "win":
  232.         gameTimerDelay -= 1
  233.         if gameTimerDelay <= 0:
  234.             level += 1
  235.             if level == len(levelenemies):
  236.                 playing = False
  237.                 winsound.PlaySound("You Win.wav", winsound.SND_NOWAIT)
  238.             else:
  239.                 winsound.PlaySound("Level Transition.wav", winsound.SND_ASYNC)
  240.                 for bullet in enemyBullets:
  241.                     bullet.reset()
  242.                     bullet.clear()
  243.                     bullet.hideturtle()
  244.                 for bullet in playerBullets:
  245.                     bullet.reset()
  246.                     bullet.clear()
  247.                     bullet.hideturtle()
  248.                 pen.clear()
  249.                 pen.write("Level {}".format(level + 1), align="center", font=("Courier", 30, "normal"))
  250.                 enemyBullets = []
  251.                 playerBullets = []
  252.  
  253.                 SpawnLevelEnemies(level)
  254.                 state = "play"
  255.                 pen.clear()
  256.                 gameTimerDelay = 20
  257.  
  258.     if state == "lose":
  259.         gameTimerDelay -= 2
  260.         if gameTimerDelay <= 0:
  261.             playing = False
  262.  
  263.  
  264.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement