Help with snake game in sdl/c

Discussion in 'C++' started by joellllmal, Aug 31, 2010.

  1. joellllmal

    joellllmal New Member

    Joined:
    Aug 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 <iostream>
    
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice