Python: #4 Key pressing and creating the snake game in pygame

+`

Hi all,

Today in this article we are going to discuss about the key pressing in the pygame and we are also start creating the snake game.

To know more about our pygame series you can surely check our pygame article in this page – https://knowledge-junction.in/dashboard/python-pygame-series/

Now let’s start

import pygame
initilize_pygame_module = pygame.init()

game_display = pygame.display.set_mode((900,600))

title = pygame.display.set_caption("My first Python game")

# Specific game variables
exit_game = False
game_over = False

while not exit_game:

    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            exit_game = True

As we handled the problem in last article we will continue with this code

Now we will control the keys

import pygame
initilize_pygame_module = pygame.init()

game_display = pygame.display.set_mode((900,600))

title = pygame.display.set_caption("My first Python game")

# Specific game variables
exit_game = False
game_over = False

while not exit_game:

    for event in pygame.event.get():

        if event.type==pygame.QUIT:
            exit_game = True
            exit_game = False
    while not exit_game:
    
     for event in pygame.event.get():

        if event.type==pygame.QUIT:
            exit_game = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                print( "right arrow key")

Now if we press any key it will print the statement

Explanation:

In this we first used “if” statement to check if the user has pressed ANY key. If any key is pressed then we are checking if that key is right arrow key and if it’s right arrow key then it will print the statement.

Now let’s see it works or not:

Output:

Fig 1 : Python : Printing the statement when we press on right arrow key
Fig 1 : Python : Printing the statement when we press on right arrow key

As we can see in the above image when I pressed the right arrow key it is printing the statement. Like this we can do with any key

Now let’s start creating our first snake game in pygame with Python

import pygame
initilize_pygame_module = pygame.init()

game_display = pygame.display.set_mode((900,600))

title = pygame.display.set_caption("My first Python game")

# Before creating the loop we will make some specific variable
exit_game = False
# We have created this variable because when the user will exit the game this exit_game will become true'''
# Creating the variable when the game will be over
game_over = False

# Creating the loop 
while not exit_game:
    pass
# This explains that the display will not disappear till the game_exit becomes True
# Pass statement is given so the error should not occur till we do not complete the whole loop
    
    for event in pygame.event.get():

        if event.type==pygame.QUIT:
            exit_game = True

We will remove the key pressing statements as we are making it from starting. Till now in the above code we have used the statements which we have discussed in last articles from the above statements we will start our game.

Now for making the snake game we will need to make characters, background and the food for the snake. So first we will make white background. As we are have a black display we just need to color it.

We will create the colors which we are going to need for background, character and food

Background Color Variables:

For the colors we are using RGB values

white = (255, 255, 255)
red = (255, 3, 3)
# For black all zero because black is not a color
black = (0, 0, 0)

To get this RGB values we can use the google rgb picker.

Fig : 2 Python: RGB picker
Fig : 2 Python: RGB picker

We will fill the color to our black display in the next article till the stay tuned with our pygame series.

Thank you for reading 📖

Have a nice day🙂

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: