|
| 1 | +import turtle |
| 2 | + |
| 3 | +# Create screen |
| 4 | +from turtle import Turtle |
| 5 | + |
| 6 | +sc = turtle.Screen() |
| 7 | +sc.title("Pong game") |
| 8 | +sc.bgcolor("white") |
| 9 | +sc.setup(width=1000, height=600) |
| 10 | + |
| 11 | +# Left paddle |
| 12 | +left_pad = turtle.Turtle() |
| 13 | +left_pad.speed(0) |
| 14 | +left_pad.shape("square") |
| 15 | +left_pad.color("black") |
| 16 | +left_pad.shapesize(stretch_wid=6, stretch_len=2) |
| 17 | +left_pad.penup() |
| 18 | +left_pad.goto(-400, 0) |
| 19 | + |
| 20 | +# Right paddle |
| 21 | +right_pad = turtle.Turtle() |
| 22 | +right_pad.speed(0) |
| 23 | +right_pad.shape("square") |
| 24 | +right_pad.color("black") |
| 25 | +right_pad.shapesize(stretch_wid=6, stretch_len=2) |
| 26 | +right_pad.penup() |
| 27 | +right_pad.goto(400, 0) |
| 28 | + |
| 29 | +# Ball of circle shape |
| 30 | +hit_ball = turtle.Turtle() |
| 31 | +hit_ball.speed(40) |
| 32 | +hit_ball.shape("circle") |
| 33 | +hit_ball.color("blue") |
| 34 | +hit_ball.penup() |
| 35 | +hit_ball.goto(0, 0) |
| 36 | +hit_ball.dx = 5 |
| 37 | +hit_ball.dy = -5 |
| 38 | + |
| 39 | +# Create screen |
| 40 | +sc = turtle.Screen() |
| 41 | +sc.title("Pong game") |
| 42 | +sc.bgcolor("white") |
| 43 | +sc.setup(width=1000, height=600) |
| 44 | + |
| 45 | +# Left paddle |
| 46 | +left_pad = turtle.Turtle() |
| 47 | +left_pad.speed(0) |
| 48 | +left_pad.shape("square") |
| 49 | +left_pad.color("black") |
| 50 | +left_pad.shapesize(stretch_wid=6, stretch_len=2) |
| 51 | +left_pad.penup() |
| 52 | +left_pad.goto(-400, 0) |
| 53 | + |
| 54 | +# Right paddle |
| 55 | +right_pad = turtle.Turtle() |
| 56 | +right_pad.speed(0) |
| 57 | +right_pad.shape("square") |
| 58 | +right_pad.color("black") |
| 59 | +right_pad.shapesize(stretch_wid=6, stretch_len=2) |
| 60 | +right_pad.penup() |
| 61 | +right_pad.goto(400, 0) |
| 62 | + |
| 63 | +# Ball of circle shape |
| 64 | +hit_ball: Turtle = turtle.Turtle() |
| 65 | +hit_ball.speed(40) |
| 66 | +hit_ball.shape("circle") |
| 67 | +hit_ball.color("blue") |
| 68 | +hit_ball.penup() |
| 69 | +hit_ball.goto(0, 0) |
| 70 | +hit_ball.dx = 5 |
| 71 | +hit_ball.dy = -5 |
| 72 | + |
| 73 | +# Initialize the score |
| 74 | +left_player = 0 |
| 75 | +right_player = 0 |
| 76 | + |
| 77 | +# Displays the score |
| 78 | +sketch = turtle.Turtle() |
| 79 | +sketch.speed(0) |
| 80 | +sketch.color("blue") |
| 81 | +sketch.penup() |
| 82 | +sketch.hideturtle() |
| 83 | +sketch.goto(0, 260) |
| 84 | +sketch.write("Left_player : 0 Right_player: 0", |
| 85 | + align="center", font=("Courier", 24, "normal")) |
| 86 | + |
| 87 | + |
| 88 | +# Functions to move paddle vertically |
| 89 | +def paddleaup(): |
| 90 | + y = left_pad.ycor() |
| 91 | + y += 20 |
| 92 | + left_pad.sety(y) |
| 93 | + |
| 94 | + |
| 95 | +def paddleadown(): |
| 96 | + y = left_pad.ycor() |
| 97 | + y -= 20 |
| 98 | + left_pad.sety(y) |
| 99 | + |
| 100 | + |
| 101 | +def paddlebup(): |
| 102 | + y = right_pad.ycor() |
| 103 | + y += 20 |
| 104 | + right_pad.sety(y) |
| 105 | + |
| 106 | + |
| 107 | +def paddlebdown(): |
| 108 | + y = right_pad.ycor() |
| 109 | + y -= 20 |
| 110 | + right_pad.sety(y) |
| 111 | + |
| 112 | + |
| 113 | +# Keyboard bindings |
| 114 | +sc.listen() |
| 115 | +sc.onkeypress(paddleaup, "e") |
| 116 | +sc.onkeypress(paddleadown, "x") |
| 117 | +sc.onkeypress(paddlebup, "Up") |
| 118 | +sc.onkeypress(paddlebdown, "Down") |
| 119 | + |
| 120 | +while True: |
| 121 | + sc.update() |
| 122 | + |
| 123 | + hit_ball.setx(hit_ball.xcor() + hit_ball.dx) |
| 124 | + hit_ball.sety(hit_ball.ycor() + hit_ball.dy) |
| 125 | + |
| 126 | + # Checking borders |
| 127 | + if hit_ball.ycor() > 280: |
| 128 | + hit_ball.sety(280) |
| 129 | + hit_ball.dy *= -1 |
| 130 | + |
| 131 | + if hit_ball.ycor() < -280: |
| 132 | + hit_ball.sety(-280) |
| 133 | + hit_ball.dy *= -1 |
| 134 | + |
| 135 | + if hit_ball.xcor() > 500: |
| 136 | + hit_ball.goto(0, 0) |
| 137 | + hit_ball.dy *= -1 |
| 138 | + left_player += 1 |
| 139 | + sketch.clear() |
| 140 | + sketch.write("Left_player : {} Right_player: {}".format( |
| 141 | + left_player, right_player), align="center", |
| 142 | + font=("Courier", 24, "normal")) |
| 143 | + |
| 144 | + if hit_ball.xcor() < -500: |
| 145 | + hit_ball.goto(0, 0) |
| 146 | + hit_ball.dy *= -1 |
| 147 | + right_player += 1 |
| 148 | + sketch.clear() |
| 149 | + sketch.write("Left_player : {} Right_player: {}".format( |
| 150 | + left_player, right_player), align="center", |
| 151 | + font=("Courier", 24, "normal")) |
| 152 | + |
| 153 | + # Paddle ball collision |
| 154 | + if ((hit_ball.xcor() > 360 and |
| 155 | + hit_ball.xcor() < 370) and |
| 156 | + (hit_ball.ycor() < right_pad.ycor() + 40 and |
| 157 | + hit_ball.ycor() > right_pad.ycor() - 40)): |
| 158 | + hit_ball.setx(360) |
| 159 | + hit_ball.dx *= -1 |
| 160 | + |
| 161 | + if ((hit_ball.xcor() < -360 and |
| 162 | + hit_ball.xcor() > -370) and |
| 163 | + (hit_ball.ycor() < left_pad.ycor() + 40 and |
| 164 | + hit_ball.ycor() > left_pad.ycor() - 40)): |
| 165 | + hit_ball.setx(-360) |
| 166 | + hit_ball.dx *= -1 |
0 commit comments