Created
May 19, 2018 15:33
-
-
Save rauldeavila/47abaa2dd766ad4a3ae01bc268887de8 to your computer and use it in GitHub Desktop.
Feito no macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ncurses.h> | |
#include <unistd.h> | |
#define DELAY 35000 | |
void movimento_da_nave(){ | |
int x_nave = 0, | |
x = 0, | |
y_nave = 0, | |
y = 0, | |
max_x = 0, | |
max_y = 0, | |
next_x = 0, | |
direction = 0; // 0 = parado // 1 = direita // -1 = esquerda | |
getmaxyx(stdscr, max_y, max_x); // tornando o movimento dinamico de acordo com o tamanho do terminal | |
/* posicao inicial da nave :: note que o y nunca mudará! */ | |
x_nave = max_x / 2; | |
y_nave = max_y - 6; // comeca 6 pixels acima do fim da tela, pois os ultimos 5 sao da status_bar | |
/* LOOP DO MOVIMENTO EM SI */ | |
while (1) { | |
getmaxyx(stdscr, max_y, max_x); | |
y_nave = max_y - 6; | |
clear(); | |
// DESENHO DA NAVE | |
attron(COLOR_PAIR(2)); | |
mvprintw(y_nave, x_nave, " | "); | |
mvprintw(y_nave+1, x_nave, "|-^-|"); | |
attroff(COLOR_PAIR(2)); | |
refresh(); // refresh = atualizar as informaceos da tela | |
usleep(DELAY); // delay para que o movimento nao fique tao rapido (<unistd.h>) | |
/* IMPLEMENTACAO DO MOVIMENTO DA NAVE, COM COLISOES */ | |
int ch = getch(); | |
/* MOVE PARA DIREITA */ | |
if ( ch == KEY_RIGHT ){ | |
direction = 1; | |
next_x = x_nave + direction; | |
if ( (next_x + 4) >= max_x || next_x < 0) { // definindo a colisao da direita ( next_x + 4 pois a colisao eh com a asa da direita) | |
direction*= -1; | |
} else { | |
x_nave+= direction; | |
} | |
/* MOVE PARA ESQUERDA */ | |
} else if ( ch == KEY_LEFT ){ | |
direction = -1; | |
next_x = x_nave + direction; | |
if (next_x >= max_x || next_x < 0) { // colisao da esquerda usa o proprio next_x pois eh a asa da esquerda | |
direction*= -1; | |
} else { | |
x_nave+= direction; | |
} | |
} | |
} | |
} | |
int main(int argc, char *argv[]) { | |
initscr(); // inicio de ncurses | |
noecho(); // nao printa nenhum keypress na tela | |
curs_set(FALSE); // nao mostra nenhum cursor na tela | |
nodelay(stdscr, TRUE); // forma de usar getch() sem ficar esperando o input do usuario | |
keypad(stdscr, TRUE); // funcao que habilita reconhecimento de teclas especiais como arrows, ctrl e F1...19. | |
start_color(); // funcao de cores da ncurses | |
init_pair(1, COLOR_RED, COLOR_GREEN); // init_pair(CODIGO, COR DA FONTE, COR DO FUNDO); | |
init_pair(2, COLOR_BLUE, COLOR_BLACK); | |
init_pair(3, COLOR_WHITE, COLOR_WHITE); | |
movimento_da_nave(); | |
endwin(); // fim de ncurses, restaura o terminal aos valores normais | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment