Codementor Events

Simple Python Snake Game using Curses

Published Oct 01, 2015Last updated Apr 12, 2017
Simple Python Snake Game using Curses

Introduction

Here is a good description of a Snake Game from wikipedia.
Snake is a game where the player maneuvers a line which grows in length, with the line itself being a primary obstacle. There is no standard version of the game. The concept originated in the 1976 arcade game Blockade, and its simplicity has led to many implementations (some of which have the word snake or worm in the title). After a variant was preloaded on Nokia mobile phones in 1998, there was a resurgence of interest in the Snake concept as it found a larger audience.

Problem Solving Approach

Let's start with the elements of a Snake Game. Firstly, the snake itself is the main character. Secondly, it should be able to move in four directions (up, down, left, right). Each time it eats food, its body would stretch. Thirdly, the food will appear in random places. Fourthly, the snake should not eat itself. Fifth make the game faster Lastly, we should track the score.

In order to make the code easier to maintain, I used OOP and created three classes, which are Snake, Body, and Food.

Snake Class

class Snake(object):

    def __init__(self, x, y, window):
        # initialize snake:
        # - create head
        # - create body
        # - set starting x, y position

    def eat_food(self, food):
        # remove food
        # stretch body
        # add score
        # make the game faster

    def update(self):
        # update snake location (move the snake)

    def render(self):
        # draw the snake in the console using curses

    def move(self):
        # move up down left right

Body class

class Body(object):

    def __init__(self, x, y, char='#'):
        self.x = x
        self.y = y
        self.char = char

    @property
    def coor(self):
        return self.x, self.y

Food class

class Food(object):
    def __init__(self, window, char='*'):
        # set random x, y position

    def render(self):
        # draw food to console

    def randomize(self):
        # randomize x, y position

Main loop

while True:
    # clear screen
    # display the snake
    # display the food
    # display the score
    # listen to keypress event
    # respond to keypress event
    # stop the game if the head hits the body (eat itself)

I have made ​a GitHub​ repository, so feel free to use this link to view the source code.

Pros and Cons

The good thing about this game and our solution is that it is very simple. The approach is pretty simple and easy to understand even for beginners. This game could run on many platforms.

The bad thing is that the "look" of the game is pretty ancient (DOS style), so it's not so appealing. When the user presses and holds the navigation key, the snake moves faster this is due to the way I used timeout to delay the rendering in the main loop.

Improvements

Since I created this game just for fun and to teach beginners how to code a snake game, I know that there are many things that could be improved. The code itself could be more optimised. Perhaps using curses.napms to delay the rendering instead of using window.timeout. Adding sound effects and a theme song should be easy and fun. Currently this code only works on Linux and Mac OSX. In order to make it work with Windows platform, UniCurses should be used instead of curses.

Discover and read more posts from Murzal Arsya Pandji
get started