Skip to content

Commit d914852

Browse files
authored
Merge pull request #1 from HARSHV10/HARSHV10-patch-1
hey added a ping pong ball game
2 parents 29cb12d + 98198e1 commit d914852

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed
1.28 KB
Binary file not shown.
942 Bytes
Binary file not shown.
1.18 KB
Binary file not shown.

pingpong/ball.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from turtle import Turtle
2+
class Ball(Turtle):
3+
def __init__(self):
4+
super().__init__()
5+
self.penup()
6+
self.speed("slowest")
7+
self.shape("circle")
8+
self.color("white")
9+
self.xmove= 10
10+
self.ymove= 10
11+
self.move_speed =0.1
12+
def move(self):
13+
newx= self.xcor()+self.xmove
14+
newy= self.ycor()+self.ymove
15+
self.goto(newx,newy)
16+
def reflect(self):
17+
self.ymove *= -1
18+
def bounce(self):
19+
self.xmove *= -1
20+
self.move_speed *= 0.9
21+
def refresh(self):
22+
self.goto(0,0)
23+
self.move_speed = 0.1
24+
self.xmove *= -1

pingpong/main.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from turtle import Turtle ,Screen
2+
from play import Game
3+
from ball import Ball
4+
from scoreboard import Score
5+
import time
6+
screen = Screen()
7+
paddle1 = Game((350,0))
8+
paddle2 = Game((-350,0))
9+
ball = Ball()
10+
score = Score()
11+
screen.tracer(0)
12+
13+
paddle1.goto(350,0)
14+
paddle2.goto(-350,0)
15+
16+
screen.setup(width=800,height=600)
17+
screen.bgcolor("black")
18+
screen.title("PONG")
19+
20+
21+
screen.listen()
22+
if paddle1.xcor()<390 or paddle2.ycor()<-350:
23+
screen.onkey(fun=paddle1.goup,key="Up")
24+
screen.onkey(fun=paddle1.godown,key="Down")
25+
screen.onkey(fun=paddle2.goup,key="w")
26+
screen.onkey(fun=paddle2.godown,key="s")
27+
28+
game_on = True
29+
while game_on:
30+
time.sleep(ball.move_speed)
31+
ball.move()
32+
if ball.ycor()>280 or ball.ycor()<-280:
33+
ball.reflect()
34+
if ball.distance(paddle1)<40 or ball.distance(paddle2)<40:
35+
ball.bounce()
36+
37+
if ball.xcor()>400 :
38+
ball.refresh()
39+
score.goto(-300, 250)
40+
score.currentleftscr()
41+
if ball.xcor() < -400:
42+
ball.refresh()
43+
score.goto(300, 250)
44+
score.currentrightscr()
45+
screen.update()
46+
47+
48+
screen.exitonclick()

pingpong/play.py

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

pingpong/scoreboard.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from turtle import Turtle
2+
class Score(Turtle):
3+
def __init__(self):
4+
super().__init__()
5+
self.penup()
6+
self.hideturtle()
7+
self.color("white")
8+
self.lscr=0
9+
self.rscr=0
10+
11+
12+
self.updatescr()
13+
14+
def updatescr(self):
15+
self.goto(-100, 200)
16+
self.write(f"{self.lscr}", True, align="left", font=("arial", 24, "normal"))
17+
self.goto(100, 200)
18+
self.write(f"{self.rscr}", True, align="right", font=("arial", 24, "normal"))
19+
20+
def currentleftscr(self):
21+
self.lscr+=1
22+
self.clear()
23+
self.updatescr()
24+
def currentrightscr(self):
25+
self.rscr+=1
26+
self.clear()
27+
self.updatescr()
28+

0 commit comments

Comments
 (0)