예제 #1
0
void Snake::Update()
{
    if (dead) { return; }

    UpdateInputControls();

    time++;
    if (time < timeout)
    {
        return;
    }

    time = 0;

    if (CheckForWallCollision() || CheckForSelfCollision())
    {
        dead = true;
        return;
    }

    if (CheckForCoinCollision())
    {
        coin.Respawn();
    }
    else
    {
        segments.pop_back();
    }

    MoveSnake();
}
예제 #2
0
파일: main.cpp 프로젝트: snarf95/Snake
void Update()
{
	HandleInput();

	deltaLastMove += deltaTime;

	if (deltaLastMove > cooldown)
	{
		MoveSnake(dx, dy);

		deltaLastMove =	deltaLastMove - cooldown;
	}
}
예제 #3
0
void StartAlarm() {
    signal(SIGALRM, SIG_IGN);  
    if(ii % 2 == 0) {
        MoveSnake(head);
        DisplaySnake(head);
    }
    else
        ClearSnake(head);

    if(head -> row == foodpos.row && head -> col == foodpos.col) {
        foodpos = DisplayFood(head);
        Grow(head);
        score += 10;
        sprintf(scorestr,"%d",score);
        mvaddstr(1,23,scorestr);
    }
    if(IsFail(head)) {
        isfailed = 1;
        Gameover();
    } else if(chinput != 'q') {
		ii++;
		signal(SIGALRM, StartAlarm);  
    }
}
int snake_exec( int fdfb, int fdrc, int fdlcd, char *cfgfile )
{
	struct timeval	tv;
	int		x;

	if ( FBInitialize( 720, 576, 8, fdfb ) < 0 )
		return -1;

	setup_colors();

	if ( RcInitialize( fdrc ) < 0 )
		return -1;

	Fx2ShowPig( 540, 449, 135, 96 );

	while ( doexit != 3 ) {
		DrawMaze( );	/* 0 = all */

		doexit=0;
		while ( !doexit ) {
			tv.tv_sec = 0;
			tv.tv_usec = 100000;
			x = select( 0, 0, 0, 0, &tv );		/* 10ms pause */

			RcGetActCode( );
			MoveSnake();
#if defined(USEX) || defined(HAVE_SPARK_HARDWARE) || defined(HAVE_DUCKBOX_HARDWARE)
			FBFlushGrafic();
#endif
		}

		FreeSnake();

		if ( doexit != 3 ) {
			actcode=0xee;
			DrawFinalScore();
			DrawGameOver();
#if defined(USEX) || defined(HAVE_SPARK_HARDWARE) || defined(HAVE_DUCKBOX_HARDWARE)
			FBFlushGrafic();
#endif

			doexit=0;
			while (( actcode != RC_OK ) && !doexit ) {
				tv.tv_sec = 0;
				tv.tv_usec = 200000;
				x = select( 0, 0, 0, 0, &tv );		/* 100ms pause */
				RcGetActCode( );
			}
		}
	}

	Fx2StopPig();

	/* fx2 */
	/* buffer leeren, damit neutrino nicht rumspinnt */
	realcode = RC_0;
	while ( realcode != 0xee ) {
		tv.tv_sec = 0;
		tv.tv_usec = 300000;
		x = select( 0, 0, 0, 0, &tv );		/* 300ms pause */
		RcGetActCode( );
	}

	RcClose();
	FBClose();

	return 0;
}
예제 #5
0
void main() 
{	
	allegro_init();
	install_keyboard();
	set_color_depth(32);
	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1000, 600, 0, 0);
	install_int_ex(ContaMsec, MSEC_TO_TIMER(1));
	install_int_ex(ContaSec, SECS_TO_TIMER(1));	
	install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL);
	MIDI* bgSound = load_midi("sounds/BGM.mid");
	SAMPLE* snakeHit = load_wav("sounds/cobraDamage.wav");
	SAMPLE* snakeHitSelf = load_wav("sounds/cobraDamage2.wav");
    SAMPLE* snakeDeath = load_wav("sounds/cobraDeath.wav");	
    SAMPLE* gameOver = load_wav("sounds/gameOver.wav");	
	InicializaPP();

	int swt = 0;
	

	
	struct Cobra snake[500]; //Estrutura que representa a cobra e suas bmpPartes.

	int xst=0, yst=10, xst2=190, yst2 = 40; //Coordenadas de referência da área de Status do jogo
	int xL = 0, yL = 425, xL2 = 190, yL2 = 590; //Coordenadas de referência da Tabela de Comidas
	
	buffer = create_bitmap(800,600); //Bitmap principal de desenho
	status = create_bitmap(200,600); //Bitmap principal de desenho
	clear_to_color(status, makecol(0,0,0));
	clear_to_color(buffer, makecol(0,0,0));

	//INICIO: Inicializacao Sprites
	bmpSapo = load_bitmap("images/sapo_20x20.bmp",pallet);
	bmpAranha = load_bitmap("images/ARANHA_20X20.bmp",pallet);
	bmpMaca = load_bitmap("images/MACA_20x20.bmp",pallet);
	bmpMilho = load_bitmap("images/milho_20X20.bmp",pallet);
	bmpCobraHead = load_bitmap("images/cobraHeadVerde.bmp",pallet);
	bmpFundo = load_bitmap("images/fundoAreia.bmp",pallet);
	bmpCobrinha = load_bitmap("images/cobrinha.bmp",pallet);
	bmpParte = load_bitmap("images/cobraCorpo2.BMP",pallet);
	bmpBordaVert = load_bitmap("images/bordaVert.bmp",pallet);
	bmpBordaHoriz = load_bitmap("images/bordaHoriz.bmp",pallet);
	myfont = load_font("Terminal.pcx", pallet, NULL); //Fonte personalizada
	//FIM: Inicializacao Sprites



	//INICIO: Comidas
	Comida	cmdSapo;
	cmdSapo.x = 300;
	cmdSapo.y = 400;
	cmdSapo.bmp = bmpSapo;
	cmdSapo.spStand = load_wav("sounds/sapoStand.wav");
	cmdSapo.spDie = load_wav("sounds/sapoDie.wav");
	cmdSapo.visible = true;
	cmdSapo.ponto = 50;
	cmdSapo.taman = 1;
	
	Comida	cmdAranha;
	cmdAranha.x = 60;
	cmdAranha.y = 500;	
	cmdAranha.bmp = bmpAranha;
	cmdAranha.spStand = load_wav("sounds/sapoStand.wav");
	cmdAranha.spDie = load_wav("sounds/aranhaDie.wav");	
	cmdAranha.visible = true;
	cmdAranha.ponto = 30;
	cmdAranha.taman = 3;
		
	Comida	cmdMaca;
	cmdMaca.x = 100;
	cmdMaca.y = 100;
	cmdMaca.bmp = bmpMaca;
    cmdMaca.spStand = load_wav("sounds/sapoStand.wav");
	cmdMaca.spDie = snakeBit;
	cmdMaca.visible = true;
	cmdMaca.ponto = 20;
	cmdMaca.taman = 4;
	
	
	Comida	cmdMilho;
	cmdMilho.x = 600;
	cmdMilho.y = 100;	
	cmdMilho.bmp = bmpMilho;
	//cmdmilho.spStand = load_wav("sounds/sapoStand.wav");
	cmdMilho.spDie = snakeBit;	
	cmdMilho.visible = true;
	cmdMilho.ponto = 10;
	cmdMilho.taman = 5;
	
	Comida	cmdCobrinha;	
	cmdCobrinha.x = 600;
	cmdCobrinha.y = 480;	
	cmdCobrinha.bmp = bmpCobrinha;
	cmdCobrinha.spStand = load_wav("sounds/cobrinhaStand.wav");
	cmdCobrinha.spDie = load_wav("sounds/cobrinhaDie.wav");	
	cmdCobrinha.visible = true;
	cmdCobrinha.ponto = 40;
	cmdCobrinha.taman = 2;
	
	Comida comidas[20];
	comidas[0] = cmdSapo;
	comidas[1] = cmdAranha;
	comidas[2] = cmdMaca;
	comidas[3] = cmdMilho;
	comidas[4] = cmdCobrinha;
	comidas[5] = cmdAranha;
	comidas[6] = cmdMilho;
	comidas[7] = cmdMilho;
	comidas[8] = cmdMilho;
	comidas[9] = cmdCobrinha;
	comidas[10] = cmdSapo;
	comidas[11] = cmdAranha;
	comidas[12] = cmdMaca;
	comidas[13] = cmdMilho;
	comidas[14] = cmdCobrinha;
	comidas[15] = cmdAranha;
	comidas[16] = cmdMilho;
	comidas[17] = cmdMilho;
	comidas[18] = cmdMilho;
	comidas[19] = cmdCobrinha;
	//FIM: Comidas
	
	Vida vidas[6];
    for (int i = 0;i < 6; i++){
        vidas[i].x = i*30; 
        vidas[i].y = 0;
        vidas[i].bmp = bmpCobraHead;
        vidas[i].visible = true;
    }
    	
	//INICIO: Desenho da área de status do jogo. Pontos, Fase, Tamanho, Tempo, Vidas
	for (int i = -1;i<2;i++){rect(status, xst+i, yst+i, xst2-i, 40-i, makecol(210,192,152));}
	
	textprintf_ex(status, myfont, xst+5,68, makecol(255,0,0), -1,"Fase: 1" );
	for (int i = -1;i<2;i++){rect(status, xst+i, yst+50+i, xst2-i, yst2+50-i, makecol(210,192,152));}
		
	for (int i = -1;i<2;i++){rect(status, xst+i, yst+100+i, xst2-i, yst2+100-i, makecol(210,192,152));}
	
	for (int i = -1;i<2;i++){rect(status, xst+i, yst+150+i, xst2-i, yst2+150-i, makecol(210,192,152));}
	
	for (int i = 0;i < 6; i++){
        if (vidas[i].visible){
           draw_sprite(status, vidas[i].bmp, vidas[i].x, vidas[i].y+195);
        }
    }
	//FIM: Desenho da área status do jogo. Pontos, Fase, Tamanho, Tempo, Vidas
		
	//INICIO: Desenho da Tabela de Comida do jogo.
	textprintf_ex(status, myfont, xL,yL-20, makecol(255,0,0 ), -1,"Comidas" );
	for (int i = -1;i<2;i++){rect(status, xL+i, yL+i, xL2-i, yL2-i, makecol(210,192,152));}
	draw_sprite(status, bmpMilho, xL+10, yL+10);
	textprintf_ex(status, font, xL+40,yL+20, makecol(255,255,0 ), -1,"10 pontos" );
	draw_sprite(status, bmpMaca, xL+10, yL+40);
	textprintf_ex(status, font, xL+40,yL+50, makecol(255,0,0 ), -1,"20 pontos" );
	draw_sprite(status, bmpAranha, xL+10, yL+70);
	textprintf_ex(status, font, xL+40,yL+80, makecol(153,0,255 ), -1,"30 pontos" );
	draw_sprite(status, bmpCobrinha, xL+10, yL+100);
	textprintf_ex(status, font, xL+40,yL+110, makecol(255,255,153 ), -1,"40 pontos" );
	draw_sprite(status, bmpSapo, xL+10, yL+130);
	textprintf_ex(status, font, xL+40,yL+140, makecol(110,220,0 ), -1,"50 pontos " );
	//FIM: Desenho da Tabela de Comida do jogo.


	//INICIO: Desenho do campo de ação da cobra.
    draw_sprite(buffer, bmpFundo, xC,yC);
	draw_sprite(buffer, bmpBordaVert, xC,yC);
	draw_sprite(buffer, bmpBordaVert, xC2-20,yC);
	draw_sprite(buffer, bmpBordaHoriz, xC,xC);
	draw_sprite(buffer, bmpBordaHoriz, xC,yC2-20);
	//FIM: Desenho do campo de ação da cobra
	

	//INICIO: Desenho da cobra
	snake[0].bmp = bmpCobraHead;  		     		
	for (int i = 1;i<tamCobra;i++){
			snake[i].bmp = bmpParte;  		   
	}
	//FIM: Desenho da cobra	
	
    char control;
    int veloc;
 control = menu_inicial();
  if (control == 'j'){veloc = 120;}else{veloc = 50;}
	
	play_midi(bgSound,0);		
	
	draw_sprite(screen,status,800,0);
	
	DesenhaCampoAcao();
		int x=20, y=20 , xx, yy;
		int dirx = 20, diry = 0;		
		int tipoCol = 0;
		int tamComida; //Tamanho que a comida capturada aumentará a cobra
		int ptsComida; //Quantidade de pontos que a comida vale
		int pontosAtual = 0; //Quantidade de pontos
	
	
	

	
		while (!key[KEY_ESC]) {

			DesenhaCampoAcao();		
			

 if(control == 't'){
			if (key[KEY_UP]){
		 	   if (dirx != 0) {
	 	  		  diry = -20;
		 	   	  dirx = 0;
	  			  }
			}
			
			if (key[KEY_DOWN]){
			   if (dirx != 0) {
				   diry = 20;
				   dirx = 0;
			   }
			}
			if (key[KEY_LEFT]){
			   if (diry != 0) {
				   dirx = -20;
				   diry = 0;
			   }
			}
			if (key[KEY_RIGHT]){
			   if (diry != 0) {
		  		  dirx = 20;
			   	  diry = 0;
	  			  }
			}

}
else if(control = 'j') {
     
 swt = inp32(0x379);
                 
         if (swt == 95){
		       if ((countMsecCmd / 100) > 1){
     		 	   if (dirx == 20 && diry == 0) {
		              diry = 20;
				      dirx = 0;
 			        }
		 	        else if (dirx == 0 && diry == 20) {
	                  dirx = -20;
				      diry = 0;
			        }	  			  
		 	        else if (dirx == -20 && diry == 0) {
	  		          diry = -20;
		 	   	      dirx = 0;
                    }	 
		 	        else if (dirx == 0 && diry == -20) {
                      dirx = 20;
			   	      diry = 0;
	  			    }	            
                    countMsecCmd = 0;  
               }       			  	  			  
			}
			
            if (swt == 63){
		       if ((countMsecCmd / 100) > 1){
     		 	   if (dirx == 0 && diry == 20) {
		              diry = 0;
				      dirx = 20;
 			        }
		 	        else if (dirx == 20 && diry == 0) {
	                  dirx = 0;
				      diry = -20;
			        }	  			  
		 	        else if (dirx == 0 && diry == -20) {
	  		          diry = 0;
		 	   	      dirx = -20;
                    }	 
		 	        else if (dirx == -20 && diry == 0) {
                      dirx = 0;
			   	      diry = 20;
	  			    }	            
                    countMsecCmd = 0;  
               }       			  	  			  
			}    
}     
			
		if ((countMsec / veloc) > 1){
		   			   
			x += dirx;
			y += diry;

			if (VerifCollision(snake, comidas, x, y, &xx, &yy, tamCobra, &tipoCol, &ptsComida, &tamComida)){
	           switch (tipoCol){
			   		  case 0:
					  	   break;
				  	   case 1://colisao com corpo
		           	   		textprintf_ex(screen, font, 0,560, makecol(255,0,0 ), -1,"Colisao Em   : x: %d  y: %d TipoCol: %d", xx, yy, tipoCol); 
				   			tipoCol = 0;
				   			ResetaPosicaoCobra(snake, tamCobra);
				   			play_sample(snakeHitSelf,255,90,1000,0);				   			
				   			if (qtdVidas > 1){qtdVidas=0;} else {qtdVidas = -1;}
                            MostraVidas(qtdVidas,vidas);
 				   			x=20; 
				   			y=20; 
				   			dirx = 20; 
				   			diry = 0;
				   			
				   			AcendeLuzes(); 
				   				
				  	   break;
			 		   case 2://colisao obstaculos
		           	   		textprintf_ex(screen, font, 0,560, makecol(255,0,0 ), -1,"Colisao Em   : x: %d  y: %d TipoCol: %d", xx, yy, tipoCol); 
				   			tipoCol = 0;
				   			ResetaPosicaoCobra(snake, tamCobra);
				   			play_sample(snakeHit,255,90,1000,0);
				   			qtdVidas--;
				   			MostraVidas(qtdVidas,vidas);				   			
 				   			x=20; 
				   			y=20; 
				   			dirx = 20; 
				   			diry = 0;
				   			
      				        AcendeLuzes(); 
                            						
					   break;
				  	   case 3: //colisao comida
					   		textprintf_ex(screen, font, 0,560, makecol(255,0,0 ), -1,"Colisao Em   : x: %d  y: %d TipoCol: %d", xx, yy, tipoCol); 
					   		AumentaCobra(snake, &tamCobra, tamComida, bmpParte);
					   		pontosAtual += ptsComida;
					   		tipoCol = 0;
					   		CriaComida(comidas);
				  	   break;					   
				  	  
	  			}
	  			
				 			
	  		}
	  		
	   			   
		   MoveSnake(snake, x, y, tamCobra); 
		   
		   MostraStatus(pontosAtual, tamCobra);
		   draw_sprite(screen,status,800,0);
		   countMsec = 0;
	  		
			
			for (int i = 0;i < tamCobra;i++){
		  		draw_sprite(buffer, snake[i].bmp, snake[i].x,snake[i].y);
			}



			for (int i = 0;i < 10;i++){
		 		if (comidas[i].visible){
				   	 draw_sprite(buffer, comidas[i].bmp, comidas[i].x,comidas[i].y);
				}
			}			
			
			

			draw_sprite(screen,buffer,0,0);
				
 		    IntercalaLeds();
			
		  }
        else if(qtdVidas < 0)
        {
            play_sample(snakeDeath,255,90,1000,0);
  			stop_midi();
 			play_sample(gameOver,255,90,1000,0);
            GravaRank(pontosAtual, countSec, tamCobra);
            clear_to_color(buffer, makecol(165,229,101));
            clear_to_color(screen, makecol(165,229,101));
            MostraRank();
            textprintf_ex(buffer, myfont, 300,500, makecol(255,0,0), -1,"Aperte ESC para sair...." );
            draw_sprite(screen,buffer,0,0);        
  			break;
        }
        }

        while (!key[KEY_ESC]) {}
        FinalizaPP();
}