Snake tall

Snake game
 avatar
unknown
csharp
a month ago
2.9 kB
1
Indexable
import pygame
import random
import time

# Initialize Pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 800, 600

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

# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")

# Clock and font
clock = pygame.time.Clock()
font = pygame.font.SysFont("comicsans", 35)

# Snake settings
snake_pos = [100, 50]  # [x, y]
snake_body = [[100, 50], [90, 50], [80, 50]]
snake_direction = 'RIGHT'
change_to = snake_direction
speed = 15

# Ball settings
ball_pos = [random.randrange(1, (WIDTH // 10)) * 10, random.randrange(1, (HEIGHT // 10)) * 10]
score = 0

def message(text, color, x, y):
    """Display a message on the screen."""
    msg = font.render(text, True, color)
    screen.blit(msg, [x, y])

# Main game loop
def game():
    global snake_pos, snake_body, snake_direction, change_to, ball_pos, score

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP and not change_to == 'DOWN':
                    change_to = 'UP'
                if event.key == pygame.K_DOWN and not change_to == 'UP':
                    change_to = 'DOWN'
                if event.key == pygame.K_LEFT and not change_to == 'RIGHT':
                    change_to = 'LEFT'
                if event.key == pygame.K_RIGHT and not change_to == 'LEFT':
                    change_to = 'RIGHT'

        # Update direction
        snake_direction = change_to

        if snake_direction == 'UP':
            snake_pos[1] -= 10
        if snake_direction == 'DOWN':
            snake_pos[1] += 10
        if snake_direction == 'LEFT':
            snake_pos[0] -= 10
        if snake_direction == 'RIGHT':
            snake_pos[0] += 10

        # Snake body growing
        snake_body.insert(0, list(snake_pos))
        if snake_pos == ball_pos:
            score += 1
            ball_pos = [random.randrange(1, (WIDTH // 10)) * 10, random.randrange(1, (HEIGHT // 10)) * 10]
        else:
            snake_body.pop()

        # Game over conditions
        if (
            snake_pos[0] < 0 or
            snake_pos[0] > WIDTH-10 or
            snake_pos[1] < 0 or
            snake_pos[1] > HEIGHT-10
        ):
            running = False

        for block in snake_body[1:]:
            if snake_pos == block:
                running = False

        # Graphics
        screen.fill(BLACK)
        for block in snake_body:
            pygame.draw.rect(screen, GREEN, pygame.Rect(block[0], block[1], 10, 10))

        pygame.draw.rect(screen, RED, pygame.Rect(ball_pos[0], ball_pos[1], 10, 10))

        message(f"Score: {score}", WHITE, 10, 10)

        pygame.display.flip()
        clock.tick(speed)

    time.sleep(2)
    pygame.quit()
    quit()

game()
Editor is loading...
Leave a Comment