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)
يقول لي لا يمكن استراد الموديول لا اعرف السبب