Basic Snake Game in Python with Source Code

Basic Snake Game in Python

Not many games have endured as long as Snake. From early versions on Nokia phones to modern adaptations, Snake has remained a simple, addictive classic. If you’re a beginner or an aspiring Python developer, creating a Snake game is a fun project that helps you understand the essentials of game development.

Build a Snake Game?

Snake is an excellent introductory project for several reasons:

  • Simple Concept: The mechanics of the game are easy to understand, making it a great learning tool.
  • Game Logic: You’ll learn about controlling objects (the snake), handling collisions, and updating the game screen.
  • Input Handling: You’ll gain experience managing user inputs like key presses to move the snake.

Let’s dive into the code and see how we can build the Snake game from scratch.

Step 1: Setting Up the Environment

Make sure you have Python and Pygame installed before we begin coding. If you don’t have Pygame installed yet, you can install it via pip:

pip install pygame

Step 2: The Basic Structure of the Game

Our game will involve:

  • A snake that moves around the screen.
  • Food that appears randomly, which the snake will “eat” to grow.
  • Collision detection to end the game when the snake runs into the wall or itself.
See also  Expense Tracker in Python with Source Code

Let’s start by setting up the game window and the snake.

import pygame
import time
import random

# Initialize the Pygame library
pygame.init()

# Define game window size
window_width = 600
window_height = 400

# Set up display
game_window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Snake Game")

# Define colors
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)

# Set game clock for controlling speed
clock = pygame.time.Clock()

# Snake settings
snake_size = 10
snake_speed = 15
snake_body = [[100, 50], [90, 50], [80, 50]]

# Initial snake direction
direction = 'RIGHT'
change_to = direction

# Food settings
food_position = [random.randrange(1, (window_width // snake_size)) * snake_size,
                 random.randrange(1, (window_height // snake_size)) * snake_size]
food_spawned = True

Step 3: Game Mechanics – Moving the Snake

We’ll control the snake using arrow keys, allowing it to move up, down, left, or right. In this section, we’ll set up how the snake moves and grows when it eats food.

Screenshot-2024-09-30-084721-1024x490 Basic Snake Game in Python with Source Code
Basic Snake Game in Python

PHP PROJECT:- CLICK HERE

Complete Code

Step 4: Game Loop

Any game’s central component is the game loop. It runs continuously, updating the game state and rendering the graphics on the screen. Here, we’ll include:

  • Input handling: Responding to key presses.
  • Movement: Updating the snake’s position.
  • Collision detection:determining if the game is finished.

Step 5: Explanation of the Code

  1. Pygame Initialization:
  • We use pygame.init() to initialize all the Pygame modules. This prepares the game for rendering and user input.
  1. Game Window:
  • The game window is created with a size of 600×400 pixels, and we use the set_mode() function to display it.
  1. Snake Movement:
  • The snake moves by updating its position in the move_snake() function. We adjust the snake’s head based on the direction and handle boundary and self-collisions in check_collision().
  1. Game Loop:
  • The main game loop continuously checks for user input, updates the snake’s position, checks for collisions, and renders the game.
  1. Game Over Handling:
  • If the snake runs into itself or the walls, the game over screen is displayed, and the game restarts.
See also  Captcha Generator in Python with Free Source Code

New Project :-https://www.youtube.com/@Decodeit2

Step 6: Enhancing the Snake Game

Here are some ideas to make your Snake game more exciting:

  • Scoring: Include a score counter that rises with each meal the snake consumes.
  • Levels: Increase the snake’s speed as the game progresses to make it more challenging.
  • Obstacles: Introduce obstacles that the snake must avoid.
  • Multiplayer Mode: Give separate snakes to each of the two players to control on the same screen.

  • Basic Snake Game in Python with Source Code
  • Basic Snake Game in Python

1 comment

Post Comment