Tag Archives: linux

Snake Game Using Python Curses

In this tutorial we will learn how to create a snake game using python and curses.

What is Curses?

The curses is a library that can be used to create text user interface application. It is terminal controlled library i.e. the code written using curses can only be run through terminal.

Import Libraries

To start with creating a snake game using curses, we first need to import the following libraries:

The above import will work fine for Linux based systems, to make it compatible for windows you need to install curses. To do this you need to download curses for windows according to your python version from python extension packages and then run the following command:

Initializing Game Screen

After importing required libraries, first we need to initialize game screen and get the maximum height and width of the opened terminal screen. Using these height and width we will create a window for the game.

In the above code win.keypad(1) will initiate the keyboard to take the user’s input for the game. Also curses.curs_set(0) will set the cursor mode to be invisible in the screen.

Initialize snake and apple initial positions

Next, we will initialize the snake and apple(food) starting positions in the game screen. Also, we will initialize our game score to be zero.

In the above code win.addch() will add a diamond like symbol in the game screen according to specified apple position.

Specifying Game Over Conditions

For the snake game there are basically two conditions which defines how game will end. First if snake collide with one of the game window boundaries and second if snake collides with itself.

Playing the Game

In this game we will use four keyboard buttons, ‘up’, ‘down’, ‘left’ and ‘right’. To get a user input from keyboard we need to use win.getch() function.

In the above code win.border(0) will create a border around our game screen.

Now we will see the logic to move snake and eat apple. According to the game rules, snake will continue to move in the same direction if user do not press any button. Also snake can not move backward. In case user press any button, we need to update snake head’s position. Let’s see code:

Next there are two situations. One, in next step snake will move to a new position and second, in next step snake will eat apple. In case snake only moves to a new position, we will add one unit at it’s head and remove one unit from its tail according to the pressed direction. Another situation, if snake eats apple then we will add one unit at snake’s head and do not remove from it’s tail. We will also display a new apple at different location. Let’s see the code:

Then finally we will display the score in the screen and quit the game window.

Here are some images of the game that we have just created.

The full code can be found here.

Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Connecting Raspberry pi to proxy server

In this tutorial, I am going to tell you how to connect raspberry pi to the proxy server.

As most of the colleges have internet connection through local ethernet having its own proxy settings so for downloading packages on raspi you have to set up local proxy settings.

Here are the simple steps for connecting raspi to the proxy server.

  1. Open Root Terminal (Ctrl+Alt+T) and then type:

    This command creates a file named 10proxy and then add proxy settings to this file by typing:

    and save it. You need to add in the exact format as written above (do not leave any semicolon or space).
  2. Again in Root Terminal  type:

    then add proxy settings to this file by typing:

    and save it. If you want to know more about environment variables follow this link https://help.ubuntu.com/community/EnvironmentVariables
  3. Again in Root Terminal type:

    This opens bash file scroll to the end of the file and type:

    and save it. If you want to know more about .bashrc follow this link https://askubuntu.com/questions/540683/what-is-a-bashrc-file-and-what-does-it-do
  4. At last in Root Terminal type
  5. In order to download the file using sudo, update sudoers. First, open sudoers using

    Then add the following line so that sudo is able to use the above environment variables

    Reboot pi for the changes to work.
  6. Then run the following command in the Root Terminal for checking if the internet is working

Hope this helps. Enjoy!!!