Untitled

mail@pastecode.io avatar
unknown
python
a year ago
3.6 kB
21
Indexable
Never
import pygame
import random
import sys
from time import sleep

# Initialize pygame
pygame.init()

# Set up colors for background, snake and food
background_color = (0xFF, 0xFF, 0xF5)
snake_color = (0x20, 0x9f, 0xb5)
food_color = (0xd2, 0xf3, 0x39)

# Set up the window size and title of game
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Snake Game')

# Define the snake class and its movement
class Snake:
    def __init__(self):
        self.position = [100, 50]
        self.body = [[100, 50], [90, 50], [80, 50]]
        self.direction = 'RIGHT'
        self.change_to = self.direction

    def move(self):
        # Check for direction change input from user
        if self.change_to == 'LEFT':
            self.direction = 'LEFT'
        if self.change_to == 'RIGHT':
            self.direction = 'RIGHT'
        if self.change_to == 'UP':
            self.direction = 'UP'
        if self.change_to == 'DOWN':
            self.direction = 'DOWN'

        # Update position based on direction
        if self.direction == 'LEFT':
            self.position[0] -= 10
        elif self.direction == 'RIGHT':
            self.position[0] += 10
        elif self.direction == 'UP':
            self.position[1] -= 10
        elif self.direction == 'DOWN':
            self.position[1] += 10

        # Update body of snake
        self.body.insert(0, list(self.position))
        self.body.pop()

    def draw_snake(self):
        for part in self.body:
            pygame.draw.rect(screen, snake_color, pygame.Rect(part[0], part[1], 10, 10))

# Define the food class and its position on screen    
class Food:
    def __init__(self):
        self.position = [random.randint(1, WINDOW_WIDTH//10)*10, random.randint(1, WINDOW_HEIGHT//10)*10]

    def draw_food(self):
        pygame.draw.rect(screen, food_color, pygame.Rect(self.position[0], self.position[1], 10, 10))

# Create snake and food objects
snake = Snake()
food = Food()

# Define the game loop
def game_loop():
    score = 0
    # Main game loop
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT and snake.direction != 'RIGHT':
                    snake.change_to = 'LEFT'
                elif event.key == pygame.K_RIGHT and snake.direction != 'LEFT':
                    snake.change_to = 'RIGHT'
                elif event.key == pygame.K_UP and snake.direction != 'DOWN':
                    snake.change_to = 'UP'
                elif event.key == pygame.K_DOWN and snake.direction != 'UP':
                    snake.change_to = 'DOWN'

        # Call the move function of snake
        snake.move()

        # Check if snake has eaten food or not
        if snake.position == food.position:
            score += 1
            food = Food()
        else:
            snake.body.pop()

        # Fill background color to screen
        screen.fill(background_color)

        # Draw the snake and food on screen
        snake.draw_snake()
        food.draw_food()
        draw_score(score)
        pygame.display.update()

        sleep(0.1)

# Define a function to display score on the screen
def draw_score(score):
    font = pygame.font.Font('freesansbold.ttf', 25)
    text = font.render('Score: ' + str(score), True, (0xFF, 0xFF, 0xF5))
    screen.blit(text, [10, 10])

# Call the game loop function to start the game
game_loop()