ملف اللعبة الاساسى:
#01 Set Up window
from turtle import Screen
window = Screen()
window.setup(width=1000, height=800)
window.bgcolor("black")
window.title("Ping Pong Game")
window.tracer(0)
# Building the Game
# ~~~~~~~~~~~~~~~~~
from players import Player
# ------------------------
from ball import Ball
# ------------------------
from score import Score
# ------------------------
from playground import PlayGround
# ------------------------
import time
p1Color = "lawngreen"
p2Color = "SkyBlue"
# Paddles Generation
playerOne = Player(470, p1Color)
playerTwo = Player(-470, p2Color)
# Adding the game playground
playground = PlayGround()
# Generating the Ball
ball = Ball("white")
# Adding score for each player
score1 = Score(playerOne.score, playerOne.playerName, p1Color, 250)
score2 = Score(playerTwo.score, playerTwo.playerName, p2Color, -250)
# Raise the vilocity of the ball
wait = .01
window.listen()
# Player One Keys
window.onkeypress(playerOne.moveUp, "Up")
window.onkeypress(playerOne.moveDown, "Down")
window.onkeyrelease(playerOne.moveUp, "Up")
window.onkeyrelease(playerOne.moveDown, "Down")
# Player Two Keys
window.onkeypress(playerTwo.moveUp, "w")
window.onkeypress(playerTwo.moveDown, "s")
window.onkeyrelease(playerTwo.moveUp, "w")
window.onkeyrelease(playerTwo.moveDown, "s")
gameOn = True
while gameOn:
ball.move()
time.sleep(wait)
if (ball.ycor() >= 380 or ball.ycor() <= -380):
ball.yRun *= -1
# If the ball hit any of the paddles
if (450 > ball.xcor() >= 440 and ball.distance(playerOne.playerPaddle) <= 90) or (-450 < ball.xcor() <= -440 and ball.distance(playerTwo.playerPaddle) <= 90):
ball.xRun *= -1
# UI Enhancing 'Change the ball color when hit a paddle
if ball.xcor() >= 440:
ball.color(p1Color)
else:
ball.color(p2Color)
# Increasing the ball speed
wait *= .9
if (ball.xcor() >= 500 or ball.xcor() <= -500):
# Increasing players' scores
# PlayerTwo Increasing
if ball.xcor() >= 500:
playerTwo.IncreaseScore()
print(playerTwo.score)
score2.showScore(playerTwo.score)
# PlayerOne Increasing
elif ball.xcor() <= -500:
playerOne.IncreaseScore()
print(playerOne.score)
score1.showScore(playerOne.score)
# reseting
ball.home()
window.update()
ball.xRun *= -1
wait = .01
time.sleep(1)
window.update()
window.exitonclick()
المضارب:
from turtle import Turtle
class Paddle(Turtle):
"""Paddle is a turtle"""
def __init__(self, positionX, color):
super().__init__()
self.createPaddle(positionX, color)
def createPaddle(self, PX, cr):
"""PX: Position of the paddle on x axis | cr: is the color of the paddle"""
self.shape("square")
self.penup()
self.shapesize(stretch_wid=8, stretch_len=1)
self.setx(PX)
self.color(cr)
انشاء اللاعبين:
from paddles import Paddle
from turtle import Screen, Turtle
window = Screen()
class Player:
def __init__(self, x, c):
"""x: for x Position | c: for color of the Paddle"""
self.playerName = window.textinput("Ping Pong Game", "Welcome, Gentleman\nWhat's your Nick name?").capitalize()
self.playerPaddle = Paddle(x, c)
self.score = 0
def IncreaseScore(self):
self.score += 10
# Movement Functions
def moveUp(self):
self.playerPaddle.goto(self.playerPaddle.xcor(), self.playerPaddle.ycor() + 20)
def moveDown(self):
self.playerPaddle.goto(self.playerPaddle.xcor(),self.playerPaddle.ycor() - 20)
def stop(self):
self.playerPaddle.sety(0)
انشاء النقاط:
from turtle import Turtle
class Score(Turtle):
def __init__(self, score, name, color, xp):
super().__init__()
self.penup()
self.hideturtle()
self.color(color)
self.goto(xp, 330)
self.name = name
self.showScore(score)
def showScore(self, score):
self.clear()
return self.write(f"{self.name}:{score}", font=("courier", 24, "normal"), align= "center")
ملف الكرة:
from turtle import Turtle
class Ball(Turtle):
def __init__(self, clr):
super().__init__()
self.shape("circle")
self.penup()
self.color(clr)
self.xRun = 4
self.yRun = 4
def move(self):
self.goto(self.xcor() + self.xRun, self.ycor() + self.yRun)
واخيرا ملف تقسيمة الملعب:
from turtle import Turtle
class PlayGround(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.color("white")
self.hideturtle()
self.goto(490, 390)
self.pendown()
self.square()
self.square()
self.penup()
self.setx(0)
self.pendown()
self.sety(-390)
self.penup()
self.home()
self.shape("circle")
self.showturtle()
self.color("black")
def square(self):
self.right(90)
self.forward(780)
self.right(90)
self.forward(980)
*والحمد لله رب العالمين على كل شئ وربنا يوفقنا جميعا *