I share Ping Pong Game with you

from turtle import Turtle,Screen
import time

class Paddle(Turtle):
def init(self,position):
super().init()
self.color(‘white’)
self.shape(‘square’)
self.shapesize(5,1)
self.penup()
self.goto(position)

def move_up(self):
    if self.ycor() < 270:
        self.sety(self.ycor()+30)

def move_down(self):
    if self.ycor() > -270:
        self.sety(self.ycor()-30)

class Ball(Turtle):
def init(self):
super().init()
self.color(‘blue’)
self.shape(‘circle’)
self.penup()
self.speed = 3
self.dx = 1
self.dy = -1

def move(self):
    self.setx(self.xcor()+1 * self.speed * self.dx)
    self.sety(self.ycor()+1 * self.speed * self.dy)

class Score_baord(Turtle):
def init(self,postion,name):
super().init()
self.color(‘white’)
self.penup()
self.hideturtle()
self.goto(postion)
self.score = 0
self.write(f’{name}: {self.score}',align=‘center’,font=(‘Arial’,22,‘normal’))

def increase_score(self,name):
    self.score +=1
    self.clear()
    self.write(f'{name}: {self.score}',align='center',font=('Arial',22,'normal'))

def winning(self,name):
    self.screen.clear()
    self.screen.bgcolor('green')
    self.goto(0,0)
    self.write(f"{name} is winning",align='center',font=('Arial',22,'normal'))

screen = Screen()
screen.bgcolor(‘black’)
screen.setup(width=800,height=600)
screen.title(‘Ping Pong Game’)
Player_A = screen.textinput(‘player name’,"Player_1 name: ")
Player_b = screen.textinput(‘player name’,"Player_2 name: ")
screen.tracer(0)

r_paddle = Paddle((370,0))
l_paddle = Paddle((-370,0))
ball = Ball()
r_score = Score_baord((200,250),Player_A)
l_score = Score_baord((-200,250),Player_b)

game_on = True
while game_on:
screen.update()
time.sleep(0.01)
ball.move()

screen.listen()
screen.onkey(l_paddle.move_up,'w')
screen.onkey(l_paddle.move_down,'s')
screen.onkey(r_paddle.move_up,'Up')
screen.onkey(r_paddle.move_down,'Down')

# border collision
if ball.ycor() > 280 or ball.ycor() < -280:
    ball.dy *=-1

if ball.xcor() > 375 or ball.xcor() < -375:
    if ball.xcor() > 375:
        l_score.increase_score(Player_b)
    else:
        r_score.increase_score(Player_A)

    ball.home()
    ball.dx *=-1
    ball.dy =-1

if r_score.score == 5 or l_score.score == 5:
    game_on = False
    if l_score.score == 5 :
        l_score.winning(Player_b)
    else:
        r_score.winning(Player_A)
    

# hitting paddle 
if ( ball.dx > 0 ) and (360 < ball.xcor() < 370) and r_paddle.ycor()+50 >= ball.ycor() >= r_paddle.ycor()-50:
    ball.dx *=-1

if ( ball.dx < 0 ) and (-360 > ball.xcor() > -370) and l_paddle.ycor()+50 >= ball.ycor() >= l_paddle.ycor()-50:
    ball.dx *=-1

screen.exitonclick()

4 إعجابات

ممكن اخي ترسل الكود منسق كاملا حتى يسهل علينا تجربته

إعجابَين (2)