الملف الرئيسى main
from turtle import Screen
from paddle import Paddle
from ball import Ball
from border import Border
from scorebord import Scoreborde
import time
screen=Screen()
screen.title("Ping pong game")
screen.bgcolor("black")
screen.setup(700,700)
screen.tracer(0)
r_paddle=Paddle((300,0))
l_paddle=Paddle((-300,0))
ball=Ball()
border=Border() # الحواف 680 x=340 , y=340
r_score=Scoreborde((-170,290))
l_score=Scoreborde((170,290))
while True:
screen.update()
time.sleep(.01)
ball.move()
if ball.ycor() > 320 or ball.ycor() < -320:
ball.dy *=-1
if ball.xcor() > 320:
ball.home()
ball.dx *=-1
ball.dy *=-1
l_score.increase_score()
if ball.xcor() < -320 :
ball.home()
ball.dx *=-1
r_score.increase_score()
if ball.distance(r_paddle) < 30 or ball.distance(l_paddle) < 30 : # 10+8+10
ball.dx *=-1
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")
screen.exitonclick()
ملف المضارب
from turtle import Turtle
class Paddle (Turtle):
def __init__(self,posstion):
super().__init__()
self.shape('square')
self.color('white')
self.penup()
self.goto(posstion)
self.shapesize(5,1)
def up(self):
if self.ycor() < 300:
self.sety(self.ycor()+50)
def down(self):
if self.ycor() > -300:
self.sety(self.ycor()-50)
ملف الكرة
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.shapesize(.8,.8) # 16px
self.color("blue")
self.penup()
self.speed=3
self.dx=1
self.dy=1
def move(self):
self.setx(self.xcor()+self.dx*self.speed)
self.sety(self.ycor()+self.dy*self.speed)
ملف عمل الحدود
from turtle import Turtle
class Border(Turtle):
def __init__(self):
super().__init__()
self.color('white')
self.penup()
self.pensize(10)
self.hideturtle()
self.goto(-340,340)
self.create_border()
def create_border(self):
self.pendown()
for _ in range(4):
self.fd(680)
self.rt(90)
ملف عمل لوحة الاهداف
from turtle import Turtle
class Scoreborde(Turtle):
def __init__(self,postion):
super().__init__()
self.score=0
self.hideturtle()
self.penup()
self.color('white')
self.goto(postion)
self.score_update()
def score_update(self):
self.write(f'Score:{self.score}'
,align='center'
,font=("arial",26,"bold"))
def increase_score(self):
self.clear()
self.score+=1
self.score_update()
صورة اللعبة