Code Monkey home page Code Monkey logo

shabir-mp / snake-game Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 8 KB

This Snake Game is a classic arcade game implemented using Python and the Pygame library. The game involves controlling a snake to eat food and grow longer while avoiding collisions with the walls and the snake's own body. It is a single-player game where the objective is to achieve the highest possible score by consuming as much food as possible.

License: MIT License

Python 100.00%
arcade-game pygame python python-arcade python-game python-games simple-game simple-project single-player-game snake-game

snake-game's Introduction

Snake Game v1.0.0

This Snake Game is a classic arcade game implemented using Python and the Pygame library. The game involves controlling a snake to eat food and grow longer while avoiding collisions with the walls and the snake's own body. It is a single-player game where the objective is to achieve the highest possible score by consuming as much food as possible. Enjoy playing the Snake Game, a classic arcade experience brought to life with Python and Pygame! Challenge yourself to beat your high score and master the art of controlling the ever-growing snake.

About

made-with-python GitHub license

  • Developer: Shabir Mahfudz Prahono - @shabir-mp
  • Application creation date: 28 June 2024

Features

  • Simple Graphics: The game uses basic rectangular graphics for the snake and food, with different colors to distinguish them.
  • Keyboard Controls: The snake is controlled using the arrow keys on the keyboard.
  • Score Tracking: The game keeps track of and displays the player's score based on the number of food items consumed.
  • Game Over and Restart: When the game is over, the player can choose to quit or restart the game.

Requirements

To play the Snake Game, you need:

  • Python 3.x: Python is the programming language used to run the game.
  • Pygame Library: This Python library is required for handling graphics, events, and sound in the game.

Installation Steps

  1. Install Python:

  2. Install Pygame:

    • Open a command prompt or terminal.

    • Use the following command to install Pygame using pip, Python's package installer:

      pip install pygame

How to Play

  1. Download the Snake Game Code:

    • Save the provided Python code in a file, e.g., snake_game.py.
  2. Run the Game:

    • Open a command prompt or terminal.

    • Navigate to the directory where snake_game.py is saved.

    • Run the game by executing the following command:

      python snake_game.py
  3. Game Controls:

    • Use the arrow keys on your keyboard to control the snake:
      • Up Arrow: Move the snake upwards.
      • Down Arrow: Move the snake downwards.
      • Left Arrow: Move the snake to the left.
      • Right Arrow: Move the snake to the right.
  4. Game Objective:

    • Guide the snake to eat the green food squares that randomly appear on the screen.
    • Each time the snake eats food, it grows longer.
    • Avoid collisions with the walls and the snake's own body, as these will end the game.
  5. Scoring:

    • The score increases with each piece of food consumed.
    • Try to achieve the highest possible score before the snake collides with an obstacle.
  6. Game Over:

    • The game ends when the snake collides with a wall or its own body.
    • Upon game over, you can press Q to quit the game or C to play again.

Application Preview

SNKGM 1

1. View of the game. User can move the snake (black box) by clicking the up, down, right, left keys on the keyboard. The target is the green box.

SNKGM 2

2. Game over display. User can press Q key on keyboard to exit, and U to restart the game.

Code Explanation

1. Imports and Initialization

import pygame
import time
import random

pygame.init()
  • Explanation:
    • pygame is a Python library used for game development.
    • time and random are standard Python libraries.
    • pygame.init() initializes all the imported pygame modules and must be called before using any other Pygame functions.

2. Colors

white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
  • Explanation:
    • These variables store RGB tuples representing different colors used in the game interface.
    • For example, white is pure white (255, 255, 255) and black is pure black (0, 0, 0).

3. Screen Setup

dis_width = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('Snake Game')
  • Explanation:
    • dis_width and dis_height set the dimensions of the game window to 800 pixels wide and 600 pixels high.
    • pygame.display.set_mode((dis_width, dis_height)) creates a Pygame display surface with the specified dimensions.
    • pygame.display.set_caption('Snake Game') sets the title of the game window to 'Snake Game'.

4. Clock and Game Parameters

clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
  • Explanation:
    • clock is an instance of pygame.time.Clock() used to control the game's frame rate.
    • snake_block determines the size of each segment of the snake.
    • snake_speed controls how fast the snake moves; it represents the number of pixels the snake moves per frame update.

5. Fonts

font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
  • Explanation:
    • font_style and score_font are Pygame Font objects created using pygame.font.SysFont.
    • They are used for rendering text on the game screen with specified fonts ("bahnschrift" and "comicsansms") and sizes (25 and 35, respectively).

6. Functions

  • Your_score(score):

    def Your_score(score):
        value = score_font.render("Your Score: " + str(score), True, black)
        dis.blit(value, [0, 0])
    • Explanation:
      • Your_score() function renders and displays the player's current score on the game screen.
      • score_font.render("Your Score: " + str(score), True, black) creates a text surface with the score rendered in black color.
      • dis.blit(value, [0, 0]) draws the rendered text (value) at the top-left corner of the screen.
  • our_snake(snake_block, snake_List):

    def our_snake(snake_block, snake_List):
        for x in snake_List:
            pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
    • Explanation:
      • our_snake() function draws the snake on the game screen based on its current segments.
      • It iterates through snake_List, which contains coordinates of each segment of the snake.
      • pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block]) draws a rectangle (snake segment) at position (x[0], x[1]) with dimensions snake_block x snake_block in black color (black) on the dis surface.
  • message(msg, color):

    def message(msg, color):
        mesg = font_style.render(msg, True, color)
        dis.blit(mesg, [dis_width / 6, dis_height / 3])
    • Explanation:
      • message() function displays a message on the game screen.
      • It renders the text msg using the font_style and with the specified color.
      • dis.blit(mesg, [dis_width / 6, dis_height / 3]) draws the rendered message (mesg) at a specific position on the screen, which is (dis_width / 6, dis_height / 3).

7. GameLoop Function

def gameLoop():
    game_over = False
    game_close = False

    x1 = dis_width / 2
    y1 = dis_height / 2

    x1_change = 0
    y1_change = 0

    snake_List = []
    Length_of_snake = 1

    foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
    foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0

    while not game_over:
        while game_close == True:
            dis.fill(blue)
            message("You Lost! Press Q-Quit or C-Play Again", red)
            Your_score(Length_of_snake - 1)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        gameLoop()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x1_change = -snake_block
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block
                    x1_change = 0

        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change
        dis.fill(white)
        pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
        snake_Head = []
        snake_Head.append(x1)
        snake_Head.append(y1)
        snake_List.append(snake_Head)
        if len(snake_List) > Length_of_snake:
            del snake_List[0]

        for x in snake_List[:-1]:
            if x == snake_Head:
                game_close = True

        our_snake(snake_block, snake_List)
        Your_score(Length_of_snake - 1)

        pygame.display.update()

        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
            foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
            Length_of_snake += 1

        clock.tick(snake_speed)

    pygame.quit()
    quit()
  • Explanation:
    • gameLoop() function manages the main game logic and loop.
    • Initialization:
      • game_over and game_close control the game state (running or ended).
      • x1 and y1 represent the initial position of the snake's head.
      • x1_change and y1_change store the velocity of the snake in the x and y directions.
      • snake_List keeps track of the snake's body segments.
      • Length_of_snake tracks the current length of the snake.
      • foodx and foody store the coordinates of the food that the snake eats.
    • Main Loop:
      • while not game_over: runs the game until the player quits or loses.
      • while game_close == True: manages the game over screen:
        • Fills the screen with blue (dis.fill(blue)).
        • Displays a message indicating the player lost and instructions to quit or play again.
        • Updates the display and waits for player input (pygame.event.get()).
      • Event handling (for event in pygame.event.get()):
        • Processes events such as quitting the game (pygame.QUIT event) or controlling the snake's direction (pygame.KEYDOWN events).
      • Game Logic:
        • Checks if the snake collides with boundaries (if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:) and sets game_close to True if true.
        • Updates the snake's position (x1 += x1_change, y1 += y1_change) based on user input

License

This project is licensed under the MIT License. See the LICENSE file for more details.


Github Footer

snake-game's People

Contributors

shabir-mp avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.