Skip to content

Commit 85e963b

Browse files
Update Day-22
1 parent a8a3a28 commit 85e963b

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

Day-22-PongGame/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Pong-Game
2+
3+
### An implementation of PongGame.
4+
5+
<img src= 'https://user-images.githubusercontent.com/65078610/105690319-d8c0b700-5f21-11eb-8401-df2c88c7348d.gif' width="500">

Day-22-PongGame/ball.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from turtle import Turtle
2+
import time
3+
4+
class Ball(Turtle):
5+
6+
def __init__(self):
7+
super().__init__()
8+
self.shape('circle')
9+
self.penup()
10+
self.color('azure')
11+
self.move_x = 10
12+
self.move_y = 10
13+
self.pace = 0.1
14+
15+
def move(self):
16+
new_x = self.xcor() + self.move_x
17+
new_y = self.ycor() + self.move_y
18+
self.goto(new_x, new_y)
19+
20+
def wall_bounce(self):
21+
self.move_y *= -1
22+
23+
def paddle_bounce(self):
24+
self.move_x *= -1
25+
if self.pace > 0.02:
26+
self.pace -= 0.01
27+
28+
def home(self):
29+
time.sleep(1)
30+
self.move_x *= -1
31+
self.goto(0,0)
32+
self.pace = 0.1

Day-22-PongGame/main.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from turtle import Screen
2+
from scoreboard import Scoreboard
3+
from paddle import Paddle
4+
from ball import Ball
5+
import time
6+
7+
screen = Screen()
8+
screen.title("Pong Game")
9+
screen.setup(width=800, height=600)
10+
screen.bgcolor('darkslategray')
11+
screen.tracer(0)
12+
13+
lhs_scoreboard = Scoreboard(-50)
14+
rhs_scoreboard = Scoreboard(50)
15+
rhs_paddle = Paddle((350,0))
16+
lhs_paddle = Paddle((-350,0))
17+
ball = Ball()
18+
screen.tracer(0)
19+
20+
screen.listen()
21+
screen.onkey(lhs_paddle.up, 'w')
22+
screen.onkey(lhs_paddle.down, 's')
23+
screen.onkey(rhs_paddle.up, 'Up')
24+
screen.onkey(rhs_paddle.down, 'Down')
25+
26+
game_on = True
27+
while game_on:
28+
time.sleep(ball.pace)
29+
ball.move()
30+
screen.update()
31+
32+
# Detect collision with top & bottom wall
33+
if (ball.ycor() > 280 or ball.ycor() < -280):
34+
ball.wall_bounce()
35+
36+
# Detect collision with paddle
37+
if (ball.distance(lhs_paddle) < 50 or ball.distance(rhs_paddle) < 50) and (ball.xcor() == 330 or ball.xcor() == -330):
38+
ball.paddle_bounce()
39+
40+
# Detect collision with left & right wall
41+
if ball.xcor() > 330:
42+
lhs_scoreboard.increment()
43+
ball.home()
44+
elif ball.xcor() < -330:
45+
rhs_scoreboard.increment()
46+
ball.home()
47+
48+
screen.exitonclick()

Day-22-PongGame/paddle.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from turtle import Turtle
2+
BOUNDARY_LIMIT = 220
3+
4+
class Paddle(Turtle):
5+
6+
def __init__(self,position):
7+
super().__init__()
8+
self.shape('square')
9+
self.penup()
10+
self.color('aquamarine')
11+
self.shapesize(stretch_wid=5, stretch_len=1)
12+
self.goto(position)
13+
14+
def up(self):
15+
if self.ycor() <= BOUNDARY_LIMIT:
16+
new_y = self.ycor() + 20
17+
self.goto(self.xcor(), new_y)
18+
19+
def down(self):
20+
if self.ycor() >= -BOUNDARY_LIMIT:
21+
new_y = self.ycor() - 20
22+
self.goto(self.xcor(), new_y)

Day-22-PongGame/scoreboard.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from turtle import Turtle
2+
ALIGNMENT = 'center'
3+
FONT = ("Courier", 24, "normal")
4+
Y = 250
5+
6+
class Scoreboard(Turtle):
7+
8+
def __init__(self,x):
9+
super().__init__()
10+
self.center_line()
11+
self.score = 0
12+
self.penup()
13+
self.hideturtle()
14+
self.color('cadetblue')
15+
self.goto(x,Y)
16+
self.write(f'{self.score}', align=ALIGNMENT, font=FONT)
17+
18+
def increment(self):
19+
self.score += 1
20+
self.clear()
21+
self.write(f'{self.score}', align=ALIGNMENT, font=FONT)
22+
23+
def center_line(self):
24+
line = Turtle()
25+
line.hideturtle()
26+
line.speed('fastest')
27+
line.color('aquamarine')
28+
line.pensize(3)
29+
line.setheading(270)
30+
for y in range(300,-300,-20):
31+
line.penup()
32+
line.goto(0,y)
33+
line.pendown()
34+
line.forward(10)

0 commit comments

Comments
 (0)