In this game, you are a spaceship with an alien spaceship trying to eliminate you, you have to shoot the alien ship with you lasers. In order to shoot lasers, you click E. In order to move, use you right and left arrow keys. If the spaceship gets to your level, you lose!

In order to play the game, copy the code below and paste it into you python file. After that, run the code and it should take you to the same screen as the video above!

Copy the code for FREE:!

import turtle
import random

# Set up the screen
screen = turtle.Screen()
screen.setup(width=800, height=600)
screen.bgcolor("dark green")
screen.title("MP1")

# Create the spaceship
spaceship = turtle.Turtle()
spaceship.shape("square")
spaceship.color("gold")
spaceship.penup()
spaceship.speed(50)
spaceship.setposition(0, -250)
spaceship.setheading(140)

spaceship_speed = 50

# Create the laser
laser = turtle.Turtle()
laser.shape("arrow")
laser.color("red")
laser.penup()
laser.speed(50)
laser.setheading(90)
laser.shapesize(1.25, 1.25)
laser.hideturtle()

laser_speed = 25
laser_state = "ready"

# Create the alien
alien = turtle.Turtle()
alien.shape("circle")
alien.color("white")
alien.penup()
alien.speed(0)
alien.setposition(random.randint(-380, 380), 250)

alien_speed = 5

# Functions to move the spaceship
def move_left():
x = spaceship.xcor()
x -= spaceship_speed
if x < -380:
x = -380
spaceship.setx(x)

def move_right():
x = spaceship.xcor()
x += spaceship_speed
if x > 380:
x = 380
spaceship.setx(x)

# Function to shoot the laser
def shoot_laser():
global laser_state
if laser_state == "ready":
laser_state = "fire"
x = spaceship.xcor()
y = spaceship.ycor() + 10
laser.setposition(x, y)
laser.showturtle()

# Keyboard bindings
turtle.listen()
turtle.onkeypress(move_left, "Left")
turtle.onkeypress(move_right, "Right")
turtle.onkeypress(shoot_laser, "e")

# Main game loop
while True:
# Move the alien
x = alien.xcor()
x += alien_speed
alien.setx(x)

# Check for border collision
if alien.xcor() > 380 or alien.xcor() < -380:
alien_speed *= -1
y = alien.ycor()
y -= 40
alien.sety(y)

# Move the laser
if laser_state == "fire":
y = laser.ycor()
y += laser_speed
laser.sety(y)

# Check for laser collision with alien
if laser.distance(alien) < 15:
laser.hideturtle()
laser_state = "ready"
laser.setposition(0, -400)
alien.setposition(random.randint(-380, 380), 250)

# Check for spaceship collision with alien
if spaceship.distance(alien) < 15:
spaceship.hideturtle()
alien.hideturtle()
print("Game Over")
break

# Check if laser is out of bounds
if laser.ycor() > 275:
laser.hideturtle()
laser_state = "ready"

turtle.done()

Leave a Reply

Your email address will not be published. Required fields are marked *