Build a snake game in Python Day-1

April 29, 2020 ( last updated : April 30, 2020 )
Python pygame curses

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


Introduction

How are you guys😀! In the next following days, I’m going to introduce an interesting and small project implemented in Python, using some third-party libraries to create a snake game. I know it’s not a big deal to complete such a project, but it really gives me a kind of enjoyment in these self-isolation days🤓.

Actually, this idea is inspired by a YouTuber and he did a snake game using curses library which is natively supported by Python. You can check it out here 👉Creating a Snake game with Python in under 5 minutes👈. I followed his code and made it, below is the outcome.

snake_01_curse

Honestly, I was caught by the keywords ‘5 minutes’, it took me more than 5 minutes actually. However, I should say after completing this naive game my passion was ignited and I thought it’s time for implementing a beautiful version to make me feel happy. As a result, I’ve just achieved this game in first version, and it will be explained in detail in the next days. Here is the glance of the game interface.

snake_02_1st_glance

I intend to introduce the development in the following steps:

Now, let’s jump into the curses to start the journey.

About curses

The curses is a library for people who want to create command-line applications. It’s based on the terminal on Linux or Mac to build textual interfaces, using the keyboard to interact with users. It’s a built-in Python library and provides lots of APIs to implement powerful functionalities. In short, there are two key concepts in this module.

1. Initializing and closing a window

We can simply think a window is a kind of canvas that contains all the objects that will display on a terminal. We can create multiple windows with different sizes and place them around your screen. If we add something on a window, we should refresh the window to display the changes and if we don’t use the window anymore, we should close it properly.

import curses

main_scr = curses.initscr()  # initialize a main screen, getting the window object
# do something
main_scr.refresh()	# in order to update the window
# job is done
curses.endwin()     # close the window

If we want to declare a window with a specific size, we should use another method called newwin(), this method can set the window’s start coordinates, height, and width.

board = curses.newwin(height, width, 0, 0)  # initialize a window with the size of (height, width) on the screen (0, 0)

2. Adding and displaying characters

As we know, characters can be represented either in strings or letters one by one. Text can be printed on a window by using the following statements.

board.addch(0, 0, '$')  # painting a character '$' on the screen (0, 0)
board.addstr(0, 0, "Hello, snake") # putting a sentence on the screen (0, 0)

The above is enough for creating a simple terminal snake game, for full APIs please check official documents 👉here👈. Let’s dive into more at tomorrow, see ya.


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

Originally published April 29, 2020
Latest update April 30, 2020

Related posts :