-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
157 lines (127 loc) · 4.18 KB
/
main.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
import turtle
import winsound
window = turtle.Screen()
window.title("Ping Pong")
window.bgcolor("black")
window.setup(width=800, height=600)
window.tracer(0) # stops the window from updating
# 1st Paddle
paddle_one = turtle.Turtle()
paddle_one.speed(0) # speed of animation, '0' for MAX
paddle_one.color("white")
paddle_one.shape("square")
paddle_one.shapesize(stretch_wid=5, stretch_len=1) # 20*5 height
paddle_one.penup()
paddle_one.goto(-350, 0) # (0, 0) is in middle
wid_one = 5
# 2nd Paddle
paddle_two = turtle.Turtle()
paddle_two.speed(0) # speed of animation, '0' for MAX
paddle_two.color("white")
paddle_two.shape("square")
paddle_two.shapesize(stretch_wid=5, stretch_len=1)
paddle_two.penup()
paddle_two.goto(350, 0) # (0, 0) is in middle
wid_two = 5
# Ball
ball = turtle.Turtle()
ball.speed(0) # speed of animation, '0' for MAX
ball.color("white")
ball.shape("circle")
ball.penup()
ball.goto(0, 0) # (0, 0) is in middle
ball.dx = 0.2 # ball moves by 2 pixels
ball.dy = -0.2
# for scoring
score_one = 0
score_two = 0
write_score = turtle.Turtle()
write_score.speed(0)
write_score.color("white")
write_score.penup()
write_score.hideturtle()
write_score.goto(0, 260)
write_score.write("Player One: 0 Player Two: 0", align="center", font=("Courier", 24, "normal"))
# movement of paddle
def paddle_one_up():
y = paddle_one.ycor() # coordinates
y += 50
paddle_one.sety(y)
def paddle_one_down():
y = paddle_one.ycor() # coordinates
y -= 50
paddle_one.sety(y)
def paddle_two_up():
y = paddle_two.ycor() # coordinates
y += 50
paddle_two.sety(y)
def paddle_two_down():
y = paddle_two.ycor() # coordinates
y -= 50
paddle_two.sety(y)
# Keyboard Events
window.listen()
# Left one
window.onkeypress(paddle_one_up, 'w')
window.onkeypress(paddle_one_down, 's')
# right one
window.onkeypress(paddle_two_up, 'Up')
window.onkeypress(paddle_two_down, 'Down')
# main loop for the game to run
while True:
window.update()
# Ball Movement
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# Ball's Border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1 # reversing direction
winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)
if ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1 # reversing direction
winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)
if ball.xcor() > 390: # past the paddle
ball.goto(0, 0)
ball.dx = -0.2
if ball.dy > 0 :
ball.dy = 0.2
else :
ball.dy = -0.2
score_one += 1
write_score.clear()
write_score.write("Player One: {} Player Two: {}".format(score_one, score_two), align="center",
font=("Courier", 24, "normal"))
if (wid_one != 1 and wid_two != 1) :
wid_one -= 1
wid_two += 1
paddle_one.shapesize(stretch_wid=wid_one, stretch_len=1)
paddle_two.shapesize(stretch_wid=wid_two, stretch_len=1)
if ball.xcor() < -390: # past the paddle
ball.goto(0, 0)
ball.dx = 0.2
if ball.dy > 0 :
ball.dy = 0.2
else :
ball.dy = -0.2
score_two += 1
write_score.clear()
write_score.write("Player One: {} Player Two: {}".format(score_one, score_two), align="center",
font=("Courier", 24, "normal"))
if (wid_one != 1 and wid_two != 1) :
wid_two -= 1
wid_one += 1
paddle_two.shapesize(stretch_wid=wid_two, stretch_len=1)
paddle_one.shapesize(stretch_wid=wid_one, stretch_len=1)
# Collisions b/w ball & paddle
if (340 < ball.xcor() < 350) and (paddle_two.ycor() + 40 > ball.ycor() > paddle_two.ycor() - 40):
ball.setx(340)
ball.dx *= -1.05
ball.dy *= 1.05
winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)
if (-340 > ball.xcor() > -350) and (paddle_one.ycor() + 40 > ball.ycor() > paddle_one.ycor() - 40):
ball.setx(-340)
ball.dx *= -1.05
ball.dy *= 1.05
winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)