-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
108 lines (87 loc) · 2.55 KB
/
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
# Simple pong game in python 3
# this game is based on turtle
import turtle
import os
Width = 800
Height = 600
score_a = 0
score_b = 0
#Window setup
window = turtle.Screen()
window.title("Pong Game By ankurrai1")
window.bgcolor("black")
window.setup(width = Width, height = Height)
window.tracer(1)
def draw_fig(x,y,shape,color,isPaddle = True):
paddle = turtle.Turtle()
paddle.speed(0)
paddle.shape(shape)
paddle.color(color)
if isPaddle:
paddle.shapesize(stretch_wid = 5, stretch_len = 1)
paddle.penup()
paddle.goto(x,y)
return paddle
paddle_a = draw_fig(-350,0,"square","white")
paddle_b = draw_fig(350,0,"square","white")
ball = draw_fig(0,0,"square","white",False)
def getpen_to():
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
return pen
def update_score(pen,A,B):
pen.clear()
pen.write("player A:{} player B: {}".format(A,B),align="center",font=("Arial",15, "normal"))
# this two will define the speed of ball move
ball.dx = 5
ball.dy = - 5
def move_paddel(paddle,direction,movement = 20):
y = paddle.ycor()
if direction == "up":
y +=20
else if direction == "down":
y -=20
paddle.sety(y)
#keyboard Bindings
window.listen()
window.onkeypress(move_paddel(paddle_a,"up"),"w")
window.onkeypress(move_paddel(paddle_a,"down"),"s")
window.onkeypress(move_paddel(paddle_b,"up"),"Up")
window.onkeypress(move_paddel(paddle_b,"down"),"Down")
while True:
window.update()
ball.setx(ball.xcor()+ball.dx)
ball.sety(ball.ycor()+ball.dy)
# top bottom boder checking
if ball.ycor()>290:
ball.sety(290)
ball.dy *=-1
os.system("afplay pong.wav&")
if ball.ycor()< -290:
ball.sety(-290)
ball.dy *=-1
os.system("afplay pong.wav&")
# left right boder checking
if ball.xcor()>390:
ball.goto(0,0)
ball.dx *=-1
score_a += 1
update_score(score_a,score_b)
if ball.xcor()< -390:
ball.goto(0,0)
ball.dy *=-1
score_b += 1
update_score(score_a,score_b)
# paddle ball collision detection
if ( ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() <paddle_b.ycor()+50 and ball.ycor()>paddle_b.ycor() -50):
ball.setx(340)
ball.dx*=-1
os.system("afplay pong.wav&")
if ( ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() <paddle_a.ycor()+50 and ball.ycor()>paddle_a.ycor() -50):
ball.setx(-340)
ball.dx*=-1
os.system("afplay pong.wav&")