Repository navigation
Expand file tree
/
Copy pathpong
More file actions
200 lines (131 loc) · 3.71 KB
/
Copy pathpong
File metadata and controls
200 lines (131 loc) · 3.71 KB
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
# ball.py
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.shape('circle')
self.shapesize(stretch_wid=1,stretch_len=1)
self.color('purple')
self.penup()
self.x_move = 1
self.y_move = 1
def increase_speed(self):
self.x_move *= 1.15
def move(self):
new_x = self.xcor() + self.x_move
new_y = self.ycor() + self.y_move
self.goto(new_x,new_y)
def collision_y(self):
self.y_move *= -1
def collision_x(self):
self.x_move *= -1
def reset_left(self):
self.x_move = 1
self.goto(0,0)
self.collision_x()
def reset_right(self):
self.x_move = -1
self.goto(0, 0)
self.collision_x()
# paddle.py
from turtle import Turtle
class Paddle(Turtle):
def __init__(self,position):
super().__init__()
self.shape('square')
self.color('white')
self.shapesize(stretch_wid=5,stretch_len=1)
self.penup()
self.goto(position)
def up(self):
New_Y = self.ycor()
X = self.xcor()
if self.ycor() <250:
self.goto(X,New_Y+20)
def down(self):
New_Y = self.ycor()
X = self.xcor()
if self.ycor() > -240:
self.goto(X, New_Y - 20)
AI_Y = 0
AI_X = -350
class Paddle_AI(Turtle): # For AI
def __init__(self):
super().__init__()
self.shape('square')
self.color('blue')
self.shapesize(stretch_wid=5, stretch_len=1)
self.penup()
self.setx(AI_X)
self.speed = 1
def up(self):
New_Y = self.ycor()
X = self.xcor()
if self.ycor() < 250:
self.goto(X, New_Y + 20)
def down(self):
New_Y = self.ycor()
X = self.xcor()
if self.ycor() > -240:
self.goto(X, New_Y - 20)
# scoreboard.py
from turtle import Turtle
class ScoreBoard(Turtle):
def __init__(self):
super().__init__()
self.color('white')
self.penup()
self.hideturtle()
self.l_score = 0
self.r_score = 0
self.update_scoreboard()
def update_scoreboard(self):
self.goto(-100,200)
self.write(self.l_score,align='center',font=('Courier',80,'normal'))
self.goto(100,200)
self.write(self.r_score, align='center', font=('Courier', 80, 'normal'))
def l_point(self):
self.clear()
self.l_score += 1
self.update_scoreboard()
def r_point(self):
self.clear()
self.r_score += 1
self.update_scoreboard()
# main.py
from turtle import Turtle, Screen
from paddle import Paddle
from ball import Ball
from scoreboard import ScoreBoard
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor('black')
screen.title('Pong')
screen.tracer(0)
r_paddle = Paddle((350,0))
l_paddle = Paddle((-350,0))
ball = Ball()
scoreboard= ScoreBoard()
screen.listen()
screen.onkey(r_paddle.up,"Up")
screen.onkey(r_paddle.down,"Down")
screen.onkey(l_paddle.up,'w')
screen.onkey(l_paddle.down,'s')
game_is_on = True
while game_is_on:
# ball.speed('fastest')
if ball.xcor() > 380 or ball.xcor() < -380:
if ball.xcor() > 380:
scoreboard.l_point()
ball.reset_left()
elif ball.xcor() < -380:
scoreboard.r_point()
ball.reset_right()
if ball.ycor() > 280 or ball.ycor() < -280:
ball.collision_y()
if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
ball.increase_speed()
ball.collision_x()
ball.move()
screen.update()
screen.exitonclick()