Introduction
Snake Game in Python utilizes Pygame, a free and open-source Python library used to make games. The snake was an unquestionably famous game, generally recollected from the 1990s time cells. It was the main game on their telephone for some individuals around then. It's also a great game from which to gain the fundamentals of game making.
Building Snake Game using PyGame
Firstly we will need to install the Pygame library in Python to begin making the game. After installation, we can start importing necessary libraries and making the background screen of the game. The game will have a black background, and the snake can be green in color. The score will be displayed on the left upper corner of the screen. So let's begin coding.
Steps
We will discuss these steps in detail further, but this is what the layout should look like
Running Python Code from File
Adding the Game Resources
Step 1: Hello Bunny
Step 2: Add Scenery
Step 3: Make the Bunny Move
Step 4: Turning the Bunny
Step 5: Shoot, Bunny, Shoot!
Step 6: Take Up Arms! Badgers!
Step 7: Collisions with Badgers and Arrows
Step 8: Add a HUD with Health Meter and Clock
Step 9: Win or Lose
Step 10: Gratuitous Music and Sound Effects
Step 1: Import necessary libraries
- From that point forward, we characterize the width and height of the window wherein the game will be played.
-
What's more, characterize the shading in RGB design that we will use in our game for showing text.
import pygame
import time
import random
snake_speed = 15
# Window size
window_x = 720
window_y = 480
# defining 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)
blue = pygame.Color(0, 0, 255)
Stage 2: After bringing in libraries, we want to instate Pygame utilizing pygame.init() technique which helps to initialize all imported pygame modules.
- Make a game window utilizing the width and stature characterized in the past advance. We need this because the snake will grow, challenging the game as the score increases.
-
Here pygame.time.Clock() will be utilized further in the principal rationale of the game to change the snake's speed. Changing the snake's speed is not necessarily essential but adds a lot of flexibility and fun to the game.
pygame.init()
# Initialise game window
pygame.display.set_caption('Coding Ninjas Snakes')
game_window = pygame.display.set_mode((window_x, window_y))
# FPS (frames per second) controller
fps = pygame.time.Clock()
Stage 3: Initialize snake position and its size.
- After introducing the snake position, instate the natural product position haphazardly in the characterized tallness and width.
- By setting bearing to RIGHT, we guarantee that the snake should move right to the screen, at whatever point a client runs the program/gain.
- You need to store every one of the snake's positions that have met the snake in a rundown. For example, if the length of a piece the snake in a rundown has met, the snake in a rundown has met of the snake is 30, and the stage size is 5. So each sixth situation in the rundown is the place of one part of the snake's body.
-
We have also added the x y coordinates to make it easier to understand the positions of the snake.
# defining snake default position
snake_position = [100, 50]
# defining first 4 blocks of snake
# body
snake_body = [ [100, 50],
[90, 50],
[80, 50],
[70, 50]
]
# fruit position
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]
fruit_spawn = True
# setting default snake direction
# towards right
direction = 'RIGHT'
change_to = direction
Stage 4: Create a capacity to show the score of the player.
- In this capacity, initially, we're making a textual style object; for example, the text style shading will go here. This will game the game more fun and interactive.
- Then we utilize render to make a foundation surface that we will change at whatever point our score refreshes.
- Make a rectangular article for the text surface item (where text will be invigorated)
-
Then, we are showing our score utilizing blit. blit takes two contention screen.blit(background,(x,y))
# initial score
score = 0
# displaying Score function
def show_score(choice, color, font, size):
# creating font object score_font
score_font = pygame.font.SysFont(font, size)
# create the display surface object
# score_surface
score_surface = score_font.render('Score : ' + str(score), True, color)
# create a rectangular object for the
# text surface object
score_rect = score_surface.get_rect()
# displaying text
game_window.blit(score_surface, score_rect)
Step 5: Now, create a game over function that will represent the score after the snake is hit by a wall or itself.
- In the first line, we create a font object to display scores.
- Then we are creating text surfaces to render scores.
- After that, we are setting the position of the text in the middle of the playable area.
- Display the scores using blit and update the score by updating the surface using flip().
- We use sleep(2) to wait for 2 seconds before closing the window using quit().
# game over function
def game_over():
# creating font object my_font
my_font = pygame.font.SysFont('times new roman', 50)
# creating a text surface on which text
# will be drawn
game_over_surface = my_font.render('Your Score is : ' + str(score), True, red)
# create a rectangular object for the text
# surface object
game_over_rect = game_over_surface.get_rect()
# setting position of the text
game_over_rect.midtop = (window_x/2, window_y/4)
# blit will draw the text on screen
game_window.blit(game_over_surface, game_over_rect)
pygame.display.flip()
# after 2 seconds we will quit the
# program
time.sleep(2)
# deactivating pygame library
pygame.quit()
# quit the program
quit()
Follow this link to find Part 2 of this blog: Part 2