برمجة لعبة Snake باستخدام Python

برمجة لعبة Snake باستخدام Python


لعبة الثعبان تُعتبر من الألعاب الكلاسيكية والبسيطة التي تمثل تحديًا ممتعًا للكثيرين في هذا المقال، سنلقي نظرة على الكود الخاص في اللعبة  ونشارك معكم  الكود البرمجي الذي يمكنكم استخدامه لفهم كيفية إنشاء هذه اللعبة باستخدام لغة البرمجة Python.


لعبة الثعبان هي تحدٍّ كلاسيكي يتطلب من اللاعب التحكم في ثعبان يتحرك عبر لوحة اللعب، وهدفه هو تناول الطعام لزيادة طوله. مع مرور الوقت، يتزايد التحدي حيث يجب على اللاعب تجنب تصادم الثعبان بنفسه أو بحواف اللوحة.




الكود 

**********************************************************************************

import pygame

import random

pygame.init()

width, height = 640, 480

screen = pygame.display.set_mode((width, height))

black = pygame.Color(0, 0, 0)

white = pygame.Color(255, 255, 255)

red = pygame.Color(255, 0, 0)

green = pygame.Color(0, 255, 0)

orange = pygame.Color(255, 165, 0)

blue = pygame.Color(74, 243, 255)

yellow = pygame.Color(253, 245, 1)

snake_size = 20

fruit_size = 20

snake_position = [100, 50]

snake_body = [[100, 50], [90, 50], [80, 50]]

fruit_position = [random.randrange(1, (width // snake_size)) * snake_size,

                  random.randrange(1, (height // snake_size)) * snake_size]

fruit_spawn = True

direction = 'RIGHT'

change_to = direction

score = 0


clock = pygame.time.Clock()


def game_over():

    font = pygame.font.SysFont('Verdana', 25)

    text = font.render('Game Over! Your score is: ' + str(score), True, white)

    screen.blit(text, [(width - text.get_width()) // 

    2, height - text.get_height() - 80])

    pygame.display.flip()

    pygame.time.wait(2000)

    pygame.quit()


font = pygame.font.SysFont('Verdana', 20)

game_name_font = pygame.font.SysFont('Verdana', 30)

game_name_text = game_name_font.render("King Soft", True, white)

game_name_position = ((width - game_name_text.get_width()) // 2, 10)


while True:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            pygame.quit()


        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_RIGHT:

                change_to = 'RIGHT'

            if event.key == pygame.K_LEFT:

                change_to = 'LEFT'

            if event.key == pygame.K_UP:

                change_to = 'UP'

            if event.key == pygame.K_DOWN:

                change_to = 'DOWN'

    if change_to == 'RIGHT' and direction != 'LEFT':

        direction = 'RIGHT'

    if change_to == 'LEFT' and direction != 'RIGHT':

        direction = 'LEFT'

    if change_to == 'UP' and direction != 'DOWN':

        direction = 'UP'

    if change_to == 'DOWN' and direction != 'UP':

        direction = 'DOWN'

    if direction == 'RIGHT':

        snake_position[0] += snake_size

    if direction == 'LEFT':

        snake_position[0] -= snake_size

    if direction == 'UP':

        snake_position[1] -= snake_size

    if direction == 'DOWN':

        snake_position[1] += snake_size

    snake_body.insert(0, list(snake_position))

    if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:

        score += 10

        fruit_spawn = False

    else:

        snake_body.pop()

    if not fruit_spawn:

        fruit_position = [random.randrange(1, (width // snake_size)) * snake_size,

                          random.randrange(1, (height // snake_size)) * snake_size]

    fruit_spawn = True

    screen.fill(black)

    for pos in snake_body:

        pygame.draw.rect(screen, green, pygame.Rect(pos[0], pos[1], 

        snake_size, snake_size, border_radius=4))

    pygame.draw.rect(screen, red, pygame.Rect(fruit_position[0], 

    fruit_position[1], fruit_size, fruit_size, border_radius=4))

    if snake_position[0] < 0 or snake_position[0] > width - snake_size:

        game_over()

    if snake_position[1] < 0 or snake_position[1] > height - snake_size:

        game_over()

    for block in snake_body[1:]:

        if snake_position[0] == block[0] and snake_position[1] == block[1]:

            game_over()

    score_text = font.render("Score: " + str(score), True, yellow)

    screen.blit(score_text, (10, 10))

    screen.blit(game_name_text, game_name_position)

    pygame.draw.rect(screen, yellow, pygame.Rect(0, 0, width, 2))  

    pygame.draw.rect(screen, yellow, pygame.Rect(0, height - 2, width, 2)) 

    pygame.draw.rect(screen, yellow, pygame.Rect(0, 0, 2, height)) 

    pygame.draw.rect(screen, yellow, pygame.Rect(width - 2, 0, 2, height))  

    head_rect = pygame.Rect(snake_position[0], snake_position[1], snake_size, snake_size)

    fruit_rect = pygame.Rect(fruit_position[0], fruit_position[1], fruit_size, fruit_size)

    if head_rect.colliderect(fruit_rect):

        score += 10

        fruit_spawn = False

        snake_body.append([0, 0]) 

    pygame.display.update()

    clock.tick(11)

    #king soft

**********************************************************************************

 


هدفنا من هذا المقال هو توفير لمحة سريعة حول كيفية برمجة لعبة الثعبان باستخدام Python والتشجيع على استكشاف الكود .

 يمكنك تحميل الكود وتخصيصه وفقًا لأفكارك الخاصة.


لا تترددوا في استكشاف المزيد وتحسين اللعبة بإضافة ميزات جديدة أو تعديلات على الكود، لاحظوا كيف يمكن للبرمجة أن تكون ممتعة وتعليمية في نفس الوقت.


روابط للتواصل :


قناتنا على يوتيوب 

صفحتنا على فيسبوك 

حسابنا على انستقرام 

 

نسعد بكم وبملاحظاتكم دائماً ..



Previous Post
No Comment
Add Comment
comment url