-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ping_pong.py
151 lines (108 loc) · 3.65 KB
/
Ping_pong.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# __Author__ __Lencof__
# Ping_pong.py
import turtle as t
import pygame, tkinter
# Score varibales
player_a_score = 0
player_b_score = 0
win = t.Screen() # creating a window
win.title("Ping-Pong Game") # Giving name to the game.
win.bgcolor('black') # providing color to the HomeScreen
win.setup(width=800,height=600) # Size of the game panel
win.tracer(0) # which speed up's the game
# Creating left paddie for the game
paddle_left = t.Turtle()
paddle_left.speed(0)
paddle_left.shape('square')
paddle_left.color('red')
paddle_left.shapesize(stretch_wid=5,stretch_len=1)
paddle_left.penup()
paddle_left.goto(-350,0)
# Creating a ringht paddie for the game
paddle_right = t.Turtle()
paddle_right.speed(0)
paddle_right.shape('square')
paddle_right.shapesize(stretch_wid=5,stretch_len=1)
paddle_right.color('red')
paddle_right.penup()
paddle_right.goto(350,0)
# Creating a pong ball for the game
ball = t.Turtle()
ball.speed(0)
ball.shape('circle')
ball.color('yellow')
ball.penup()
ball.goto(0,0)
ball_dx = 1.5 # Setting up the pixels for the ball movement.
all_by = 1.5
# Creating a pen for updating the Score
pen = t.Turtle()
pen.speed(0)
pen('skyblue')
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.write("Player A: 0 Player B: 0 ",align="center",font=('Monaco',24,"normal"))
# Moving the left Paddle using the keyboard
def paddle_left_up():
y = paddle_left.ycor()
y = y + 15
paddle_left.sety(y)
# Moving the left paddle down
def paddle_left_down():
y = paddle_left.ycor()
y = y - 15
paddle_left.sety(y)
# Moving the right paddle up
def paddle_right_up():
y = paddle_right.ycor()
y = y + 15
paddle-right.sety(y)
# MOving right paddle down
def paddle_right_down():
y = paddle_right.ycor()
y = y - 15
paddle_right.sety(y)
# Keyboard binding
win.listen()
win.onkeypress(paddle_left_up,"w")
win.onkeypress(paddle_left_down,"s")
win.onkeypress(paddle_right_up,"Up")
win.onkeypress(paddle_right_down,"Down")
# Main Game Loop
while True:
win.update() # This methods is mandatory to run any game
# Moving the ball
ball.setx(ball.xcor() + ball_dx)
ball.sety(ball.ycor() + ball_dy)
# setting up the border
if ball.ycor() > 290: # Right top paddle Border
ball.sety(290)
ball_dy = ball_dy * -1
if ball.ycor() < -290: # Left top paddle Border
ball.sety(-290)
ball_dy = ball_dy * -1
if ball.xcor() > 390: # right width paddle Border
ball.goto(0,0)
ball_dx = ball_dx * -1
player_a_score = player_a_score + 1
pen.clear()
pen.write("Player A: {} Player B: {} ".format(player_a_score,player_b_score),align="center",font=('Monaco',24,"normal"))
os.system("afplay wallhit.wav&")
if(ball.xcor()) < -390: # Left width paddle Border
ball.goto(0,0)
ball_dx = ball_dx * -1
player_b_score = player_b_score + 1
pen.clear()
pen.write("Player A: {} Player B: {} ".format(player_a_score,player_b_score),align="center",font=('Monaco',24,"normal"))
os.system("afplay wallhit.wav&")
# Handling the collisions with paddles.
if(ball.xcor() > 340) and (ball.xcor() < 350) and (ball.ycor() < paddle_right.ycor() + 40 and ball.ycor() > paddle_right.ycor() - 40):
ball.setx(340)
ball_dx = ball_dx * -1
os.system("afplay paddle.wav&")
if(ball.xcor() < -340) and (ball.xcor() > -350) and (ball.ycor() < paddle_left.ycor() + 40 and ball.ycor() > paddle_left.ycor() - 40):
ball.setx(-340)
ball_dx = ball_dx * -1
os.system("afplay paddle.wav&")
exit() # close