كود لعبة الثعبان بلغة بايثون Python

0
بايثون, برمجة الألعاب, لعبة الثعبان, أكواد بايثون جاهزة, مشاريع بايثون, مكتبة Turtle, تعلم بايثون, كود بايثون, القرماني أكس كود, TokyoEdTech
👾 ألعاب بايثون | AQX Code

كود احترافي للعبة الثعبان (Snake Game) بلغة بايثون

نشارك معكم في هذا المقال كوداً برمجياً متكاملاً مصمماً بلغة بايثون للعبة الثعبان الشهيرة الكلاسيكية، والذي تم برمجته وتطويره من قبل المبرمج المبدع TokyoEdTech. الميزة الذهبية في هذا الكود هي احتوائه على شرح مفصل لجميع السطور البرمجية، مما يتيح لك فهماً أعمق لطريقة عمل لغة بايثون ويمنحك القدرة الكاملة على التعديل والتجربة بمرونة! 🚀

⚙️
مكتبات بايثون المطلوبة (Prerequisites)

من المهم جداً قبل استخدام ونسخ الكود البرمجي أدناه، التأكد من استدعاء وتثبيت المكتبات التالية في بيئة بايثون الخاصة بك لضمان عمل اللعبة بدون أي أخطاء:

🐢 turtle
⏱️ time
🎲 random

بعد التأكد من استدعاء هذه المكتبات، أنت الآن جاهز للانطلاق وتجربة الكود الممتع! 🎉

💻
الكود البرمجي الكامل للعبة الثعبان
snake_game_aqx.py
# Simple Snake Game in Python 3 for Beginners
# By @TokyoEdTech (Brought to you by AQX Code)

import turtle
import time
import random

delay = 0.1

# Score Variables
score = 0
high_score = 0

# Set up the screen window
wn = turtle.Screen()
wn.title("Snake Game by @TokyoEdTech")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0)  # Turns off the screen updates

# Snake head styling
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"

# Snake food styling
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0, 100)

segments = []

# Pen (Score display)
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0  High Score: 0", align="center", font=("Courier", 24, "normal"))

# Functions for movement
def go_up():
    if head.direction != "down":
        head.direction = "up"

def go_down():
    if head.direction != "up":
        head.direction = "down"

def go_left():
    if head.direction != "right":
        head.direction = "left"

def go_right():
    if head.direction != "left":
        head.direction = "right"

def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 20)

    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 20)

    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 20)

    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 20)

# Keyboard bindings (Connecting keys to movement)
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")

# Main game loop
while True:
    wn.update()

    # Check for a collision with the border
    if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
        time.sleep(1)
        head.goto(0, 0)
        head.direction = "stop"

        # Hide the segments
        for segment in segments:
            segment.goto(1000, 1000)

        # Clear the segments list
        segments.clear()

        # Reset the score & delay
        score = 0
        delay = 0.1

        pen.clear()
        pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

    # Check for a collision with the food
    if head.distance(food) < 20:
        # Move the food to a random spot
        x = random.randint(-290, 290)
        y = random.randint(-290, 290)
        food.goto(x, y)

        # Add a segment to the snake body
        new_segment = turtle.Turtle()
        new_segment.speed(0)
        new_segment.shape("square")
        new_segment.color("grey")
        new_segment.penup()
        segments.append(new_segment)

        # Shorten the delay (increase speed)
        delay -= 0.001

        # Increase the score
        score += 10
        if score > high_score:
            high_score = score

        pen.clear()
        pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

    # Move the end segments first in reverse order
    for index in range(len(segments) - 1, 0, -1):
        x = segments[index - 1].xcor()
        y = segments[index - 1].ycor()
        segments[index].goto(x, y)

    # Move segment 0 to where the head is
    if len(segments) > 0:
        x = head.xcor()
        y = head.ycor()
        segments[0].goto(x, y)

    move()

    # Check for head collision with the body segments
    for segment in segments:
        if segment.distance(head) < 20:
            time.sleep(1)
            head.goto(0, 0)
            head.direction = "stop"

            # Hide the segments
            for segment in segments:
                segment.goto(1000, 1000)
            
            segments.clear()
            score = 0
            delay = 0.1

            pen.clear()
            pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

    time.sleep(delay)

wn.mainloop()
💡 ختاماً..

تعتبر تطبيقات الألعاب البسيطة مثل "لعبة الثعبان" من أقوى التطبيقات العملية التي ترسخ المفاهيم البرمجية (مثل الحلقات التكرارية، الشروط، والدوال) في ذهن المبرمج المبتدئ. من خلال قراءة التعليقات المرفقة داخل الكود، يمكنك الآن التعديل على اللعبة، تغيير ألوان الثعبان، أو حتى زيادة سرعته لإنشاء نسختك الخاصة من اللعبة!

🎮 تحدي القرماني أكس كود!

بعد تشغيلك للكود البرمجي للعبة الثعبان، ما هو أعلى Score (رقم قياسي) استطعت تحقيقه قبل الاصطدام؟ وهل قمت بإضافة أي تعديل على الكود؟

👇 شاركنا رقمك القياسي وتجربتك في التعليقات!

إرسال تعليق

0 تعليقات
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
إرسال تعليق (0)

#buttons=( أقبل ! ) #days=(20)

يستخدم موقعنا ملفات تعريف الارتباط لتعزيز تجربتك. لمعرفة المزيد
Accept !