diff --git a/.gitignore b/.gitignore index efd8b5d..2214249 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ labirintoo.exe +nbproject/ diff --git a/Labirintilt.h b/Labirintilt.h new file mode 100644 index 0000000..c4fc892 --- /dev/null +++ b/Labirintilt.h @@ -0,0 +1,95 @@ +#ifndef LABIRINTILT_H +#define LABIRINTILT_H + +#include + +#include "Player.h" +#include "Scenario.h" + +#define STAGES 9 + +typedef struct { + int level; + Player player; + Traffic traffics[4]; +} Game; + +void new_Game(Game * game) +{ + game->level = 1; + new_Player(&(game->player)); + int i; + + for (i = 0; i < STAGES / 2; i ++) { + new_Traffic(&(game->traffics[i])); + } +} + +void describe_scenario(Game game) +{ + int i; + for (i = 3; i >=0; i--) { + printf("stage: %d ", i); + describe_traffic(game.traffics[i]); + printf("\n\n"); + } +} + +void move_scenario(Game * game, int speed_min) +{ + int i,j; + Space * space; + Traffic traffic; + for (i = 0; i < STAGES / 2; i ++) { + if (game->traffics[i].speed < speed_min) { + continue; + } + traffic = game->traffics[i]; + for (space = traffic.first_space, j =0; j < traffic.num_spaces; j ++ , space = space->next) { + space->x_start++; + space->x_final++; + if (space->x_start > 120) { + srand(time(NULL)); + space->x_final = 0; + space->x_start = -15; + rotateTraffic(&(game->traffics[i]), space); + } + } + } + speed_min = (speed_min++) % 5; +} + +int colision(Game game) { + int stage = (game.player.y) / 5; +// printf("\nstage: %d\n",stage); + if (stage % 2 == 0) { + return 0; + } + Traffic traffic = game.traffics[stage / 2]; + Space * space; + int count; + for (space = traffic.first_space, count = 0;count < traffic.num_spaces; count++, space = space->next) { + if ((game.player.x >= space->x_start) && (game.player.x + 5 <= space->x_final)) { + return 0; + } + } + return 1; +} + +int survive(Game * game) { + return die(&game->player); +} + +void next_level(Game * game) { + game->level ++; + game->player.lives ++; + describe_player(game->player); + reset_position_player(&game->player); +} + +int win(Game game) { + int stage = game.player.y / 5 + 1; + return stage == STAGES; +} + +#endif /* LABIRINTILT_H */ diff --git a/Player.h b/Player.h new file mode 100644 index 0000000..adb412c --- /dev/null +++ b/Player.h @@ -0,0 +1,89 @@ +#ifndef PLAYER_H +#define PLAYER_H + + +#define PLAYER_STEP 5 + +typedef struct { + int x; // posição horizontal do jogador + int y; // posição vertical do jogador + int lives; // quantidade de vidas restantes +} Player; + + +typedef int (*FunctionMove) (Player * player); + +// Inicia +void new_Player(Player *); + +// Coloca as coordenadas iniciais do jogador +int reset_position_player(Player *); + +// Diminui a quantidade de vidas e retorna a quantidade restante +int die(Player * player); + +void new_Player(Player * player) +{ + player->lives = 3; + reset_position_player(player); +} + +int reset_position_player(Player * player) +{ + player->x = 60; + player->y = 0; +} + +int die(Player * player) +{ + reset_position_player(player); + return --player->lives; +} + + + +int move_player_up(Player * player) { + if (player->y > 35) { + return 0; + } + player->y = player->y + PLAYER_STEP; +} + +int move_player_down(Player * player) { + if (player->y <= 0) { + return 0; + } + player->y = player->y - PLAYER_STEP; +} + +int move_player_left(Player * player) { + if (player->x <= 0) { + return 0; + } + player->x = player->x - PLAYER_STEP; +} + +int move_player_right(Player * player) { + if (player->x >= 115) { + return 0; + } + player->x = player->x + PLAYER_STEP; +} + +void move_player(int key, Player * player) { + FunctionMove function_move[4] = { + move_player_left, + move_player_up, + move_player_right, + move_player_down + }; + + (*(function_move[key]))(player); +} + +void describe_player(Player player) +{ + printf("---PLAYER----\nlives: %d\nx: %d\ny: %d\n\n", player.lives, player.x, player.y); +} + +#endif /* PLAYER_H */ diff --git a/Player.h.gch b/Player.h.gch new file mode 100644 index 0000000..ffa5ddf Binary files /dev/null and b/Player.h.gch differ diff --git a/Scenario.h b/Scenario.h new file mode 100644 index 0000000..2c82b6c --- /dev/null +++ b/Scenario.h @@ -0,0 +1,89 @@ +#ifndef SCENARIO_H +#define SCENARIO_H + +typedef struct S_space{ + int x_start; + int x_final; + struct S_space * next; +} Space; + +void new_Space(Space * space, int x1); + +void new_Space(Space * space, int x1) +{ + space->x_start = x1; + space->x_final = x1 +15; + space->next = NULL; +} + +typedef struct { + int num_spaces; + int speed; + Space * first_space; +} Traffic; + +void add_Space(Traffic * traffic, Space * space) +{ + + if (!traffic->first_space) { + traffic->first_space = space; + space->next = space; + return; + } + Space * last = traffic->first_space; + while(last->next != traffic->first_space) { + last = last->next; + } + last->next = space; + space->next = traffic->first_space; +} + +void rotateTraffic(Traffic * traffic, Space * space) +{ + traffic->first_space = space; +} + + +void new_Traffic(Traffic * traffic) +{ + int num_spaces = 1 + rand() % 3; + int band = 120/num_spaces; + int last_x = 0; + int i; + Space * space; + + traffic->num_spaces = num_spaces; + traffic->speed = 1 + rand() % 5; + traffic->first_space = NULL; + + for (i = 0; i < traffic->num_spaces; i++) { + last_x += last_x + 15 + rand() % band; + space = malloc(sizeof(Space)); + new_Space(space, last_x); + add_Space(traffic, space); + } +} + + +void describe_space(Space space) { + printf("%d <--> %d", space.x_start, space.x_final); +} + + +void describe_traffic(Traffic traffic) +{ + printf("speed: %d\n", traffic.speed); + Space * space; + int i; + + for (i =0, space = traffic.first_space; i < traffic.num_spaces; i++, space= space->next){ + describe_space(*space); + printf(" "); + } + printf("\n"); +} + + + +#endif /* SCENARIO_H */ + diff --git a/assertions.h b/assertions.h new file mode 100644 index 0000000..f1f69b4 --- /dev/null +++ b/assertions.h @@ -0,0 +1,45 @@ +#ifndef ASSERTIONS_H +#define ASSERTIONS_H + +void assertTrue(int var) { + if (!var) { + printf("esperado: true, encontrado: %d", var); + exit(1); + } + printf("."); +} + +void assertFalse(int var) { + if (var) { + printf("esperado: false, encontrado: %d", var); + exit(1); + } + printf("."); +} + +void assertEquals(int expected, int value) { + if (value != expected) { + printf("esperado: %d, encontrado: %d", expected, value); + exit(1); + } + printf("."); +} + +void assertGreaterThan(int expected, int value) { + if (value <= expected) { + printf(", esperado maior que %d, encontrado: %d", expected, value); + exit(1); + } + printf("."); +} + +void assertLessThan(int expected, int value) { + if (value >= expected) { + printf(", esperado menor que %d, encontrado: %d", expected, value); + exit(1); + } + printf("."); +} + +#endif /* ASSERTIONS_H */ + diff --git a/labirintoo.c b/labirintoo.c new file mode 100644 index 0000000..73ff1f7 --- /dev/null +++ b/labirintoo.c @@ -0,0 +1,411 @@ +#include +#include +#include +#include "Labirintilt.h" +#include +#define LEVELS 8 + +#define RESOLUTION 80 +void Desenha(); + +/// COLORS +float CorObstaculo[4] = { 0, 0, 1, 1}; +float CorPlayer[4] = { 1, 0, 0, 1}; +int page = 0; +Game game; + +void desenha_retangulo(int x1, int x2, int y) +{ + glBegin(GL_QUADS); + glVertex2f(x1, y + 5); + glVertex2f(x1, y); + glVertex2f(x2, y); + glVertex2f(x2, y + 5); + glEnd(); +} + +void set_color(float color[4]) { + glColor4f(color[0], color[1], color[2], color[3]); +} + +void desenha_cenario(Traffic * traffics, int tam) +{ + int i; + int j; + Traffic traffic; + Space * space; + Space inicio; + Space fim; + + glClearColor(1, 1, 1, 1); + + set_color(CorObstaculo); + + new_Space(&inicio, -15); + new_Space(&fim, 120); + + for (i = 0; i < tam; i ++) { + traffic = traffics[i]; + inicio.next = traffic.first_space; + for (space = &inicio, j = 0; j < traffic.num_spaces ; j++, space = space->next) { + if (space->x_final < 0 || space->x_start > 120) { + continue; + } + desenha_retangulo(space->x_final, space->next->x_start, 5 + i*10 ); + } + desenha_retangulo(space->x_final, 120, 5 + i*10 ); + } +} + +void desenha_player(Player player) { + if (player.lives < 4 ){ + CorPlayer[3] = 1.0 / (4 - player.lives); + } + set_color(CorPlayer); + desenha_retangulo(player.x, player.x + 5, player.y); +} + +// Função callback chamada quando o tamanho da janela � alterado +void AlteraTamanhoJanela(GLsizei w, GLsizei h) +{ + + glViewport(0, 0, w, h); + + // Inicializa o sistema de coordenadas + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + // Estabelece a janela de sele��o (esquerda, direita, inferior, + // superior) + gluOrtho2D (0.0f, 120.0f, 0.0f, 45.0f); +} + +void alerta(char* mensagem, char * titulo) { + MessageBox(NULL, mensagem, titulo, MB_OK); +} + +void game_over() +{ + alerta("Você perdeu, tente novamente", "Alerta"); + page = 0; +} + +// Fun��o callback chamada para gerenciar eventos de teclas +void moves (int key, int x, int y) +{ + move_player(key - GLUT_KEY_LEFT, &game.player); + Desenha(); + if (colision(game) && !survive(&game)) { + game_over(); + } + if (win(game)) { + next_level(&game); + muda_cor_fase(); + } +// Desenha(); +} + +void muda_cor_fase() { + float vermelho = rand()/(float)RAND_MAX; + float verde = rand()/(float)RAND_MAX; + float azul = rand()/(float)RAND_MAX; + CorObstaculo[0] = vermelho; + CorObstaculo[1] = verde; + CorObstaculo[2] = azul; + +// CorPlayer[0] = verde; +// CorPlayer[1] = azul; +// CorPlayer[2] = vermelho; + + glutPostRedisplay(); +} + +void paginaAjuda() { + glClear(GL_COLOR_BUFFER_BIT); + glColor3f(1,1,1); + glPushMatrix(); + glTranslatef(53,38,0); + glScalef(0.015, 0.015, 0.015); // diminui o tamanho do fonte + glLineWidth(1.3); // define a espessura da linha + DesenhaTextoStroke(GLUT_STROKE_ROMAN,"Instrucoes"); + glPopMatrix(); + glPushMatrix(); + glTranslatef(25,35,0); + glScalef(0.011, 0.011, 0.011); + glLineWidth(1); // define a espessura da linha + DesenhaTextoStroke(GLUT_STROKE_ROMAN,"O objetivo do jogo e chegar do outro lado da via, evitando o contato com os obstaculos."); + glPopMatrix(); + glPushMatrix(); + glTranslatef(25,33,0); + glScalef(0.011, 0.011, 0.011); + glLineWidth(1); // define a espessura da linha + DesenhaTextoStroke(GLUT_STROKE_ROMAN,"Voce tem 3 tentativas para isso."); + glPopMatrix(); + glPushMatrix(); + glTranslatef(25,27,0); + glScalef(0.011, 0.011, 0.011); + glLineWidth(1); // define a espessura da linha + DesenhaTextoStroke(GLUT_STROKE_ROMAN,"Para controlar o player utilize as teclas direcionais do teclado ou as teclas: 'a', 'w', 'd' e 's'"); + glPopMatrix(); + glPushMatrix(); + glTranslatef(25,21,0); + glScalef(0.011, 0.011, 0.011); + glLineWidth(1); // define a espessura da linha + DesenhaTextoStroke(GLUT_STROKE_ROMAN,"Para sair do menu Instrucoes pressione o botao direito do mouse, e escolha a opção desejada."); + glPopMatrix(); + //Executa os comandos OpenGL para renderização. +// glutMainLoop(); +// system("pause"); +} + +void paginainicial(void){ + glClearColor(0.0, 0.0, 0.0, 1); + glPushMatrix(); + glColor3f(.439200, 0.858800, 0.858800); + glTranslatef(15,28,0); + glScalef(0.05, 0.05, 0.05); + glLineWidth(2); // define a espessura da linha + DesenhaTextoStroke(GLUT_BITMAP_TIMES_ROMAN_24," LABIRINTILT"); + glPopMatrix(); + glPushMatrix(); + glColor3f(1, 1, 0); + glTranslatef(0,21,0); + glScalef(0.02, 0.02, 0.02); + glLineWidth(1); // define a espessura da linha + DesenhaTextoStroke(GLUT_BITMAP_TIMES_ROMAN_24," NOVO JOGO"); + glPopMatrix(); + glPushMatrix(); + glTranslatef(1.5,17,0); + glScalef(0.02, 0.02, 0.02); + DesenhaTextoStroke(GLUT_BITMAP_TIMES_ROMAN_24," AJUDA"); + glPopMatrix(); + glPushMatrix(); + glTranslatef(0.75,13,0); + glScalef(0.02, 0.02, 0.02); + DesenhaTextoStroke(GLUT_BITMAP_TIMES_ROMAN_24," SAIR"); + glPopMatrix(); + glPushMatrix(); + glTranslatef(35,34,0); + glScalef(2, 2, 2); + glColor3f(.45, 0.86, 0.86); + glBegin(GL_QUADS); + glVertex3f(0,0,0); + glVertex3f(6,0,0); + glVertex3f(6,1,0); + glVertex3f(0,1,0); + glEnd(); + glPopMatrix(); + glPushMatrix(); + glTranslatef(38,36.5,0); + glScalef(2, 2, 2); + glColor3f(.4, 0.4, 1); + glBegin(GL_QUADS); + glVertex3f(0,0,0); + glVertex3f(6,0,0); + glVertex3f(6,1,0); + glVertex3f(0,1,0); + glEnd(); + glPopMatrix(); + glPushMatrix(); + glTranslatef(31,39,0); + glScalef(2, 2, 2); + glColor3f(.45, 0.86, 0.86); + glBegin(GL_QUADS); + glVertex3f(0,0,0); + glVertex3f(6,0,0); + glVertex3f(6,1,0); + glVertex3f(0,1,0); + glEnd(); + glPopMatrix(); + glFlush(); + glutMainLoop(); +} + +void MenuPrincipal(int op) { + page = op; + if (op == 1) { + game.level = 1; + game.player.lives = 3; + reset_position_player(&game.player); + } +} + +void CriaMenu() +{ + int menu; + + menu = glutCreateMenu(MenuPrincipal); + glutAddMenuEntry("Menu",4); + glutAddMenuEntry("Novo jogo",1); + glutAddMenuEntry("Ajuda",2); + glutAddMenuEntry("Sair",3); + + glutAttachMenu(GLUT_RIGHT_BUTTON); +} + +void DesenhaTextoStroke(void *font, char *string) +{ + // Exibe caractere a caractere + while(*string) + glutStrokeCharacter(GLUT_STROKE_ROMAN,*string++); +} + +void Mouse(int button, int state,int x, int y){ + int level = y/5; + float vermelho = rand()/(float)RAND_MAX; + float verde = rand()/(float)RAND_MAX; + float azul = rand()/(float)RAND_MAX; + + /*if(button != GLUT_RIGHT_BUTTON){ + return; + } + + CorObstaculo[0] = vermelho; + CorObstaculo[1] = verde; + CorObstaculo[2] = azul; + CorPlayer[0] = verde; + CorPlayer[1] = azul; + CorPlayer[2] = vermelho; + + glClearColor(azul, vermelho, verde, 1.0f); + */ + if (button == GLUT_RIGHT_BUTTON){ + if (state == GLUT_DOWN){ + CriaMenu(); + } + } + if(button == GLUT_LEFT_BUTTON){ + printf("(%i,%i)\n",x,y); + if(x>(565) && x <(746)){ + if(y>(240) && y <(268)){ + MenuPrincipal(1); + } + if(y>285 && y <312){ + MenuPrincipal(2); + } + if(y>330 && y <355){ + MenuPrincipal(3); + } + } + } + + glutPostRedisplay(); +} + +void teclado (unsigned char key, int x, int y) +{ + switch(key){ + case 'a': + move_player_left(&game.player); + break; + case 'w': + move_player_up(&game.player); + if (win(game)) { + next_level(&game); + muda_cor_fase(); + } + break; + case 'd': + move_player_right(&game.player); + break; + case 's': + move_player_down(&game.player); + break; + case 27: + if (page) { + page = 0; + return; + } + exit(0); + } +} + +// Fun��o respons�vel por inicializar par�metros e vari�veis +void Inicializa (void) +{ + // Define a cor de fundo da janela de visualiza��o como branca + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); +} + + +int speed_min = 1; + +void Timer(int i) +{ + int K = RESOLUTION/(10 * game.level); + if (i >= K) { + move_scenario(&game, speed_min); + speed_min = (++speed_min) % 5; + i = 1; + } + + glutPostRedisplay(); + Desenha(); + if (colision(game) && !survive(&game)) { + game_over(); + } + glutTimerFunc(1000/RESOLUTION ,Timer, i + game.level); +} + +void Desenha() +{ + glClear(GL_COLOR_BUFFER_BIT); + switch(page) { + case 1: + desenha_cenario(game.traffics, 4); + desenha_player(game.player); + break; + case 2: + paginaAjuda(); + break; + case 3: + exit(0); + default: + paginainicial(); + } + + glFlush(); +} + +int main() +{ + new_Game(&game); + + // Define do modo de opera��o da GLUT + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); + + // Especifica a posi��o inicial da janela GLUT + glutInitWindowPosition(5,5); + + // Especifica o tamanho inicial em pixels da janela GLUT + glutInitWindowSize(3*450,500); +// gluOrtho2D (0.0f, 45.0f, 0.0f, 45.0f); + + // Cria a janela passando como argumento o t�tulo da mesma + glutCreateWindow("Labirintilt"); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + + + // Registra a fun��o callback de redimensionamento da janela de visualiza��o + glutReshapeFunc(AlteraTamanhoJanela); + + // Registra a fun��o callback para tratamento das teclas ASCII + glutMouseFunc(Mouse); + glutSpecialFunc(moves); + // Registra a fun��o callback de redesenho da janela de visualiza��o + glutDisplayFunc(Desenha); + + glutKeyboardFunc (teclado); + glutTimerFunc(1000/RESOLUTION ,Timer,1); + + // Chama a fun��o respons�vel por fazer as inicializa��es + Inicializa(); + + // Inicia o processamento e aguarda intera��es do usu�rio + glutMainLoop(); + + return 0; +} diff --git a/labirintoo.cpp b/labirintoo.cpp deleted file mode 100644 index 88f95fd..0000000 --- a/labirintoo.cpp +++ /dev/null @@ -1,241 +0,0 @@ -#include -#include -#include -#include -#define LEVELS 8 - -void DesenhaObstaculos(); -void DesenhaPlayer(); -int colidiu(); - -typedef struct { - int x1; - int x2; -} Obstaculo; - -typedef struct { - int x; - int y; - int lives; -} Player; - -Player player; -Obstaculo obstaculos[LEVELS/2]; -float CorObstaculo[3] = { 0, 0, 1}; -float CorPlayer[3] = { 1, 0, 0}; - -void inicializaObstaculos() { - int xs[LEVELS] = { - 5,15, - 115,120, - 0,10, - 18, 25, - }; - int i; - for (i =0; i< LEVELS/2; i++ ) { - obstaculos[i].x1 =xs[i*2]; - obstaculos[i].x2 = xs [i*2+1]; - } -} - -void resetPlayerPosition() { - player.x = 60; - player.y = 0; -} - -void inicializaPlayer() { - player.lives = 3; - resetPlayerPosition(); -} - -void Desenha(void) -{ - glClear(GL_COLOR_BUFFER_BIT); - DesenhaObstaculos(); - DesenhaPlayer(); - glFlush(); -} - -void DesenhaObstaculos() { - int level; - int x1; - int x2; - int i; - int y; - glColor3f(CorObstaculo[0], CorObstaculo[1], CorObstaculo[2]); - - for (i=0; i 35){ - alerta("YOU WON!!", ":)"); - exit(0); - } - break; - case GLUT_KEY_DOWN: - if (player.y > 0){ - player.y -= 5; - } - break; - case GLUT_KEY_LEFT: - if (player.x > 0) { - player.x -= 5; - } - break; - case GLUT_KEY_RIGHT: - if (player.x < 115) { - player.x += 5; - } - break; - case 27: - exit(0); - } - Desenha(); - if (colidiu()) { - resetPlayerPosition(); - player.lives--; - if (!player.lives){ - alerta("GAME OVER", ":("); - exit(0); - } - } - Desenha(); -} - -void Mouse(int button, int state,int x, int y){ - int level = y/5; - float vermelho = rand()/(float)RAND_MAX; - float verde = rand()/(float)RAND_MAX; - float azul = rand()/(float)RAND_MAX; - - if(button != GLUT_RIGHT_BUTTON){ - return; - } - - - CorObstaculo[0] = vermelho; - CorObstaculo[1] = verde; - CorObstaculo[2] = azul; - CorPlayer[0] = verde; - CorPlayer[1] = azul; - CorPlayer[2] = vermelho; - - glClearColor(azul, vermelho, verde, 1.0f); - - glutPostRedisplay(); -} - -int colidiu() -{ - int level = player.y / 5; - if (level % 2 == 0) { - return 0; - } - - return((obstaculos[level/2].x1 > player.x) || (obstaculos[level/2].x2 < player.x + 5)); -} - - -void teclado (unsigned char key, int x, int y) -{ - if (key == 27) - exit(0); -} -// Função responsável por inicializar parâmetros e variáveis -void Inicializa (void) -{ - // Define a cor de fundo da janela de visualização como branca - glClearColor(1.0f, 1.0f, 1.0f, 1.0f); -} - -// Programa Principal -int main(void) -{ - inicializaObstaculos(); - - inicializaPlayer(); - // Define do modo de operação da GLUT - glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); - - // Especifica a posição inicial da janela GLUT - glutInitWindowPosition(5,5); - - // Especifica o tamanho inicial em pixels da janela GLUT - glutInitWindowSize(3*450,450); - gluOrtho2D (0.0f, 40.0f, 0.0f, 40.0f); - - // Cria a janela passando como argumento o título da mesma - glutCreateWindow("Labirintoo"); - - // Registra a função callback de redesenho da janela de visualização - glutDisplayFunc(Desenha); - - // Registra a função callback de redimensionamento da janela de visualização - glutReshapeFunc(AlteraTamanhoJanela); - - // Registra a função callback para tratamento das teclas ASCII - glutMouseFunc(Mouse); - glutSpecialFunc(moves); - - glutKeyboardFunc (teclado); - // Chama a função responsável por fazer as inicializações - Inicializa(); - - // Inicia o processamento e aguarda interações do usuário - glutMainLoop(); - - return 0; -} diff --git a/tests b/tests new file mode 100755 index 0000000..cd53465 Binary files /dev/null and b/tests differ diff --git a/tests.c b/tests.c new file mode 100644 index 0000000..b70706e --- /dev/null +++ b/tests.c @@ -0,0 +1,249 @@ +#include +#include +#include "Labirintilt.h" +#include "assertions.h" + +void test_create_player(); + +void test_create_game(); + +void test_create_traffic(); + +void test_create_space(); + +void test_move_player(); + +void test_move_scenario(); + +void test_colision(); + +void test_survive(); + +void test_creation_new_spaces(); + +void test_passa_level(); + +int main () +{ + test_colision(); + test_create_player(); + test_create_space(); + test_create_traffic(); + test_create_game(); + test_move_player(); + test_move_scenario();; + test_survive(); + test_creation_new_spaces(); +} + +void test_create_player() +{ + Player player; + new_Player(&player); + + assertEquals(60, player.x); + assertEquals(0, player.y); + assertEquals(3, player.lives); + printf("Create Player OK\n"); +} + +void test_create_space() +{ + Space space; + new_Space(&space, 20); + + assertEquals(20, space.x_start); + assertEquals(20 + 15, space.x_final); + printf("Create Space OK\n"); +} + +void test_create_traffic() +{ + Traffic traffic; + new_Traffic(&traffic); + + assertGreaterThan(0, traffic.num_spaces); + Space * i; + for (i = traffic.spaces; i; i = i->next) { + assertEquals(i->x_start + 15, i->x_final); + } + printf("Create Traffic OK\n"); +} + +void test_create_game() +{ + Game game; + + new_Game(&game); + + assertEquals(1, game.level); + assertEquals(3, game.player.lives); + assertGreaterThan(1, game.traffics[0].num_spaces); + assertGreaterThan(1, game.traffics[1].num_spaces); + assertGreaterThan(1, game.traffics[2].num_spaces); + assertGreaterThan(1, game.traffics[3].num_spaces); + + printf("Create Game OK\n"); +} + +void test_move_player() +{ + #ifndef GLUT_KEY_LEFT + #define GLUT_KEY_LEFT 0 + #define GLUT_KEY_UP 1 + #define GLUT_KEY_RIGHT 2 + #define GLUT_KEY_DOWN 3 + #endif + + Player player; + new_Player(&player); + player.x = 60; + player.y = 20; + + move_player(GLUT_KEY_LEFT, &player); + assertEquals(55, player.x); + + move_player(GLUT_KEY_UP, &player); + assertEquals(25, player.y); + + move_player(GLUT_KEY_RIGHT, &player); + assertEquals(60, player.x); + + move_player(GLUT_KEY_DOWN, &player); + assertEquals(20, player.y); + + printf("move player OK\n"); +} + +void test_move_scenario() +{ + Game game; + new_Game(&game); + + int i; + + for (i = 0; i < 4; i++) { + game.traffics[i].speed = 1 + i; + game.traffics[i].num_spaces = 1; + game.traffics[i].spaces[0].x_start = 20; + game.traffics[i].spaces[0].x_final = 30; + } + + move_scenario(&game, 1); + for (i = 0; i < 4; i ++) { + assertEquals(21, game.traffics[i].spaces[0].x_start); + assertEquals(31, game.traffics[i].spaces[0].x_final); + } + + move_scenario(&game, 2); + assertEquals(21, game.traffics[0].spaces[0].x_start); + assertEquals(31, game.traffics[0].spaces[0].x_final); + for (i = 1; i < 4; i ++) { + assertEquals(22, game.traffics[i].spaces[0].x_start); + assertEquals(32, game.traffics[i].spaces[0].x_final); + } + + move_scenario(&game, 3); + assertEquals(21, game.traffics[0].spaces[0].x_start); + assertEquals(31, game.traffics[0].spaces[0].x_final); + assertEquals(22, game.traffics[1].spaces[0].x_start); + assertEquals(32, game.traffics[1].spaces[0].x_final); + for (i = 2; i < 4; i ++) { + assertEquals(23, game.traffics[i].spaces[0].x_start); + assertEquals(33, game.traffics[i].spaces[0].x_final); + } + + move_scenario(&game, 4); + assertEquals(21, game.traffics[0].spaces[0].x_start); + assertEquals(22, game.traffics[1].spaces[0].x_start); + assertEquals(32, game.traffics[1].spaces[0].x_final); + assertEquals(23, game.traffics[2].spaces[0].x_start); + assertEquals(33, game.traffics[2].spaces[0].x_final); + assertEquals(24, game.traffics[3].spaces[0].x_start); + assertEquals(34, game.traffics[3].spaces[0].x_final); + + move_scenario(&game, 5); + assertEquals(21, game.traffics[0].spaces[0].x_start); + assertEquals(31, game.traffics[0].spaces[0].x_final); + assertEquals(22, game.traffics[1].spaces[0].x_start); + assertEquals(32, game.traffics[1].spaces[0].x_final); + assertEquals(23, game.traffics[2].spaces[0].x_start); + assertEquals(33, game.traffics[2].spaces[0].x_final); + assertEquals(24, game.traffics[3].spaces[0].x_start); + assertEquals(34, game.traffics[3].spaces[0].x_final); + + printf("move scenario OK\n"); +} + +void test_colision() +{ + Game game; +// printf("1\n\n"); + new_Game(&game); + + describe_traffic(game.traffics[0]); + game.traffics[0].num_spaces = 1; +// printf("2\n\n"); + game.traffics[0].first_space->x_start = 50; +// printf("3\n\n"); + game.traffics[0].first_space->x_final = 60; + + game.player.y = 10; + game.player.x = 48; + + assertTrue(colision(game)); + + game.player.x = 61; + assertTrue(colision(game)); + + game.player.x = 56; + assertTrue(colision(game)); + + printf("Colision OK\n"); +} + +void test_survive() +{ + Game game; + new_Game(&game); + + + game.player.x = 30; + game.player.y = 30; + survive(&game); + + assertEquals(2, game.player.lives); + assertEquals(60, game.player.x); + assertEquals(0, game.player.y); + + game.player.x = 30; + game.player.y = 30; + survive(&game); + assertEquals(1, game.player.lives); + assertEquals(60, game.player.x); + assertEquals(0, game.player.y); + + game.player.x = 30; + game.player.y = 30; + survive(&game); + assertEquals(0, game.player.lives); + + printf("Survive OK\n"); +} + +void test_creation_new_spaces() +{ + Game original; + Game * game = &original; + new_Game(game); + + game->traffics[0].num_spaces = 1; + new_Space(&game->traffics[0].spaces[0], 119); + + move_scenario(game, 1); +// + assertLessThan(0, game->traffics[0].spaces[0].x_final); + assertEquals(game->traffics[0].spaces[0].x_final - 15, game->traffics[0].spaces[0].x_start); + + printf("crete new spaces OK\n"); +} \ No newline at end of file