صورة لي العبة
ملف العبة الرءيسي ( main.py )
from turtle import Screen, Turtle
from ball import Ball
from paddles import Paddels
from breakout_rewards import RewardManager, Reward
import time
screen_ = Screen()
screen_.bgcolor("black")
screen_.setup(width=800, height=600)
screen_.tracer(0)
Instructions = Turtle()
Instructions.hideturtle()
Instructions.penup()
Instructions.color("white")
Instructions.goto(-380, 100)
Instructions.write(f"Turtle Loses Lives 2\nThe circle increases the life by 2\nThe square increases the life by 1\nThe triangle increases the life by 1 \n انتظر 5 ثواني , Wait 5 seconds", align="left", font=("Arial", 24, "normal"))
screen_.update()
time.sleep(4.9)
screen_.clear()
# إعداد الشاشة
screen = Screen()
screen.bgcolor("#ADD8E6")
screen.setup(width=2000, height=1000)
screen.tracer(0)
# إنشاء كائنات
paddels_ = Paddels()
ball = Ball()
blocks_manager = RewardManager()
blocks = blocks_manager.paddles
lives = 3
lives_display = Turtle()
lives_display.hideturtle()
lives_display.color("black")
lives_display.penup()
lives_display.goto(-900, 400)
lives_display.write(f"Lives: {lives}", align="left", font=("Arial", 24, "normal"))
# حركة الأسهم
screen.listen()
screen.onkeypress(paddels_.run_Left, "Left")
screen.onkeypress(paddels_.run_Right, "Right")
# تشغيل اللعبة
time.sleep(0.2)
game_on = True
while game_on:
ball.move()
screen.update()
time.sleep(0.01)
# ارتداد الكرة عند التصادم
if ball.xcor() >= 980 or ball.xcor() <= -980:
ball.bounce_x()
# حدود السقف
if ball.ycor() >= 480:
ball.bounce_y()
# حدود الأرض
if ball.ycor() <= -500:
ball.goto(0, 0)
ball.bounce_y()
# تقليل عدد الحية
lives -= 1
lives_display.clear()
lives_display.write(f"Lives: {lives}", align="left", font=("Arial", 24, "normal"))
# انهاء اللعبة
if lives <= 0:
lives_display.clear()
lives_display.goto(0, 0)
lives_display.write("Game Over", align="center", font=("Arial", 36, "normal"))
game_on = False
break
# الاصطدام مع المضرب
if ball.distance(paddels_) <= 60 and ball.ycor() <= -460:
ball.bounce_y()
# تكرار على الطوب لإخفائه عند الاصطدام
for block in blocks[:]:
if len(blocks[:])<=0:
break
if ball.distance(block) < 50:
ball.bounce_y()
if isinstance(block, Reward):# لو الطوبة مكافأة ==True
block.is_active = True
else:
block.hideturtle()
blocks.remove(block)
# تحريك الجوائز
for block in blocks:
if isinstance(block, Reward):
block.move()
block.showturtle()
if block.distance(paddels_) < 50 and block.shape()=="turtle":
block.hideturtle()
blocks.remove(block)
lives -= 2
lives_display.clear()
lives_display.write(f"Lives: {lives}", align="left", font=("Arial", 24, "normal"))
elif block.distance(paddels_) < 50 and block.shape() == "circle":
block.hideturtle()
blocks.remove(block)
lives += 2
lives_display.clear()
lives_display.write(f"Lives: {lives}", align="left", font=("Arial", 24, "normal"))
elif block.distance(paddels_) < 50 and block.shape() == "square":
block.hideturtle()
blocks.remove(block)
lives += 1
lives_display.clear()
lives_display.write(f"Lives: {lives}", align="left", font=("Arial", 24, "normal"))
elif block.distance(paddels_) < 50 and block.shape() == "triangle":
block.hideturtle()
blocks.remove(block)
lives += 1
lives_display.clear()
lives_display.write(f"Lives: {lives}", align="left", font=("Arial", 24, "normal"))
elif block.ycor() <= -500:
block.hideturtle()
blocks.remove(block)
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.shapesize(0.8)
self.x_move = 12
self.y_move = 12
# الانتقال باستخدام x_move و y_move
def move(self):
new_x = self.xcor() + self.x_move
new_y = self.ycor() + self.y_move
self.goto(new_x, new_y)
# لعكس الاتجاه بعد الاصطدام
def bounce_y(self):
self.y_move *= -1
def bounce_x(self):
self.x_move *= -1
ملف المضرب paddle.py
from turtle import Turtle
class Paddels(Turtle):
def __init__(self):
super().__init__()
# شكل المضرب
self.color_ = ["blue", "white", "green", "braen"]
self.shape("square")
self.color("black")
self.shapesize (stretch_wid=1,stretch_len=6)
self.penup()
self.goto(0, -475)
self.move_distance = 50
def run_Left(self):
if self.xcor() > -930:
self.setx(self.xcor() - self.move_distance)
def run_Right(self):
if self.xcor() < 930:
self.setx(self.xcor() + self.move_distance)
ملف breakout_rewards.py
from turtle import Turtle
import random
class Reward(Turtle):
SHAPES = ["turtle", "circle", "square", "triangle"]
COLORS = ["red", "white", "blue", "green"]
def __init__(self, position):
super().__init__()
self.penup()
self.set_random_properties()
self.goto(position)
self.hideturtle()
self.is_active = False
def set_random_properties(self):
self.shape(random.choice(self.SHAPES))
self.color(random.choice(self.COLORS))
self.shapesize(stretch_wid=random.uniform(0.5, 0.9))
def move(self):
if self.is_active:
new_y = self.ycor() - 10
self.sety(new_y)
class PaddleBlock(Turtle):
COLOR_OPTIONS = ["#0436A4", "#812121", "#006400"]
def __init__(self, position, length):
super().__init__()
self.shape("square")
self.color(random.choice(self.COLOR_OPTIONS))
self.penup()
self.goto(position)
self.shapesize(stretch_wid=1, stretch_len=length)
class RewardManager:
def __init__(self):
self.paddle_positions = {
1: [(-850 + i * 190, 470) for i in range(10)],
2: [(-850 + i * 170, 150) for i in range(11)],
3: [(-850 + i * 150, 310) for i in range(12)],
4: [(-850 + i * 130, 230) for i in range(14)],
5: [(-850 + i * 110, 390) for i in range(16)]
}
#تخزين الطوب و الجوايز
self.paddles = []
self.rewards = []
#استدعاء الدوال
self.create_rwards()
self.create_paddles()
def create_paddles(self):
for level, positiond in self.paddle_positions.items():
self.length_ = 8 - level #8 - 5 = 3 اقل عرض عندنا 3 يبقا الرقم الي المفروض نطرح منو لحد ما يدينا 3 اخر حاجة
for position in positiond:
self.paddles.append(PaddleBlock(position=position, length=self.length_))
def create_rwards(self):
for level_, positions_rward in self.paddle_positions.items():
self.numpers_gift = (random.randint(15, 30)) // len(self.paddle_positions)
sampled_positions = random.sample(positions_rward, min(self.numpers_gift, len(positions_rward)))
self.rewards.extend(sampled_positions)
for position_rwardq in sampled_positions:
reward = Reward(position_rwardq)
self.paddles.append(reward)
# Paddle width in the game with different values of stretch_len
# Maximum width
# max_width = 1900
# badding_lift=50
# badding_Right=50
# If stretch_len = 7:
# paddles = 10 * 140 = 700
# distance = 9 * 50 = 200
# total = 1400 + 450 = 1850px
# If stretch_len = 6:
# paddles = 11* 120 = 1320
# distance = 10 * 50 = 500
# total = 1320 + 500 = 1820px
# If stretch_len = 5:
# paddles = 12 * 100 = 600
# distance = 11 * 50 = 250
# total = 1200 + 550 = 1750px
# If stretch_len = 4:
# paddles = 14 * 80 = 560
# distance = 13 * 50 = 300
# total = 1120 + 650 = 1770px
# If stretch_len = 3:
# paddles = 16 * 60 = 960
# distance = 15 * 50 = 350
# total = 960 + 750 = 1710px