Skip to content

Commit 92cdd4c

Browse files
committed
Add Arcade Pong Game using Python
1 parent 0cd6634 commit 92cdd4c

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

Pong Arcade Game/Pong Game.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from turtle import Screen,Turtle
2+
from paddle import Paddle
3+
from ball import Ball
4+
from scoreboard import Scoreboard
5+
import time
6+
screen = Screen()
7+
screen.bgcolor("black")
8+
screen.setup(width=800, height=600)
9+
screen.title('Pong Game')
10+
screen.tracer(0)
11+
r_paddle=Paddle((350,0))
12+
l_paddle=Paddle((-350,0))
13+
ball=Ball()
14+
scoreboard=Scoreboard()
15+
16+
screen.listen()
17+
screen.onkey(r_paddle.go_up, "Up")
18+
screen.onkey(r_paddle.go_down, "Down")
19+
screen.onkey(l_paddle.go_up, "w")
20+
screen.onkey(l_paddle.go_down, "s")
21+
game_is_on = True
22+
while game_is_on:
23+
screen.update()
24+
time.sleep(ball.move_speed)
25+
ball.move()
26+
#detect collision with wall
27+
if ball.ycor() > 280 or ball.ycor() < -280:
28+
#bounce
29+
ball.bounce_y()
30+
#detect collision with paddle
31+
if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
32+
ball.bounce_x()
33+
#detect if r_paddle misses
34+
if ball.xcor() > 380:
35+
ball.reset_position()
36+
scoreboard.l_point()
37+
if ball.xcor() < -380:
38+
ball.reset_position()
39+
scoreboard.r_point()
40+
screen.exitonclick()

Pong Arcade Game/ball.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from turtle import Turtle
2+
class Ball(Turtle):
3+
def __init__(self):
4+
super().__init__()
5+
self.shape("circle")
6+
self.color("white")
7+
self.penup()
8+
self.x_move = 10
9+
self.y_move = 10
10+
self.move_speed = 0.1
11+
12+
def move(self):
13+
new_x = self.xcor() + self.x_move
14+
new_y = self.ycor() + self.y_move
15+
self.goto(new_x, new_y)
16+
17+
def bounce_y(self):
18+
self.y_move *= -1
19+
self.move_speed *= 0.9
20+
def bounce_x(self):
21+
self.x_move *= -1
22+
self.move_speed *= 0.9
23+
def reset_position(self):
24+
self.goto(0,0)
25+
self.bounce_x()
26+
self.move_speed = 0.1

Pong Arcade Game/paddle.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from turtle import Turtle
2+
3+
class Paddle(Turtle):
4+
def __init__(self, position):
5+
super().__init__()
6+
self.shape("square")
7+
self.shapesize(stretch_wid=5, stretch_len=1)
8+
self.color("white")
9+
self.penup()
10+
self.goto(position)
11+
def go_up(self):
12+
new_y = self.ycor() + 20
13+
self.goto(self.xcor(), new_y)
14+
def go_down(self):
15+
new_y = self.ycor() - 20
16+
self.goto(self.xcor(), new_y)

Pong Arcade Game/scoreboard.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from turtle import Turtle
2+
3+
class Scoreboard(Turtle):
4+
def __init__(self):
5+
super().__init__()
6+
self.color("white")
7+
self.penup()
8+
self.hideturtle()
9+
self.l_score = 0
10+
self.r_score = 0
11+
self.update_scoreboard()
12+
def update_scoreboard(self):
13+
self.clear()
14+
self.goto(-100, 200)
15+
self.write(self.l_score, align="center", font=("Courier", 80, "normal"))
16+
self.goto(100, 200)
17+
self.write(self.r_score, align="center", font=("Courier", 80, "normal"))
18+
def l_point(self):
19+
self.l_score += 1
20+
self.update_scoreboard()
21+
def r_point(self):
22+
self.r_score += 1
23+
self.update_scoreboard()

0 commit comments

Comments
 (0)