Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import os
- import time
- import math
- #Set up the screen
- wn = turtle.Screen()
- wn.bgcolor("black")
- wn.title("Space Invaders")
- #Draw border
- border_pen = turtle.Turtle()
- border_pen.hideturtle()
- border_pen.speed(0)
- border_pen.color("white")
- border_pen.penup()
- border_pen.setposition(-300,-300)
- border_pen.pendown()
- border_pen.pensize(3)
- #Loop for drawing
- for side in range(4):
- #Forward 600 steps
- border_pen.fd(600)
- #Turn 90 degrees
- border_pen.lt(90)
- #Create the player
- player = turtle.Turtle()
- player.color("blue")
- player.shape("triangle")
- player.penup()
- player.speed(0)
- player.setposition(0, -250)
- player.setheading(90)
- playerspeed = 15
- #Create the enemy
- enemy = turtle.Turtle()
- enemy.color("Red")
- enemy.shape ("circle")
- enemy.penup()
- enemy.speed(0)
- enemy.setposition(-200, 250)
- enemyspeed = 20
- #Create the weapon
- bullet = turtle.Turtle()
- bullet.color("yellow")
- bullet.shape("triangle")
- bullet.penup()
- bullet.speed(0)
- bullet.setheading (90)
- bullet.shapesize(0.5, 0.5)
- bulletspeed = 20
- #Define bullet states
- #ready - ready to fire
- #fire - bullet if firing
- bulletstate = "ready"
- #Move the player left and right
- def move_left():
- #Get the player's x coords
- x = player.xcor()
- x-= playerspeed
- #Setting left boundary
- 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 needs changed
- global bulletstate
- if bulletstate == "ready":
- bulletstate = "fire"
- 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()
- #When pressed left, call function move left
- turtle.onkey(move_left, "Left")
- turtle.onkey(move_right, "Right")
- turtle.onkey(fire_bullet, "space")
- #Main game loop
- while True:
- if bulletstate == "ready":
- bullet.hideturtle()
- #Move the bullet to above player
- x = player.xcor()
- y = player.ycor()
- bullet.setposition(x, y+10)
- #Move the enemy
- x = enemy.xcor()
- x += enemyspeed
- enemy.setx(x)
- #Move the enemy back and down
- if enemy.xcor() > 280:
- y = enemy.ycor()
- #Bring enemy closer to the player
- y -= 50
- enemy.sety(y)
- #When the enemy is touching the right wall, change enemyspeed to
- #enemyspeed * -1, (2*-1 is -2) so that it will go the other way.
- enemyspeed *= -1
- if enemy.xcor() < -280:
- y = enemy.ycor()
- y-= 40
- enemy.sety(y)
- enemyspeed *= -1
- #Shoot the bullet
- if bulletstate == "fire":
- bullet.showturtle()
- y = bullet.ycor()
- y += bulletspeed
- bullet.sety(y)
- #Check bullet if touching top
- if bullet.ycor() > 275:
- bullet.hideturtle()
- #Move the bullet to above player
- x = player.xcor()
- y = player.ycor()
- bullet.setposition(x, y+10)
- bulletstate = "ready"
- #Check for contact with enemy
- if isCollision(bullet, enemy):
- #Reset the bullet
- bullet.hideturtle()
- bulletstate = "ready"
- x = player.xcor()
- y = player.ycor()
- bullet.setposition (x, y+10)
- #Reset the enemy
- enemy.setposition (-200, 250)
- #If enemy touches player
- if isCollision(player, enemy):
- player.hideturtle()
- enemy.hideturtle()
- print("Game over :(")
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement