コード例 #1
0
int main(int argc, char **argv )
{
	//inits
    al_init();
    al_init_ttf_addon();
    al_install_keyboard();
    al_install_mouse();
    al_install_audio();
    al_init_acodec_addon();
    al_init_primitives_addon();
    al_reserve_samples(1000);
    al_init_image_addon();
    ALLEGRO_KEYBOARD_STATE key;
    ALLEGRO_MOUSE_STATE mouse;
    
    ALLEGRO_DISPLAY *display;
    al_set_new_display_flags(ALLEGRO_WINDOWED);
    
    display = al_create_display(1024, 768);
    
    al_hide_mouse_cursor(display);

	//
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_TIMER *timer = NULL;
    bool redraw = true;
    timer = al_create_timer(1.0/fps);
    event_queue = al_create_event_queue();
    
    //SAMPLES
    
    
    ///OBJECTS
    cPlayer oPlayer;
    oPlayer.create();
    
    cLevel oLevel;
    oLevel.init();
    
    ////
    al_clear_to_color(al_map_rgb(0,0,0));
    al_flip_display();
    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    al_start_timer(timer);
    al_reserve_samples(1000);
    
    //RUN
    while(1<2)
    {  
        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue, &ev);
        al_get_keyboard_state(&key);
        al_get_mouse_state(&mouse);
        al_get_mouse_state_axis(&mouse,0);
        al_get_mouse_state_axis(&mouse,1);
        if(ev.type == ALLEGRO_EVENT_TIMER)
        {
			
			//Runny tunny
			{
				oLevel.createLevel();
				oPlayer.run(&key);
				//player collision check
				for(int i = 0;i<oLevel.blocknum;i++)
				{
					oPlayer.checkCollision(oLevel.oBlock[i].x,oLevel.oBlock[i].y,&key);
				}
				if(al_key_down(&key,ALLEGRO_KEY_ESCAPE))
				{
					return 0;
				}
			
			
			}
            redraw = true;
        }
        else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            break;   
        }
        if(redraw && al_is_event_queue_empty(event_queue))
        {
			//draw
			al_clear_to_color(al_map_rgb(0,0,0));
		
			oPlayer.draw();
			oLevel.draw();
			//////
		
            redraw = false;
            //
            al_flip_display();
        }
    }
    return 0;
};
コード例 #2
0
static int mallegro_Read(MWCOORD *dx, MWCOORD *dy, MWCOORD *dz, int *bp)
{
static int mz; 
static int hidingmouse;
  
  if (!al_is_mouse_installed()) return 0;
  

int buttons = 0;
int mickeyz = 0;

    al_get_next_event(a_event_queue_m, &a_event); //remove from queue

switch(a_event.type){
    case ALLEGRO_EVENT_MOUSE_AXES:
    case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
    case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
        break; 
    case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY:        
    case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
    default:
        return 0;
}

	al_get_mouse_state_axis(&mstate, 2); // 2= read z-axis vertical wheel	
	//calculate wheel button (up/down)
	if(mstate.z != mz)
	    mickeyz = mstate.z - mz;
	else
	    mickeyz = 0;
	mz = mstate.z;
	
	al_get_mouse_state(&mstate); //call above returns no button press
	//microwindows expects the mouse position at the unzoomed position - so divide
	*dx=mstate.x/zoomfactor; 
	*dy=mstate.y/zoomfactor;
    *dz = 0; //unused
	*bp = 0;

if (mstate.buttons & 1) {
    /* Primary (e.g. left) mouse button is held. */
    buttons |= MWBUTTON_L;
}
if (mstate.buttons & 2) {
    buttons |= MWBUTTON_R;
    /* Secondary (e.g. right) mouse button is held. */
}
if (mstate.buttons & 4) {
    /* Tertiary (e.g. middle) mouse button is held. */
    buttons |= MWBUTTON_M;
}

if (mickeyz > 0)
    buttons |= MWBUTTON_U;  
if (mickeyz < 0)
    buttons |= MWBUTTON_D;  

*bp = buttons;

return 2; //2=absolute mouse position

}
コード例 #3
0
ファイル: ShopState.cpp プロジェクト: FlipskiZ/My-Games-Dump
void ShopState::update(Engine* engine){
    ALLEGRO_EVENT events;
    al_wait_for_event(event_queue, &events);

    if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
        engine->quit();
    }

    if(events.type == ALLEGRO_EVENT_TIMER){

        al_get_mouse_state(&mouseState);

        al_get_keyboard_state(&keyState);

        //Update Mouse Variables +
        mouseX = al_get_mouse_state_axis(&mouseState,0);
        mouseY = al_get_mouse_state_axis(&mouseState,1);

        if(al_mouse_button_down(&mouseState, 1)){
            if(!mouseButtonLeft)
                mouseButtonLeftClick = true;
            else
                mouseButtonLeftClick = false;
            mouseButtonLeft = true;
        }else{
            mouseButtonLeft = false;
        }
        //Update Mouse Variables -

        //Player Input +
        if(al_key_down(&keyState, ALLEGRO_KEY_W)){
            playerDeltaY = -playerMovementSpeed;
        }else if(al_key_down(&keyState, ALLEGRO_KEY_S)){
            playerDeltaY = playerMovementSpeed;
        }if(al_key_down(&keyState, ALLEGRO_KEY_A)){
            playerDeltaX = -playerMovementSpeed;
        }else if(al_key_down(&keyState, ALLEGRO_KEY_D)){
            playerDeltaX = playerMovementSpeed;
        }

        if(al_key_down(&keyState, ALLEGRO_KEY_ESCAPE)){
            if(lastKeyPress != ALLEGRO_KEY_ESCAPE){
                engine->popState();
                lastKeyPress = ALLEGRO_KEY_ESCAPE;
            }
        }
        //Player Input -

        //Update Player Variables +

        if(mouseButtonLeft){
            if(playerFiringSpeedHelper >= playerFiringSpeed){
                playerFiringSpeedHelper = 0;

                int x, y, width, height, damage;
                float moveSpeed, angle = 0;

                width = 8, height = 8, x = playerCenterX - width/2, y = playerCenterY - height/2, damage = 1;
                moveSpeed = playerBulletSpeed;

                for(int i = 0; i < playerShots; i++){
                    //Shots Angle Calculation -- START
                    if(playerShots != 1){
                        angle = (-atan2(playerCenterX - mouseX, playerCenterY - mouseY)) + (-playerBulletSpread/2+(playerBulletSpread/(playerShots-1))*i);
                    }else{
                        angle = -atan2(playerCenterX - mouseX, playerCenterY - mouseY);
                    }

                    int spread = round(playerBulletSpread*100);

                    angle += (-spread/2+rand()%spread)/100.0;

                    //Shots Angle Calculation -- END

                    Bullet *newBullet = new Bullet();
                    newBullet->setPos(x, y);
                    newBullet->setDimensions(width, height);
                    newBullet->setDamage(damage);
                    newBullet->setAngleSpeed(moveSpeed, angle);
                    newBullet->setPlayerShot(true);

                    addBulletToList(newBullet);
                }
            }
        }

        if(playerX+playerDeltaX >= 0 && playerX+playerDeltaX + playerWidth < 400 && playerY >= 0 && playerY + playerHeight < 400){
            playerX += playerDeltaX;
        }
        if(playerX >= 0 && playerX + playerWidth < 400 && playerY+playerDeltaY >= 0 && playerY+playerDeltaY + playerHeight < 400){
            playerY += playerDeltaY;
        }

        playerCenterX = playerX + playerWidth/2, playerCenterY = playerY + playerHeight/2;

        playerDeltaX = 0;
        playerDeltaY = 0;
        //Update Player Variables -

        //Update Entities +
        for(int i = 0; i < MAX_BUTTONS; i++){
            if(buttonList[i] != NULL && buttonList[i]->checkActive()){
                buttonList[i]->update();
                if(buttonList[i]->clicked){
                    switch(buttonList[i]->buttonId){
                        case 0:
                            engine->popState();
                            break;

                        case 1:
                            engine->changeState(PlayState::instance());
                            break;

                        case 2:
                            engine->changeState(EditorState::instance());
                            break;

                        case 3:
                            engine->quit();
                            break;
                    }
                }
            }
        }

        for(int i = 0; i < MAX_BULLETS; i++){
            if(bulletList[i] != NULL && bulletList[i]->checkActive()){
                bulletList[i]->update();
            }
        }
        //Update Entities -

        //Rest +
        if(!al_key_down(&keyState, lastKeyPress)){
            lastKeyPress = NULL;
        }

        playerFiringSpeedHelper++;
        //Rest -

        engine->draw();
    }
}
コード例 #4
0
ファイル: rkr.cpp プロジェクト: SocratesDz/kyks
int main()
{          
	const float FPS = 60.0;

	if(!al_init())
	{
		al_show_native_message_box(NULL, "Fatal Error", NULL, "No se pudo inicializar Allegro", NULL, ALLEGRO_MESSAGEBOX_ERROR);
		return -1;
	}
	
	al_set_new_display_flags(ALLEGRO_WINDOWED); // Pone la ventana en modo Windowed
	ALLEGRO_DISPLAY *display = al_create_display(ScreenWidth, ScreenHeight);

	// Pone la posición en la que debe salir l`a ventana
	//al_set_window_position(display, 0, 30);

	// Pone el título de la ventana
	al_set_window_title(display, "Killer Bunny");

	if(!display)	// Si no se pudo crear la ventana, entonces pone un mensaje de error
	{
		al_show_native_message_box(NULL, "Error", NULL, "No se pudo crear la pantalla", NULL, ALLEGRO_MESSAGEBOX_ERROR);
		return -1;
	}

	al_install_keyboard();
	al_install_mouse();
	al_init_image_addon();
	al_init_font_addon();
	al_init_ttf_addon();
	al_init_primitives_addon();

	// -----------------------------------------------------------------

	ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
	ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS);
	ALLEGRO_KEYBOARD_STATE keyState;

	// Utilizado para debugging
	ALLEGRO_FONT *font = al_load_font("sprites/DroidSans.ttf", 10, 0);

	
	ALLEGRO_BITMAP *fondo1 = al_load_bitmap("sprites/fondo1.png");
	
	
	bool done = false;

	// ---------Estructuras del juego-----------------------------------

	struct Player player;
	player.image = al_load_bitmap("sprites/player.png");
	al_convert_mask_to_alpha(player.image, al_map_rgb(255,255,255));
	player.x = ScreenWidth / 2;
	player.y = ScreenHeight / 2;
	player.w = al_get_bitmap_width(player.image);
	player.h = al_get_bitmap_height(player.image);
	player.moveSpeed = 3;
	player.degrees = -ALLEGRO_PI/2;
   	player.alive = true;
   	player.clip = 6;
   	player.vida = 100;

	
	struct Bala bala;
	
	//~ void Bala::update()
	//~ {
		//~ bala.dx = cosf(player.xmouse - player.x);
		//~ bala.dy = senf(player.ymouse - player.y);
		//~ 
		//~ if(bala.shot){
			//~ 
			//~ bala.x += bala.dx;
			//~ bala.y += bala.dy;
			//~ }
		//~ 
	//~ }
	
	bala.image = al_load_bitmap("sprites/bullet.png");
	bala.x = player.x+50;
	bala.y = player.y+25;
	bala.shot = false;
	
	
	struct Enemigo robot;
	robot.image = al_load_bitmap("sprites/Robot_sprites.png");
	robot.death = al_load_bitmap("sprites/explosiondelrobot.png");
	al_convert_mask_to_alpha(robot.death, al_map_rgb(255, 255, 255));
	//al_convert_mask_to_alpha(robot.image, al_map_rgb(255,255,255));
	robot.x = 50;
	robot.y = 50;
	robot.w = al_get_bitmap_width(robot.image);
	robot.h = al_get_bitmap_height(robot.image);
	robot.velocidad_x = 0.23;
	robot.velocidad_y = 0.23;
	robot.fuerza= 0.5;
	robot.vida=50;
	
	//~ void Weapon::recargar()
	//~ {
		//~ for(int i = 0; i < 6; i++)
		//~ {
			//~ bullets[i] = bala;
		//~ }
	//~ }
	
	// -----------------------------------------------------------------

	// Esta variable guardará los eventos del mouse
	ALLEGRO_MOUSE_STATE mouseState;
	
	// Registro varias fuentes de eventos
	al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_display_event_source(display));
	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_mouse_event_source());

	// Inicializo el temporizador principal
	al_start_timer(timer);

	while(!done)
	{
		// La variable de los eventos
		ALLEGRO_EVENT ev;
		al_wait_for_event(event_queue, &ev); // Y aquí espero por los eventos


		if(ev.type == ALLEGRO_EVENT_TIMER)
		{
			// Dos funciones para pasar eventos del mouse y del teclado
			al_get_keyboard_state(&keyState);
			al_get_mouse_state(&mouseState);
			
			// Esto detecta la posición del mouse y lo guarda a un par de variables
			player.xmouse = al_get_mouse_state_axis(&mouseState, 0);
			player.ymouse = al_get_mouse_state_axis(&mouseState, 1);

			// Si presiono Esc entonces me saca del juego
			if(al_key_down(&keyState, ALLEGRO_KEY_ESCAPE))
			{
				done = true;
			}
			// Si presiono A entonces el valor x se reduce, osea, se va a la izquierda
			if(al_key_down(&keyState, ALLEGRO_KEY_A))
			{
				player.x -= player.moveSpeed;
			}
			// Si... meh, ya sabes lo que sigue
			if(al_key_down(&keyState, ALLEGRO_KEY_D))
			{
				player.x += player.moveSpeed;
			}
			// ...
			if(al_key_down(&keyState, ALLEGRO_KEY_W))
			{
				player.y -= player.moveSpeed;
			}
			// ...
			if(al_key_down(&keyState, ALLEGRO_KEY_S))
			{
				player.y += player.moveSpeed;
			}
			// Mata al robot
			if(al_key_down(&keyState, ALLEGRO_KEY_K))
			{
                robot.vida -= 10;
            }
					
		}
		
		// Esto permite que el jugador se mueva con el mouse
		player.degrees = atan2((player.ymouse-player.y),(player.xmouse-player.x));
					
		// La Inteligencia Artificial del enemigo
		if(robot.alive && player.alive){
			if(robot.x < player.x) robot.x += robot.velocidad_x;
			if(robot.x > player.x) robot.x -= robot.velocidad_x;
			if(robot.y > player.y) robot.y -= robot.velocidad_y;
			if(robot.y < player.y) robot.y += robot.velocidad_y;
		}
		// Uso de las funciones para las colisiones
		//if(Collision(player.x, player.y, 50, 50, robot.x, robot.y, 34, 34)) player.alive = false;
		if(PixelCol(player.image, robot.image, player.x-(player.w/2), player.y-(player.h/2), player.w, player.h, robot.x, robot.y, robot.w/7, robot.h)) player.vida -= robot.fuerza;

		if(player.vida==0) player.alive = false;
		
		if(robot.vida<=0){
			robot.vida = 0;
			robot.alive = false;
		}

		al_clear_to_color(al_map_rgb(255, 255, 255));	// Se pinta todo a negro
		al_draw_scaled_bitmap(fondo1,0, 0, 256, 256, 0, 0, ScreenWidth, ScreenHeight, 0);	// Se dibuja el fondo
		if(player.alive){	// Si el jugador está vivo
			al_draw_rotated_bitmap(player.image, 25, 25, player.x, player.y, player.degrees, 0); // Dibujo el jugador
			al_draw_rotated_bitmap(bala.image, 0, 0, player.x+5, player.y+5, player.degrees, 0); // Dibujo la bala (esto hay que quitarlo)
		}
		if(robot.alive){
			al_draw_bitmap_region(robot.image, 0, 0, 60, 52, robot.x, robot.y, 0); // Dibujo el robot
			}
		else
		{
			robot.w = al_get_bitmap_width(robot.death);
			robot.h = al_get_bitmap_width(robot.death);
			al_draw_bitmap_region(robot.death, 0, 0, robot.w/4, robot.h, robot.x, robot.y, 0);
        }
        
        // Esto es para el debugging
        
        // Dibujo rectángulos para las colisiones
        al_draw_rectangle(player.x-(player.w/2), player.y-(player.h/2), (player.x+player.w)-(player.w/2), (player.y+player.h)-(player.h/2), al_map_rgb(0, 255, 0), 1.0);
        al_draw_rectangle(robot.x, robot.y, robot.x+(robot.w/7), robot.y+robot.h, al_map_rgb(0, 255, 0), 1.0);
        
        // Escribo en una esquina de la pantalla, información respecto al personaje
		al_draw_textf(font, al_map_rgb(255,255,255), ScreenWidth-10, 2, ALLEGRO_ALIGN_RIGHT, "Player x, y : %.1f %.1f", player.x, player.y);
		al_draw_textf(font, al_map_rgb(255,255,255), ScreenWidth-10, 12, ALLEGRO_ALIGN_RIGHT, "Rotation (rad): %.5f", player.degrees);
		al_draw_textf(font, al_map_rgb(255,255,255), ScreenWidth-10, 22, ALLEGRO_ALIGN_RIGHT, "Rotation (degrees): %.2f", (player.degrees*180)/ALLEGRO_PI);
		
		// Status bar
		al_draw_filled_rectangle(0,0,player.vida*2,15, al_map_rgb(0,255,0));
		
		// Actualizo la pantalla (flip)
		al_flip_display();
		
		
	}
	
	//-----After party (hay que limpiar)--------------------------------
	
	// A destruirlo todo!! BAM BAM BAM, KABOOM!!
	al_destroy_font(font);
	al_destroy_bitmap(fondo1);
	al_destroy_bitmap(robot.image);
	al_destroy_display(display);
	al_destroy_event_queue(event_queue);
	al_destroy_bitmap(player.image);
	al_destroy_timer(timer);

	return 0;
}
コード例 #5
0
int main(){
    srand(time(0));

	Engine engine;

    fitnessFile << "Current Generation|Best Fitness|Average Fitness" << endl;

	//Initialize the Engine
	engine.init("Neural Network", screenWidth, screenHeight, false);

    if(!loadConfiguration()){
        al_show_native_message_box(display,
            "File not found",
            "File not found",
            "Check if the file config.ini exist inside the config folder",
            NULL,
        ALLEGRO_MESSAGEBOX_ERROR);
        engine.quit();
    }

	//Load the Menu
	engine.changeState(PlayState::instance());

    //Timestep Variables
    double t = 0.0;
    double dt = 1/logicSpeed;

    double currentTime = 0.0;
    double newTime = 0.0;
    double frameTime = 0.0;

    double accumulator = 0.0;

	//Main Loop
    while(engine.running()){
        ALLEGRO_EVENT events;
        al_wait_for_event(event_queue, &events);

        timerEvent = false;

        if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
            engine.quit();
        }

        if(events.type == ALLEGRO_EVENT_TIMER){
            timerEvent = true;
        }
        //Main Timer Event +
        if(timerEvent){
            al_get_mouse_state(&mouseState);

            al_get_keyboard_state(&keyState);

            //Update Mouse Variables +
            mouseX = al_get_mouse_state_axis(&mouseState,0);
            mouseY = al_get_mouse_state_axis(&mouseState,1);
            mouseWheel = al_get_mouse_state_axis(&mouseState, 2);

            if(al_mouse_button_down(&mouseState, 1)){
                mouseButtonLeft = true;
                mouseButtonLeftClick = false;
            }else if(mouseButtonLeft){
                mouseButtonLeftClick = true;
                mouseButtonLeft = false;
            }else{
                mouseButtonLeftClick = false;
            }

            if(al_mouse_button_down(&mouseState, 2)){
                mouseButtonRight = true;
                mouseButtonRightClick = false;
            }else if(mouseButtonRight){
                mouseButtonRightClick = true;
                mouseButtonRight = false;
            }else{
                mouseButtonRightClick = false;
            }
            //Update Mouse Variables -

            //Rest +
            if(!al_key_down(&keyState, lastKeyPress)){
                lastKeyPress = 0;
            }
            //Rest -

            if(events.timer.source == timer){
                newTime = al_get_time();
                frameTime = newTime - currentTime;
                if(frameTime > 0.25)
                    frameTime = 0.25;	  // note: max frame time to avoid spiral of death
                currentTime = newTime;

                accumulator += frameTime;

                while(accumulator >= dt){
                    engine.update(); //Call the gameState specfic update
                    dt = 1/logicSpeed;
                    t += dt;
                    accumulator -= dt;
                }

                engine.draw();

                al_draw_bitmap(cursorImage, mouseX-4, mouseY-4, 0);

                al_flip_display();
                al_clear_to_color(al_map_rgb(0, 0, 0));
            }
        }
        //Main Timer Event -
    }
	// cleanup the engine
	engine.cleanup();

    fitnessFile.close();
}