Share your repls and programming experiences

← Back to all posts
Space Shooter

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)

``````    #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

``````