Skip to content
← Back to Community
Space Shooter
Profile icon
Tharindu_3064

As students it was hard but we finally managed to get it working. As a duo, we are honestly proud!!!

#Space Shooter 1.5
#By Tharindu Namal & Aakef Ahamed
import turtle
import os
import math

#Setting Bg
wn = turtle.Screen()
wn.title("Space Shooter by Oblivion Entertainment")
wn.bgcolor("Black")
#Draw game boundary
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pensize(3)
border_pen.pendown()
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)

border_pen.hideturtle()
#Creating Player
player = turtle.Turtle()
player.color("blue")
player.shape("triangle")
player.speed(0)
player.penup()
player.setposition(0,-250)
player.setheading(90)

playerspeed = 19

#Moving and keybindig

def go_left():
x = player.xcor()
x -= playerspeed
if x < -280:
x = - 280
player.setx(x)

turtle.listen()
turtle.onkey(go_left,"Left")
def go_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":
bulletstate = "fire"
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 < 15:
return True
else:
return False

turtle.listen()
turtle.onkey(go_right,"Right")
turtle.onkey(fire_bullet,"space")
playerspeed = 28
playerspeed = 24
playerspeed = 19

#Create the enemy
enemy = turtle.Turtle()
enemy.color("red")
enemy.shape("circle")
enemy.penup()
enemy.speed(0)
enemy.setposition(-200,250)
enemyspeed = 2

#Creating weapon
bullet = turtle.Turtle()
bullet.color("yellow")
bullet.shape("triangle")
bullet.speed(0)
bullet.penup()
bullet.setheading(90)
bullet.shapesize(0.5,0.5)
bullet.hideturtle()

bulletspeed = 30

#Define bullet state
#ready - ready to fire
#fire - bullet is firing
bulletstate = "ready"

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

#Move the enemy back and down if enemy.xcor() > 280: enemyspeed *= -1 y = enemy.ycor() y -= 40 enemy.sety(y) if enemy.xcor() < -280: y = enemy.ycor() y -= 40 enemyspeed *= -1 enemy.sety(y) #Move the bullet if bulletstate == "fire": y = bullet.ycor() y += bulletspeed bullet.sety(y) #Check to see if bullet is gon if bullet.ycor() > 275: bullet.hideturtle() bulletstate = "ready" #Check for a collision between enemy and bullet if isCollision(bullet,enemy): #Rest the bullet bullet.hideturtle() bulletstate = "ready" bullet.setposition(0,-400) #Rest enemy enemy.setposition(-200,250) if isCollision(player,enemy): player.hideturtle() enemy.hideturtle() print ("GAME OVER") break
Voters
Profile icon
Tharindu_3064
Comments
hotnewtop
Profile icon
katyadee

Hey! No early submissions, so moving this to Share.

Profile icon
OctavicSensei

You should place it in a REPL and post the link, then we can see your awesome game in action :D