← 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)

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
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.shapesize(0.5,0.5)
bullet.hideturtle()

bulletspeed = 30

#Define bullet state
#fire - bullet is firing

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

```.css-19sk4h4{position:relative;}.css-1bu6gr6{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:relative;}.css-1n2m10r{padding:var(--space-8);border-radius:var(--border-radius-4);background-color:var(--background-higher);}.css-1hwur6u{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;padding:var(--space-8);border-radius:var(--border-radius-4);background-color:var(--background-higher);}.css-1ubbl1f{padding:var(--space-4);padding-left:var(--space-4);padding-right:var(--space-2);font-family:var(--font-family-code);font-size:14px;line-height:var(--line-height-small);overflow-x:auto;word-break:break-word;white-space:break-spaces;overflow-wrap:anywhere;}```#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()

#Check for a collision between enemy and bullet
if isCollision(bullet,enemy):
#Rest the bullet
bullet.hideturtle()
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