make a pong game in python

Solutions on MaxInterview for make a pong game in python by the best coders in the world

showing results for - "make a pong game in python"
Veronica
24 Aug 2020
1#The best python pong game without pygame!
2
3import turtle
4
5a_wins = False
6b_wins = False
7
8# Set up the screen
9turtle.Screen()
10wn = turtle.Screen()
11wn.title("Ping Pong game by Timothy")
12wn.bgcolor("black")
13wn.setup(width=800, height=600)
14wn.tracer(0)
15
16# Score
17score_a = 0
18score_b = 0
19score_lim = 10
20switch = True
21
22# Paddle A
23paddle_a = turtle.Turtle()
24paddle_a.speed(0)
25paddle_a.color("white")
26paddle_a.shape("square")
27paddle_a.penup()
28paddle_a.goto(-350, 0)
29paddle_a.shapesize(stretch_wid=5, stretch_len=1)
30
31# Paddle B
32paddle_b = turtle.Turtle()
33paddle_b.speed(0)
34paddle_b.color("white")
35paddle_b.shape("square")
36paddle_b.penup()
37paddle_b.goto(350, 0)
38paddle_b.shapesize(stretch_wid=5, stretch_len=1)
39
40paddle_a_speed = 20
41paddle_b_speed = 20
42
43# Ball
44ball = turtle.Turtle()
45ball.speed(0)
46ball.shape("circle")
47ball.color("white")
48ball.penup()
49ball.goto(0, 0)
50ball.dy = 0.4
51ball.dx = 0.4
52
53# Pen
54pen = turtle.Turtle()
55pen.speed(0)
56pen.penup()
57pen.color("white")
58pen.hideturtle()
59pen.goto(0, 260)
60pen.write("Player A: 0  Player B: 0", align="center", font=("Courier", 24, "normal"))
61
62# Win
63win = turtle.Turtle()
64win.speed(0)
65win.penup()
66win.color("white")
67win.hideturtle()
68win.goto(0, 0)
69
70
71# Function
72def paddle_a_up():
73    y = paddle_a.ycor()
74    y += paddle_a_speed
75    paddle_a.sety(y)
76
77
78def paddle_a_down():
79    y = paddle_a.ycor()
80    y -= paddle_a_speed
81    paddle_a.sety(y)
82
83
84def paddle_b_up():
85    y = paddle_b.ycor()
86    y += paddle_b_speed
87    paddle_b.sety(y)
88
89
90def paddle_b_down():
91    y = paddle_b.ycor()
92    y -= paddle_b_speed
93    paddle_b.sety(y)
94
95
96turtle.listen()
97turtle.onkey(paddle_a_up, "w")
98turtle.onkey(paddle_a_down, "s")
99turtle.onkey(paddle_b_up, "Up")
100turtle.onkey(paddle_b_down, "Down")
101
102# Main game loop
103while True:
104    wn.update()
105
106    # Move the ball
107    ball.setx(ball.xcor() + ball.dx)
108    ball.sety(ball.ycor() + ball.dy)
109
110    # Border
111    if ball.ycor() > 290:
112        ball.sety(290)
113        ball.dy *= -1
114    elif ball.ycor() < -290:
115        ball.sety(-290)
116        ball.dy *= -1
117    elif ball.xcor() > 390:
118        ball.goto(0, 0)
119        ball.dx *= -1
120        score_a += 1
121    elif ball.xcor() < -390:
122        ball.goto(0, 0)
123        ball.dx *= -1
124        score_b += 1
125
126    # Paddle
127    if 340 < ball.xcor() < 350 and paddle_b.ycor() + 40 > ball.ycor() > paddle_b.ycor() - 40:
128        ball.setx(340)
129        ball.dx *= -1
130    if -340 > ball.xcor() > -350 and paddle_a.ycor() + 40 > ball.ycor() > paddle_a.ycor() - 40:
131        ball.setx(-340)
132        ball.dx *= -1
133
134    pen.clear()
135    pen.write(f"Player A: {score_a}  Player B: {score_b}", align="center", font=("Courier", 24, "normal"))
136
137    if score_a == score_lim:
138        turtle.clearscreen()
139        a_wins = True
140        break
141
142    elif score_b == score_lim:
143        turtle.clearscreen()
144        b_wins = True
145        break
146
147
148while True:
149    if a_wins:
150        wn.bgcolor("black")
151        win.write("Player A wins", align="center", font=("Courier", 50, "normal"))
152    elif b_wins:
153        wn.bgcolor("black")
154        win.write("Player B wins", align="center", font=("Courier", 50, "normal"))
Mario
17 Mar 2020
1print("pong game in python")