how to make a ping pong game in python using turtle

Solutions on MaxInterview for how to make a ping pong game in python using turtle by the best coders in the world

showing results for - "how to make a ping pong game in python using turtle"
Bailey
20 Feb 2018
1from turtle import *
2from random import randint, random
3from time import sleep
4
5start = 0
6
7sliderH, sliderW = 60,20
8
9totalX, totalY = 400, 400
10ballX, ballY = 0.0, 0.0
11collision = 0
12
13dX = random() + 1#randint(1,2)
14dY = random() + 1#randint(1,2)   
15
16# Screen initia
17screen = Screen()                        
18screen.setup(totalX+50,totalY+50)
19#screen.screensize(totalX,totalY)
20#screen.title("Ping Pong Game!")
21#print ('Screen size:', screen.screensize())
22screen.bgcolor('black')
23
24# Turtle 0 Creation => Draws boundaries for the Ping Pong Game
25t0 = Turtle()
26t0.hideturtle()
27t0.color('white'); t0.speed(5)
28t0.penup(); t0.goto(0,totalY//2+10); t0.write("Press 'S' to start", True, align="center")
29t0.penup(); t0.goto(-totalX//2,-totalY//2); t0.pendown(); t0.goto(-totalX//2,totalY//2); t0.goto(totalX//2,totalY//2); t0.goto(totalX//2,-totalY//2); t0.goto(-totalX//2,-totalY//2); 
30t0.penup(); #t0.goto(-180,200); t0.pendown();t0.goto(-180, -200); t0.penup()
31
32
33# Turtle 1 Creation => Acts as a slider controlled by 'Up' & 'Down' key
34t1 = Turtle()
35t1.hideturtle()
36screen.register_shape("line", ((-sliderW//2,-sliderH//2), (-sliderW//2,sliderH//2), (sliderW//2,sliderH//2), (sliderW//2,-sliderH//2)))
37t1.shape('line')
38t1.speed(0)
39t1.color('red')
40t1.setheading(90) # face east at beginning
41t1.penup(); t1.goto(-totalX//2 + 10, 0);
42t1.showturtle()
43 
44def go_up():
45    #print ('T1 position:',t1.pos())
46    t1.fd(5)
47
48def go_down():
49    t1.bk(5)
50
51def start_game():
52    global start
53    start = 1
54
55screen.onkey(go_up, 'Up')
56screen.onkey(go_down, 'Down')
57screen.onkey(start_game, 'S')
58
59
60# Turtle 2 Creation => Acts as the ping pong ball 
61t2 = Turtle()
62t2.shape('circle'); t2.speed(0)
63t2.color('blue')
64t2.penup(); t2.goto(ballX,ballY);
65t1.setheading(90)
66#print ('Ball size:', t2.turtlesize())
67
68def move_t2():
69    global sliderH, sliderW
70    global totalX, totalY
71    global ballX, ballY
72    global dX, dY
73    global collision
74    global start
75
76    t2.goto(ballX, ballY)
77
78
79##    if ballX < - totalX//2 + 10:
80##        dX = -dX
81    sliderX, sliderY = t1.pos()
82
83    # Hit on the slider
84    if ballY >= (sliderY - sliderH//2)and ballY <= (sliderY + sliderH//2) and ballX <= -totalX//2 + sliderW + 10:
85        if collision == 0:
86            dX = -dX
87            collision = 1
88    # Missed the slider
89    if ballY <= (sliderY - sliderH//2) or ballY >= (sliderY + sliderH//2):
90        if ballX <= -totalX//2 + sliderW + 10 -5:
91            start = 0
92            #bye()
93    
94    if ballX >= -totalX//2 + sliderW + 10:
95        collision = 0
96    
97    if ballX > totalX//2 -10:
98        dX = -dX
99    if ballY > totalY//2 - 10:
100        dY = -dY
101    if ballY < -totalY//2 + 10:
102        dY = -dY       
103
104    if start == 1:
105        ballX += dX
106        ballY += dY
107    else:
108        
109        ballX = 0
110        ballY = 0
111    
112    screen.ontimer(move_t2,1)
113
114move_t2()
115
116screen.listen()
117screen.mainloop()
118   
119