-
Notifications
You must be signed in to change notification settings - Fork 1
/
pong.py
147 lines (118 loc) · 4.15 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
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
# Special thanks to Christian Thompson from FreeCamp.org
# Original idea from Christian Thompson https://christianthompson.com/ and editied version with additional functions by Brandon Cox [email protected] -- future additions likely
# Overall the goal of this was to implement a basic Pong game coded in Python3 with PythonTurtle
import turtle
# Screen
s = turtle.Screen()
s.title("Pong Game")
s.bgcolor("black")
s.setup(width=800,height=600)
s.tracer(0)
#Pausing
is_paused = False
# Scoring
score1 = 0
score2 = 0
# Paddles
paddle1 = turtle.Turtle()
paddle1.speed(0)
paddle1.shape("square")
paddle1.color("white")
paddle1.shapesize(stretch_wid=5, stretch_len=1)
paddle1.penup()
paddle1.goto(350, 0)
paddle2 = turtle.Turtle()
paddle2.speed(0)
paddle2.shape("square")
paddle2.color("white")
paddle2.shapesize(stretch_wid=5, stretch_len=1)
paddle2.penup()
paddle2.goto(-350, 0)
#Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0,0)
ball.changex = .5
ball.changey = .5
#Scoring
score = turtle.Turtle()
score.speed(0)
score.color("white")
score.penup()
score.hideturtle()
score.goto(0, 260)
score.write("Player 1: 0 Player 2: 0", align="center", font=("Courier", 24, "bold"))
def gameExit():
s.bye()
def paddle1up():
ycoord = paddle1.ycor() #Getting the initial coordinate of paddle
ycoord += 20
paddle1.sety(ycoord)
def paddle1down():
ycoord = paddle1.ycor()
ycoord -= 20
paddle1.sety(ycoord)
def paddle2up():
ycoord1 = paddle2.ycor()
ycoord1 += 20
paddle2.sety(ycoord1)
def paddle2down():
ycoord1 = paddle2.ycor()
ycoord1 -= 20
paddle2.sety(ycoord1)
def gamePause():
global is_paused
if is_paused == True:
is_paused = False
else:
is_paused = True
# Listening for keyboard input
s.listen()
s.onkeypress(gamePause, "v")
s.onkeypress(gameExit, "p")
s.onkeypress(paddle1up, "w")
s.onkeypress(paddle1down, "s")
s.onkeypress(paddle2up, "i")
s.onkeypress(paddle2down, "k")
while True:
if not is_paused:
s.update()
else:
s.update() #Updating such that turtles on screen move
ball.setx(ball.xcor() + ball.changex) #Random movement in x direction
ball.sety(ball.ycor() + ball.changey) #Random movement in y direction
if ball.ycor() > 290: #Handling if ball touches top of screen
ball.sety(290) #Setting back to 290
ball.changey *= -1 #Reversing direction
if ball.xcor() > 390: #Handling if ball touches right of screen
ball.goto(0, 0) #Resetting game
ball.changex *= -1 #Reversing direction
score1 += 1
score.clear()
score.write("Player 1: {} Player 2: {}".format(score1, score2), align="center", font=("Courier", 24, "bold"))
if ball.ycor() < -290: #Handling if ball touches bottom of screen
ball.sety(-290) #Setting back to -290
ball.changey *= -1 #Reversing direction
if ball.xcor() < -390: #Handling if ball touches left of screen
ball.goto(0, 0) #Resetting game
ball.changex *= -1 #Reversing direction
score2 += 1
score.clear()
score.write("Player 1: {} Player 2: {}".format(score1, score2), align="center", font=("Courier", 24, "bold"))
if paddle1.ycor() > 250: #Handling if paddle1 reaches bottom of screen
paddle1.sety(250) #Setting it to 250
if paddle1.ycor() < -250: #Handling if paddle1 reaches bottom of screen
paddle1.sety(-250) #Setting it to -250
if paddle2.ycor() > 250: #Handling if paddle2 reaches top of screen
paddle2.sety(250) #Setting it to 250
if paddle2.ycor() < -250: #Handling if paddle2 reaches bottom of screen
paddle2.sety(-250) #Setting it to -250
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle1.ycor() + 40 and ball.ycor() > paddle1.ycor() - 50):
ball.setx(340)
ball.changex *= -1
if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle2.ycor() + 40 and ball.ycor() > paddle2.ycor() - 50):
ball.setx(-340)
ball.changex *= -1