حل مشكلة في الكود

عايز حل لي المشكل بتاعت امضرب
جربو الكود دة و خلو الكورة تخبط في اول المضرب علشان يحصل المشكلة عندكو

main.py


from paddle import Paddles
from ball import Ball
from turtle import  Screen
import time


screen = Screen()

ball = Ball()  

screen.setup(800, 600)
screen.tracer(0)
screen.bgcolor("black")
screen.title("PING PONG")

nemo_R = Paddles((350, 0))  
nemo_L = Paddles((-350, 0))


screen.listen()
screen.onkey(nemo_R.go_Up, "Up")  
screen.onkey(nemo_R.go_Down, "Down")
screen.onkey(nemo_L.go_Up, "1")  
screen.onkey(nemo_L.go_Down, "2")

game_on = True

while game_on:
    screen.update()
    time.sleep(0.1)
    ball.goto(ball.xcor()+ball.move_x,ball.ycor()+ball.move_y)
    
    if ball.ycor() >= 290 or ball.ycor() <= -290 :

        ball.move_y *=-1
    
    if (ball.xcor()>=330 and ball.distance(nemo_R)<=50) or( ball.xcor()<=-330 and ball.distance(nemo_L)<=50):

        ball.move_x *= -1
    if ball.xcor() >= 350 or ball.xcor() <= -350:
        ball.goto(0, 0)
        ball.goto(ball.xcor()-ball.move_x,ball.ycor()-ball.move_y)

        ball.move_x *= -1  


screen.exitonclick()


ball.py

from turtle import Turtle

class Ball(Turtle):  
    
    def __init__(self):
        super().__init__()

        self.shape("circle")
        self.color("white")
        self.penup()
        self.move_x=10
        self.move_y=10
0

paddle.py

from turtle import Turtle

class Paddles(Turtle):  # Capitalize the class name
    
    def __init__(self, position):  # Fix the spelling of "position"
        
      
        super().__init__()
        self.shape("square")
        self.color("white")
        self.penup()
        self.goto(position)
        self.shapesize(stretch_wid=5, stretch_len=1)  # stretch_wid for height, stretch_len for width
        
    def go_Up(self):
        new_y = self.ycor() + 20  # Increase the movement step if needed
        self.goto(self.xcor(), new_y)
        
    def go_Down(self):
        new_y = self.ycor() - 20
        self.goto(self.xcor(), new_y)

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

نفس لمشكلة عندي الكرة تضرب باالمضرب اكثر من مرا

3 إعجابات
  1. أسماء الفئات: تأكد من أن اسم الملف والفئة متطابقان (Paddles.py بدلًا من paddle.py).

  2. إعادة تعيين موضع الكرة: استخدم فقط:

if ball.xcor() >= 350 or ball.xcor() <= -350:
    ball.goto(0, 0)
    ball.move_x *= -1
  1. تحسين اكتشاف الاصطدام: زِد قيمة المسافة من 50 إلى 60:
if (ball.xcor() >= 330 and ball.distance(nemo_R) <= 60) or (ball.xcor() <= -330 and ball.distance(nemo_L) <= 60):
    ball.move_x *= -1
إعجاب واحد (1)

انا بحثت و اضفت حاجة جديدة علشان الكود يشتغل المتغير الي اسمو ( last_hit )= القيمة دة ( None ) الي معناها قيمة فارغة علشان اما احدثها تبقا lift او right
انا الي عملتو كلو ان الكورة مبتخبطش في المضرب مرتين الي بعد البعد عنهو بسافة
ملف main

from paddle import Paddles
from ball import Ball
from turtle import  Screen
import time

screen = Screen()
ball = Ball()  

screen.setup(800, 600)
screen.tracer(0)
screen.bgcolor("black")
screen.title("PING PONG")

nemo_R = Paddles((350, 0))  
nemo_L = Paddles((-350, 0))

screen.listen()
screen.onkey(nemo_R.go_Up, "Up")  
screen.onkey(nemo_R.go_Down, "Down")
screen.onkey(nemo_L.go_Up, "1")  
screen.onkey(nemo_L.go_Down, "2")

game_on=True
last_hit = None  

while game_on:
    screen.update()
    time.sleep(0.1)
    ball.goto(ball.xcor() + ball.move_x, ball.ycor() + ball.move_y)

    if ball.ycor() >= 290 or ball.ycor() <= -290:
        ball.move_y *= -1

    if (ball.xcor() >= 330 and ball.distance(nemo_R) <= 50 and last_hit != "right") or (ball.xcor() <= -330 and ball.distance(nemo_L) <= 50 and last_hit != "left"):
        ball.move_x *= -1
        
        if ball.xcor() >= 330:
            last_hit = "right"
        else:
            last_hit = "left"

   
    if ball.xcor() > 350 or ball.xcor() < -350:
        last_hit = None


    if ball.xcor() >= 350 or ball.xcor() <= -350:
        ball.goto(0, 0)
        ball.move_x *= -1
        last_hit = None  


screen.exitonclick()

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