Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import turtle
- import math
- #background color
- wn = turtle.Screen()
- wn.bgcolor("black")
- wn.title("sheild")
- #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.forward(600)
- border_pen.left(90)
- border_pen.hideturtle()
- #set the score 0
- score = 0
- #draw 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()
- #player
- player = turtle.Turtle()
- player.color("blue")
- player.shape("circle")
- player.penup()
- player.speed(0)
- player.setposition(0, -0)
- player.setheading(90)
- #enemy
- enemy= turtle.Turtle()
- enemy.color("red")
- enemy.shape("square")
- enemy.penup()
- enemy.speed(0)
- x = (0)
- y = (250)
- enemy.setposition(x, y)
- #enemy2
- enemy2= turtle.Turtle()
- enemy2.color("red")
- enemy2.shape("square")
- enemy2.penup()
- enemy2.speed(0)
- x = (0)
- y = (-250)
- enemy2.setposition(x, y)
- #create the players sheild
- sheild = turtle.Turtle()
- sheild.color("red")
- sheild.shape("square")
- sheild.penup()
- sheild.speed(0)
- sheild.setposition(0, 20)
- sheild.setheading(90)
- sheild.shapesize(2, 0.5)
- sheildspeed = 40
- #create the players 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 = 10
- #define bullet state
- bulletstate = "ready"
- #create the enemys bullet
- ebullet = turtle.Turtle()
- ebullet.color("yellow")
- ebullet.shape("triangle")
- ebullet.penup()
- ebullet.speed(0)
- ebullet.setheading(-90)
- ebullet.shapesize(0.5, 0.5)
- ebullet.hideturtle()
- ebulletspeed = 5
- #define bullet state
- ebulletstate = "ready"
- #create the players wbullet
- wbullet = turtle.Turtle()
- wbullet.color("yellow")
- wbullet.shape("triangle")
- wbullet.penup()
- wbullet.speed(0)
- wbullet.setheading(90)
- wbullet.shapesize(0.5, 0.5)
- wbullet.hideturtle()
- wbulletspeed = 5
- #define bullet state
- wbulletstate = "ready"
- #create the players bullet 2
- qbullet = turtle.Turtle()
- qbullet.color("yellow")
- qbullet.shape("triangle")
- qbullet.penup()
- qbullet.speed(0)
- qbullet.setheading(-90)
- qbullet.shapesize(0.5, 0.5)
- qbullet.hideturtle()
- qbulletspeed = 10
- #define bullet state
- qbulletstate = "ready"
- #sheild movement down
- def move_down():
- y = sheild.ycor()
- y -= sheildspeed
- if y < -20:
- y = - 20
- sheild.sety(y)
- #sheild movement up
- def move_up():
- y = sheild.ycor()
- y += sheildspeed
- if y > 20:
- y = 20
- sheild.sety(y)
- #bullet movement
- def fire_bullet():
- global bulletstate
- if bulletstate == "ready":
- bulletstate = "fire"
- #set bullet position
- x = player.xcor()
- y = player.ycor() +10
- bullet.setposition(x, y)
- bullet.showturtle()
- #wbullet movement
- def fire_wbullet():
- global wbulletstate
- if wbulletstate == "ready":
- wbulletstate = "fire"
- #set bullet position
- x = enemy2.xcor()
- y = enemy2.ycor() +10
- wbullet.setposition(x, y)
- wbullet.showturtle()
- #ebullet movement
- def fire_ebullet():
- global ebulletstate
- if ebulletstate == "ready":
- ebulletstate = "fire"
- #set bullet position
- x = enemy.xcor()
- y = enemy.ycor() +10
- ebullet.setposition(x, y)
- ebullet.showturtle()
- #qbullet movement
- def fire_qbullet():
- global qbulletstate
- if qbulletstate == "ready":
- qbulletstate = "fire"
- #set bullet position
- x = player.xcor()
- y = player.ycor() +10
- qbullet.setposition(x, y)
- qbullet.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
- #bindings
- turtle.listen()
- turtle.onkey(fire_bullet, "Left")
- turtle.onkey(fire_ebullet, "w")
- turtle.onkey(move_down, "Down")
- turtle.onkey(move_up, "Up")
- turtle.onkey(fire_qbullet, "Right")
- turtle.onkey(fire_wbullet, "s")
- #main game loop
- while True:
- player.fd(0)
- r = random.randint(0, 1000)
- if r > 995:
- fire_ebullet()
- elif r < 5:
- fire_wbullet()
- #move the bullet
- if bulletstate == "fire":
- y = bullet.ycor()
- y += bulletspeed
- bullet.sety(y)
- #move the bullet
- if wbulletstate == "fire":
- y = wbullet.ycor()
- y += wbulletspeed
- wbullet.sety(y)
- #move the bullet
- if ebulletstate == "fire":
- y = ebullet.ycor()
- y -= ebulletspeed
- ebullet.sety(y)
- #move the qbullet
- if qbulletstate == "fire":
- y = qbullet.ycor()
- y -= qbulletspeed
- qbullet.sety(y)
- #check for a collision with enemy
- if isCollision(bullet, enemy):
- #reset bullet
- bullet.hideturtle()
- bulletstate = "ready"
- bullet.setposition(0, -400)
- #update the score
- score += 10
- scorestring = "Score: %s" %score
- score_pen.clear()
- score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
- #check for a collision with enemy2
- if isCollision(wbullet, player):
- #reset wbullet
- wbullet.hideturtle()
- wbulletstate = "ready"
- wbullet.setposition(0, -400)
- #update the score
- score -= 10
- scorestring = "Score: %s" %score
- score_pen.clear()
- score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
- #check for a collision with enemy
- if isCollision(qbullet, enemy2):
- #reset qbullet
- qbullet.hideturtle()
- qbulletstate = "ready"
- qbullet.setposition(0, -400)
- #update the score
- score += 10
- scorestring = "Score: %s" %score
- score_pen.clear()
- score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
- #player collision
- if isCollision(ebullet, player):
- #reset bullet
- ebullet.hideturtle()
- ebulletstate = "ready"
- ebullet.setposition(0, 250)
- #update the score
- score -= 10
- scorestring = "Score: %s" %score
- score_pen.clear()
- score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
- #sheild collision
- if isCollision(ebullet, sheild):
- #reset bullet
- ebullet.hideturtle()
- ebulletstate = "ready"
- ebullet.setposition(0, 250)
- if isCollision(wbullet, sheild):
- #reset wbullet
- wbullet.hideturtle()
- wbulletstate = "ready"
- wbullet.setposition(0, -250)
- delay = input("Press enter to finish.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement