-
Notifications
You must be signed in to change notification settings - Fork 402
/
Pong_Game.py
121 lines (101 loc) · 3.15 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
import turtle as t
playerAscore=0
playerBscore=0
#create a window and declare a variable called window and call the screen()
window=t.Screen()
window.title("The Pong Game")
window.bgcolor("green")
window.setup(width=800,height=600)
window.tracer(0)
#Creating the left paddle
leftpaddle=t.Turtle()
leftpaddle.speed(0)
leftpaddle.shape("square")
leftpaddle.color("white")
leftpaddle.shapesize(stretch_wid=5,stretch_len=1)
leftpaddle.penup()
leftpaddle.goto(-350,0)
#Creating the right paddle
rightpaddle=t.Turtle()
rightpaddle.speed(0)
rightpaddle.shape("square")
rightpaddle.color("white")
rightpaddle.shapesize(stretch_wid=5,stretch_len=1)
rightpaddle.penup()
rightpaddle.goto(-350,0)
#Code for creating the ball
ball=t.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("red")
ball.penup()
ball.goto(5,5)
ballxdirection=0.2
ballydirection=0.2
#Code for creating pen for scorecard update
pen=t.Turtle()
pen.speed(0)
pen.color("Blue")
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.write("score",align="center",font=('Arial',24,'normal'))
#code for moving the leftpaddle
def leftpaddleup():
y=leftpaddle.ycor()
y=y+90
leftpaddle.sety(y)
def leftpaddledown():
y=leftpaddle.ycor()
y=y+90
leftpaddle.sety(y)
#code for moving the rightpaddle
def rightpaddleup():
y=rightpaddle.ycor()
y=y+90
rightpaddle.sety(y)
def rightpaddledown():
y=rightpaddle.ycor()
y=y+90
rightpaddle.sety(y)
#Assign keys to play
window.listen()
window.onkeypress(leftpaddleup,'w')
window.onkeypress(leftpaddledown,'s')
window.onkeypress(rightpaddleup,'Up')
window.onkeypress(rightpaddledown,'Down')
while True:
window.update()
#moving the ball
ball.setx(ball.xcor()+ballxdirection)
ball.sety(ball.ycor()+ballxdirection)
#border set up
if ball.ycor()>290:
ball.sety(290)
ballydirection=ballydirection*-1
if ball.ycor()<-290:
ball.sety(-290)
ballydirection=ballydirection*-1
if ball.xcor() > 390:
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() < rightpaddle.ycor() + 40 and ball.ycor() > rightpaddle.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() < leftpaddle.ycor() + 40 and ball.ycor() > leftpaddle.ycor() - 40):
ball.setx(-340)
ball_dx = ball_dx * -1
os.system("afplay paddle.wav&")