Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import turtle
  2. from random import *
  3.  
  4. wn = turtle.Screen()
  5. wn.title("Pong en classe de Première")
  6. wn.bgcolor("black")
  7. wn.setup(width=800, height=600)
  8. wn.tracer(0)
  9.  
  10. # Function
  11. def paddle_a_up():
  12. y = paddle_a.ycor()
  13. y += 20
  14. paddle_a.sety(y)
  15.  
  16. def paddle_a_down():
  17. y = paddle_a.ycor()
  18. y -= 20
  19. paddle_a.sety(y)
  20.  
  21. def paddle_b_up():
  22. y = paddle_b.ycor()
  23. y += 20
  24. paddle_b.sety(y)
  25.  
  26. def paddle_b_down():
  27. y = paddle_b.ycor()
  28. y -= 20
  29. paddle_b.sety(y)
  30.  
  31. # Keyboard binding
  32. wn.listen()
  33. wn.onkeypress(paddle_a_up, "z")
  34. wn.onkeypress(paddle_a_down, "s")
  35. wn.onkeypress(paddle_b_up, "Up")
  36. wn.onkeypress(paddle_b_down, "Down")
  37.  
  38. # Joueur A
  39. paddle_a = turtle.Turtle()
  40. paddle_a.speed(1000)
  41. paddle_a.shape("square")
  42. paddle_a.color("white")
  43. paddle_a.shapesize(stretch_wid=5, stretch_len=1)
  44. paddle_a.penup()
  45. paddle_a.goto(-350, 0)
  46.  
  47. # Joueur B
  48. paddle_b = turtle.Turtle()
  49. paddle_b.speed(0)
  50. paddle_b.shape("square")
  51. paddle_b.color("white")
  52. paddle_b.shapesize(stretch_wid=5, stretch_len=1)
  53. paddle_b.penup()
  54. paddle_b.goto(350, 0)
  55.  
  56. # Balle
  57. colours = ["cyan", "purple", "white", "blue"]
  58. shape = ["square", "square", "turtle", "classic"]
  59. for i in range(0, 5):
  60. y = randint(-150, 150)
  61. G = [-0.15, 0.15]
  62. ball = turtle.Turtle()
  63. ball.speed(0)
  64. ball.shape(shape[randint(0, 3)])
  65. ball.color(colours[randint(0, 3)])
  66. ball.penup()
  67. ball.goto(0, y)
  68. ball.dx = G[randint(0, 1)]
  69. ball.dy = G[randint(0, 1)]
  70.  
  71. # Pen
  72. pen = turtle.Turtle()
  73. pen.speed(0)
  74. pen.color("white")
  75. pen.penup()
  76. pen.hideturtle()
  77. pen.goto(0, 260)
  78. pen.write("Joueur A : 0 Joueur B : 0", align="center", font=("Courier", 18, "normal"))
  79.  
  80. # Score
  81. score_a = 0
  82. score_b = 0
  83.  
  84. # Main game loop
  85. while(True):
  86. wn.update()
  87.  
  88. # Move the ball
  89. ball.setx(ball.xcor() + ball.dx)
  90. ball.sety(ball.ycor() + ball.dy)
  91.  
  92. # Border checking
  93. if(ball.ycor() > 290):
  94. ball.sety(290)
  95. ball.dy *= -1
  96.  
  97. if(ball.ycor() < -290):
  98. ball.sety(-290)
  99. ball.dy *= -1
  100.  
  101. if(ball.xcor() > 390):
  102. ball.dx *= -1
  103. score_a += 1
  104. pen.clear()
  105. pen.write("Joueur A : " + str(score_a) + " Joueur B : " + str(score_b), align="center", font=("Courier", 18, "normal"))
  106.  
  107. if(ball.xcor() < -390):
  108. ball.dx *= -1
  109. score_b += 1
  110. pen.clear()
  111. pen.write("Joueur A : " + str(score_a) + " Joueur B : " + str(score_b), align="center", font=("Courier", 18, "normal"))
  112.  
  113. # Paddle and ball collisions
  114. if(ball.xcor() > 340 and ball.xcor() < 350 and ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() - 40):
  115. ball.dx *= -1
  116.  
  117. if(ball.xcor() < -340 and ball.xcor() > -350 and ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() - 40):
  118. ball.dx *= -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement