Thread: simple snake game under linux

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    simple snake game under linux

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well I'm sure you could write a very simple snake game using the ncurses 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    Quote Originally Posted by Salem View Post
    Well I'm sure you could write a very simple snake game using the ncurses 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

  5. #5
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    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

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    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.
    Last edited by Ocifer; 10-02-2011 at 11:36 AM.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a snake game?
    By awr7126 in forum Game Programming
    Replies: 2
    Last Post: 12-06-2010, 06:39 PM
  2. Help with snake game in sdl/c
    By joellllmal in forum Game Programming
    Replies: 4
    Last Post: 08-22-2010, 12:14 AM
  3. About SnaKE game..
    By ozumsafa in forum C Programming
    Replies: 3
    Last Post: 10-19-2007, 05:46 PM
  4. My Snake Game
    By ()Q() in forum Game Programming
    Replies: 2
    Last Post: 07-23-2002, 03:03 PM
  5. game (snake)
    By nps in forum C Programming
    Replies: 1
    Last Post: 10-09-2001, 10:37 PM