مساعدة في كود ping pong (scoreboard) 🤷‍♂️

scoreboard.py
from turtle import Turtle

class ScoreBoard(Turtle):
def init(self, x):
super().init()
self.score = 0
self.color(‘white’)
self.penup()
self.goto(x,260)
self.hideturtle()
self.update_scoreboard()

def update_scoreboard(self):
    self.clear()
    self.write(self.score, align="center", font=("Arial", 24, "normal") )

def p_one(self):
    self.score +=1 

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)

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

def increase_speed(self):
    self.x_move += 1
    self.y_move += 1   

def speed_reset(self) :
    self.x_move = 10
    self.y_move = 10

main.py
from turtle import Screen, Turtle
from paddle import Paddle
from ball import Ball
import time
from scoreboard import ScoreBoard

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()
r_score = ScoreBoard(2)
l_score = ScoreBoard(-2)

screen.listen()
screen.onkey(r_paddle.go_up, ‘Up’)
screen.onkey(r_paddle.go_down, ‘Down’)

screen.onkey(l_paddle.go_up, ‘s’)
screen.onkey(l_paddle.go_down, ‘w’)

game_on = True
while game_on:
screen.update()
# start move the ball
time.sleep(0.1)
ball.goto(ball.xcor()+ball.x_move, ball.ycor()+ball.y_move)
# Detection of collision with the upper and lower wall
if ball.ycor() >= 280 or ball.ycor() <= -280:
ball.y_move *= -1
# Collision detection with rackets
if (ball.xcor() <= -330 and ball.distance(l_paddle) <= 50) or (ball.xcor() >= 330 and ball.distance(r_paddle) <= 50) :
ball.x_move *= -1
# If you exit from the right side
if ball.xcor() > 330:
l_score.p_one()
ball.reset()
ball.goto(0,0)
ball.x_move *= -1
# If you exit from the left side
if ball.xcor() < -330:
r_score.p_one()
ball.reset()
ball.goto(0,0)
ball.x_move *= -1

ball.increase_speed()

screen.exitonclick()

tirmenal
Traceback (most recent call last):
File “c:\Users\lenovo\OneDrive\Desktop\Ping Pong\main.py”, line 5, in
from scoreboard import ScoreBoard
ImportError: cannot import name ‘ScoreBoard’ from ‘scoreboard’ (c:\Users\lenovo\OneDrive\Desktop\Ping
Pong\scoreboard.py)

يقول لي لا يمكن استراد الموديول لا اعرف السبب :thinking::thinking:

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

يبدو أن المشكلة قد تكون ناتجة عن استخدام علامات اقتباس غير صحيحة (‘ و ’ بدلاً من " أو ') في أجزاء من الكود. إليك بعض الخطوات للتحقق وإصلاح المشكلة:

استبدال علامات الاقتباس: تأكد من استخدام علامات الاقتباس العادية " " بدلاً من ‘ ’. قم باستبدالها في جميع الأماكن التي تظهر فيها هذه المشكلة، مثل:
    self.color(‘white’) إلى self.color("white")
    screen.bgcolor(‘black’) إلى screen.bgcolor("black")
    screen.title(‘Octucode: Ping Pong’) إلى screen.title("Octucode: Ping Pong")
    وهكذا لجميع العبارات التي تحتوي على علامات اقتباس غير صحيحة.

تأكد من صحة بناء الجملة (__init__): يبدو أن هناك خطأ آخر في تعريفات الدوال، حيث لم يتم كتابة دالة __init__ بشكل صحيح، بل كُتبت بدون شرطات سفلية مزدوجة. يجب تعديلها كالتالي:

في ملف scoreboard.py:

def init(self, x):

في ملف paddle.py:

def init(self, position):

في ملف ball.py:

def __init__(self):

تعديل المسافات البادئة: تأكد من أن المسافات البادئة (indentation) مضبوطة بشكل صحيح في الكود؛ إذ يجب أن تكون كل خطوة داخل الدوال والفروع في الكود مرتبة.

إعادة التحقق من أسماء الملفات: تأكد من أن اسم الملف هو scoreboard.py وليس أي اسم آخر (مثل score_board.py أو Scoreboard.py).
3 إعجابات

حصلت معايا
والكود مكنش فى أى مشكلة
وحطيت الكود بتاع scoreboard
فى ملف تانى من ملفات اللعبة
واستوردته منها
واشتغل معايا بفضل الله

غالبا بيبقى تهييس من ال vsc

3 إعجابات

ذلك لي عملت انا ايضا و اشتغل

إعجابَين (2)