|
| 1 | +import turtle |
| 2 | + |
| 3 | +wn = turtle.Screen() |
| 4 | +wn.title("Pong by @sugashsm") |
| 5 | +wn.bgcolor("black") |
| 6 | +wn.setup(width=800, height=600) |
| 7 | +wn.tracer(0) #Not making update |
| 8 | + |
| 9 | +# Score |
| 10 | +score_a = 0 |
| 11 | +score_b = 0 |
| 12 | + |
| 13 | +# Paddle A |
| 14 | +paddle_a = turtle.Turtle() |
| 15 | +paddle_a.speed(0) |
| 16 | +paddle_a.shape("square") |
| 17 | +paddle_a.color("white") |
| 18 | +paddle_a.shapesize(stretch_wid=5,stretch_len=1) |
| 19 | +paddle_a.penup() |
| 20 | +paddle_a.goto(-350, 0) |
| 21 | + |
| 22 | +# Paddle B |
| 23 | +paddle_b = turtle.Turtle() |
| 24 | +paddle_b.speed(0) |
| 25 | +paddle_b.shape("square") |
| 26 | +paddle_b.color("white") |
| 27 | +paddle_b.shapesize(stretch_wid=5,stretch_len=1) |
| 28 | +paddle_b.penup() |
| 29 | +paddle_b.goto(350, 0) |
| 30 | + |
| 31 | +# Ball |
| 32 | +ball = turtle.Turtle() |
| 33 | +ball.speed(0) |
| 34 | +ball.shape("square") |
| 35 | +ball.color("white") |
| 36 | +ball.penup() |
| 37 | +ball.goto(0, 0) |
| 38 | +ball.dx = 0.1 |
| 39 | +ball.dy = 0.1 |
| 40 | + |
| 41 | +# Points |
| 42 | +pen = turtle.Turtle() |
| 43 | +pen.speed(0) |
| 44 | +pen.shape("square") |
| 45 | +pen.color("white") |
| 46 | +pen.penup() |
| 47 | +pen.hideturtle() |
| 48 | +pen.goto(0, 260) |
| 49 | +pen.write("Player A: 0 Player B: 0", align="center", font=("Courier", 24, "normal")) |
| 50 | + |
| 51 | +# Functions |
| 52 | +def paddle_a_up(): |
| 53 | + y = paddle_a.ycor() |
| 54 | + y += 20 |
| 55 | + paddle_a.sety(y) |
| 56 | + |
| 57 | +def paddle_a_down(): |
| 58 | + y = paddle_a.ycor() |
| 59 | + y -= 20 |
| 60 | + paddle_a.sety(y) |
| 61 | + |
| 62 | +def paddle_b_up(): |
| 63 | + y = paddle_b.ycor() |
| 64 | + y += 20 |
| 65 | + paddle_b.sety(y) |
| 66 | + |
| 67 | +def paddle_b_down(): |
| 68 | + y = paddle_b.ycor() |
| 69 | + y -= 20 |
| 70 | + paddle_b.sety(y) |
| 71 | + |
| 72 | +# Keyboard bindings |
| 73 | +wn.listen() |
| 74 | +wn.onkeypress(paddle_a_up, "w") |
| 75 | +wn.onkeypress(paddle_a_down, "s") |
| 76 | +wn.onkeypress(paddle_b_up, "Up") |
| 77 | +wn.onkeypress(paddle_b_down, "Down") |
| 78 | + |
| 79 | +# Main game loop |
| 80 | +while True: |
| 81 | + wn.update() |
| 82 | + |
| 83 | + # Move the ball |
| 84 | + ball.setx(ball.xcor() + ball.dx) |
| 85 | + ball.sety(ball.ycor() + ball.dy) |
| 86 | + |
| 87 | + # Border checking |
| 88 | + if ball.ycor() > 290: |
| 89 | + ball.sety(290) |
| 90 | + ball.dy *= -1 |
| 91 | + |
| 92 | + |
| 93 | + elif ball.ycor() < -290: |
| 94 | + ball.sety(-290) |
| 95 | + ball.dy *= -1 |
| 96 | + |
| 97 | + if ball.xcor() > 350: |
| 98 | + score_a += 1 |
| 99 | + pen.clear() |
| 100 | + pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal")) |
| 101 | + ball.goto(0, 0) |
| 102 | + ball.dx *= -1 |
| 103 | + |
| 104 | + elif ball.xcor() < -350: |
| 105 | + score_b += 1 |
| 106 | + pen.clear() |
| 107 | + pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal")) |
| 108 | + ball.goto(0, 0) |
| 109 | + ball.dx *= -1 |
| 110 | + |
| 111 | + # Paddle and ball collisions |
| 112 | + if ball.xcor() < -340 and ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() - 50: |
| 113 | + ball.dx *= -1 |
| 114 | + |
| 115 | + |
| 116 | + elif ball.xcor() > 340 and ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() - 50: |
| 117 | + ball.dx *= -1 |
| 118 | + |
| 119 | + |
0 commit comments