/*! * \fn void loadGameLocale(char *file_name) * Load the file and run the play function * \param[in] file_name the filename */ void loadGameLocale(char *file_name) { csuStruct *ptr_csu_struct; clearScreen(); ptr_csu_struct=readCsuFile(file_name); if (ptr_csu_struct == NULL) { systemPause(); return; } printCsuStruct(ptr_csu_struct); if(exceedMaxNumber(ptr_csu_struct)) { printGameOver(ptr_csu_struct); systemPause(); } else { systemPause(); play(ptr_csu_struct,file_name); } }
/*! * \fn void displayFile() * Ask a filename and display it. */ void displayFile() { char file_name[SIZE_MAX_FILE_NAME]; csuStruct *ptr_csu_struct; clearScreen(); menuFileName(file_name); #ifndef PORTABLE if(readSystemPath(file_name)==false) return; #endif // PORTABLE ptr_csu_struct=readCsuFile(file_name); if (ptr_csu_struct != NULL) { printCsuStruct(ptr_csu_struct); if(exceedMaxNumber(ptr_csu_struct)) printGameOver(ptr_csu_struct); closeCsuStruct(ptr_csu_struct); } systemPause(); }
/*! * \fn void loadGame() * Load a game from a file and run the play function. */ void loadGame() { csuStruct *ptr_csu_struct; char file_name[SIZE_MAX_FILE_NAME]; clearScreen(); menuFileName(file_name); #ifndef PORTABLE if(readSystemPath(file_name)==false) return; #endif // PORTABLE ptr_csu_struct=readCsuFile(file_name); if (ptr_csu_struct == NULL) { systemPause(); return; } printCsuStruct(ptr_csu_struct); if(exceedMaxNumber(ptr_csu_struct)) { printGameOver(ptr_csu_struct); systemPause(); } else { systemPause(); play(ptr_csu_struct,file_name); } }
/*! * \fn void play(csuStruct *ptr_csu_struct, char *file_name) * Count the points * \param[in,out] *ptr_csu_struct a pointer on a csu structure * \param[in] *file_name the filename */ void play(csuStruct *ptr_csu_struct, char *file_name) { ContinueChangeDistributorOrQuit choice; bool continuer=true; clearScreen(); printPoints(ptr_csu_struct); do { menuPlayersPoints(ptr_csu_struct); if (!writeFileNewTurn(file_name,ptr_csu_struct)) systemPause(); clearScreen(); printPoints(ptr_csu_struct); if (exceedMaxNumber(ptr_csu_struct)) continuer=false; else { choice=menuContinueChangeDistributorOrQuit(); if (choice == Quit) continuer = false; if (choice == ChangeDistributor) menuChangeDistributor(ptr_csu_struct); } } while (continuer==true); printGameOver(ptr_csu_struct); if (menuDelete()) { deleteFile(file_name); systemPause(); } clearScreen(); printCsuStruct(ptr_csu_struct); closeCsuStruct(ptr_csu_struct); systemPause(); }
/*! * \fn void displayFileLocale(char *file_name) * Display the file. */ void displayFileLocale(char *file_name) { csuStruct *ptr_csu_struct; clearScreen(); ptr_csu_struct=readCsuFile(file_name); if (ptr_csu_struct != NULL) { printCsuStruct(ptr_csu_struct); if(exceedMaxNumber(ptr_csu_struct)) printGameOver(ptr_csu_struct); closeCsuStruct(ptr_csu_struct); } systemPause(); }
void Graphics::printHud(Map *map) { sf::Sprite scroll; Player *currentPlayer = getPlayerHightlight(map->getHud(), map); Case *currentCase = map->getCaseFromPos(map->getHud()); if (map->getGameOver().first == true) printGameOver(map->getGameOver().second); else { scroll.SetImage(_scrollImage); scroll.SetPosition(sf::Vector2f(1300, 0) + _offsetCoeff); app->Draw(scroll); if (currentPlayer != NULL) printPlayerOnHud(currentPlayer); if (currentCase != NULL) printCaseOnHud(currentCase); if (currentCase == NULL && currentPlayer == NULL) nothingToHud(); } }
///Modo de jogo em que só há uma vida, e termina apenas quando o jogador morre void survivalMode() { system("mode con:cols=90 lines=30"); //tamanho da tela de jogo LEVEL_SCENE hell = {RED, {176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,177, 176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,177, 176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,177,177, 176,176,176,176,176,176,176,176,178,178,176,176,176,176,176,176,178,177,177,177, 176,176,178,177,177,176,176,178,178,178,178,176,176,176,176,178,178,177,177,177, 176,176,178,177,177,177,176,178,178,178,178,176,176,176,176,178,178,178,177,177, 178,178,178,177,177,177,176,178,178,178,178,176,176,176,178,178,178,178,178,178, 178,178,177,177,177,177,176,178,178,178,178,178,176,178,178,178,178,177,178,178, 177,178,177,177,177,177,177,177,178,178,177,177,177,177,177,177,177,177,177,178, 177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177 } }; //cenário do modo de jogo FILE *arq; int score; //pontuação int level; //como só há um level nesse jogo, esta variavél serve apenas para o peso da pontuação int speed; //velocidade do jogo int rows, columns; //Linhas e colunas do mapa int rocksNum; // Número de pedras int ratNum; //Indica qual o rato a ser gerado, há no máximo 4 ratos na tela int ratsEaten; int ratsColor; //Cor do rato a ser gerado int i; // Usado nos loops for int cont; //Usado para contabilizar o número de movimentos da snake char auxDirection; // BOOL isDead; //Flag de morte da snake char map[ROWSMAX][COLUMNSMAX]; int hasPassed[ROWSMAX*COLUMNSMAX]; SNAKE snake; POSN initialCoord; //Coordenada onde a snake começa o nível POSN rocks[ROWSMAX*COLUMNSMAX]; POSN rats[4]; ratNum = 0; ratsColor = 2; ratsEaten = 0; cont = 0; isDead = 0; level = 1; score = 0; speed = 200; //speed inicial for(i=0; i<rocksNum; i++) hasPassed[i]=0; readMap(42, &rows, &columns, map, &rocksNum, rocks, &initialCoord); restartLevel(&snake, columns, rows, rats, speed, &speed, initialCoord, &isDead); print_lvlART(hell, columns + 5, 0); fadeIN(rows, columns, GREY, 1); printMap(rows, columns, map, hell.color); printRocks(rows, columns, rocksNum, rocks, map); printSnake(&snake); getch(); //pausa o jogo while(!isDead) { for(i=0; i<rocksNum; i++) { if(hasPassed[i]!=0) hasPassed[i]+=1; } print_lvlART(hell, columns + 5, 0); printScore(score); eraseTrail(snake); updateSnakeCoord(&snake); printSnake(&snake); checkColision(&snake, &isDead, rows, columns, map, rocks, rocksNum, hasPassed, &score); if(score < 0) { score = 0; //Não há score negativo! } cont++; //contador de movimentos if(cont % 10 == 0) { makeRats(rows, columns, rats, ratNum, &snake, map, &ratsColor); //gera e printa um rato ratNum++; if(ratNum == 4) //Há 4 ratos, de 0 a 3, logo rato 4 == rato 0 { ratNum = 0; } } eatRat(&snake, rats, &ratsEaten, &score, level); //checa se um rato foi comido Sleep(speed); //controla a velocidade do jogo if(speed > 100) //máxima velocidade do jogo speed -= 1; auxDirection = changeDirection(snake.direction); //lê nova direção to teclado if(auxDirection == 'P') { getch(); //Pausa o jogo } else if(auxDirection == 'C') { cheatCode(&isDead, &isDead, &speed); //Lê cheat code } else if(auxDirection == 'Q' || auxDirection == ESC) //Se o jogador aperta para sair, perde suas vidas para encerrar { isDead = 1; } else snake.direction = auxDirection; //só troca a direção se a nova direção não for uma tecla de pause ou saída } fadeOUT(rows, columns, GREY, 1); //Efeito de fade out na tela printGameOver(); arq = fopen("highscore_survival.bin", "rb+"); //Abre arquivo de highscore, informando caso de erro if(arq == NULL) printf("error"); uptadeHighScore(score, arq); //coloca o score no rank system("cls"); }
///Controla o modo story void storyMode() { FILE *arq; int lifes; int score; int level; LEVEL_SCENE temple = {GREY,{176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176, 176,176,176,176,176,176,176,178,178,178,178,178,178,176,176,176,176,176,176,176, 176,176,176,176,176,176,176,178,178,176,176,178,178,176,176,176,176,176,176,176, 176,176,176,176,176,176,176,178,178,176,176,178,178,176,176,176,176,176,176,176, 176,176,176,176,176,176,178,178,178,178,178,178,178,178,176,176,176,176,176,176, 176,176,176,176,176,178,178,178,178,178,178,178,178,178,178,176,176,176,176,176, 176,176,176,176,178,178,178,178,178,178,178,178,178,178,178,178,176,176,176,176, 176,176,176,178,178,178,178,178,178,178,178,178,178,178,178,178,178,176,176,176, 176,176,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,176,176, 176,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,176 } }; LEVEL_SCENE desert = {YELLOW,{176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176, 176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176, 176,176,176,176,176,176,176,176,176,176,176,178,178,176,176,176,176,176,176,176, 176,176,176,176,176,176,176,176,176,176,178,178,178,178,176,176,176,176,176,176, 176,176,176,176,176,176,176,176,176,178,178,178,178,178,178,176,176,176,176,176, 176,176,178,178,178,176,176,176,178,178,178,178,178,178,178,178,176,176,176,176, 176,178,178,178,178,178,176,177,177,178,178,178,178,178,178,178,178,176,176,176, 178,178,178,178,178,178,178,178,177,177,177,178,178,178,178,178,178,178,176,176, 178,178,178,178,178,178,178,178,178,177,177,177,178,178,178,178,178,178,178,176, 178,178,178,178,178,178,178,178,178,178,178,177,178,178,178,178,178,178,178,178 } }; LEVEL_SCENE forest = {GREEN,{176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,177, 176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,177, 176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,177,177, 176,176,176,176,176,176,176,176,178,178,176,176,176,176,176,176,178,177,177,177, 176,176,178,177,177,176,176,178,178,178,178,176,176,176,176,178,178,177,177,177, 176,176,178,177,177,177,176,178,178,178,178,176,176,176,176,178,178,178,177,177, 178,178,178,177,177,177,176,178,178,178,178,176,176,176,178,178,178,178,178,178, 178,178,177,177,177,177,176,178,178,178,178,178,176,178,178,178,178,177,178,178, 177,178,177,177,177,177,177,177,178,178,177,177,177,177,177,177,177,177,177,178, 177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177 } }; LEVEL_SCENE sea = {LIGHT_BLUE, {176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176, 176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176, 176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176, 176,176,176,176,178,178,178,176,176,176,176,176,178,178,178,178,176,176,176,176, 176,176,176,178,178,176,178,176,176,176,176,178,178,178,176,178,176,176,176,176, 176,176,178,178,178,176,176,176,176,176,178,178,178,178,176,176,176,176,176,178, 176,178,178,178,178,176,176,176,176,178,178,178,178,178,176,176,176,176,178,178, 178,178,178,178,178,178,176,176,178,178,178,178,178,178,178,176,176,178,178,178, 178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178, 178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178 } }; system("mode con:cols=140 lines=40"); level = 1; score = 0; lifes = 5; do { switch(level) { case 1: printLOBBYdeCONVERSA(GREY); print_lvlART(temple, 35, 0); printHistoryLines(1); playOneLevel(1, 5, 400, temple, &lifes, &score); break; case 2: playOneLevel(2, 10, 300, temple, &lifes, &score); break; case 3: printLOBBYdeCONVERSA(GREY); gotoMapXY(25, 4); textcolor(13); printf("%c", 190); printHistoryLines(2); playOneLevel(3, 20, 250, temple, &lifes, &score); break; case 4: print_lvlART(temple, 35, 0); printLOBBYdeCONVERSA(GREY); gotoMapXY(25, 4); textcolor(PINK); printf("%c", 190); //printa personagem da história printHistoryLines(3); fadeOUT(10, 30, GREY, 1); printLOBBYdeCONVERSA(YELLOW); printHistoryLines(4); playOneLevel(4, 25, 200, desert, &lifes, &score); break; case 5: playOneLevel(5, 30, 200, desert, &lifes, &score); break; case 6: playOneLevel(6, 30, 150, desert, &lifes, &score); break; case 7: printHistoryLines(5); printLOBBYdeCONVERSA(GREEN); printHistoryLines(6); playOneLevel(7, 35, 150, forest, &lifes, &score); break; case 8: playOneLevel(8, 40, 150, forest, &lifes, &score); break; case 9: playOneLevel(9, 40, 100, forest, &lifes, &score); break; case 10: printHistoryLines(7); printLOBBYdeCONVERSA(LIGHT_BLUE); printHistoryLines(8); playOneLevel(10, 50, 100, sea, &lifes, &score); break; case 11: playOneLevel(11, 50, 100, sea, &lifes, &score); break; case 12: playOneLevel(12, 55, 90, sea, &lifes, &score); printHistoryLines(9); break; case 13: printHistoryLines(10); playOneLevel(13, 60, 80, temple, &lifes, &score); break; } if(lifes > 0) { level++; //passa de fase lifes++; //ganha uma vida } else if(lifes == 0) { printGameOver(); } } while(lifes>0 && level <= 13); if(lifes > 0) { puts("\nYOU JUST WON THIS GAME, WOW!(You problably lost an expressive time of your life, or no)"); puts("Now, check out these cheat codes:"); puts("katiau\nbanban\nfofao"); getch(); //pausa system("cls"); } arq = fopen("highscore.bin", "rb+"); //Abre arquivo de highscore, informando caso de erro if(arq == NULL) printf("error"); uptadeHighScore(score, arq); //Insere score no rank }
//todo - get string instead of char! void PlayFullGame() { isGameBoy = 1; char nextMoveAux; int lastPlayer = 0; numOfPlayers = mmap(NULL, sizeof (*numOfPlayers), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); *numOfPlayers = 0; gameHasEnded = mmap(NULL, sizeof (*gameHasEnded), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); *gameHasEnded = -1; // game will be over if gameHasEnded is != -1 nextMove = mmap(NULL, sizeof(*nextMove), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); player = mmap(NULL, sizeof (*player), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); *player = 100; Currentplayer = mmap(NULL, sizeof (*Currentplayer), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); *Currentplayer = 0; a = mmap(NULL, sizeof (*a), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); b = mmap(NULL, sizeof (*b), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); char* moduleName = "/dev/snake17"; int status; int pid1 = fork(); if(pid1 == 0) { char board[BUFFER_SIZE+1]; board[BUFFER_SIZE] = '\0'; //player 1 (white) *a=open(moduleName, O_RDWR); *numOfPlayers = *numOfPlayers +1; while(*gameHasEnded == -1) { if(*player == 1) { *player = 0; *Currentplayer = 1; read(*a, board, BUFFER_SIZE); //board before printButtonPress(board,nextMove); int res = write(*a,nextMove, 1); if(res < 0 ) {*gameHasEnded =1;} read(*a, board, BUFFER_SIZE); printBoard(board); *player = 100; } } _exit(0); } else { int pid2 = fork(); if(pid2 == 0) { char board[BUFFER_SIZE]; doLongTask(); //player 2 (black) *b=open(moduleName, O_RDWR); *Currentplayer = -1; read(*b, board, BUFFER_SIZE); printBoard(board); *numOfPlayers = *numOfPlayers +1; while(*gameHasEnded == -1) { if(*player == -1) { *player = 0; *Currentplayer = -1; read(*b, board, BUFFER_SIZE); printButtonPress(board,nextMove); int res = write(*b,nextMove, 1); if(res < 0 ) {*gameHasEnded =1;} read(*b, board, BUFFER_SIZE); printBoard(board); *player = 100; *gameHasEnded = ioctl(*b, SNAKE_GET_WINNER); } } _exit(0); } else { printLogo(); doLongTask(); lastPlayer = -1; //manager while(*gameHasEnded == -1) { if(*player == 100 && *numOfPlayers==2) { char nm; scanf(" %c", &nm); if(nm != 'a' && nm != 'A' && nm != 's' && nm != 'S' && nm != 'w' && nm != 'W' && nm != 'd' && nm != 'D' && nm != '2' && nm != '4' && nm != '6' && nm != '8') { continue; } if (nm == 'w' || nm == 'W' || nm == '8') { nextMoveAux = UP; } else if (nm == 's' || nm == 'S' || nm == '2') { nextMoveAux = DOWN; } else if (nm == 'a' || nm == 'A' || nm == '4') { nextMoveAux = LEFT; } else if (nm == 'd' || nm == 'D' || nm == '6') { nextMoveAux = RIGHT; } *nextMove = nextMoveAux; *player = lastPlayer * (-1); lastPlayer = *player; } } printGameOver(); close(*a); close(*b); wait(&status); } wait(&status); } munmap(numOfPlayers, sizeof (*numOfPlayers)); munmap(gameHasEnded, sizeof (*gameHasEnded)); munmap(nextMove, sizeof (*nextMove)); munmap(player, sizeof (*player)); munmap(Currentplayer, sizeof (*Currentplayer)); munmap(a, sizeof (*a)); munmap(b, sizeof (*b)); }
int main() { // Initial setup signal(SIGINT, shutDown); openPath(); instructions(); srand(time(NULL)); game_state currGame; memset(&currGame, 0, sizeof(game_state)); // Sets up piezobuzzer for sound using designated PWM pin pwm = fopen("/sys/devices/bone_capemgr.9/slots", "w"); fseek(pwm, 0, SEEK_END); fprintf(pwm, "am33xx_pwm"); fprintf(pwm, "bone_pwm_P9_14"); fflush(pwm); // Sets the pointers to the appropriate duty and period files dirDuty = fopen("/sys/devices/ocp.3/pwm_test_P9_14.15/duty", "w"); dirT = fopen("/sys/devices/ocp.3/pwm_test_P9_14.15/period", "w"); // Main game loop while (!currGame.quit) { char *playScreen = " Press button to start! "; write(fd_lcd, playScreen, SCREEN_SIZE * 3); pressAnyButton(); // usleep necessary here to prevent signal overlap once game begins usleep(500000); // Initializes the playing screen char symbolScreen[SCREEN_SIZE + 1]; int i; for (i = 0; i < SCREEN_SIZE; i++) { symbolScreen[i] = ' '; } symbolScreen[SCREEN_SIZE] = '\0'; session_state currSession; memset(&currSession, 0, sizeof(session_state)); currSession.misses = -1; // Current game session loop while (currSession.misses < WRONG_GUESSES) { // When software counter resets, update to the next screen frame if (currSession.counter == 0) { // Respond to player's input if (currSession.correctInput && symbolScreen[0] != ' ') { currSession.currScore++; } else if (!currSession.correctInput) { currSession.misses++; } currSession.inputted = 0; currSession.correctInput = 0; if (nextScreenFrame(&currSession, symbolScreen) == -1) { shutDown(); return EXIT_FAILURE; } } // Delay inbetween input update usleep(DELAY_TIME); // Reads the current status of the button inputs read(fd_but, currGame.inputs, NUM_BUTTONS * sizeof(int)); currSession.pressed = 5; for (i = 0; i < NUM_BUTTONS; i++) { if (currGame.inputs[i] == 1) { currSession.pressed = i; } } // Play sound on buzzer which corresponds to the input // This is done less often to improve smoothness of gameplay if (currSession.counter % 15 == 0) { if (currSession.pressed == 0) { buzzer(noteA); } else if (currSession.pressed == 1) { buzzer(noteB); } else if (currSession.pressed == 2) { buzzer(noteC); } else if (currSession.pressed == 3) { buzzer(noteD); } else if (currSession.pressed == 4) { buzzer(noteE); } else { buzzer(0); } } updateSession(&currSession, symbolScreen[0]); } printGameOver(currSession.currScore, &(currGame.highScore)); pressAnyButton(); usleep(500000); // Prompts user to choose to play again currGame.quit = wantToQuit(); usleep(500000); } shutDown(); return EXIT_SUCCESS; }