Sign in to follow this  

two problems with my game so far

This topic is 4716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm writing a simple snake game to get the hang of linked lists and things. I haven't coded most of it yet but I am already having two problems. The first problem is that the "snake" seems to not move at the same speed even though I have tried to do time based movement. The other problem is if i uncomment this part in the game init:
   /*for (x = 0; x <= MAPWIDTH; x++)
        for (y = 0; y <= MAPHEIGHT; y++)
            map[x][y] = 0;*/

Somthing goes wrong and nothing is displayed in the window at all?! Anyway, here is my code so far (its nothing special yet). Also if you see anything done in a backwards way could you tell me so I don't go about it the wrong way :) thanks
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>

#define MAPWIDTH 40
#define MAPHEIGHT 40

/* Define Booleans */
typedef enum { false, true } bool;
bool one = false;

struct item
{
    int x_pos;
    int y_pos;
};    
struct item apple;

struct part
{
    int x_pos, y_pos;
    int x_vol, y_vol;
    enum Estate {RIGHT, LEFT, UP, DOWN} state;
    struct part *next_part;  /* Pointer to next part */
};    
struct part *first_part = NULL;
struct part *last_part = NULL;

int map[MAPWIDTH][MAPHEIGHT];

/* function predeclarations */
int main(int argc, char *argv[]);
bool game_init();
void game_loop();
void get_input();
void add_apple();
void move_snake(float time_passed);
void draw_apple();
void draw_snake();
void draw_part(struct part *current_part);
void draw_screen();
void game_close();

bool game_running;
float current_time; 
SDL_Surface *screen, *sprite_image;

int main(int argc, char *argv[])
{
    atexit(game_close);
    
    if (game_init() == false)
        exit(1);
    
    while (game_running == true)
        game_loop();
}

bool game_init()
{
    int x, y;
    SDL_Surface *temp;
    
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL\n");
        return false;
    }
    
    screen = SDL_SetVideoMode(400, 400, 16, SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode\n");
        return false;
    }
    
    SDL_WM_SetCaption("Snake Attempt", NULL);   
    
    temp = SDL_LoadBMP("sprites.bmp");
    if (temp == NULL)
    {
        printf("Unable to load picture\n");
        return false;
    }
    SDL_SetColorKey(temp, SDL_SRCCOLORKEY | SDL_RLEACCEL, (Uint16) SDL_MapRGB(temp->format, 0, 0, 0));     
    sprite_image = SDL_DisplayFormat(temp);
    
    SDL_FreeSurface(temp);
    
    current_time = SDL_GetTicks();
    
    first_part = malloc(sizeof(struct part));
    first_part->next_part = NULL;
    first_part->x_pos = 200;
    first_part->y_pos = 200;
    first_part->x_vol = 1;
    first_part->y_vol = 0;
    first_part->state = RIGHT;
    last_part = first_part;
    
   /*for (x = 0; x <= MAPWIDTH; x++)
        for (y = 0; y <= MAPHEIGHT; y++)
            map[x][y] = 0;*/
    
    add_apple();
    
    game_running = true;
    return true;
}    

void game_loop()
{
    float time_passed, last_time;

    last_time = current_time;
    current_time = SDL_GetTicks();
    time_passed = current_time - last_time;
    
    get_input();
    move_snake(time_passed);
    
    draw_screen();
    draw_snake();
    draw_apple();
    
    SDL_Flip( screen );
}    

void get_input()
{
    SDL_Event event;
    SDL_keysym keysym;
    
    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
            case SDL_QUIT:
                game_running = false;
                break;
                
            case SDL_KEYDOWN:
                keysym = event.key.keysym;
				switch(keysym.sym) 
                {
                    case SDLK_ESCAPE:
                        game_running = false;
                        break;
                        
                    case SDLK_LEFT:
                        first_part->x_vol = -1;
                        first_part->y_vol = 0;
                        first_part->state = LEFT;
                        break;
                    
                    case SDLK_RIGHT:
                        first_part->x_vol = 1;
                        first_part->y_vol = 0;
                        first_part->state = RIGHT;
                        break;
                    
                    case SDLK_UP:
                        first_part->x_vol = 0;
                        first_part->y_vol = -1;
                        first_part->state = UP;
                        break;
                        
                    case SDLK_DOWN:
                        first_part->x_vol = 0;
                        first_part->y_vol = 1;
                        first_part->state = DOWN;
                        break;
                }
                break;
        }
        break;
    }
}  

void add_apple()
{
    int x, y;
    
    srand( (unsigned)time( NULL ) );
    x = rand() % 40;
    srand( (unsigned)time( NULL ) );
    y = rand() % 40;
    
    apple.x_pos = x;
    apple.y_pos = y;
}    
    
    
void move_snake(float time_passed)
{
    time_passed = time_passed / 10;
    first_part->x_pos += (first_part->x_vol * time_passed);
    first_part->y_pos += (first_part->y_vol * time_passed);
}    

void draw_apple()
{
    SDL_Rect src, dest;
    
    src.x = 10;
    src.y = 0;
    src.w = 10;        
    src.h = 10;
    dest.x = apple.x_pos*10;
    dest.y = apple.y_pos*10;
    dest.w = 10;        
    dest.h = 10;

    SDL_BlitSurface( sprite_image, &src, screen, &dest );
}
    
void draw_snake()
{
    draw_part(first_part);
}    

void draw_part(struct part *current_part)
{
    SDL_Rect src, dest;
    
    src.x = 0;
    src.y = 0;
    src.w = 10;        
    src.h = 10;
    dest.x = current_part->x_pos;
    dest.y = current_part->y_pos;
    dest.w = 10;        
    dest.h = 10;

    SDL_BlitSurface( sprite_image, &src, screen, &dest );
}    

void draw_screen()
{
    int x, y;
    SDL_Rect src, dest;
    
    
    for (x = 0; x <= MAPWIDTH; x++)
        for (y = 0; y <= MAPHEIGHT; y++)
        {
            printf("X: %d, y: %d\n",x,y);
            src.x = 30;
            src.y = 0;
            src.w = 10;        
            src.h = 10;
            dest.x = x * 10;
            dest.y = y * 10;
            dest.w = 10;        
            dest.h = 10;
            
            SDL_BlitSurface( sprite_image, &src, screen, &dest );
        }    

    
}    

void game_close()
{
    SDL_FreeSurface(sprite_image);
    SDL_Quit();
}    

Share this post


Link to post
Share on other sites
Quote:
/*for (x = 0; x <= MAPWIDTH; x++)
for (y = 0; y <= MAPHEIGHT; y++)
map[x][y] = 0;*/


You need to put
Quote:
/*for (x = 0; x < MAPWIDTH; x++)
for (y = 0; y < MAPHEIGHT; y++)
map[x][y] = 0;*/


Your array is declared at map[mapwidth][mapheight] which means the max element you can reach is map[mapwidth-1][mapheight-1]

Share this post


Link to post
Share on other sites
I have another problem now :( It doesnt like this function.

[source lang=c]
void move_part(float time_passed, struct part *current_part)
{
int x, int y;

x = current_part->x_pos + (current_part->x_vol * time_passed);
y = current_part->y_pos + (current_part->y_vol * time_passed);

map_collision_detection(x, y);

current_part->x_pos = x;
current_part->y_pos = y;
}





The problem is the *current_part, but the problem is I need it to be a pointer.

edit: actualy i dont think the problem is *current_part, not sure what it is though

edit2: fixed it, man i feel silly

[Edited by - kzar on January 16, 2005 9:51:15 AM]

Share this post


Link to post
Share on other sites

This topic is 4716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this