cara, achei um programa muito parecido .. tomara q entenda.. #include <stdio.h>
#include <conio.h>
#include <windows.h>
/** Configurações... **/
const double frame_segs = 0.03;//intervalo dos frames em segundos
const int max_tiros = 4;//número máximmo de tiros na tela ao mesmo tempo
//***********************//
typedef struct {//estrutura p/ as coordenadas
int x,y;
} _coord;
typedef struct {//estrutura para os objetos
_coord pos;
int visivel,representacao;
} _obj;
_coord const l_esq_sup = {1,0},l_dir_inf = {78,21};//limites da tela
int const t_esc=27,t_esp=32,s_esq=75,s_dir=77;//constantes para melhorar a legibilidade
void oculta_cursor(void){
CONSOLE_CURSOR_INFO cur = {50,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
}
void gotoxy(int x, int y){//implementação da função gotoxy() c/ windows.h
COORD c = {x,y};
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE),c);
}
int move_obj(_obj *obj,int add_x,int add_y)
{//função para mover algum objeto
int x = obj->pos.x,y = obj->pos.y;
gotoxy(x,y);
printf("%c",t_esp);
if((x+add_x)>=l_esq_sup.x && (x+add_x)<=l_dir_inf.x) x+=add_x;
if((y+add_y)>=l_esq_sup.y && (y+add_y)<=l_dir_inf.y) y+=add_y;
obj->pos.x = x;
obj->pos.y = y;
gotoxy(x,y);
printf("%c",obj->representacao);
return 1;
}
int apaga_obj(_obj *obj)
{//função para apagar algum objeto
gotoxy(obj->pos.x,obj->pos.y);
printf("%c",t_esp);
obj->visivel = 0;
return 1;
}
_obj nave = {{40,l_dir_inf.y},1,127},tiros[max_tiros];//objeto nave e um vetor de objetos q serão os tiros
int main(int argc,char *argv[])
{
int tecla = 0,i;
for(i=0;i<max_tiros;i++)
{//zerar todos os objetos tiros
tiros[i].representacao = 58;
tiros[i].visivel = 0;
}
oculta_cursor();
gotoxy(nave.pos.x,nave.pos.y);
printf("%c",nave.representacao);
while (tecla != t_esc)
{
Sleep((unsigned long)(frame_segs*1000));//valor dos frames
for(i=0;i<max_tiros;i++)if(tiros[i].visivel)
{//este loop é responsavel por mover os tiros,e apagar aqueles que chegarem ao limite da tela
move_obj(&tiros[i],0,-1);//move os tiros
if((tiros[i].pos.y)<=(l_esq_sup.y)) apaga_obj(&tiros[i]);//apaga os tiros
}
if(_kbhit())
{//kbhit testa se foi pressionada alguma tecla
tecla = _getch();
if(tecla == 224) tecla = _getch();//se foi setas retornará 224 e será necessário pegar o valor novamentre
switch(tecla)
{
case t_esp://se foi espaço procura por um objeto tiro disponivel...
for(i=0;i<max_tiros;i++)if(!tiros[i].visivel)
{
tiros[i].pos.x = nave.pos.x;
tiros[i].pos.y = nave.pos.y-1;
tiros[i].visivel=1;
gotoxy(tiros[i].pos.x,tiros[i].pos.y);
printf("%c",tiros[i].representacao);
break;
}
break;
case s_esq://se foi esquerda move a nave para a esquerda
move_obj(&nave,-1,0);
break;
case s_dir://move para a direita
move_obj(&nave,1,0);
break;
}//fim_switch
}//fim_if(kbhit)
}//fim do loop do game
return 0;
}