Skip to content

Conversation

onchaimae1-dotcom
Copy link

import pygame
import random

pygame.init()

إعدادات الشاشة

WIDTH, HEIGHT = 600, 400
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("لعبة الثعبان")

ألوان

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

إعدادات الثعبان

snake_block = 20
snake_speed = 15

clock = pygame.time.Clock()

font_style = pygame.font.SysFont(None, 35)
score_font = pygame.font.SysFont(None, 25)

def your_score(score):
value = score_font.render("النتيجة: " + str(score), True, WHITE)
win.blit(value, [0, 0])

def message(msg, color, x, y):
mesg = font_style.render(msg, True, color)
win.blit(mesg, [x, y])

def gameLoop():
game_over = False
game_close = False

x1 = WIDTH // 2
y1 = HEIGHT // 2

x1_change = 0
y1_change = 0

snake_List = []
Length_of_snake = 1

foodx = round(random.randrange(0, WIDTH - snake_block) / 20.0) * 20.0
foody = round(random.randrange(0, HEIGHT - snake_block) / 20.0) * 20.0

while not game_over:

    while game_close:
        win.fill(BLACK)
        message("خسرت! اضغط Q للخروج أو C للعب مجددًا", RED, 50, HEIGHT // 3)
        your_score(Length_of_snake - 1)
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    game_over = True
                    game_close = False
                if event.key == pygame.K_c:
                    gameLoop()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x1_change = -snake_block
                y1_change = 0
            elif event.key == pygame.K_RIGHT:
                x1_change = snake_block
                y1_change = 0
            elif event.key == pygame.K_UP:
                y1_change = -snake_block
                x1_change = 0
            elif event.key == pygame.K_DOWN:
                y1_change = snake_block
                x1_change = 0

    if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
        game_close = True

    x1 += x1_change
    y1 += y1_change
    win.fill(BLACK)
    pygame.draw.rect(win, GREEN, [foodx, foody, snake_block, snake_block])

    snake_Head = []
    snake_Head.append(x1)
    snake_Head.append(y1)
    snake_List.append(snake_Head)

    if len(snake_List) > Length_of_snake:
        del snake_List[0]

    for x in snake_List[:-1]:
        if x == snake_Head:
            game_close = True

    for x in snake_List:
        pygame.draw.rect(win, WHITE, [x[0], x[1], snake_block, snake_block])

    your_score(Length_of_snake - 1)

    pygame.display.update()

    if x1 == foodx and y1 == foody:
        foodx = round(random.randrange(0, WIDTH - snake_block) / 20.0) * 20.0
        foody = round(random.randrange(0, HEIGHT - snake_block) / 20.0) * 20.0
        Length_of_snake += 1

    clock.tick(snake_speed)

pygame.quit()
quit()

gameLoop()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant