-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong_game.py
198 lines (152 loc) · 4.76 KB
/
pong_game.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import turtle
#import pygame
import winsound
#pygame.init()
import os
import sys
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
score_a =0
score_b =0
win = turtle.Screen()
win.setup(900,700)
win.bgcolor("blue")
win.title("Pong Game")
win.tracer(0)
pen1= turtle.Turtle()
pen1.hideturtle()
pen1.width(3)
pen1.color("light green")
pen1.left(90)
pen1.forward(350)
pen1.left(180)
pen1.forward(800)
pen0 = turtle.Turtle()
pen0.penup()
pen0.hideturtle()
pen0.setpos(-450,350)
pen0.pendown()
pen0.color("light green")
a=900
b=800
pen0.width(4)
for i in range(4):
pen0.forward(a)
pen0.right(90)
pen0.forward(b+i)
pen0.right(90)
pen0.forward(a)
pen0.right(90)
pen0.forward(b+i+1)
pen0.right(90)
left_paddle = turtle.Turtle()
left_paddle.speed(0)
left_paddle.shape("square")
left_paddle.color("peru")
left_paddle.shapesize(stretch_wid=5,stretch_len=1)
left_paddle.penup()
left_paddle.goto(-436,0)
right_paddle = turtle.Turtle()
right_paddle.speed(0)
right_paddle.shape("square")
right_paddle.color("peru")
right_paddle.shapesize(stretch_wid=5,stretch_len=1)
right_paddle.penup()
right_paddle.goto(435,0)
ball=turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("red")
ball.penup()
ball.dx = 0.85
ball.dy = 0.85
pen = turtle.Turtle()
pen.speed(0)
pen.color("violet")
pen.penup()
pen.hideturtle()
pen.goto(0,310)
pen.write("Player A: 0\t \tPlayer B: 0",align="center",font=("Comic Sans MS",24,"normal"))
def left_paddle_up():
if left_paddle.ycor()>290:
return
left_paddle.sety(left_paddle.ycor()+20)
def left_paddle_down():
if left_paddle.ycor()<-290:
return
left_paddle.sety(left_paddle.ycor()-20)
def right_paddle_up():
if right_paddle.ycor()>290:
return
right_paddle.sety(right_paddle.ycor()+20)
def right_paddle_down():
if right_paddle.ycor()<-290:
return
right_paddle.sety(right_paddle.ycor()-20)
win.listen()
win.onkeypress(left_paddle_up,'w')
win.onkeypress(left_paddle_down,'s')
win.onkeypress(right_paddle_up,'Up')
win.onkeypress(right_paddle_down,'Down')
while True:
win.update()
ball.setx(ball.xcor()+ball.dx)
ball.sety(ball.ycor()+ball.dy)
if ball.ycor()>335:
ball.sety(335)
winsound.PlaySound(resource_path("hit_ground.wav"),winsound.SND_ASYNC | winsound.SND_ALIAS)
ball.dy *= -1
if ball.xcor()>435:
ball.setx(435)
winsound.PlaySound(resource_path("hit_wall.wav"),winsound.SND_ASYNC | winsound.SND_ALIAS)
ball.dx *= -1
score_a+=1
pen.clear()
pen.write("Player A: {}\t \tPlayer B: {}".format(score_a,score_b),align="center",font=("Comic Sans MS",24,"normal"))
if ball.ycor()<-335:
ball.sety(-335)
winsound.PlaySound(resource_path("hit_ground.wav"),winsound.SND_ASYNC | winsound.SND_ALIAS)
ball.dy *= -1
if ball.xcor()<-435:
ball.setx(-435)
winsound.PlaySound(resource_path("hit_wall.wav"),winsound.SND_ASYNC | winsound.SND_ALIAS)
ball.dx*=-1
score_b+=1
pen.clear()
pen.write("Player A: {}\t \tPlayer B: {}".format(score_a,score_b),align="center",font=("Comic Sans MS",24,"normal"))
if ball.xcor()>420 and right_paddle.ycor()-50<ball.ycor()<right_paddle.ycor()+50:
ball.setx(410)
winsound.PlaySound(resource_path("bounce.wav"),winsound.SND_ASYNC | winsound.SND_ALIAS)
#sound = pygame.mixer.Sound("E:/courses/python/Python-main/Pong/bounce.wav")
#sound.play()
ball.dx*=-1
if ball.xcor()<-420 and left_paddle.ycor()-50<ball.ycor()<left_paddle.ycor()+50:
ball.setx(-410)
winsound.PlaySound(resource_path("bounce.wav"),winsound.SND_ASYNC | winsound.SND_ALIAS)
#playsound("E:/courses/python/Python-main/Pong/bounce.wav")
#sound = pygame.mixer.Sound("E:/courses/python/Python-main/Pong/bounce.wav")
#sound.play()
ball.dx*=-1
if score_a == 5:
pen.clear()
pen1.clear()
pen.goto(0,0)
right_paddle.clear()
left_paddle.clear()
ball.clear
winsound.PlaySound(resource_path("game_win.wav"),winsound.SND_ASYNC | winsound.SND_ALIAS)
pen.write("Player A Wins",align="center",font=("Comic Sans MS",24,"bold"))
break
elif score_b == 5:
pen.clear()
pen1.clear()
right_paddle.clear()
left_paddle.clear()
pen.goto(0,0)
ball.clear
winsound.PlaySound(resource_path("game_win.wav"),winsound.SND_ASYNC | winsound.SND_ALIAS)
pen.write("Player B Wins",align="center",font=("Comic Sans MS",24,"bold"))
break
win.exitonclick()