Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. import turtle
  2. import time
  3.  
  4. delay = 0.1
  5.  
  6. # setup screen
  7. wn = turtle.Screen()
  8. wn.title("Shooter Co-op")
  9. wn.bgcolor("black")
  10. wn.setup(width=600, height=600)
  11. wn.tracer(0)
  12.  
  13. head = turtle.Turtle()
  14. head.speed(0.1)
  15. head.shape("square")
  16. head.color("blue")
  17. head.penup()
  18. head.goto(0, -200)
  19. head.direction = "stop"
  20.  
  21. head2 = turtle.Turtle()
  22. head2.speed(0.1)
  23. head2.shape("square")
  24. head2.color("yellow")
  25. head2.penup()
  26. head2.goto(0, 200)
  27. head2.direction = "stop"
  28.  
  29. new_wall = turtle.Turtle()
  30. new_wall.speed(0)
  31. new_wall.color("grey")
  32. new_wall.goto(-300, 0)
  33. new_wall.shape("triangle")
  34.  
  35. new_wall2 = turtle.Turtle()
  36. new_wall2.speed(0)
  37. new_wall2.color("grey")
  38. new_wall2.shape("triangle")
  39. new_wall2.goto(300, 0)
  40.  
  41. bullet = turtle.Turtle()
  42. bullet.speed(0)
  43. bullet.shape("triangle")
  44. bullet.setheading(90)
  45. bullet.color("red")
  46. bullet.hideturtle()
  47. bullet.penup()
  48. bulletspeed = 30
  49. bullet.shapesize(0.5, 0.5)
  50.  
  51. bullet2 = turtle.Turtle()
  52. bullet2.speed(0)
  53. bullet2.shape("triangle")
  54. bullet2.setheading(270)
  55. bullet2.color("red")
  56. bullet2.hideturtle()
  57. bullet2.penup()
  58. bullet2speed = - 30
  59. bullet2.shapesize(0.5, 0.5)
  60.  
  61. bullet2state = "ready"
  62.  
  63.  
  64. def fire_bullet2():
  65.     global bullet2state
  66.     if bullet2state == "ready":
  67.         bullet2state = "fire"
  68.         x = head2.xcor()
  69.         y = head2.ycor() - 10
  70.         bullet2.setposition(x, y)
  71.         bullet2.showturtle()
  72.  
  73.  
  74. wn.listen()
  75. wn.onkeypress(fire_bullet2, "space")
  76.  
  77. bulletstate = "ready"
  78.  
  79. def fire_bullet():
  80.     global bulletstate
  81.     if bulletstate == "ready":
  82.         bulletstate = "fire"
  83.         x = head.xcor()
  84.         y = head.ycor() + 10
  85.         bullet.setposition(x, y)
  86.         bullet.showturtle()
  87.  
  88. wn.listen()
  89. wn.onkeypress(fire_bullet, "p")
  90.  
  91. def go_up():
  92.     head.direction = "up"
  93.  
  94.  
  95. def go_down():
  96.     head.direction = "down"
  97.  
  98.  
  99. def go_left():
  100.     head.direction = "left"
  101.  
  102.  
  103. def go_right():
  104.     head.direction = "right"
  105.  
  106.  
  107. def move():
  108.     if head.direction == "up":
  109.         y = head.ycor()
  110.         head.sety(y + 20)
  111.  
  112.     if head.direction == "down":
  113.         y = head.ycor()
  114.         head.sety(y - 20)
  115.  
  116.     if head.direction == "left":
  117.         x = head.xcor()
  118.         head.setx(x - 20)
  119.  
  120.     if head.direction == "right":
  121.         x = head.xcor()
  122.         head.setx(x + 20)
  123.  
  124.     wn.listen()
  125.     wn.onkeypress(go_up, "i")
  126.     wn.onkeypress(go_down, "k")
  127.     wn.onkeypress(go_left, "j")
  128.     wn.onkeypress(go_right, "l")
  129.  
  130.  
  131. def go_up1():
  132.     head2.direction = "up"
  133.  
  134.  
  135. def go_down1():
  136.     head2.direction = "down"
  137.  
  138.  
  139. def go_left1():
  140.     head2.direction = "left"
  141.  
  142.  
  143. def go_right1():
  144.     head2.direction = "right"
  145.  
  146.  
  147. def move1():
  148.     if head2.direction == "up":
  149.         y = head2.ycor()
  150.         head2.sety(y + 20)
  151.  
  152.     if head2.direction == "down":
  153.         y = head2.ycor()
  154.         head2.sety(y - 20)
  155.  
  156.     if head2.direction == "left":
  157.         x = head2.xcor()
  158.         head2.setx(x - 20)
  159.  
  160.     if head2.direction == "right":
  161.         x = head2.xcor()
  162.         head2.setx(x + 20)
  163.  
  164.     wn.listen()
  165.     wn.onkeypress(go_up1, "w")
  166.     wn.onkeypress(go_down1, "s")
  167.     wn.onkeypress(go_left1, "a")
  168.     wn.onkeypress(go_right1, "d")
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. while True:
  176.         wn.update()
  177.  
  178.         move()
  179.  
  180.         move1()
  181.  
  182.         time.sleep(delay)
  183.         if bulletstate == "fire":
  184.             y = bullet.ycor()
  185.             y += bulletspeed
  186.             bullet.sety(y)
  187.  
  188.         if bullet.ycor() > 300:
  189.             bullet.hideturtle()
  190.             bulletstate = "ready"
  191.  
  192.         if head.ycor() > 0:
  193.             head.hideturtle()
  194.  
  195.         if head2.ycor() < 0:
  196.             head2.hideturtle()
  197.  
  198.         if head.ycor() > 0:
  199.             head.hideturtle()
  200.  
  201.         if bullet.distance(head2) < 20:
  202.             head2.hideturtle()
  203.  
  204.         if bullet2state == "fire":
  205.             y = bullet2.ycor()
  206.             y += bullet2speed
  207.             bullet2.sety(y)
  208.  
  209.         if bullet2.ycor() < -300:
  210.             bullet2.hideturtle()
  211.             bullet2state = "ready"
  212.  
  213.         if bullet2.distance(head) < 20:
  214.             head.hideturtle()
  215.  
  216. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement