إليكم كود لعبه البينج بونج للهواتف المحموله:
الكود الرئيسي:
from turtle import Turtle , Screen
from paddle import Paddle
from ball import Ball
from luxuries import Luxuries
from time import sleep
window=Screen ()
window.setup (550,550)
window.tracer (0)
window.bgcolor ('light gray')
r_paddle = Paddle ((280,0))
l_paddle = Paddle ((-280,0))
ball = Ball ()
luxuries = Luxuries ()
def choice ( x, y):
if x>0 and y>0:
r_paddle.up ()
elif x>0 and y<0:
r_paddle.dawn ()
elif x<0 and y>0:
l_paddle.up ()
elif x<0 and y<0:
l_paddle.dawn ()
window.onscreenclick (choice)
time_sleep = 0.1
while True:
ball.move ()
if ( ball.xcor() >= 260 and ball.distance (r_paddle) <= 50 ):
ball.goto (ball.xcor()-5,ball.ycor())
ball.x_pos *= -1
time_sleep *= 0.9
if ( ball.xcor() <= -260 and ball.distance (l_paddle) <= 50 ):
ball.goto (ball.xcor()+5,ball.ycor())
ball.x_pos *= -1
time_sleep *= 0.9
if (ball.ycor() >= 300) or (ball.ycor() <= -300):
ball.y_pos *= -1
if ( ball.xcor() >= 300 ) :
luxuries.player2 += 1
luxuries.print_score(luxuries.write)
ball.goto (0,0)
ball.x_pos *= -1
time_sleep = 0.1
if ( ball.xcor() <= -300 ):
luxuries.player1 += 1
luxuries.print_score(luxuries.write)
ball.goto (0,0)
ball.x_pos *= -1
time_sleep = 0.1
sleep (time_sleep)
window.update ()
window.exitonclick ()
كود المضارب:
from turtle import Turtle
class Paddle (Turtle):
def __init__(self,position):
super().__init__()
self.shape ('square')
self.penup ()
self.shapesize (5,1)
self.goto (position)
self.color ('blue')
def up (self):
if not self.ycor () >= 260:
self.goto (self.xcor(),self.ycor () + 20)
def dawn (self):
if not self.ycor () <= -260:
self.goto (self.xcor(),self.ycor () - 20)
كود الكره:
from turtle import Turtle
from time import sleep
class Ball (Turtle):
def __init__(self):
super ().__init__()
self.shape ('circle')
self.color ('red')
self.penup ()
self.x_pos = 10
self.y_pos = 10
def move (self):
self.goto (self.xcor () + self.x_pos , self.ycor () + self.y_pos)
كود لبعض الإضافات:
from turtle import Turtle
class Luxuries ():
def __init__(self):
self.heading = (90,270,0,180)
self.create_border ()
self.lists = [
[-450,200,90],
[450,200,90],
[-450,-200,270],
[450,-200,270]]
self.create_arrow ()
self.player1 = 0
self.player2 = 0
write = Turtle ()
self.write = write
self.print_score (self.write)
def create_border (self):
for x in range (4):
border = Turtle ('square')
border.penup ()
border.shapesize (33,1)
border.setheading (self.heading[x])
border.forward (320)
def create_arrow (self):
for x in range (4):
arrow = Turtle ('triangle')
arrow.penup ()
arrow.shapesize (7,5,7)
arrow.goto (self.lists [x][0],self.lists [x][1])
arrow.setheading (self.lists [x][2])
def print_score (self,write):
write.clear ()
write.clear ()
write.hideturtle ()
write.penup ()
write.goto(0,250)
write.write (f'{self.player2} {self.player1}',align = 'center', font = ('Arial' , 10, 'normal'))
ملاحظه : لازم يكون التليفون مقلوب بالجنب (وضع الدوران التلقائي) علشان تشوف الاسهم