PDA

View Full Version : simple snake game under linux



newbie0101
09-01-2011, 05:51 AM
hello all, I've been looking for a simple snake game for linux in c code.
I couldn't find anything. All I found was written for windows that contains graphics.h or any other windows library. Have any of you ever written such a game for linux?
I would really appreciate if you'd give me a sample code or a webpage where it is.
I don't know how to write it myself because don't know what library i should use to manipulate the snake with arrows.
Thank you very much for your time

Salem
09-01-2011, 06:41 AM
Well I'm sure you could write a very simple snake game using the ncurses (http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) library.

> I don't know how to write it myself because don't know what library i should use to manipulate the snake with arrows.
It might come as a shock to you, but there isn't a "snake" library.
If you have any library capable of reading the keyboard, and any other (possibly even the same) library for drawing images on screen, then you can combine the two with a snake game.

For example, take the ncurses approach, draw a "wall" around the edge of the screen and place a "@" in the middle of the screen.
Move it around in response to you pressing arrow keys, and print "you're dead" if you run into a wall.
That much alone will keep you busy for a few days, and teach you a lot about what to do next.

MK27
09-01-2011, 06:53 AM
Another option to consider is openGL, which might be easier to learn to use in 2D than trying to do the same thing with ncurses. OpenGL is cross-platform and well supported under linux; you'll find plenty of information, tutorials, etc. if you google. It requires the use of an additional library to provide a system and user interface. Glut and SDL are two such toolkits.

newbie0101
09-01-2011, 06:57 AM
Well I'm sure you could write a very simple snake game using the ncurses (http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) library.

> I don't know how to write it myself because don't know what library i should use to manipulate the snake with arrows.
It might come as a shock to you, but there isn't a "snake" library.
If you have any library capable of reading the keyboard, and any other (possibly even the same) library for drawing images on screen, then you can combine the two with a snake game.

For example, take the ncurses approach, draw a "wall" around the edge of the screen and place a "@" in the middle of the screen.
Move it around in response to you pressing arrow keys, and print "you're dead" if you run into a wall.
That much alone will keep you busy for a few days, and teach you a lot about what to do next.

Thank you very much for your quick response, i've already installed libncurses5-dev and i am going to study it :)

Rodaxoleaux
09-26-2011, 04:24 AM
Instead of using a text-based library and then combining different other libs for keyboard input and graphics based on the text including window creation, wouldn't it just be easier to use a library like Allegro or SDL or OpenGL

Ocifer
10-02-2011, 11:33 AM
I've used OpenGL and SDL on Ubuntu, Scientific Linux, and Fedora. I highly recommend that combo (SDL and OpenGL). The GLUT library is also user friendly, but I found it introduced artifacts in the program's actual performance.

Someone here recommended SDL and I've been using it in conjunction with OpenGL since.