예제 #1
0
파일: Keyboard.c 프로젝트: hjna/eece381Mod1
alt_u8 getKeyboard(){
	alt_u8 data;

	if (keyboard_init == 1){
	
		ps2 = alt_up_ps2_open_dev("/dev/ps2_0");
		
		alt_up_ps2_init(ps2); //initialize device
		if(ps2->device_type == PS2_KEYBOARD){
			printf("keyboard connected\n");
		}
		else if(ps2->device_type  == PS2_MOUSE ){
			printf("connected to mouse\n");
		}
		else if(ps2->device_type  == PS2_UNKNOWN){
			printf("unknown device\n");
		}
		else
			printf("Not connected\n");


		alt_up_ps2_clear_fifo(ps2); //clear buffer
		set_keyboard_rate(ps2, 0x00);
		keyboard_init = 0; //initialize device once per reset
	}
	decode_scancode(ps2, &decode, &data, &ascii);
	return data;
}
예제 #2
0
bool Graphic::init()
{
    set_color_depth(m_depth); // profondeur des couleurs 8, 16, 24 ou 32
    set_uformat(U_ASCII); // pour gérer les accents

    allegro_init();

    if (install_mouse() == -1)
    {
        std::cerr << ("Failed to install mouse") << std::endl;
        return false;
    }
    if (install_keyboard() != 0)
    {
        std::cerr << ("Failed to install keyboard") << std::endl;
        return false;
    }
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, m_resX, m_resY, 0, 0) != 0)
    {
        std::cerr << ("Failed to enter in graphic mode") << std::endl;
        return false;
    }

    set_keyboard_rate(500, 100);

    show_mouse(screen);

    return this->_imageLoadSprites();
}
예제 #3
0
파일: kbctrl.cpp 프로젝트: drbokko/86Duino
KeyboardController::KeyboardController(USBHost &pusb) {
	USBHost *p = &pusb;
	p->RegisterKeyboardClass(this);
	if(__allegro_function_init == 0)
	{
		allegro_init();
		__allegro_function_init = ALLEGRO_USE_KEYBOARD;
	}
	
	if(kbInit == false)
	{
		install_timer();
		install_keyboard();
		kbInit = true;
	}
	
	kbQueue = CreateQueue(KBQUEUESIZE);
	//if(kbQueue == NULL) return;
	LOCK_VARIABLE(kbQueue);
	keyboard_lowlevel_callback = keypress_watcher;
	set_keyboard_rate(0, 0); // close scan rate
}
예제 #4
0
// Procédure d'ouverture du mode graphique Allegro
void initAllegro()
{
    set_uformat(U_ASCII);
    allegro_init();
    // Souris allegro
    install_mouse();
    install_keyboard();
    install_timer();
    set_color_depth(32);
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,1000,550,0,0)!=0)
    {
        set_color_depth(24);
        if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,1000,550,0,0)!=0)
        {
            allegro_message("probleme ouverture mode graphique");
            allegro_exit();
            exit(EXIT_FAILURE);
        }
    }
    // Souris allegro
    show_mouse(screen);
    //initialisation du temps de prise de saisie
    set_keyboard_rate(300,100);
}
int main(int argc, char *argv[])
{//start of main function

	//set gameover = true to stop the game from loading before the player plays
	GAMEOVER=true;

	//variable declerations
	int id;

	//variables used for the threads
	pthread_t pthread0;
    pthread_t pthread1;
    int threadid0 = 0;
	int threadid1 = 1;
	int threadid2 = 2;
	int threadid3 = 3;
	int threadid4 = 4;
	int threadid5 = 5;
	int threadid6 = 6;


	//set is intro to true to play the intro
	bool inIntro =true; 

	//variable used to display the players message
	char *playermessage;

	//map variables 
	int mapxoff = 0;
	int mapyoff = 0;

	//game speed variables declerations and initalization
	int frames_per_sec=0;
	int frames_done=0;
	int old_time =0;
	int totaltime=0;

	//declare allegro variables
	//BITMAP *buffer;
	BITMAP *intro;

	//initalize the classes
	//player *owl;
	sprite *hearts[3];
	sprhandler = new spritehandler();
	snake *snakeboss;

	//initalize allegro
	allegro_init();
	install_keyboard();
	install_timer();
	set_keyboard_rate(1000,1000);
	install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL);

	//initalize the screen
	set_color_depth(COLORDEPTH);
	set_gfx_mode(MODE,WIDTH,HEIGHT,0,0);

	//lock both the varibale and the funciton to be used by the interrupt handler
	LOCK_VARIABLE(ticks);
	LOCK_FUNCTION(ticker);
	//install the interrupt handler which calls the ticker 60 times a second
	install_int_ex(ticker,BPS_TO_TIMER(FPS));
	
	//lock both the varibale and the funciton to be used by the interrupt handler
	//these 
	LOCK_VARIABLE(gametime);
	LOCK_FUNCTION(game_time_ticker);
	
	//install the interrupt handler which calls the game time ticker
	//game time ticker is in 10ths of seconds
	install_int_ex(game_time_ticker,BPS_TO_TIMER(10));

	//create back buffer
    buffer = create_bitmap(SCREEN_W, SCREEN_H);

	//create the intro screen by loading the bitmap into the intro buffer
	intro = load_bitmap("flightless title.bmp",NULL);
	if(!intro){
		allegro_message("Error loading intro image");
		return 1;
	}

	//seed the random function
	srand(time(NULL));
	
	//load fonts
	FONT *dicot_22_font;
	PALETTE palette;
	
	//load the the font for the header of the application
	dicot_22_font = load_font("dicot_22.pcx", palette, NULL);
	
	//load and display the intro screen
	while(inIntro)
	{
		//display the intro screen for the game
		blit(intro,screen,0,0,0,0,SCREEN_W,SCREEN_H);
		
		//wait for the player to press enter to start the game
		if(key[KEY_ENTER]){
			inIntro=false;
			GAMEOVER=false;
		}
		
		//if the player presses esc then exit the game and the intro
		if(key[KEY_ESC]){
			inIntro=false;
			GAMEOVER=true;
		}
	}
	//for as many badies make sprites to hold the bugs
	for(int i=0;i<BADIES;i++)
	{
			//initalize the bug sprite
			 bug[i] = new bugs();

			//load the bug sprite sheet
			if(!bug[i]->load("bug.bmp")){
			allegro_message("Error loading bug sprite");
			return 1;
			}

			//initalize the bug propertys
			bug[i]->init();
			
			//add sprites to the sprite handler
			sprhandler->addbugs(bug[i]);
	}
	//initalize the player
	owl = new player();

	//load the players sprite sheet
	if(!owl->load("owl sprite_new.bmp")){
		allegro_message("Error loading owl sprite");
		return 1;
	}
	
	//initalize the sprite properties
	owl->init();

	//add the player object to the sprite handler
	sprhandler->addsprite(owl);

	//create the new snake object
	snakeboss = new snake();

	//load the snake
	if(!snakeboss->load("snake_Tiles.bmp")){
		allegro_message("Error loading snake sprite");
		return 1;
	}
	//initalize the snake
	snakeboss->init();
	//snakeboss->alive=false;

	//add the snake to the sprite handler
	sprhandler->addboss(snakeboss);

	//create for loop to load the players health indicator
	for(int i=1;i<4;i++)
	{
		//create new sprite objects to hold the images
		hearts[i-1] = new sprite();

		//load the players health image
		if(!hearts[i-1]->load("FullHealth.bmp")){
			allegro_message("Error loading player Health");
			return 1;
		}
			//set sprite properties
			hearts[i-1]->x = SCREEN_W-35*i;
			hearts[i-1]->y = 10;
			hearts[i-1]->width = 30;
			hearts[i-1]->height = 30;
			hearts[i-1]->velx = 0;
			hearts[i-1]->vely = 0;
			hearts[i-1]->animcolumns = 0;
			hearts[i-1]->curframe = 0;
			hearts[i-1]->totalframes =1;
			hearts[i-1]->animdir = 0;
			//object type of non moving sprite
			hearts[i-1]->objecttype=2;
			hearts[i-1]->alive=0;

		//add the players health to the sprite handler
		sprhandler->addsprite(hearts[i-1]);
	}
	

	//create the music samples
	SAMPLE *ingamesong;

	//load the wav file into the samples
	//main game soundtrack
	ingamesong=load_sample("melo-24_Clip.wav");
	if(ingamesong==NULL){
			allegro_message("Error loading game soundtrack");
			return 1;
	}

	//use mappy.h global function MapLoad to load the .FMP file
	MapLoad("flightless.FMP");
	
	//play the sample music
	int playing = play_sample(ingamesong,150,125,1000,true);

	//start the clock to time the player
	startTime = time(NULL);

	//create the thread for player
    id = pthread_create(&pthread0, NULL, playerdraw_thread, (void*)&threadid5);

	//create the thread for the bugs
	id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid0);
	id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid1);
	id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid2);
	id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid3);
	id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid4);

	//create the thread to draw the snake
	id = pthread_create(&pthread1, NULL, snakedraw_thread, (void*)&threadid0);

	//main game loop
	while (!GAMEOVER)
	{		

		while (ticks==0)
		{
			rest(1);
		}
		while(ticks>0)
		{
			if(key[KEY_ESC])
			{
				playermessage="You quit :( Please come back again.";
				GAMEOVER=true;
			}
			
			//get the previous value of ticks and store them
			old_ticks=ticks;

			//each time we hit this loop we increment the mapxoffset +1 			
			if(mapxoff<mapwidth*mapblockwidth)
			{
				//if the player is not hiding update the map
				if(owl->state!=2)
				{
					mapxoff++;
				}
			}
			//draw the background tiles via the mappy global funciton MapDrawBG
			MapDrawBG(buffer,mapxoff,0,0,0,WIDTH-1,HEIGHT-1);
	
			//draw the players health indicator based on the owls health/30
			for (int i=0;i<((owl->health)/30);i++)
			{
				hearts[i]->draw(buffer);
			}
			
			//lock the varibales to protect them from the unsynced threads
			pthread_mutex_lock(&threadsafe);

			//get the player input
			owl->getinput();	

			sprhandler->updatebugs();
			sprhandler->updatesnake();

			//decrement the ticks
			ticks--;
			
			//unlock the mutex
			pthread_mutex_unlock(&threadsafe);
			//if the logic is taking too long abort and draw the screen
			if(old_ticks<=ticks)
				break;
		}
		//for every second that passess we need to call this function
		if(gametime - old_time >= 10)
		{
			//frames_per_sec holds the number of frames drawn in the last sec
			frames_per_sec = frames_done;

			//reset these variables for the next second
			frames_done =0;
			old_time=gametime;
		}
		//Decrement the players health
		if(gametime%120==0)
		{
 			owl->takedamage(2);
		}
		if(sprhandler->getsnakes(0)->alive==false && gametime%400==0)
		{
			sprhandler->getsnakes(0)->resurrect();
		}
		//check players health
		if(owl->health<=0)
		{
			playermessage="You have died...please remeber to eat next time.";
			GAMEOVER=true;
		}
		//stop the window from scrolling
		if(GAMEOVER==true ||mapxoff>=mapwidth*mapblockwidth-WIDTH)
		{
			if(mapxoff>=mapwidth*mapblockwidth-WIDTH)
				playermessage="Great job player you passed the game! Way to go.";
			//stop the map from moving
			mapxoff=mapwidth*mapblockwidth-WIDTH;
			
			//stop the music from playing
			stop_sample(ingamesong);

			//create the music samples
			SAMPLE *cheer=NULL;
	
			//load the wav file into the sample
			cheer=load_sample("app.wav");
			if(cheer==NULL){
					allegro_message("Error loading game soundtrack");
					return 1;
				}

			//play the sample music
			play_sample(cheer,255,125,1000,true);

			//message the player...even if they are a quiter!
			textprintf_ex(screen,dicot_22_font,200,SCREEN_H/2,makecol(255,0,0),-1,playermessage);

			rest(2000);
 	  	 	GAMEOVER=true;
		}
		//show frames fer second on the screen by pressing "cntrl f"
		if(key[KEY_F])
		{
			if(key_shifts & KB_CTRL_FLAG)
			{
				//switch the showFPS flag to display the FPS
				SHOWFPS=!SHOWFPS;
			}
			//SHOWFPS=SHOWFPS;
		}
		if(SHOWFPS==true)
			textprintf_ex(buffer,font,20,20,WHITE,0,"frames= :%d",frames_per_sec);

		if(key[KEY_H])
		{
			if(key_shifts & KB_CTRL_FLAG)
			{
				//switch the SHOWHEALTH flag to display the FPS
				SHOWHEALTH=!SHOWHEALTH;
			}
			//SHOWHEALTH=SHOWHEALTH;
		}
		
      
		//update the screen using a double buffering technique
		acquire_screen();

		blit(buffer,screen,0,0,0,0,WIDTH,HEIGHT);
		release_screen();
		
		//everytime we bilt from buffer to screen increase frames_done
		frames_done++;
    }
	
	//find the time that the game ended
	endTime = time(NULL);

		//kill the mutex (thread protection)
    pthread_mutex_destroy(&threadsafe);

	//call to create the final image to present to the user
 	create_close(buffer,dicot_22_font,owl);
	
	//Garbage Collection clean up all of the objects that are being used in memory
	remove_keyboard();
	remove_timer();
	
	//destory the bitmaps that we are using
	destroy_bitmap(buffer);
	destroy_bitmap(intro);

	//destroy the map
	MapFreeMem();

	//delete the sprites
	delete sprhandler;

	//call to allegro exit
	allegro_exit();
	return 0;

}//end of main
예제 #6
0
int main()
{
    init();
    set_window_title("Team27, inc - Innocent Snake");
    srand((unsigned)time(NULL));

    //kondisi map awal
    int map[SIZE_Y][SIZE_X] = {-1,-1,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1,-1, 0, 0, 0, 0,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1,-1,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1
                              };

    int counter = 0;
    int posX, posY;
    int loaded = 0;
    int x_his, y_his;
    int i, j, moveX = 1, moveY = 0;

    BITMAP *buffer      = create_bitmap(SCREEN_W, SCREEN_H);
    BITMAP *path        = create_bitmap(OBJECT_SIZE, OBJECT_SIZE);
    BITMAP *wall        = load_bitmap("wall.bmp", NULL);
    BITMAP *head        = load_bitmap("head.bmp", NULL);
    BITMAP *snake_body  = load_bitmap("body.bmp", NULL);
    BITMAP *eat         = create_bitmap(OBJECT_SIZE, OBJECT_SIZE);
    BITMAP *bg_map      = create_bitmap(SCREEN_W, SCREEN_H);

    SNAKE  *snake       = NULL;
    SNAKE  *pointer     = NULL;

    clear_to_color(path, makecol(255, 255, 255)); //jalan berwarna putih
    clear_to_color(eat,  makecol(255, 255, 255)); //makanan backgroundnya putih
    circlefill(eat, OBJECT_SIZE/2, OBJECT_SIZE/2, OBJECT_SIZE/2 - 4, makecol(255, 0, 0)); //makanan berwarna merah

    //membuat map snake di bitmap
    for(i=0; i<SIZE_Y; i++)
    {
        for(j=0; j<SIZE_X; j++)
        {
            switch(map[i][j])
            {
            case PATH :
                draw_sprite(bg_map, path, j*OBJECT_SIZE, i*OBJECT_SIZE);
                break;
            case WALL :
                draw_sprite(bg_map, wall, j*OBJECT_SIZE, i*OBJECT_SIZE);
                break;
            }
        }
    }

    //inisialisasi awal posisi snake
    add_body(&snake, 3, 1, map);
    map[snake->y][snake->x] = HEAD;
    add_body(&snake, 2, 1, map);
    add_body(&snake, 1, 1, map);

    set_keyboard_rate(10, 10);

    while (!key[KEY_ESC])
    {
        //deteksi apakah sudah ada makanan di map
        if(loaded==0) {
            do {
                posX = rand()%(SIZE_X-1) + 1;
                posY = rand()%(SIZE_Y-1) + 1;
            } while(map[posY][posX] != PATH);
            map[posY][posX] = EAT;
            loaded = 1;
        }

        //deteksi keyboard yang ditekan dan mengubah arah gerak snake
        if(counter==5) {
            if(key[KEY_UP] && moveY==0)
            {
                moveX=0;
                moveY=-1;
                clear_keybuf();
            }
            else if(key[KEY_DOWN] && moveY==0)
            {
                moveX=0;
                moveY=1;
                clear_keybuf();
            }
            else if(key[KEY_LEFT] && moveX==0)
            {
                moveX=-1;
                moveY=0;
                clear_keybuf();
            }
            else if(key[KEY_RIGHT] && moveX==0)
            {
                moveX=1;
                moveY=0;
                clear_keybuf();
            }
            x_his = snake->x;
            y_his = snake->y;

            //kalau menabrak sesuatu maka keluar
            if(!move_snake(snake, moveX, moveY, map)) break;

            //jika bertemu makanan maka tambah badan
            if(map[posY][posX] != EAT)
            {
                add_body(&snake, x_his, y_his, map);
                loaded = 0;
            }
            counter=0;
        }
        counter++;

        //tempelkan semua obyek ke buffer
        draw_sprite(buffer, bg_map, 0, 0);
        draw_sprite(buffer, eat, posX*OBJECT_SIZE, posY*OBJECT_SIZE);
        for(pointer = snake; pointer != NULL; pointer = pointer->next)
        {
            switch(map[pointer->y][pointer->x])
            {
            case BODY :
                draw_sprite(buffer, snake_body, pointer->x*OBJECT_SIZE, pointer->y*OBJECT_SIZE);
                break;
            case HEAD :
                draw_sprite(buffer, head, pointer->x*OBJECT_SIZE, pointer->y*OBJECT_SIZE);
                break;
            }
        }

        //tampilkan buffer ke screen
        draw_sprite(screen, buffer, 0, 0);
        clear_bitmap(buffer);
        rest(10);
    }

    //destroy semua bitmap
    destroy_bitmap(head);
    destroy_bitmap(buffer);
    destroy_bitmap(path);
    destroy_bitmap(wall);
    destroy_bitmap(snake_body);
    destroy_bitmap(eat);

    deinit();
    return 0;
}
예제 #7
0
파일: main.c 프로젝트: caribe/Neopendolous
int main(int argc, char **argv) {
	int ret;
	char strbuf[256];
	
	allegro_init();
	
	set_color_depth(24);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0)) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Allegro says: %s\n", allegro_error);
		return 1;
	}

	install_timer();
	install_keyboard();
	install_mouse();
	if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL)==-1) printf("No sounds avaible.\n");
	graph_init();

	srand(time(0));

	set_keyboard_rate(100, 150);
	gui_bg_color = colors[gray];
	gui_fg_color = colors[black];

	LOCK_VARIABLE(counter);
	LOCK_FUNCTION(game_timer);
	install_int_ex(game_timer, BPS_TO_TIMER(6));
	
	if (argv[0][0]!='\0') {
		char *path;

		for (path=argv[0]; *path; path++) /* nothing */;
		if (path!=argv[0]) {
			for (;*path!='/' && path>=argv[0]; path--);
			path++;
			*path = '\0';
			chdir(argv[0]);
		}
	}
	
	if ((datafile = load_datafile("data.dat"))==NULL) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Datafile data.dat not found\n");
		return 1;
	}
	
	font = datafile[DATA_FONT].dat;
	gui_font_baseline = 1;

	main_dialog[0].bg = colors[darkgray];
	main_dialog[1].dp = datafile[DATA_SCREEN].dat;
	main_dialog[2].dp = datafile[DATA_LOGO].dat;

	for (ret=1; main_dialog[ret].proc; ret++) {
		main_dialog[ret].fg = colors[black];
		main_dialog[ret].bg = colors[gray];
	}
	
	while(1) {
		text_mode(-1);

		switch (do_dialog(main_dialog, -1)) {
		case DMAIN_START:
			ret = start_game();
		
			if (ret==GAME_ABORT) break;

			if (ret==GAME_END) {
				int i;
				ret=0;
				for (i=1; i<8; i++) if (players[i].points > players[ret].points) ret = i;
			}
			scare_mouse();
			clear_to_color(screen, colors[darkgray]);
			unscare_mouse();

			sprintf(strbuf, "The winner is %s!!!", players[ret].name);
			alert("GAME OVER", strbuf, NULL, "OK", NULL, 0, 0);

			report();
			break;
		case DMAIN_EDIT:
			start_editor();
			break;
		case DMAIN_EXIT:
			remove_sound();
			return 0;
		}
	}
	return 0;
}