عملت لعبة جديدة ( Brick )

صورة لي العبة


ملف العبة الرءيسي ( 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
إعجابَين (2)

اللهم بارك جميل جدا
الله يوفقك

إعجابَين (2)

عمل رائع
بس ياريت تشرحلنا ملف breakout_rewards.py
لانو مافهمت طريقة حاولت كتير ومافهمت
:sweat_smile:

إعجابَين (2)

تمام
بس الاول في حاجة حابب اوضحها في دول ولا

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)

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

بص هو الشرح علي قضي بس حاول تفهم :joy:
و لو في اي حاجة اسال هجوبك ان شاء الله

# و دة عمليات حسابية علشان الطوب سبقا مظبوط 

# 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





class RewardManager:  # تعريف الفئة RewardManager التي ستدير المكافآت والمضارب في اللعبة
    def __init__(self): 
        # هنا بنحدد مواقع الطوب والمكافآت حسب المستوى
        self.paddle_positions = {
            1: [(-850 + i * 190, 470) for i in range(10)],  # تحديد مواقع الطوب للمستوى 1
            2: [(-850 + i * 170, 150) for i in range(11)],  # تحديد مواقع الطوب للمستوى 2
            3: [(-850 + i * 150, 310) for i in range(12)],  # تحديد مواقع الطوب للمستوى 3
            4: [(-850 + i * 130, 230) for i in range(14)],  # تحديد مواقع الطوب للمستوى 4
            5: [(-850 + i * 110, 390) for i in range(16)]   # تحديد مواقع الطوب للمستوى 5
        }
        # هنا بنخزن الطوب والمكافآت في قوائم فارغة
        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  # حساب طول المضرب لكل مستوى (طول المضرب يتناقص مع كل مستوى)
            # عند المستوى 5، سيكون طول المضرب 3 (8 - 5 = 3)

            for position in positiond:  # المرور عبر كل موقع في المستوى الحالي
               
                self.paddles.append(PaddleBlock(position=position, length=self.length_)) # إضافة مضرب (PaddleBlock) مع الموقع والطول المحدد

    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)  



 #(( .items() )الوظيفة دي بترجع حجتين يعني في المثال دة بترجع السطر رقم كام و موقع الكوبة)

 #  self.length_ = 8 - level #8 - 5 = 3 اقل عرض  عندنا 3 يبقا الرقم الي المفروض نطرح منو لحد ما يدينا 3 اخر حاجة دي بتاعت عرض الطوب 

 #  self.numpers_gift = (random.randint(15, 30)) // len(self.paddle_positions) للحصول على توزيع عادل للمكافآت بين المستويات.
 #الكود دة بيدين رقم في كل مرة من 15 الي 30 و بيتقصم علي عدد اسطر الطوب ( random.randint(15, 30)) // len(self.paddle_positions )

 #  sampled_positions = random.sample(positions_rward, min(self.numpers_gift, len(positions_rward)))

#random.sample دي بتختار  عدد عشوائي من اللي في القائمة و بتخزنهم في قائمه 

# min() ودي  بتاخد الاصغر  من اللي في القائمة و بتخزنهم في قائمه استخدمتها علشان مش عايزها تديني عدد اكبر من المواقع الي عندي



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

و فيه حجات جديدة انا بحثت عنها و فهمتها فا لو معرفتش اشرحة استني أ. ابراهيم هيفهمهالك ان شاء الله

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

ما شاء الله عليك دائما مبدع اللعبة رائعة والتجربة تذكرنا باللعبة زمان :ok_hand:

إعجابَين (2)

شكرا اخي بدر علي تعليقق و تجربتك لي اللعبة :rose:

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

اول شي دالة move ليش استخدامها

إعجابَين (2)

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

إعجابَين (2)

ولله حاولت اقرأ بس فتت بلحيط اكتر
عمليات لحساب الكثيرة لماذا استخدمتها

إعجابَين (2)

علشان احسب اوزع الطوب بشكل مناسب و اخلي في مسافات بين كل طوبة

إعجابَين (2)

تمام شكرا لك على توضيح
لازم حط لكود عندي واقرأ اكتر من مرة
وشكرا على توضيح
بعد توضيح وشرح اكيد هلئ بس راجع لكود اكتر من مرة رح افهم
شكرا ئلك عزبتك معي

إعجابَين (2)

ربنا يوفقق و يعنك بالتوفيق

إعجابَين (2)