Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: snake_game.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script implements a simple Snake game using Pygame.
- Functions:
- - display_text: Displays text on the screen.
- - draw_snake: Draws the snake on the game display.
- - game_over_screen: Displays the game over screen with the final score.
- - draw_gradient_background: Fills the background with a gradient of dark green.
- - game_loop: Main game loop that handles game logic, user input, and rendering.
- Requirements:
- - Python 3.x
- - Pygame
- Usage:
- 1. Make sure Python and Pygame are installed on your system.
- 2. Run the script using Python.
- 3. Use the arrow keys to control the snake.
- 4. Eat the red food blocks to grow the snake and earn points.
- 5. Avoid running into the walls or the snake itself.
- 6. Press 'Q' to quit the game at any time.
- Additional Notes:
- - The snake starts in the center of the screen and moves in the direction specified by the arrow keys.
- - The game ends when the snake collides with the walls or itself.
- - The player's score increases by 10 points each time the snake eats food.
- - The game over screen displays the final score and provides options to play again or quit.
- - The background is filled with a gradient of dark green color.
- """
- import pygame
- import random
- # Initialize Pygame
- pygame.init()
- # Define colors
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- RED = (255, 0, 0)
- GREEN = (0, 128, 0) # Dark green
- # Set up the game window
- WINDOW_WIDTH = 800
- WINDOW_HEIGHT = 600
- game_display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
- pygame.display.set_caption("Snake Game")
- # Set up the clock
- clock = pygame.time.Clock()
- # Set up the font
- font = pygame.font.SysFont(None, 30)
- # Define block size and FPS
- BLOCK_SIZE = 20
- FPS = 10
- # Define directions
- UP = 0
- DOWN = 1
- LEFT = 2
- RIGHT = 3
- # Function to display text on the screen
- def display_text(text, color, x, y):
- text_surface = font.render(text, True, color)
- game_display.blit(text_surface, (x, y))
- # Function to draw the snake
- def draw_snake(snake_list):
- for i, segment in enumerate(snake_list):
- color = BLACK if i == 0 else GREEN # First block is black, rest are green
- pygame.draw.rect(
- game_display, color, [segment[0], segment[1], BLOCK_SIZE, BLOCK_SIZE]
- )
- # Function to display game over screen
- def game_over_screen(score):
- game_display.fill(WHITE)
- display_text(
- " Game Over !", RED, WINDOW_WIDTH / 2 - 100, WINDOW_HEIGHT / 2 - 30
- )
- display_text(
- f" Your Score: {score}", BLACK, WINDOW_WIDTH / 2 - 80, WINDOW_HEIGHT / 2 + 10
- )
- display_text(
- "Press C to Play Again or Q to Quit",
- BLACK,
- WINDOW_WIDTH / 2 - 160,
- WINDOW_HEIGHT / 2 + 50,
- )
- pygame.display.update()
- # Function to fill background with gradient
- def draw_gradient_background():
- for y in range(WINDOW_HEIGHT):
- shade = int(
- 255 * (1 - (y / WINDOW_HEIGHT))
- ) # Calculate shade based on y-coordinate
- pygame.draw.rect(game_display, (0, shade, 0), (0, y, WINDOW_WIDTH, 1))
- # Main game loop
- def game_loop():
- snake_list = []
- snake_length = 1
- snake_head = [WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2]
- direction = RIGHT
- score = 0
- food_pos = [
- random.randrange(0, WINDOW_WIDTH - BLOCK_SIZE, BLOCK_SIZE),
- random.randrange(0, WINDOW_HEIGHT - BLOCK_SIZE, BLOCK_SIZE),
- ]
- game_exit = False
- game_over = False
- while not game_exit:
- while game_over:
- game_over_screen(score)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- game_exit = True
- game_over = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_q:
- game_exit = True
- game_over = False
- elif event.key == pygame.K_c:
- game_loop()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- game_exit = True
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT and direction != RIGHT:
- direction = LEFT
- elif event.key == pygame.K_RIGHT and direction != LEFT:
- direction = RIGHT
- elif event.key == pygame.K_UP and direction != DOWN:
- direction = UP
- elif event.key == pygame.K_DOWN and direction != UP:
- direction = DOWN
- # Move the snake
- if direction == UP:
- snake_head[1] -= BLOCK_SIZE
- elif direction == DOWN:
- snake_head[1] += BLOCK_SIZE
- elif direction == LEFT:
- snake_head[0] -= BLOCK_SIZE
- elif direction == RIGHT:
- snake_head[0] += BLOCK_SIZE
- # Check for collision with food
- if snake_head[0] == food_pos[0] and snake_head[1] == food_pos[1]:
- food_pos = [
- random.randrange(0, WINDOW_WIDTH - BLOCK_SIZE, BLOCK_SIZE),
- random.randrange(0, WINDOW_HEIGHT - BLOCK_SIZE, BLOCK_SIZE),
- ]
- snake_length += 1
- score += 10
- # Update the snake list
- snake_list.insert(0, list(snake_head))
- if len(snake_list) > snake_length:
- del snake_list[-1]
- # Check for collision with walls or itself
- if (
- snake_head[0] >= WINDOW_WIDTH
- or snake_head[0] < 0
- or snake_head[1] >= WINDOW_HEIGHT
- or snake_head[1] < 0
- or snake_head in snake_list[1:]
- ):
- game_over = True
- # Fill the background with gradient
- draw_gradient_background()
- # Draw the food
- pygame.draw.rect(
- game_display, RED, [food_pos[0], food_pos[1], BLOCK_SIZE, BLOCK_SIZE]
- )
- # Draw the snake
- draw_snake(snake_list)
- # Display score
- display_text(f"Score: {score}", BLACK, 10, 10)
- # Update the display
- pygame.display.update()
- # Set the FPS
- clock.tick(FPS)
- pygame.quit()
- quit()
- # Start the game loop
- game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement