Ping Pong Game using Python

from turtle import Turtle

class Ball(Turtle):
def init(self):
super().init()
self.shape(“circle”)
self.color(“white”)
self.penup()
self.x_move = 10
self.y_move = 10

إعجابَين (2)

برافو كمل و إن شاء الله تستفيد

إعجاب واحد (1)

شكرا. بس هو كانت المشكلة بس في آلية الرفع. هو مش زي Github في رفع الملفات. "أنا طالب في كلية الحاسبات والمعلومات جامعة كفر الشيخ قسم IT " اللي محتاج حاجة يكلمني

إعجاب واحد (1)

متابعة المناقشة من Ping Pong Game using Python:

ball.py

from turtle import Turtle

class Ball(Turtle):
def init(self):
super().init()
self.shape(“circle”)
self.color(“white”)
self.penup()
self.x_move = 10
self.y_move = 10

main .py

from turtle import Turtle, Screen
from paddle import Paddle
from ball import Ball
from score import Scoreboard
import time

screen = Screen()
screen.setup(width = 800,height = 600)
screen.bgcolor(‘black’)
screen.title(“OctuCode: Ping Pong”)
screen.tracer(0)
r_paddle = Paddle((350, 0 ))
l_paddle= Paddle((-350, 0))
ball = Ball()
scoreboard = Scoreboard()

screen.listen()
screen.onkey(r_paddle.go_up, “Up”)
screen.onkey(r_paddle.go_down, “Down”)
screen.onkey(l_paddle.go_up, “w”)
screen.onkey(l_paddle.go_down, “s”)

default_sleep = 0.1
game_on = True
while game_on:
screen.update()
time.sleep(default_sleep)
ball.goto(ball.xcor() + ball.x_move, ball.ycor() + ball.y_move)

# Detect collision with upper and lower wall || اكتشاف التصادم مع الحائط العلوي والسفلي
if ball.ycor() >= 280 or ball.ycor() <= -280:
    ball.y_move *= -1

# Collision detection with bats || اكتشاف التصادم مع المضارب
if( ball.xcor() >= 330 and ball.distance(r_paddle) <= 50) or (ball.xcor() <= -330 and ball.distance(l_paddle) <= 50):
    ball.x_move *= -1
    default_sleep *= 0.9

# If you exit from the right || اذا خرجت من جهة اليمين
if ball.xcor() > 400:
    ball.goto((0,0))
    ball.x_move *= -1
    default_sleep = 0.1
    scoreboard.l_point()

# If you exit from the left || اذا خرجت من جهة اليسار
if ball.xcor() < -400:
    ball.goto((0,0))
    ball.x_move *= -1
    default_sleep = 0.1
    scoreboard.r_point()
 
if scoreboard.left_score == 7 or scoreboard.right_score == 7:
    scoreboard.game_over()
    game_on = False

screen.exitonclick()

paddle.py

from turtle import Turtle

class Paddle(Turtle):
def init(self, position):
super().init()
self.shape(“square”)
self.color(“white”)
self.penup()
self.goto(position)
self.shapesize(stretch_wid = 5, stretch_len = 1)

def go_up(self):
    self.goto(self.xcor(), self.ycor() + 40)
def go_down(self):
    self.goto(self.xcor(), self.ycor() - 40)

score.py

from turtle import Turtle
class Scoreboard(Turtle):
def init(self):
super().init()
self.color(“white”)
self.penup()
self.hideturtle()
self.left_score = 0
self.right_score = 0
self.update_scoreboard()

def l_point(self):
    self.left_score += 1
    self.update_scoreboard()
    
def r_point(self):
    self.right_score += 1
    self.update_scoreboard()


def update_scoreboard(self):
    self.clear()
    self.goto(-100, 250)
    self.write(self.left_score, align = 'center', font = ('courier', 40, 'normal'))
    self.goto(100, 250)
    self.write(self.right_score, align = 'center', font = ('courier', 40, 'normal'))

def game_over(self):
    self.screen.bgcolor("darkred")
    self.goto(0, 0)
    self.write(f"Game Over \nYOU WIN!", align = "center", font =("Arial", 24, "normal"))
إعجاب واحد (1)