daily pastebin goal
73%
SHARE
TWEET

first game

emil20013 Dec 15th, 2018 (edited) 157 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import turtle
  2. import os
  3. import time
  4.  
  5. #set up the screen
  6. wn = turtle.Screen()
  7. wn.bgcolor("black") #background colour
  8. wn.title("PvP 1.0") #title on the screen
  9.  
  10. #Draw Border
  11. border_pen = turtle.Turtle()
  12. border_pen.speed(0)
  13. border_pen.color("white")
  14. border_pen.penup()
  15. border_pen.setposition(-300,-300)
  16. border_pen.pensize(3)
  17. border_pen.pendown()
  18. for side in range(4):
  19.     border_pen.fd(600)
  20.     border_pen.lt(90)
  21. border_pen.hideturtle()
  22.  
  23. #Variables
  24. winner = '0'
  25. B1D = '0'
  26.  
  27. #player1
  28. player1 = turtle.Turtle()
  29. player1.color("blue")
  30. player1.shape("triangle")
  31. player1.penup()
  32. player1.speed(0)
  33. player1.setposition(-280, -280)
  34. player1.setheading(90)
  35.  
  36. player1speed = 15
  37.  
  38. #player2
  39. player2 = turtle.Turtle()
  40. player2.color("purple")
  41. player2.shape("square")
  42. player2.penup()
  43. player2.speed(0)
  44. player2.setposition(280, -280)
  45. player2.setheading(0)
  46.  
  47. #create enemy
  48. enemy = turtle.Turtle()
  49. enemy.color("red")
  50. enemy.shape("circle")
  51. enemy.penup()
  52. enemy.speed(0)
  53. enemy.setposition(-200, 250)
  54. enemy.hideturtle()
  55.  
  56. #bullet for P1
  57. bullet1 = turtle.Turtle()
  58. bullet1.color("red")
  59. bullet1.shape("square")
  60. bullet1.penup()
  61. bullet1.speed(0)
  62. bullet1.shapesize(0.5, 0.5)
  63. bullet1.setheading(0)
  64. bullet1.hideturtle()
  65.  
  66. bulletspeed1 = 30
  67. bulletstate1 = "ready"
  68.  
  69.  
  70. #score system!
  71. scoreP1 = 100
  72.  
  73. scoreP1_pen = turtle.Turtle()
  74. scoreP1_pen.speed(0)
  75. scoreP1_pen.color("white")
  76. scoreP1_pen.penup()
  77. scoreP1_pen.setposition(-290, 300)
  78. scorestring = "P1 HP: %s" %scoreP1
  79. scoreP1_pen.write(scorestring, False, align="left", font=("Arail", 18, "normal"))
  80. scoreP1_pen.hideturtle()
  81.  
  82. scoreP2 = 100
  83.  
  84. scoreP2_pen = turtle.Turtle()
  85. scoreP2_pen.speed(0)
  86. scoreP2_pen.color("white")
  87. scoreP2_pen.penup()
  88. scoreP2_pen.setposition(160, 300)
  89. scorestring = "P2 HP: %s" %scoreP2
  90. scoreP2_pen.write(scorestring, False, align="left", font=("Arail", 18, "normal"))
  91. scoreP2_pen.hideturtle()
  92.  
  93.  
  94. #player1 movement
  95. def upP1():
  96.     player1.sety(player1.ycor()+20)
  97.     player1.setheading(90)
  98. def downP1():
  99.     player1.sety(player1.ycor()-20)
  100.     player1.setheading(30)
  101. def leftP1():
  102.     player1.setx(player1.xcor()-20)
  103.     player1.setheading(180)
  104. def rightP1():
  105.     player1.setx(player1.xcor()+20)
  106.     player1.setheading(0)
  107.  
  108. #player2 movement
  109. def upP2():
  110.     player2.sety(player2.ycor()+20)
  111.     player2.setheading(90)
  112. def downP2():
  113.     player2.sety(player2.ycor()-20)
  114.     player2.setheading(30)
  115. def leftP2():
  116.     player2.setx(player2.xcor()-20)
  117.     player2.setheading(180)
  118. def rightP2():
  119.     player2.setx(player2.xcor()+20)
  120.     player2.setheading(0)
  121.    
  122. def firebullet1():
  123.     global bulletstate1
  124.     global B1D
  125.     if bulletstate1 == 'ready':
  126.         bulletstate1 = 'fire'
  127.         if player1.heading() == 90:
  128.             bullet1.setheading(90)
  129.             XP1 = player1.xcor()
  130.             YP1 = player1.ycor() + 10
  131.             bullet1.setposition(XP1, YP1)
  132.             bullet1.showturtle()
  133.             B1D = 'up'
  134.  
  135.         if player1.heading() == 30:
  136.             bullet1.setheading(30)
  137.             XP1 = player1.xcor()
  138.             YP1 = player1.ycor() - 10
  139.             bullet1.setposition(XP1, YP1)
  140.             bullet1.showturtle()
  141.             B1D = 'down'
  142.  
  143.         if player1.heading() == 180:
  144.             bullet1.setheading(180)
  145.             XP1 = player1.xcor() - 10
  146.             YP1 = player1.ycor()
  147.             bullet1.setposition(XP1, YP1)
  148.             bullet1.showturtle()
  149.             B1D = 'left'
  150.  
  151.         if player1.heading() == 0:
  152.             bullet1.setheading(0)
  153.             XP1 = player1.xcor() + 10
  154.             YP1 = player1.ycor()
  155.             bullet1.setposition(XP1, YP1)
  156.             bullet1.showturtle()
  157.             B1D = 'right'
  158.  
  159. def P1heading():
  160.     global p1heading
  161.     p1heading = player1.heading
  162.     if p1heading == 90:
  163.         print("WIP")
  164.        
  165.  
  166. #if collided
  167. def collisionCheck_P1_P2():    
  168.     global crashed
  169.     crashed = False
  170.     player1X = player1.xcor()
  171.     player1Y = player1.ycor()
  172.     player2X = player2.xcor()
  173.     player2Y = player2.ycor()
  174.     player1Pos = (int(player1X), int(player1Y))
  175.     player2Pos = (int(player2X), int(player2Y))
  176.     if player1Pos == player2Pos:
  177.         crashed = True
  178.     return crashed
  179.  
  180. def collisionCheck_B1_P2():
  181.     global crashed
  182.     crashed = False
  183.     player2X = player2.xcor()
  184.     player2Y = player2.ycor()
  185.     bullet1X = bullet1.xcor()
  186.     bullet1Y = bullet1.yxor()
  187.     bullet1Pos = (int)bullet1X), (int)bullet1Y)
  188.     player2Pos = (int)player2X), (int)player2Y)
  189.     if bullet1Pos == player2Pos:
  190.         crashed = 'B1P2'
  191.     return crashed
  192.  
  193. turtle.listen()
  194. #P1
  195. turtle.onkey(leftP1, "Left")
  196. turtle.onkey(rightP1, "Right")
  197. turtle.onkey(upP1, "Up")
  198. turtle.onkey(downP1, "Down")
  199. turtle.onkey(firebullet1, "e")
  200. #P2
  201. #turtle.onkey(firebullet2, "space")
  202. turtle.onkey(leftP2, "a")
  203. turtle.onkey(rightP2, "d")
  204. turtle.onkey(upP2, "w")
  205. turtle.onkey(downP2, "s")
  206.  
  207. #main game loop
  208. while True:
  209.  
  210.     #Keep system going
  211.     enemy.setposition(0, 0)
  212.  
  213.     collisionCheck_P1_P2()
  214.     if crashed == True:
  215.         #P1
  216.         scoreP1 -= 25
  217.         scorestring = "P1 HP: %s" %scoreP1
  218.         scoreP1_pen.clear() #UPDATE SCORE
  219.         scoreP1_pen.write(scorestring, False, align="left", font=("Arail", 18, "normal"))
  220.         #P2
  221.         scoreP2 -= 25
  222.         scorestring = "P1 HP: %s" %scoreP1
  223.         scoreP2_pen.clear() #UPDATE SCORE
  224.         scoreP2_pen.write(scorestring, False, align="left", font=("Arail", 18, "normal"))
  225.         print("connected")
  226.         player1.setposition(-280, -280)
  227.         player2.setposition(280, -280)
  228.     else:
  229.         print("not connected!")
  230.    
  231.     collisionCheck_B1_P2()
  232.     if crashed == 'B2P2':
  233.         scoreP1 -= 10
  234.         scorestring = "P1 HP: %s" %scoreP1
  235.         scoreP1_pen.clear() #UPDATE SCORE
  236.         scoreP1_pen.write(scorestring, False, align="left", font=("Arail", 18, "normal"))
  237.  
  238.  
  239.     if scoreP1 == 0 or scoreP2 == 0:
  240.         print("Game Over!")
  241.         if scoreP1 == 0:
  242.             winner = 'Player 2'
  243.             print("The winner is " + winner + "!")
  244.             break
  245.             turtle.done()
  246.         else:
  247.             winner = 'Player 1'
  248.             print("The winner is " + winner + "!")
  249.             break
  250.             turtle.done()
  251.    
  252.     if bulletstate1 == 'fire':
  253.         if B1D == 'up':
  254.             YP1 = bullet1.ycor()
  255.             YP1 += bulletspeed1
  256.             bullet1.sety(YP1)
  257.  
  258.         if B1D == 'down':
  259.             YP1 = bullet1.ycor()
  260.             YP1 -= bulletspeed1
  261.             bullet1.sety(YP1)
  262.  
  263.         if B1D == 'left':
  264.             XP1 = bullet1.xcor()
  265.             XP1 -= bulletspeed1
  266.             bullet1.setx(XP1)
  267.  
  268.         if B1D == 'right':
  269.             XP1 = bullet1.xcor()
  270.             XP1 += bulletspeed1
  271.             bullet1.setx(XP1)
  272.    
  273.     if bullet1.ycor() > 275:
  274.         bullet1.hideturtle()
  275.         bulletstate1 = 'ready'
  276.  
  277.     if bullet1.ycor() < -275:
  278.         bullet1.hideturtle()
  279.         bulletstate1 = 'ready'
  280.  
  281.     if bullet1.xcor() > 275:
  282.         bullet1.hideturtle()
  283.         bulletstate1 = 'ready'
  284.  
  285.     if bullet1.xcor() < -275:
  286.         bullet1.hideturtle()
  287.         bulletstate1 = 'ready'
  288.  
  289.  
  290.  
  291.    
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.    
  300.  
  301.  
  302.  
  303.  
  304. \
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top