Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Vooperia Invader
- #Original Code by Christian Thompson
- #Edits by Eric Pham
- import turtle
- import os
- import math
- import random
- import tkinter as tk
- n=-200
- p=-200
- g=200
- r=200
- j=200
- d=0
- a=0
- #Set up the screen
- wn = turtle.Screen()
- wn.bgcolor("black")
- wn.title("Vooperia Invaders")
- wn.bgpic("VooperiaBackground.png")
- #Play Music
- os.system("afplay Vooperian_Anthem.mp3&")
- #Register the shapes
- turtle.register_shape("Yam.gif")
- turtle.register_shape("SpikeDab.gif")
- turtle.register_shape("potato.gif")
- #Set the score to 0
- score = 0
- #Draw the score
- score_pen = turtle.Turtle()
- score_pen.speed(0)
- score_pen.color("white")
- score_pen.penup()
- score_pen.setposition(-290, 280)
- scorestring = "Score: %s" %score
- score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
- score_pen.hideturtle()
- #Create the player turtle
- player = turtle.Turtle()
- player.color("blue")
- player.shape("SpikeDab.gif")
- player.penup()
- player.speed(0)
- player.setposition(0, -250)
- player.setheading(90)
- playerspeed = 15
- #Choose a number of enemies
- number_of_enemies = 23
- #Create an empty list of enemies
- enemies = []
- #Add enemies to list
- for i in range(number_of_enemies):
- #Create the enemy
- enemies.append(turtle.Turtle())
- for enemy in enemies:
- enemy.color("red")
- enemy.shape("Yam.gif")
- enemy.penup()
- enemy.speed(0)
- d= d+1
- if d % 6==0:
- n=p
- j= r-(10*d)
- g=j
- x = n
- y = g
- enemy.setposition(x, y)
- else:
- x = n
- y = g
- enemy.setposition(x, y)
- n= n+80
- enemyspeed = 2
- #Create the player's bullet
- bullet = turtle.Turtle()
- bullet.color("yellow")
- bullet.shape("potato.gif")
- bullet.penup()
- bullet.speed(0)
- bullet.setheading(90)
- bullet.shapesize(0.5, 0.5)
- bullet.hideturtle()
- bulletspeed = 25
- #Define bullet state
- #ready - ready to fire
- #fire - bullet is firing
- bulletstate = "ready"
- #Move the player left and right
- def move_left():
- x = player.xcor()
- x -= playerspeed
- if x < -280:
- x = - 280
- player.setx(x)
- def move_right():
- x = player.xcor()
- x += playerspeed
- if x > 280:
- x = 280
- player.setx(x)
- def fire_bullet():
- #Declare bulletstate as a global if it needs changed
- global bulletstate
- if bulletstate == "ready":
- os.system("afplay laser.wav&")
- bulletstate = "fire"
- #Move the bullet to the just above the player
- x = player.xcor()
- y = player.ycor() +10
- bullet.setposition(x, y)
- bullet.showturtle()
- def isCollision(t1, t2):
- distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
- if distance < 25:
- return True
- else:
- return False
- def mute():
- os.system("killall afplay")
- #Create keyboard bindings
- turtle.listen()
- turtle.onkey(move_left, "Left")
- turtle.onkey(move_right, "Right")
- turtle.onkey(fire_bullet, "space")
- while True:
- m = input("\n")
- if (m =='enter'):
- a=1+a
- else:
- continue
- #Main game loop
- while True:
- char = input("\n")
- if a==0:
- break
- else:
- continue
- for enemy in enemies:
- #Move the enemy
- x = enemy.xcor()
- x += enemyspeed
- enemy.setx(x)
- #Move the enemy back and down
- if enemy.xcor() > 280:
- for e in enemies:
- y = e.ycor()
- y -= 40
- e.sety(y)
- enemyspeed *= -1
- if enemy.xcor() < -280:
- for e in enemies:
- y = e.ycor()
- y -= 40
- e.sety(y)
- enemyspeed *= -1
- #Check for a collision between the bullet and the enemy
- if isCollision(bullet, enemy):
- os.system("afplay WilhelmScream.wav&")
- #Reset the bullet
- bullet.hideturtle()
- bulletstate = "ready"
- bullet.setposition(0, -400)
- #Reset the enemy
- enemies.remove(enemy)
- enemy.hideturtle()
- #Update the score
- score += 10
- scorestring = "Score: %s" %score
- score_pen.clear()
- score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
- if isCollision(player, enemy):
- os.system("afplay OOF.wav&")
- player.hideturtle()
- enemy.hideturtle()
- print("Game Over")
- score_pen.setposition(-300, 0)
- scorestring = "YOU LOSE"
- os.system("killall afplay")
- score_pen.write(scorestring, False, align="left", font=("Arial", 100, "normal"))
- score_pen.hideturtle()
- turtle.done()
- break
- if score == 230:
- print("YOU WON!")
- score_pen.setposition(-300, 0)
- scorestring = "YOU WON!"
- score_pen.write(scorestring, False, align="left", font=("Arial", 100, "normal"))
- score_pen.hideturtle()
- os.system("killall afplay")
- turtle.done()
- break
- if (char == 'ESC'):
- mute()
- #Move the bullet
- if bulletstate == "fire":
- y = bullet.ycor()
- y += bulletspeed
- bullet.sety(y)
- #Check to see if the bullet has gone to the top
- if bullet.ycor() > 275:
- bullet.hideturtle()
- bulletstate = "ready"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement