int main(void) { srand((unsigned int)time(NULL)); int snake[TAILLE_SNAKE][2], len_snake=10, pommes[TAILLE_POMME][2], len_pommes=5, direction[2]={0,-1}, score=0, done=0; const unsigned long int tempsDepart=Microsecondes(); unsigned long int prochainAffichage=0,prochainDeplacement=0; initSnake(snake,len_snake); initPommes(pommes, len_pommes, snake, len_snake); InitialiserGraphique(); CreerFenetre(BORD,BORD,60*TAILLE_CASE+2*BORD,40*TAILLE_CASE+BORD+40); ChoisirEcran(1); /*EffacerEcran(CouleurParNom("black")); */ while(!done){ if(Microsecondes()>prochainAffichage){ printScreen(snake, len_snake, pommes, len_pommes, tempsDepart, score); prochainAffichage=Microsecondes()+CYCLE; } GestionTouche(snake, direction, &done); if(Microsecondes()>prochainDeplacement){ DeplacementSnake(snake, len_snake, direction); CheckCollisionBord(snake); CheckCollisionPomme(len_pommes, pommes, snake, len_snake, &score); prochainDeplacement=Microsecondes()+100000; } } }
int main() { srand(time(NULL)); Stat stat; char name[50]; Snake snake; Pos food; ToGiveTread toGiveThread = {RIGHT, false, false}; showLeaderboard(); initStat(&stat); strcpy(name, stat.nickname); HANDLE hReadThread = CreateThread(NULL, 0, ReadThread, &toGiveThread, 0, NULL); strcpy(stat.nickname, name); initField(); initSnake(&snake); initFood(&snake, &food, &stat); updateAds(&stat); do{ Sleep(sleepTime(stat.level)); }while(!updateSnake(&snake, &food, &toGiveThread, &stat)); toGiveThread.toStopThread = true; clearScr(); updateScore(&stat); CloseHandle(hReadThread); showLeaderboard(); return 0; }
void Model::startGame() { idGameTimer = 0; gameStatus = STOPPED; presentVector = Qt::Key_Right; previousVector = Qt::Key_Right; haveMooved = false; bonusTiks = 0; isBonusVisible = false; bonusVisibleTimeLeft = 20; snakeSpeed = NORMAL_SNAKE_SPEED; score = 0; bonusAte = 0; ate = 0; life = 1; initWalls(); initSnake(); addGameElement(FOOD); gameTimer.start(snakeSpeed, this); idGameTimer = gameTimer.timerId(); gameStatus = STARTED; }
Core::Core(const int &width, const int &height) { _width = width; _height = height; _direction = DIRECTION_UP; _food = false; _speed = 50000; initSnake(_width, _height); }
///////////////////////主函数 int main() { initSnake(); info(); drawSnake(); level(); doGame(); gameOver(); return 0; }
void Core::run() { if (this->_lib->initWindow(this->_width, this->_height) == EXIT_FAILURE) return ; initSnake(); addItem(FOOD); displaySnake(); while (this->_isRunning == true) { if ((this->_key = this->_lib->getKey()) != -1) handleKey(); HandleDirection(); this->_lib->clearScreen(); addItem(FOOD); displaySnake(); isOnFood(); usleep(50000); } this->_lib->gameOver(this->_score); }
// launch game void paramGame() { char* playerName = malloc(sizeof(char)); printf("\nGIVE the name of player : "); scanf("%s", playerName); if(playerName != NULL) { initPlayer(playerName); } char* r = malloc(sizeof(char)); printf("\nGIVE 'r' to RUN : "); scanf("%s", r); if(strcmp(r, "r") == 0){ initCompteur(); direction = 2; initSnake(); initFoodPosition(); }else{ printf("error\n"); } }
int main(void) { int snake[300][2], len_snake=10, pommes[20][2], len_pommes=5, i; unsigned long int temps = Microsecondes(); initSnake(snake); initPommes(pommes); srand(time(NULL)); InitialiserGraphique(); CreerFenetre(BORD,BORD,600+2*BORD,400+BORD+40); printTerrain(snake, len_snake, pommes, len_pommes); printTime(temps); Touche(); FermerGraphique(); return EXIT_SUCCESS; }
int main(void) { srand((unsigned int)time(NULL)); int snake[TAILLE_SNAKE][2], len_snake=10, pommes[TAILLE_POMME][2], len_pommes=5, direction[2]={0,-1}, score=0, done=0; unsigned long int tempsDepart=Microsecondes(), tempsActuel,tempsTmp; unsigned long int prochainAffichage=0,prochainDeplacement=0; initSnake(snake,len_snake); initPommes(pommes, len_pommes, snake, len_snake); InitialiserGraphique(); CreerFenetre(BORD,BORD,60*TAILLE_CASE+2*BORD,40*TAILLE_CASE+BORD+40); ChoisirEcran(1);/*on dessine tout dans un ecran virtuel (cela joue le role de buffer) puis dans la fonction printScreen on copie l'ecran virtuel sur l'ecran reel*/ while(!done){ tempsActuel=Microsecondes(); if(Microsecondes()>prochainAffichage){ printScreen(snake, len_snake, pommes, len_pommes, tempsDepart,tempsActuel, score); prochainAffichage=Microsecondes()+CYCLE; } tempsTmp=Microsecondes(); GestionTouche(snake, direction, &done); tempsDepart+=Microsecondes()-tempsTmp; if(Microsecondes()>prochainDeplacement){ DeplacementSnake(snake, len_snake, direction); CheckCollisionBord(snake, &done); CheckCollisionQueue(snake,len_snake,&done); CheckCollisionPomme(len_pommes, pommes, snake, &len_snake, &score); prochainDeplacement=Microsecondes()+50000; } } FermerGraphique(); return EXIT_SUCCESS; }
int main(void) { srand((unsigned int)time(NULL)); int snake[TAILLE_SNAKE][2], len_snake=10, pommes[20][2], len_pommes=10, direction[2]={0,-1}; const unsigned long int tempsDepart=Microsecondes(); unsigned long int prochainAffichage=0,prochainDeplacement=0; initSnake(snake); initPommes(pommes); InitialiserGraphique(); CreerFenetre(BORD,BORD,60*TAILLE_CASE+2*BORD,40*TAILLE_CASE+BORD+40); ChoisirEcran(1); /*EffacerEcran(CouleurParNom("black")); */ while(1){ if(Microsecondes()>prochainAffichage){ printScreen(snake, len_snake, pommes, len_pommes, tempsDepart, 45 ); prochainAffichage=Microsecondes()+CYCLE; } GestionTouche(snake,direction); VerificationBords(snake); if(Microsecondes()>prochainDeplacement){ DeplacementSnake(snake,len_snake,direction); prochainDeplacement=Microsecondes()+100000; } if(ToucheEnAttente()==1 && Touche()==XK_Escape){ FermerGraphique(); return EXIT_SUCCESS; } } }
int main(){ //printf("Press start to begin the game\n"); int set = 1; while(1) { /* Reset display */ reset_hardware(); reset_software(); //initBorder(); if(set){ fancy_splash(); enable_splash_screen(); } play_splash_sound(); play_sound(); //printf("Press any button to start!\n"); if(set){ wait_for_continue(); disable_splash_screen(); } /* init border */ //initPowBoard(board, seed); /* Reset snakes and display them */ //PLAYER1 = 1; //PLAYER2 = 2; struct Snake snake_player1[SNAKE_SIZE]; struct SnakeInfo info1; initSnake(snake_player1, 256/16, 256/16, PLAYER1, &info1); struct Snake snake_player2[SNAKE_SIZE]; struct SnakeInfo info2; initSnake(snake_player2, 256/16, 320/16, PLAYER2, &info2); PLAYER1_SLEEP_CYCLES = DEFAULT_SLEEP_CYCLE / SLEEP_TIME; PLAYER2_SLEEP_CYCLES = DEFAULT_SLEEP_CYCLE / SLEEP_TIME; //printf("Game is starting"); int player1_dir[4]; int player2_dir[4]; player1_dir[right_dir] = 1; player1_dir[left_dir] = 0; player1_dir[up_dir] = 0; player1_dir[down_dir] = 0; player2_dir[right_dir] = 1; player2_dir[left_dir] = 0; player2_dir[up_dir] = 0; player2_dir[down_dir] = 0; initBorder(); initPowUps(); drawStartPowUps(snake_player1, snake_player2); if(!set){ wait_for_continue(); } set = 0; int paused = 0; unsigned char code; int count_player1 = 0; int count_player2 = 0; int pressed_player1 = getPlayer1Controller(); int pressed_player2 = getPlayer2Controller(); int potential_pressed1; int potential_pressed2; int old_potential1; int old_potential2; int game = 1; int collision = 0; int p1_move_collision = 0; int p2_move_collision = 0; int moved = 0; while(1) { /*Check if paused button pushed*/ if( (count_player1 >= PLAYER1_SLEEP_CYCLES && (check_paused(pressed_player1) != 0) ) || (count_player2 >= PLAYER2_SLEEP_CYCLES && (check_paused(pressed_player2) != 0) ) ){ pressed_player1 = 0; pressed_player2 = 0; count_player1 = 0; count_player2 = 0; paused = !paused; //printf("Paused is now %d\n", paused); } /* Move player 1 */ if(count_player1 >= PLAYER1_SLEEP_CYCLES && !paused ){ p1_move_collision = movement(code, snake_player1, player1_dir, pressed_player1, PLAYER1, &info1); if(p1_move_collision) game_winner = 2; check_powerup_col(snake_player1, snake_player2, player1_dir, PLAYER1, &info1); //movement(code, snake_player2, player2_dir, food, pressed_player1, PLAYER2); check_powerup_buttons(pressed_player1, snake_player1, snake_player2, freeze, PLAYER1, &info1); count_player1 = 0; pressed_player1 = 0; moved = 1; //printf("Moving player1"); } /* Move player 2 */ if(count_player2 >= PLAYER2_SLEEP_CYCLES && !paused){ p2_move_collision = movement(code, snake_player2, player2_dir, pressed_player2, PLAYER2, &info2); if(p2_move_collision) game_winner = 1; check_powerup_col(snake_player2, snake_player1, player2_dir, PLAYER2, &info2); check_powerup_buttons(pressed_player2, snake_player2, snake_player1,freeze, PLAYER2, &info2); count_player2 = 0; pressed_player2 = 0; moved = 1; } /* Get controls from player1 everytime and store control for later if pressed */ old_potential1 = potential_pressed1; potential_pressed1 = getPlayer1Controller(); if( potential_pressed1 != 0 && old_potential1 != potential_pressed1){ pressed_player1 = potential_pressed1; //printf("pressed player1: %d\n", pressed_player1); } /* Get controls from player2 everytime and store control for later if pressed */ old_potential2 = potential_pressed2; potential_pressed2 = getPlayer2Controller(); if( potential_pressed2 != 0 && old_potential2 != potential_pressed2){ pressed_player2 = potential_pressed2; } if( moved ){ collision = snakeCol(snake_player1, snake_player2); play_move_sound(); } if (collision || p1_move_collision || p2_move_collision){ //printf("breaking"); play_gameover_sound(); play_sound(); draw_winner(game_winner); usleep(SLEEP_TIME*250000); //wait_for_continue(); break; } play_sound(); //printf("Random: %d", PRNG(40)); moved = 0; count_player1++; count_player2++; usleep(SLEEP_TIME*1000); } //printf("New Game starting\n"); } //printf("GAME OVER\n"); return 0; }
int main() { SysTick_Config(SystemCoreClock/100); magazyn = malloc(sizeof(Element)*1200); Timer3Conf(); Joystick_Initialize(); Buttons_Initialize(); initDisplay(); lcdClean(); Timer3Disable(); Timer1Conf(); lcdMenu(); while(1) { while(tickCounter<10); tickCounter=0; switch(Buttons_GetState()) { case BUTTON_INT0: //lcdClean(); initSnake(); game = 1; while(game == 1) { //while(tickCounter<10); //tickCounter=0; inputControl = 0; switch(Joystick_GetState()) { case JOYSTICK_UP: inputControl = 1; break; case JOYSTICK_LEFT: inputControl = 2; break; case JOYSTICK_DOWN: inputControl = 4; break; case JOYSTICK_RIGHT: inputControl = 8; break; default: inputControl = oldControl; } if(inputControl == forbidden) inputControl = oldControl; if(reactCount > 0) { game = react(inputControl); --reactCount; } } lcdString(250, 150, "Koniec gry"); while(1); break; case BUTTON_KEY1: lcdString(250, 150, "Opcja 2"); while(1); break; case BUTTON_KEY2: lcdString(250, 150, "Opcja 3"); while(1); break; } } return 0; }
//主函数 int main() { clock_t start,finish; int isPause = 1; int chance=0; float RewardTime=0; int z; setCursorVisible(0); setConsoleTitle("贪吃蛇--D_Y。 2010.1.11"); //标题 ch=menu(); ch2=menuDouble(); if(ch2=='1') z=0; if(ch2=='2') z=1; initSnake(SNAKE_MIN_LEN,z); drawMap(); drawSnake(z); //PlaySound("d:\\泡泡堂轻松欢快音乐音效WAV.wav",NULL, SND_ASYNC|SND_NODEFAULT|SND_LOOP ); switch(ch) //不同的难度对应不同的奖励食物消失时间 { case '1': RewardTime=2500; case '2': RewardTime=3000; case '3': RewardTime=3500; } while (!isOver[0]&&!isOver[1]) //当两条蛇任意一条死亡则游戏结束 { srand((uint32)time(NULL)); if (!isPause) { moveSnake(z); if (!isFood) drawFood(); while(((point[1]+point[0])%4==0)&&((point[1]+point[0])>3)&&p==1) //规定在得分大于三分时每吃掉四个常规食物产生一个奖励食物 { if (!isReward) drawReward(); start=clock(); //画出食物,记录此刻时间 break; } finish=start+RewardTime ; //结束时间为开始时间加规定的奖励食物出现时间 if((isReward==1)&&(finish<=clock())) { drawBlock(myReward.x, myReward.y, BS_SPACE); //超出规定时间则将奖励食物用背景方框覆盖 myReward.x=-1; myReward.y=-1; isReward=0; } if(((point[1]+point[0])%3==0)&&((point[1]+point[0])>2)&&q==0) { clearBar(); drawBar(); } setTextColor(myColors[4]); //规定出输出分数等信息 gotoTextPos(MAP_BASE_X*2 -14 , MAP_BASE_Y + 2); printf("Player 1"); gotoTextPos(MAP_BASE_X*2 -14 , MAP_BASE_Y + 4); printf("Score: %d.", point[0]); if(z==1) { gotoTextPos(MAP_BASE_X*2 + MAP_WIDTH*2+4 , MAP_BASE_Y + 2); printf("Player 2"); gotoTextPos(MAP_BASE_X*2 + MAP_WIDTH*2+4 , MAP_BASE_Y + 4); printf("Score: %d.", point[1]); } } DELAY(ch); //赋予游戏速度 delayMS(80- snakeLength[0]*2); //游戏速度随着蛇身增长而增加 if (jkHasKey()) //键盘控制 { switch (jkGetKey()) { case JK_UP: if (snakeDir[0] != DIR_DOWN) snakeDir[0] = DIR_UP; break; case 'w': if (snakeDir[1] != DIR_DOWN) snakeDir[1] = DIR_UP; break; case JK_DOWN: if (snakeDir[0] != DIR_UP) snakeDir[0] = DIR_DOWN; break; case 's': if (snakeDir[1] != DIR_UP) snakeDir[1] = DIR_DOWN; break; case JK_LEFT: if (snakeDir[0] != DIR_RIGHT) snakeDir[0] = DIR_LEFT; break; case 'a': if (snakeDir[1] != DIR_RIGHT) snakeDir[1] = DIR_LEFT; break; case JK_RIGHT: if (snakeDir[0] != DIR_LEFT) snakeDir[0] = DIR_RIGHT; case 'd': if (snakeDir[1] != DIR_LEFT) snakeDir[1] = DIR_RIGHT; break; case JK_ENTER: case JK_SPACE: isPause = !isPause; break; case JK_ESC: isOver[0]=1; break; default: break; } } } PlaySound("d:\\超级马里奥兄弟 死掉音.wav",NULL, SND_ASYNC|SND_NODEFAULT ); gotoTextPos(MAP_BASE_X + MAP_WIDTH - 7, MAP_BASE_Y + MAP_HEIGHT + 1); if(isOver[0]==1&&z==0) printf("Game Over!!"); if(isOver[0]==1&&z==1) printf("Player 2 win!!"); if(isOver[1]==1) printf("Player 1 win!!");//游戏结束,在规定处显示分数 getch(); return 0; }
void initInterface(Player * players, int nOfPlayers) { char name[100]; char buff[100]; int points; int level, curY = 0; int status = 0; system("mode con:cols=50 lines=50"); system("cls"); setFontSize(15, 20); if(nOfPlayers > 0) initScoreBoard(players, nOfPlayers); strcpy(name, "DEFAULT"); system("cls"); setCursorPosition(CONSOLESIZE/2 - 10,curY++); puts("Write your name:"); setCursorPosition(CONSOLESIZE/2 - 15,curY++); fgets(buff, 100, stdin); buff[strlen(buff) - 1] = '\0'; if(strlen(buff)>1) strcpy(name, buff); while(!status) { printf("Hi, %s, what level do you want to take? (0..9)\n", name); fgets(buff, 100, stdin); status = sscanf(buff, "%i", &level); if(!status || level>9 || level < 0) { status = 0; puts("Try again:"); curY++; } } puts("Press anything to start..."); getch(); points = initSnake(level); curY = 0; setConsoleColor(COLORDEF); setCursorPosition(0,curY++); Player p; strcpy(p.name, name); p.points = points; addToScoreBoard(&p, &nOfPlayers); free(players); players = getScoreBoard(&nOfPlayers); while(1) { setConsoleColor(COLORDEF); system("cls"); setCursorPosition(0,0); printf("HOORAY! You've got %i points. Type NO if you want to stop\n Type SCORE if you want to look through the scoreboard\nType anything else if you want to try again\n", points); fgets(buff, 100, stdin); if(!strcmp(buff, "NO\n")) break; else if(!strcmp(buff, "SCORE\n")) initScoreBoard(players, nOfPlayers); else { points = initSnake(level); p.points = points; addToScoreBoard(&p, &nOfPlayers); free(players); players = getScoreBoard(&nOfPlayers); } } }