Space invaders game using python

Yahya Gok
3 min readMar 23, 2020
https://www.brusheezy.com/brushes/10498-space-invaders

Space Invaders (Japanese: スペースインベーダー, Hepburn: Supēsu Inbēdā) is a 1978 arcade game created by Tomohiro Nishikado. It was manufactured and sold by Taito in Japan, and licensed in the United States by the Midway division of Bally. Within the shooter genre, Space Invaders was the first fixed shooter and set the template for the shoot ’em up genre. The goal is to defeat wave after wave of descending aliens with a horizontally moving laser to earn as many points as possible.

Space Invaders was an immediate commercial success; by 1982, it had grossed $3.8 billion,with a net profit of $450 million, making it the best-selling video game and highest-grossing “entertainment product” at the time. Adjusted for inflation, the many versions of the game are estimated to have grossed over $13 billion in total revenue as of 2016, making it the highest-grossing video game of all time.(1)

we will be creating a game using python 3.8(that was the latest while I was coding)and python’s turtle module. All the codes are taken from
Christian Thompson’s you tube video , I will share the link at the bottom.


import turtle
import winsound
import math
import random
#set up screen
wn= turtle.Screen()
wn.bgcolor("black")
wn.title("Space invaders")
wn.bgpic("Space_invaders_background.gif")
#Register the shapes
turtle.register_shape("invader.gif") #this is pic.of invader
turtle.register_shape("player.gif") # this is pic.of player

#Draw border
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
#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
player= turtle.Turtle()
player.color("blue")
player.shape("player.gif")
player.penup()
player.speed(0)
player.setposition(0,-250)
player.setheading(90)

playerspeed = 15

enemyspeed = 2
#Choose a number of enemies
number_of_enemies = 5
#Create an empty list of enemies
enemies = []



#Add enemies to the list
for i in range(number_of_enemies):
# Create the enemy
enemies.append(turtle.Turtle())
for enemy in enemies:
enemy.color("red")
enemy.shape("invader.gif")
enemy.penup()
enemy.speed(0)
x= random.randint(-200,200)
y= random.randint(100,250)
enemy.setposition(x,y)



#Create the player's bullet
bullet = turtle.Turtle()
bullet.color("yellow")
bullet.shape("triangle")
bullet.penup()
bullet.speed(0)
bullet.setheading(90)
bullet.shapesize(0.5,0.5)
bullet.hideturtle()

bulletspeed = 70

#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 need changed
global bulletstate
if bulletstate == "ready":
winsound.PlaySound("laser.wav", winsound.SND_ASYNC)
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 < 15:
return True
else:
return False




#Create keyboard bindings
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(fire_bullet, "space")


#Main game loop
while True:

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:
#Move all the enemies down
for e in enemies:
y = e.ycor()
y -= 30
e.sety(y)
enemyspeed *= -1
#Change enemy direction
if enemy.xcor() < -280:
#Move all enemies down
for e in enemies:
y = e.ycor()
y -= 30
e.sety(y)
enemyspeed *= -1
# check for a collision between bullet and enemy
if isCollision(bullet,enemy):
winsound.PlaySound("explosion.wav", winsound.SND_ASYNC)
# Reset the bullet
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0, -340)
# reset the enemy
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)
#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):
winsound.PlaySound("explosion.wav", winsound.SND_ASYNC)
player.hideturtle()
enemy.hideturtle()
print("Game Over")
break



#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"
while True:
wn.update()

references

(1) https://en.wikipedia.org/wiki/Space_Invaders

Christian Thompson’s you-tube channel

https://www.youtube.com/channel/UC2vm-0XX5RkWCXWwtBZGOXg

--

--