Build a snake game in Python Day-4

May 03, 2020 ( last updated : May 04, 2020 )
Python OOP

https://github.com/cao-weiwei/


šŸ˜A snake game could contains lots of features, if we want to improve the previous work, we must have clues about how to make progressšŸ¤”. Next we will use OOP ideas to make a fancy snake game.

Contrary to procedure-oriented programming where programs are designed as blocks of statements to manipulate data, object-oriented programming organizes the program to combine data and functionality and wrap it inside something called an ā€œObjectā€šŸ§.

OOP analysis and design

There is a structured approach for analyzing and designing a software product using OOP concepts. The most important thing is identifying the objects of the product and figuring out the relationships between each other. Iā€™ll explain the process of making the snake game asšŸ„³:

  1. Clarifying the requirements of snake game;
  2. Drawing a diagram to show how the snake game works;
  3. Identifying the objects and defining their relationships;
  4. Making a design of the game.

Clarifying requirements

šŸ¤“Iā€™ll focus on the following set of requirements while designing the snake game:

Drawing process diagram and making prototype

The above whole process can be drawn in a figure as:

01_snake_game_flow

According to the process, I made mock-up of the interface.

02_snake_game_prototype

Identifying objects and defining relationships

As we knew, the most obvious objects in the game are the snake and the food. How to identify others and develop their relationships are vital points for making this game.

Board: Everything in the game must be drawn or displayed on a board, like painting on canvas. And this board will contain a snake, food, and a score counter. It will initialize a game and end the game according to the conditions.

Snake: A snake, in the game, actually is a list of coordinates. we have to know the start position and its length. For the snake, it can move on the screen and eat food. And sometimes, it may die due to collision with the border or itself.

Food: As the same as the snake, itā€™s also represented by a pair of coordinates on the screen. For the food, we should know whether it is eaten by the snake then we can draw another on the screen.

Button: This is a kind of UI component to receive a start signal and a terminate signal from use.

03_class_diagram

Making design of the game

Finally, based on previous analysis and objects we got, I made some UI of the game.

04_demo-ui

Next, weā€™ll jump onto the pygame, which is a most famous game development library in Python.


If you like my articles please give me a star or leave comments below, thanks!

Originally published May 03, 2020
Latest update May 04, 2020

Related posts :