පයිතන් වලින් නයි ගේම් එකක් හදමු

 




YouTube ලින්ක් එක


Python code

import pygame

import random


# Initialize Pygame

pygame.init()


# Constants

WIDTH, HEIGHT = 400, 400

GRID_SIZE = 20

GRID_WIDTH = WIDTH // GRID_SIZE

GRID_HEIGHT = HEIGHT // GRID_SIZE

SNAKE_SPEED = 5


# Colors

WHITE = (255, 255, 255)

GREEN = (0, 255, 0)

RED = (255, 0, 0)

BLACK = (0, 0, 0,)


# Directions

UP = (0, -1)

DOWN = (0, 1)

LEFT = (-1, 0)

RIGHT = (1, 0)


# Font

score = 0


# Initialize the screen

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("Snake Game")


# Initialize the snake

snake = [(GRID_WIDTH // 2, GRID_HEIGHT // 2)]

snake_direction = RIGHT


# Initialize the food

food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1))


# Game loop

running = True

clock = pygame.time.Clock()


# Display "Press any key to start" message

font = pygame.font.Font(None, 36)

start_text = font.render("Press any key to start", True, GREEN)


screen.fill(WHITE)

screen.blit(start_text, (WIDTH // 2 - 140, HEIGHT // 2 - 20))

pygame.display.flip()


# Wait for a key press to start the game

start = False

while not start:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            pygame.quit()

            quit()

        elif event.type == pygame.KEYDOWN:

            start = True


# Game loop

while running:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            #running = False

            pygame.quit()

            quit()

        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_UP and snake_direction != DOWN:

                snake_direction = UP

            elif event.key == pygame.K_DOWN and snake_direction != UP:

                snake_direction = DOWN

            elif event.key == pygame.K_LEFT and snake_direction != RIGHT:

                snake_direction = LEFT

            elif event.key == pygame.K_RIGHT and snake_direction != LEFT:

                snake_direction = RIGHT


    # Move the snake

    new_head = (snake[0][0] + snake_direction[0], snake[0][1] + snake_direction[1]) #coordinate of 

    snake.insert(0, new_head)


    # Check if the snake ate the food

    if snake[0] == food:

        food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1))

        score += 1

    else:

        snake.pop()


    # Check if the snake collided with itself or the wall

    if (

        snake[0] in snake[1:]

        or snake[0][0] < 0

        or snake[0][0] >= GRID_WIDTH

        or snake[0][1] < 0

        or snake[0][1] >= GRID_HEIGHT

    ):

        running = False

        

    

    # Draw the screen

    screen.fill(BLACK)

    for segment in snake:

        pygame.draw.rect(

            screen, GREEN, (segment[0] * GRID_SIZE, segment[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE)

        )

    pygame.draw.rect(screen, RED, (food[0] * GRID_SIZE, food[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))


    # Display the score in the top left corner

    font = pygame.font.Font(None, 36)

    score_text = font.render(f"Score: {score}", True, GREEN)

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


    pygame.display.flip()


    # Control game speed

    clock.tick(SNAKE_SPEED)


#after while loop

# Print "Game Over" and final score

font = pygame.font.Font(None, 72)

game_over_text = font.render("Game Over", True, RED)

score_text = font.render(f"Score: {score}", True, GREEN)

screen.fill(WHITE)

screen.blit(game_over_text, (WIDTH // 2 - 100, HEIGHT // 2 - 50))

screen.blit(score_text, (WIDTH // 2 - 70, HEIGHT // 2 + 50))

pygame.display.flip()

while True:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            pygame.quit()

            quit()





Comments

Popular posts from this blog