Thread: Help with snake game in sdl/c

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Help with snake game in sdl/c

    Okay, first of all, thanks in advance to anyone who helps me. Here's my problem.....
    I'm making a snake game in sdl with c++ and i have some code so far it is supposed to place the picture of the snake on the screen and now i have to move it... That is the problem i tried acouple of things to put into the empty functions: Snake:handle_input(), and Snake::move() ( shown in the code ), i cannot find out anyway to make it so when i press one of the directional keys once the snake starts the move and keeps moving without having to hold down the key. If you find anything else wrong with the code please tell me and thank you.

    Sorry for now writing correctly in sentences. I just didn't feel like it for some reason, sorry.

    Well, here's the code:

    Code:
    #include "sdl.h"
    #include "sdl_image.h"
    #include <string>
    
    using namespace std;
    
    #define KEY_NONE 0
    #define KEY_DOWN 1
    #define KEY_UP 2
    
    SDL_Surface* buffer = NULL;
    SDL_Surface* snake = NULL;
    
    SDL_Event event;
    char key[323] = {0};
    
    int SNAKE_HEIGHT = 31;
    int SNAKE_WIDTH = 31;
    
    class Snake
    {
        private:
            SDL_Rect box;
            bool Left, Right, Up, Down;
        public:
            Snake();
            void False();
            void handle_input();
            void move();
            void show();
    };
    
    SDL_Surface* load_image(string filename)
    {
        SDL_Surface* loadedimage = NULL;
        SDL_Surface* optimizedimage = NULL;
    
        loadedimage = IMG_Load(filename.c_str());
    
        if (loadedimage != NULL)
        {
            optimizedimage = SDL_DisplayFormat( loadedimage );
    
            SDL_FreeSurface(loadedimage);
    
            if (optimizedimage != NULL)
            {
                SDL_SetColorKey(optimizedimage, SDL_SRCCOLORKEY, 0xff00ff);
            }
        }
        return optimizedimage;
    }
    
    void draw_surface(SDL_Surface* sur, int x, int y, SDL_Rect* clip)
    {
        SDL_Rect pos = {x, y};
        SDL_BlitSurface(sur, clip, buffer, &pos);
    }
    
    
    Snake::Snake()
    {
        box.x = 0;
        box.y = 0;
    
        box.w = SNAKE_WIDTH;
        box.h = SNAKE_HEIGHT;
    
        False();
    }
    
    void Snake::False()
    {
        Left = false;
        Right = false;
        Up = false;
        Down = false;
    }
    
    void Snake::handle_input()
    {
    
    }
    
    void Snake::move()
    {
    
    }
    
    void Snake::show()
    {
        draw_surface(snake, box.x, box.y, 0);
    }
    
    bool init()
    {
        if (SDL_Init(SDL_INIT_VIDEO) == -1)
        {
            return false;
        }
    
        buffer = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    
        if (!buffer)
        {
            return false;
        }
    
        return true;
    }
    
    bool load_files()
    {
        snake = load_image("gfx/worm.png");
    
    
        if(snake == NULL)
        {
            return false;
        }
    
        return true;
    }
    
    void clean_up()
    {
        SDL_FreeSurface(snake);
        SDL_FreeSurface(buffer);
    
        SDL_Quit();
    }
    
    void Event_handler()
    {
        if (event.type == SDL_KEYUP)
        {
            key[event.key.keysym.sym] == KEY_UP;
        }
        if (event.type == SDL_KEYDOWN)
        {
            key[event.key.keysym.sym] == KEY_DOWN;
        }
    }
    int main(int argc, char* argv[])
    {
        bool done = false;
    
        if(!init())
        {
            return 1;
        }
    
        if(!load_files())
        {
            return 1;
        }
    
        while (!done)
        {
            Snake mySnake;
            while (SDL_PollEvent(&event))
            {
                if (event.type == SDL_QUIT)
                {
                    done = true;
                }
                Event_handler();
            }
    
            mySnake.handle_input();
            mySnake.move();
    
            if (SDL_Flip(buffer) == -1)
            {
                return 1;
            }
    
            mySnake.show();
        }
    
        clean_up();
        return 0;
    }
    Thank you again.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The snake should always move regardless of input. Input merely changes his direction.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I see that a lot of your code is from the lazy foo tutorials, and that you have tried to then put this into a working game loop design, nothing wrong with that as such but i think you should actually type the functions in rather than copy paste stuff, you will get a better understanding of what is going on in my opinion.

    Anyway, continue to look at the SDL tutorials by lazy foo, you need to check the animation tutorial at least, and definitely timers.
    After this once you can get your snake to move across your screen in one direction,
    then it is just a case of setting a new direction on key input, blitting the appropriate tile etc etc

    so the loop will incorporate
    "snake is continually blitted across the screen in whatever the current direction is until a keypress event changes the current direction "
    //...


    edit > I see in your code your are checking 'key up or key down', you should change this and instead check for 'which' key was hit,

    if keyhit = uparrow, newdirection = up..etc
    Last edited by rogster001; 08-19-2010 at 08:21 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Well thanks to both of you the direction really helps and I did type everything in myself cuz for some reason I like typing I never copy and paste ha but thanks anyways I'll try the code I thought of with the direction idea when I get the chance seems Like it'll work.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'll try the code I thought of with the direction idea when I get the chance seems Like it'll work.
    The fact that the snake moves by itself is not a programming issue but a requirement of the game. It's not that it will work but that is has to work that way. As the programmer it is your job to fulfill that requirement. It is usually best to analyze the game and design your approach and your initial objects prior to starting the actual programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  4. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  5. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM

Tags for this Thread