الملف الرئيسى:
from turtle import Screen
from segment import Segment
from player import Player
import time,random
from score import Score
screen=Screen()
screen.setup(600,600)
screen.bgcolor("black")
screen.bgpic("bg.gif")
screen.tracer(0)
segment=Segment()
player=Player()
score=Score()
screen.listen()
screen.onkey(player.go_up,"Up")
screen.onkey(player.go_down,"Down")
screen.onkey(player.go_left,"Left")
screen.onkey(player.go_right,"Right")
while True:
screen.update()
time.sleep(.1)
segment.move() # تحريك الكرات
player.move() # تحريك اللاعب
# اذا اصطدم اللاعب بالحائط
if (
player.xcor() >= 280 or player.xcor() <= -280
or
player.ycor() >= 280 or player.ycor() <= -280
):
player.home() # ارجاع اللاعب للمنتصف
player.rt(180) # عكس اتجاة اللاعب
player.sd =20 # اعادة السرعة
score.score_decrease() # انقاص عدد المحاولات
# اذا اصطدم اللاعب بالكرة
for x in segment.segments:
if player.distance(x) <= 20:
x.goto(random.randint(-250,250),random.randint(-250,250))
player.sd +=5 # زيادة السرعة
score.score_increase()
# اذا انتهت المحاولات
if score.tri <= 0:
score.game_over()
break
screen.exitonclick()
ملف اللاعب
from turtle import Turtle
class Player(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.sd= 20 # متغير السرعة
self.shapesize(3,3)
self.color('orange')
def move(self):
self.fd(self.sd)
def go_up(self):
self.setheading(90)
def go_down(self):
self.setheading(270)
def go_left(self):
self.setheading(180)
def go_right(self):
self.setheading(0)
ملف الكرات
from turtle import Turtle
import random
class Segment:
def __init__(self):
self.segments=[]
self.create_segment()
def create_segment(self):
for _ in range(10): # ميثود لانشاء الكرات
new_segment=Turtle('circle')
new_segment.up()
new_segment.color(random.choice(['cyan','orange','blue','yellow','white','antique white','dark cyan']))
new_segment.goto(random.randint(-250,250),random.randint(-250,250))
new_segment.rt(random.choice((360,90,180,270)))
self.segments.append(new_segment)
def move(self):
for segment in self.segments:
segment.fd(30)
if segment.xcor() > 280 or segment.xcor() < -280:
segment.lt(180)
elif segment.ycor() > 280 or segment.ycor() < -280:
segment.lt(180)
ملف السكور
from turtle import Turtle
class Score(Turtle):
def __init__(self):
super().__init__()
self.score = 0
self.tri = 5
self.up()
self.color("white")
self.hideturtle()
self.score_writing()
def score_writing(self):
self.clear()
self.goto(-250,250)
self.write(f"Score: {self.score}",font=('arial',22,'bold'),align='left')
self.goto(250,250)
self.write(f"Tries: {self.tri}",font=('arial',22,'bold'),align='right')
def score_increase(self): # ميثود زيادة النقاط
self.score +=1
self.score_writing()
def score_decrease(self): # ميثود نقص المحاولات
self.tri -=1
self.score_writing()
def game_over(self):
self.home()
self.color('black')
self.write(f"Game over\nFinal score{self.score}"
,align="center",font=("Arial",26,'bold'))
النتيجة: