diff --git a/pingpong/__pycache__/ball.cpython-310.pyc b/pingpong/__pycache__/ball.cpython-310.pyc new file mode 100644 index 000000000..acbf49c9f Binary files /dev/null and b/pingpong/__pycache__/ball.cpython-310.pyc differ diff --git a/pingpong/__pycache__/play.cpython-310.pyc b/pingpong/__pycache__/play.cpython-310.pyc new file mode 100644 index 000000000..e0c2ff19a Binary files /dev/null and b/pingpong/__pycache__/play.cpython-310.pyc differ diff --git a/pingpong/__pycache__/scoreboard.cpython-310.pyc b/pingpong/__pycache__/scoreboard.cpython-310.pyc new file mode 100644 index 000000000..feca1f01b Binary files /dev/null and b/pingpong/__pycache__/scoreboard.cpython-310.pyc differ diff --git a/pingpong/ball.py b/pingpong/ball.py new file mode 100644 index 000000000..75b244eca --- /dev/null +++ b/pingpong/ball.py @@ -0,0 +1,24 @@ +from turtle import Turtle +class Ball(Turtle): + def __init__(self): + super().__init__() + self.penup() + self.speed("slowest") + self.shape("circle") + self.color("white") + self.xmove= 10 + self.ymove= 10 + self.move_speed =0.1 + def move(self): + newx= self.xcor()+self.xmove + newy= self.ycor()+self.ymove + self.goto(newx,newy) + def reflect(self): + self.ymove *= -1 + def bounce(self): + self.xmove *= -1 + self.move_speed *= 0.9 + def refresh(self): + self.goto(0,0) + self.move_speed = 0.1 + self.xmove *= -1 diff --git a/pingpong/main.py b/pingpong/main.py new file mode 100644 index 000000000..ed0382935 --- /dev/null +++ b/pingpong/main.py @@ -0,0 +1,48 @@ +from turtle import Turtle ,Screen +from play import Game +from ball import Ball +from scoreboard import Score +import time +screen = Screen() +paddle1 = Game((350,0)) +paddle2 = Game((-350,0)) +ball = Ball() +score = Score() +screen.tracer(0) + +paddle1.goto(350,0) +paddle2.goto(-350,0) + +screen.setup(width=800,height=600) +screen.bgcolor("black") +screen.title("PONG") + + +screen.listen() +if paddle1.xcor()<390 or paddle2.ycor()<-350: + screen.onkey(fun=paddle1.goup,key="Up") + screen.onkey(fun=paddle1.godown,key="Down") + screen.onkey(fun=paddle2.goup,key="w") + screen.onkey(fun=paddle2.godown,key="s") + +game_on = True +while game_on: + time.sleep(ball.move_speed) + ball.move() + if ball.ycor()>280 or ball.ycor()<-280: + ball.reflect() + if ball.distance(paddle1)<40 or ball.distance(paddle2)<40: + ball.bounce() + + if ball.xcor()>400 : + ball.refresh() + score.goto(-300, 250) + score.currentleftscr() + if ball.xcor() < -400: + ball.refresh() + score.goto(300, 250) + score.currentrightscr() + screen.update() + + +screen.exitonclick() diff --git a/pingpong/play.py b/pingpong/play.py new file mode 100644 index 000000000..3378bec6e --- /dev/null +++ b/pingpong/play.py @@ -0,0 +1,18 @@ +from turtle import Turtle , Screen + +class Game(Turtle) : + def __init__(self,position): + super().__init__() + self.penup() + self.color("white") + self.shape("square") + self.shapesize(4, 1) + self.goto(position) + def goup(self): + newy = self.ycor()+20 + self.goto(self.xcor(),newy) + def godown (self): + newy = self.ycor()-20 + self.goto(self.xcor(),newy) + + diff --git a/pingpong/scoreboard.py b/pingpong/scoreboard.py new file mode 100644 index 000000000..92e8e4b37 --- /dev/null +++ b/pingpong/scoreboard.py @@ -0,0 +1,28 @@ +from turtle import Turtle +class Score(Turtle): + def __init__(self): + super().__init__() + self.penup() + self.hideturtle() + self.color("white") + self.lscr=0 + self.rscr=0 + + + self.updatescr() + + def updatescr(self): + self.goto(-100, 200) + self.write(f"{self.lscr}", True, align="left", font=("arial", 24, "normal")) + self.goto(100, 200) + self.write(f"{self.rscr}", True, align="right", font=("arial", 24, "normal")) + + def currentleftscr(self): + self.lscr+=1 + self.clear() + self.updatescr() + def currentrightscr(self): + self.rscr+=1 + self.clear() + self.updatescr() +