|
| 1 | +# A simple Pong game using Python |
| 2 | + |
| 3 | + |
| 4 | +import turtle |
| 5 | +import winsound |
| 6 | + |
| 7 | +wn = turtle.Screen() |
| 8 | +wn.title('Pong Game') |
| 9 | +wn.bgcolor('blue') |
| 10 | +wn.setup(width=800, height=600) |
| 11 | +wn.tracer(0) |
| 12 | + |
| 13 | + |
| 14 | +# Score |
| 15 | + |
| 16 | +score_a = 0 |
| 17 | +score_b = 0 |
| 18 | + |
| 19 | + |
| 20 | +# Paddle A |
| 21 | + |
| 22 | +paddle_a = turtle.Turtle() |
| 23 | +paddle_a.speed(0) |
| 24 | +paddle_a.shape("square") |
| 25 | +paddle_a.color("grey") |
| 26 | +paddle_a.shapesize(stretch_wid=5, stretch_len=1) |
| 27 | +paddle_a.penup() |
| 28 | +paddle_a.goto(-350,0) |
| 29 | + |
| 30 | + |
| 31 | +# Paddle B |
| 32 | + |
| 33 | +paddle_b = turtle.Turtle() |
| 34 | +paddle_b.speed(0) |
| 35 | +paddle_b.shape("square") |
| 36 | +paddle_b.color("grey") |
| 37 | +paddle_b.shapesize(stretch_wid=5, stretch_len=1) |
| 38 | +paddle_b.penup() |
| 39 | +paddle_b.goto(350,0) |
| 40 | + |
| 41 | + |
| 42 | +# Ball |
| 43 | + |
| 44 | +ball = turtle.Turtle() |
| 45 | +ball.speed(0) |
| 46 | +ball.shape("circle") |
| 47 | +ball.color("red") |
| 48 | +ball.penup() |
| 49 | +ball.goto(0,0) |
| 50 | +ball.dx = 0.2 |
| 51 | +ball.dy = 0.2 |
| 52 | + |
| 53 | + |
| 54 | +# Pen |
| 55 | + |
| 56 | +pen = turtle.Turtle() |
| 57 | +pen.speed(0) |
| 58 | +pen.color('White') |
| 59 | +pen.penup() |
| 60 | +pen.hideturtle() |
| 61 | +pen.goto(0, 260) |
| 62 | +pen.write("Player A : 0 Player B : 0", align = 'center', font = ('Courier', 24, 'normal')) |
| 63 | + |
| 64 | + |
| 65 | +# Functions |
| 66 | + |
| 67 | +def paddle_a_up(): |
| 68 | + y = paddle_a.ycor() |
| 69 | + y += 20 |
| 70 | + paddle_a.sety(y) |
| 71 | + |
| 72 | +def paddle_a_down(): |
| 73 | + y = paddle_a.ycor() |
| 74 | + y -= 20 |
| 75 | + paddle_a.sety(y) |
| 76 | + |
| 77 | +def paddle_b_up(): |
| 78 | + y = paddle_b.ycor() |
| 79 | + y += 20 |
| 80 | + paddle_b.sety(y) |
| 81 | + |
| 82 | +def paddle_b_down(): |
| 83 | + y = paddle_b.ycor() |
| 84 | + y -= 20 |
| 85 | + paddle_b.sety(y) |
| 86 | + |
| 87 | + |
| 88 | +# Keyboard binding |
| 89 | + |
| 90 | +wn.listen() |
| 91 | +wn.onkeypress(paddle_a_up, "w") |
| 92 | +wn.onkeypress(paddle_a_down, "s") |
| 93 | +wn.onkeypress(paddle_b_up, "Up") |
| 94 | +wn.onkeypress(paddle_b_down, "Down") |
| 95 | + |
| 96 | + |
| 97 | +# Main game loop |
| 98 | + |
| 99 | +while True: |
| 100 | + |
| 101 | + wn.update() |
| 102 | + |
| 103 | + # Move the ball |
| 104 | + |
| 105 | + ball.setx(ball.xcor() + ball.dx) |
| 106 | + ball.sety(ball.ycor() + ball.dy) |
| 107 | + |
| 108 | + # Border Checking |
| 109 | + |
| 110 | + if ball.ycor() > 290: |
| 111 | + ball.sety(290) |
| 112 | + ball.dy *= -1 |
| 113 | + winsound.PlaySound('bounce.wav', winsound.SND_ASYNC) |
| 114 | + |
| 115 | + if ball.ycor() < -290: |
| 116 | + ball.sety(-290) |
| 117 | + ball.dy *= -1 |
| 118 | + winsound.PlaySound('bounce.wav', winsound.SND_ASYNC) |
| 119 | + |
| 120 | + if ball.xcor() < -390: |
| 121 | + ball.goto(0, 0) |
| 122 | + ball.dx *= -1 |
| 123 | + score_b += 1 |
| 124 | + pen.clear() |
| 125 | + pen.write("Player A : {} Player B : {}".format(score_a, score_b), align = 'center', font = ('Courier', 24, 'normal')) |
| 126 | + |
| 127 | + if ball.xcor() > 390: |
| 128 | + ball.goto(0, 0) |
| 129 | + ball.dx *= -1 |
| 130 | + score_a += 1 |
| 131 | + pen.clear() |
| 132 | + pen.write("Player A : {} Player B : {}".format(score_a, score_b), align = 'center', font = ('Courier', 24, 'normal')) |
| 133 | + |
| 134 | + # Paddle & Ball collision |
| 135 | + |
| 136 | + if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() - 40): |
| 137 | + ball.setx(340) |
| 138 | + ball.dx *= -1 |
| 139 | + winsound.PlaySound('S27.wav', winsound.SND_ASYNC) |
| 140 | + |
| 141 | + if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() - 40): |
| 142 | + ball.setx(-340) |
| 143 | + ball.dx *= -1 |
| 144 | + winsound.PlaySound('S27.wav', winsound.SND_ASYNC) |
| 145 | + |
| 146 | +# END |
| 147 | +# YAY! |
0 commit comments