Making Snake game – Part 1: the menu

Now that I posted 3 version of snake (Snake 1, Snake 2, Snake 3), let’s remake Snake from skratch. We will start with the menu. You can find the whole code and the code of the different parts of the tutorial here in this GITHUB REPOSITORY (there is also a snake.7 version with the latest version with some sound adjustments).

This is the menu

That is all we will have for this first part. This is done just to wait the user to press s without making the game start immediately.

Wait the user’s input

We used this function to make the computer to wait for the input:

event = pygame.event.wait()

The possible inputs: quit, escape or ‘s’ to start

The code is this for the input, the ‘s’ of course will do nothing but print start

        while True:
            event = pygame.event.wait()
            if event.type == pygame.QUIT:
                break
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    break
                if event.key == pygame.K_s:
                    print("Start")
                    break
        pygame.quit()

The write function

This is the function that makes you write “press s to start”

    def write(t, x: int = 0, y: int = 0, middle: str = "both", color="Coral") -> pygame.Surface:
        text = Game.font.render(t, 1, pygame.Color(color))
        if middle == "both":
            rect_middle = text.get_rect(center=((Game.WIDTH // 2, Game.HEIGHT //2)))
            Game.screen.blit(text, rect_middle)
        else:
            Game.screen.blit(text, (x, y))
        pygame.display.update()
        return text

In the class Game you got some attributes that are useful, like the font that tells the computer to use arial characters of size 24.

In the write function you render the text, then you blit it on the Game.screen, on the surface and use pygame.display.update() to show it.

If you put middle=”both”, it will be centered on the Game.screen. It is Game.screen, because screen is a variable of the class Game.

    pygame.init()
    pygame.font.init()
    font = pygame.font.SysFont("Arial", 24)
    WIDTH: int = 400
    HEIGHT: int = 400
    screen = pygame.display.set_mode((WIDTH, HEIGHT))

 

Live coding

The whole code for the menu

A little bit revisited.

import pygame


class Game:
    "Starts the game"
    pygame.init()
    pygame.font.init()
    font = pygame.font.SysFont("Arial", 24)
    WIDTH: int = 400
    HEIGHT: int = 400
    screen = pygame.display.set_mode((WIDTH, HEIGHT))


    def menu() -> None:
        "The starting menu waitin to press s"
        Game.write("Press s to start", middle="both")

        while True:
            event = pygame.event.wait()
            if event.type == pygame.QUIT:
                break
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    break
                if event.key == pygame.K_s:
                    print("Start")
                    break
        pygame.quit()

    def write(t, x: int = 0, y: int = 0, middle: str = "both", color="Coral") -> pygame.Surface:
        text = Game.font.render(t, 1, pygame.Color(color))
        if middle == "both":
            rect_middle = text.get_rect(center=((Game.WIDTH // 2, Game.HEIGHT //2)))
            Game.screen.blit(text, rect_middle)
        else:
            Game.screen.blit(text, (x, y))
        pygame.display.update()
        return text


Game.menu()

 

[hoop name=”games_1″]

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.