Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import random
- import winsound
- # variables
- playing = True
- state = "play"
- level = 0
- playerSpeed = 25
- bulletSpeed = 25
- bulletDelay = 3
- alienDX = 25
- alienDY = 25
- playerHealth = 50
- currentBulletDelay = bulletDelay
- enemyBulletDelay = 10
- gameTimerDelay = 20
- playerpic = "Player.gif"
- alien1 = "Alien 1.gif"
- alien2 = "Alien 2.gif"
- alien3 = "Alien 3.gif"
- alien4 = "Alien 4.gif"
- alien5 = "Alien 5.gif"
- alien6 = "Alien 6.gif"
- playerBullets = []
- spawnedEnemies = []
- enemyBullets = []
- levelenemies = [
- [2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # level 1
- [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1], # level 2
- [3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1], # level 3
- [4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1], # level 4
- [5, 5, 5, 4, 4, 5, 5, 3, 3, 4, 4, 5, 5, 3, 4], # level 5
- [6, 6, 6, 5, 5, 5, 5, 4, 3, 4, 4, 5, 6 ,2 ,1] # level 6
- ]
- # screen
- wn = turtle.Screen()
- wn.title("Space Invaders")
- wn.bgcolor("black")
- wn.setup(width=600, height=800)
- wn.addshape(playerpic)
- wn.addshape(alien1)
- wn.addshape(alien2)
- wn.addshape(alien3)
- wn.addshape(alien4)
- wn.addshape(alien5)
- wn.addshape(alien6)
- # Player
- player = turtle.Turtle()
- player.speed(0)
- player.shape(playerpic)
- player.color("red")
- player.penup()
- player.sety(-350)
- # pen
- pen = turtle.Turtle()
- pen.speed(0)
- pen.penup()
- pen.hideturtle()
- pen.goto(0, 0)
- # Functions
- def PlayerMoveLeft():
- if state == "play":
- x = player.xcor()
- x -= playerSpeed
- if x < -300:
- x = -300
- player.setx(x)
- def PlayerMoveRight():
- if state == "play":
- x = player.xcor()
- x += playerSpeed
- if x > 300:
- x = 300
- player.setx(x)
- def SpawnPlayerBullet():
- global currentBulletDelay
- if state == "play":
- if currentBulletDelay > bulletDelay:
- winsound.PlaySound("playerLaser.wav", winsound.SND_ASYNC)
- currentBulletDelay = 0
- newbullet = turtle.Turtle()
- newbullet.penup()
- newbullet.setx(player.xcor())
- newbullet.sety(player.ycor() + 10)
- newbullet.speed(0)
- newbullet.shape("circle")
- newbullet.color("white")
- newbullet.shapesize(stretch_wid=0.2, stretch_len=0.2)
- playerBullets.append(newbullet)
- def SpawnLevelEnemies(levelNumber):
- aliensToSpawn = levelenemies[levelNumber]
- i = 0
- for y in range(3):
- for x in range(5):
- newalien = turtle.Turtle()
- newalien.penup()
- newalien.setpos(x * 50 - 100, y * -50 + 350)
- newalien.speed(0)
- newalien.shape("Alien {}.gif".format(aliensToSpawn[i]))
- newalien.color("red")
- newalien.hp = aliensToSpawn[i]
- spawnedEnemies.append(newalien)
- i += 1
- def SpawnEnemyBullet(x, y):
- global enemyBulletDelay
- if enemyBulletDelay > 5:
- winsound.PlaySound("alienLasers.wav", winsound.SND_ASYNC)
- enemyBulletDelay = 0
- newbullet = turtle.Turtle()
- newbullet.penup()
- newbullet.setx(x)
- newbullet.sety(y)
- newbullet.speed(0)
- newbullet.shape("circle")
- newbullet.color("red")
- newbullet.shapesize(stretch_wid=0.2, stretch_len=0.2)
- enemyBullets.append(newbullet)
- wn.listen()
- wn.onkeypress(PlayerMoveLeft, "a")
- wn.onkeypress(PlayerMoveRight, "s")
- wn.onkeypress(SpawnPlayerBullet, "space")
- SpawnLevelEnemies(level)
- while playing:
- wn.update()
- currentBulletDelay += 1
- enemyBulletDelay += 1
- # if the game is in play mode
- if state == "play":
- # move bullets
- for bullet in playerBullets:
- y = bullet.ycor()
- y += bulletSpeed
- bullet.sety(y)
- if y >= 400:
- # remove bullet
- playerBullets.remove(bullet)
- bullet.reset()
- for bullet in enemyBullets:
- y = bullet.ycor()
- y -= bulletSpeed
- bullet.sety(y)
- if y <= -400:
- # remove bullet
- enemyBullets.remove(bullet)
- bullet.reset()
- i = random.randrange(0, len(spawnedEnemies))
- selectedEnemy = spawnedEnemies[i]
- SpawnEnemyBullet(selectedEnemy.xcor(), selectedEnemy.ycor())
- # move all aliens to the side
- edge = False
- for alien in spawnedEnemies:
- x = alien.xcor()
- x += alienDX
- if x <= -250 or x >= 250:
- edge = True
- alien.setx(x)
- # if an alien hits the edge. Move all aliens down and reverse direction
- if edge:
- alienDX *= -1
- for alien in spawnedEnemies:
- y = alien.ycor()
- y -= alienDY
- alien.sety(y)
- # if an alien reaches the bottom of the screen then game over
- if y <= -380:
- state = "lose"
- pen.color("red")
- pen.write("GAMEOVER", align="center", font=("Courier", 50, "normal"))
- # compare bullet position to enemies
- for alien in spawnedEnemies:
- for bullet in playerBullets:
- if alien.xcor() - 20 < bullet.xcor() < alien.xcor() +20 and alien.ycor() - 20 < bullet.ycor() < alien.ycor() + 20:
- playerBullets.remove(bullet)
- bullet.reset()
- bullet.clear()
- bullet.hideturtle()
- alien.hp -= 1
- if alien.hp == 0:
- winsound.PlaySound("alienExplosion.wav", winsound.SND_ASYNC)
- if spawnedEnemies.__contains__(alien):
- spawnedEnemies.remove(alien)
- alien.reset()
- alien.clear()
- alien.hideturtle()
- else:
- winsound.PlaySound("hitHurt.wav", winsound.SND_ASYNC)
- # compare enemy bullet to player
- for bullet in enemyBullets:
- if player.xcor()-20 < bullet.xcor() < player.xcor() + 20 and -350 < bullet.ycor() < -340:
- winsound.PlaySound("playerExplosion.wav", winsound.SND_NOWAIT)
- state = "lose"
- pen.color("red")
- pen.write("YOU DIED", align="center", font=("Courier", 30, "normal"))
- winsound.PlaySound("You Lose.wav", winsound.SND_NOWAIT)
- # if all enemies are dead
- if len(spawnedEnemies) == 0:
- state = "win"
- pen.color("green")
- pen.write("YOU WIN", align="center", font=("Courier", 30, "normal"))
- if state == "win":
- gameTimerDelay -= 1
- if gameTimerDelay <= 0:
- level += 1
- if level == len(levelenemies):
- playing = False
- winsound.PlaySound("You Win.wav", winsound.SND_NOWAIT)
- else:
- winsound.PlaySound("Level Transition.wav", winsound.SND_ASYNC)
- for bullet in enemyBullets:
- bullet.reset()
- bullet.clear()
- bullet.hideturtle()
- for bullet in playerBullets:
- bullet.reset()
- bullet.clear()
- bullet.hideturtle()
- pen.clear()
- pen.write("Level {}".format(level + 1), align="center", font=("Courier", 30, "normal"))
- enemyBullets = []
- playerBullets = []
- SpawnLevelEnemies(level)
- state = "play"
- pen.clear()
- gameTimerDelay = 20
- if state == "lose":
- gameTimerDelay -= 2
- if gameTimerDelay <= 0:
- playing = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement