Sign in to follow this  
feliperuiz

Align to grid

Recommended Posts

Hey, I'm developing a game like bomber man in the C language using the SDL and I need to align my character to the grid. I thought of a way of doing it, but it feels too much work-around programming. The code for this is: #define GRID 32 ... case SDLK_LEFT: for(i=0;i<GRID/speed;i++) { walk('l',speed,screen); SDL_BlitSurface(background,NULL,screen,NULL); SDL_Delay(50); SDL_BlitSurface(Kenny.image,&(Kenny.src),screen,&(Kenny.dst)); } ... //here is the same procedure for the other keys void walk (char direction, int speed, SDL_Surface *screen) { switch(direction) { case 'u': Kenny.step.up++; Kenny.step.left=0; Kenny.step.right=0; Kenny.step.down=1; if(Kenny.dst.y<=0) { Kenny.dst.y-=0; } else { Kenny.dst.y-=speed; } Kenny.src.y=0; //here I change Kenny.src.x=((Kenny.step.up+1)%4)*32; //the sprites break; ... //here is the same procedure for the other directions default: break; } } I have some urgency on this 'cause this game needs to be ready by tuesday. Thanks. [Edited by - feliperuiz on July 5, 2008 5:57:06 AM]

Share this post


Link to post
Share on other sites
Not sure if this is what you had in mind, but something along these lines:

#include <stdio.h>

int AlignToGrid (int n, int gridsize)
{
return (n / gridsize) * gridsize;
}

int main ()
{
int i=0;
for (i=0; i < 120; ++i)
{
printf ("Characters position is %i, aligned to grid %i.\n", i, AlignToGrid (i, 32));
}

return 0;
}

Share this post


Link to post
Share on other sites

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