Building a game using pygame

Introduction
End Goal
Starting the project
Initializing the game window
Updating the pygame window
Global constants for snake game

Global constants for snake game

Ranvir Singh · 1 mins read ·
python pygame

We will need to add a few constants to our game which can be used to handle the movements, total size and grid architecture of the game.

For our snake game, the snake will be any one of these grids at any given time.

I am creating a new file to keep all my constants.

constants.py

SCREEN_WIDTH: int = 810
SCREEN_HEIGHT: int = 510

# number of grids
GRID_SIZE: int = 30

# Height and width of single grid
GRID_WIDTH: int = SCREEN_WIDTH / GRID_SIZE
GRID_HEIGHT: int = SCREEN_HEIGHT / GRID_SIZE

# Movements
MOVE_UP: tuple = (0, -1)
MOVE_DOWN: tuple = (0, 1)
MOVE_LEFT: tuple = (-1, 0)
MOVE_RIGHT: tuple = (1, 0)
First, we are defining the total width and height of the screen. We will use the variables defined here in the main file as well.

Then we are defining basic grid stuff. We will use these variables later during the game development.

Finally, we are creating some constants that will help us with the movement. We are using a tuple to define the 2-dimensional movement.

Finally, we can start using these constants in main.py file.

from constants import SCREEN_WIDTH, SCREEN_HEIGHT

# width, height
SCREEN_SIZE: tuple = SCREEN_WIDTH, SCREEN_HEIGHT

pygame.display.set_mode(SCREEN_SIZE)
It’s always great to keep your code structured and keep it well separated. It is always easier to find and make changes that way.



About Author

Ranvir Singh

Greetings! Ranvir is an Engineering professional with 3+ years of experience in Software development.

Please share your Feedback:

Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!