1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
class JC_Game
{
private:
int ilarg;
int ihaut;
SDL_Surface *screen;
SDL_Event event;
bool game;
public:
JC_Game(int x, int y);
void CreateFenetre();
};
JC_Game::JC_Game(int x, int y)
{
ilarg = x;
ihaut = y;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
exit(1);
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(ilarg, ihaut, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if(screen == NULL){
exit(2);
}
//SDL_ShowCursor(SDL_DISABLE );
}
void JC_Game::CreateFenetre()
{
JC_Player MyPlayer;
game = true;
unsigned int last_time = SDL_GetTicks();
while(game)
{
if (SDL_GetTicks() > last_time + (20))
{ last_time = SDL_GetTicks();
if(SDL_PollEvent(&event) && event.type == SDL_QUIT)break;
if(event.key.state == SDL_PRESSED)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE :game = false; break;
case SDLK_UP :MyPlayer.monter();break;
case SDLK_DOWN :MyPlayer.descendre();break;
case SDLK_LEFT :MyPlayer.gauche();break;
case SDLK_RIGHT :MyPlayer.droite();break;
}
}
SDL_Rect rectus = MyPlayer.GetRectheros();
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 100, 100, 100));
SDL_BlitSurface(MyPlayer.GetSurfaceheros(),NULL,screen,&rectus);
SDL_Flip(screen);
}
}
//SDL_ShowCursor(SDL_ENABLE);
SDL_FreeSurface(screen);
}
class JC_Player
{
private:
SDL_Surface *heros;
SDL_Rect rectheros;
public:
JC_Player();
void monter();
void descendre();
void gauche();
void droite();
SDL_Rect GetRectheros();
SDL_Surface* GetSurfaceheros();
};
JC_Player::JC_Player()
{
char *szName = "./heros.bmp";
heros = SDL_LoadBMP("./heros.bmp");
SDL_SetColorKey(heros, SDL_SRCCOLORKEY, SDL_MapRGB(heros->format,255,0,0));
rectheros.w = 32;
rectheros.h = 40;
rectheros.x = 256;
rectheros.y = 440;
}
void JC_Player::monter()
{
if(rectheros.y > 0)
rectheros.y -= 20;
}
void JC_Player::descendre()
{
if(rectheros.y < 440)
rectheros.y += 20;
}
void JC_Player::gauche()
{
if(rectheros.x > 0)
rectheros.x -= 16;
}
void JC_Player::droite()
{
if(rectheros.x < 608)
rectheros.x += 16;
}
SDL_Rect JC_Player::GetRectheros()
{
return rectheros;
}
SDL_Surface* JC_Player::GetSurfaceheros()
{
return heros;
} |
Partager