示例#1
0
void DrawObjectsList2(WindowRef w, RECTPTR r, long n)
// called once for each item in VList to draw nth line
{
	char s[256];
	short right, style = normal;
	Point p;
	ListItem item = model->GetNthListItem(n, 0, &style, s);
	
	if (item.owner == 0) return;
	
	GetPen(&p);	
	PenNormal();
	
	right = DrawBullet(item, r, FALSE, 0);
	
	MyMoveTo(right + 4, p.v - OBJECTLINEOFFSET);

	// set text-face according to style requested
	TextFontSizeFace(kFontIDGeneva,LISTTEXTSIZE,style);
	drawstring(s);		
	DrawBullet(item, r, TRUE, 0);
	TextFontSizeFace(kFontIDGeneva,LISTTEXTSIZE,normal);
}
示例#2
0
void HiliteObjectsRect(DialogPtr w, RECTPTR r, long n)
// limits the horizontal selection based on text string width
{
	char s[256];
	short left, right, style = normal;
	ListItem item = model->GetNthListItem(n, 0, &style, s);
	
	if (item.owner == 0) return;
	
	TextFontSizeFace(kFontIDGeneva,LISTTEXTSIZE,style);
	
	right = DrawBullet(item, r, FALSE, 0);
	
	left = right + 3;
	if (left > r->left) r->left = left;
	right = r->left + stringwidth(s) + 3;
	if (right < r->right) r->right = right;		// selects only upto right edge of text

	TextFontSizeFace(kFontIDGeneva,LISTTEXTSIZE,normal);
}
示例#3
0
int main()
{

	app.window.create(sf::VideoMode(800,600,32),"Window");
	app.window.setFramerateLimit(60);
	void ProcessInput();
	void EndProgram();
	void DrawBullet(int numOfBullets);
	void MoveBullet(int numOfBullets);
	while (app.window.isOpen())	{
		app.window.clear();			//Clear the screen
		app.window.draw(player.sprite);		//Draw the player
		DrawBullet(app.totalBullets);		//Draw the existing bullets
		app.window.display();			//Display the sprites
		ProcessInput();				//Process the user's input
		MoveBullet(app.totalBullets);		//Move the existing bullets
		if (player.canShoot == false) app.CheckBulletClock(); //The player cannot shoot at any given time. There should
		EndProgram();		//Check if the user wishes to exit (it's separated from ProcessInput 
	}
	return 0;
}
示例#4
0
文件: Game2.cpp 项目: scheeloong/Cpp
int main(void)
{
	// primitive variables
	bool done = false; // for game loop
	bool redraw = true;  // for redraw. 
	const int FPS = 60;  // Frames Per Second
	bool isGameOver = false; 
	// initialize
	// object variables
	SpaceShip ship, ship2;
	Bullet bullet[NUM_BULLETS]; 
	Bullet bullet2[NUM_BULLETS]; 
	// Initialize 
	ALLEGRO_DISPLAY *display = NULL; 
	ALLEGRO_EVENT_QUEUE *event_queue = NULL ;
	ALLEGRO_TIMER *timer = NULL;
	ALLEGRO_FONT *font18 = NULL; 
	ALLEGRO_BITMAP *image1 = NULL , *image2 = NULL; 
	if(!al_init()) return -1; 
	display = al_create_display(WIDTH, HEIGHT); 
		if(!display) return -1; 

	// initializing primitives (for drawing ship) 
	al_init_primitives_addon(); 
	al_install_keyboard(); 
	al_init_font_addon();
	al_init_ttf_addon(); 
	al_init_image_addon(); 
	event_queue = al_create_event_queue(); 
	timer = al_create_timer(1.0/FPS); 
	// initialize objects
	srand(time(NULL)); // sees number generator with current time. 
						// as time changes, the random number changes too. 
	InitShip(ship); 
	InitShip2(ship2); 
	InitBullet(bullet, NUM_BULLETS); // array of bullets for ship
	InitBullet(bullet2, NUM_BULLETS); // array of bullets for ship2
	font18 = al_load_font("Arial.ttf", 18, 0); 
	image1 = al_load_bitmap("Ship1.bmp"); 
	image2 = al_load_bitmap("Ship2.bmp"); 
	// register keyboard. 
	al_register_event_source(event_queue, al_get_keyboard_event_source()); 
	al_register_event_source(event_queue, al_get_timer_event_source(timer)); 
	al_register_event_source(event_queue, al_get_display_event_source(display)); 
	// START TIMER
	al_start_timer(timer); 
	while(!done)
	{
		ALLEGRO_EVENT ev; 
		// wait for event to come in from queue. 
		al_wait_for_event(event_queue, &ev); 
		// only happens once every 60 seconds max. 
		if (ev.type == ALLEGRO_EVENT_TIMER)
		{
			redraw = true; 
			// update the position of the ship, depending if the keys are pressed. 
			if(keys[UP]) MoveShipUp(ship2);
			if(keys[DOWN]) MoveShipDown(ship2);
			if(keys[LEFT]) MoveShip2Left(ship2);
			if(keys[RIGHT]) MoveShip2Right(ship2);
			if(keys[W]) MoveShipUp(ship);
			if(keys[S]) MoveShipDown(ship);
			if(keys[A]) MoveShipLeft(ship);
			if(keys[D]) MoveShipRight(ship);

			if(!isGameOver)
			{
				UpdateBullet(bullet, NUM_BULLETS);  // update the position of the bullet
				UpdateBullet2(bullet2, NUM_BULLETS); 
				// test if all the objects collide after all the objects move. 
				CollideBullet(bullet, NUM_BULLETS, ship2, ship); 
				CollideBullet2(bullet2, NUM_BULLETS, ship, ship2); 
				if(ship.lives <= 0 || ship2.lives <=0) isGameOver = true; 
			}
		}
		else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) done = true; 
		else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch(ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = true;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = true;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = true;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = true; 
				FireBullet2(bullet2, NUM_BULLETS, ship2); // break to fire one bullet only
				break; 
			case ALLEGRO_KEY_W:
				keys[W] = true;
				break;
			case ALLEGRO_KEY_A:
				keys[A] = true;
				break;
			case ALLEGRO_KEY_S:
				keys[S] = true;
				break;
			case ALLEGRO_KEY_D:
				keys[D] = true;
				break;
			case ALLEGRO_KEY_E:
				keys[E] = true; 
				FireBullet(bullet, NUM_BULLETS, ship); // break to fire one bullet only
				break; 
			}
		}

		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch(ev.keyboard.keycode)
			{
				case ALLEGRO_KEY_ESCAPE:
					done = true;
					break;
				case ALLEGRO_KEY_UP:
					keys[UP] = false;
					break;
				case ALLEGRO_KEY_DOWN:
					keys[DOWN] = false;
					break;
				case ALLEGRO_KEY_LEFT:
					keys[LEFT] = false;
					break;
				case ALLEGRO_KEY_RIGHT:
					keys[RIGHT] = false;
					break;
				case ALLEGRO_KEY_SPACE:
					keys[SPACE] = false; 
					break; 
				case ALLEGRO_KEY_W:
					keys[W] = false;
					break;
				case ALLEGRO_KEY_A:
					keys[A] = false;
					break;
				case ALLEGRO_KEY_S:
					keys[S] = false;
					break;
				case ALLEGRO_KEY_D:
					keys[D] = false;
					break;
				case ALLEGRO_KEY_E:
					keys[E] = false; 
					// break to fire one bullet only
					break; 
			}
		}

		// if redraw is true and no queue for event. 
		if ( redraw && al_is_event_queue_empty(event_queue))
		{
			redraw = false; 

			if (!isGameOver)
			{
				// draws the latest position of bullet and ship that has been updated. 
				DrawShip(ship, image1); // Draw ship
				DrawShip(ship2, image2); 
				DrawBullet(bullet, NUM_BULLETS); // draw bullets 
				DrawBullet(bullet2, NUM_BULLETS); 
				al_draw_textf(font18, al_map_rgb(255,0,255), 5, 5, 0, "Player 1 Lives left: %i \nScore: %i\n", ship.lives, ship.score); 
				al_draw_textf(font18, al_map_rgb(255,0,255), 50, 50, 0, "Player 2Lives left: %i \nScore: %i\n", ship2.lives, ship2.score);
			}
			else 
			{ al_draw_textf(font18, al_map_rgb(0,255,255), WIDTH/2, HEIGHT/2, ALLEGRO_ALIGN_CENTRE,
							"GAME OVER. P1 Final Score: %i\n P2 Final Score %i",ship.score, ship2.score); 

			}
			al_flip_display(); 
			al_clear_to_color(al_map_rgb(0, 0, 0)); 
		}
	}
	al_destroy_display(display); 
	return 0;
}
示例#5
0
int main(void)
{
    bool done = false; // Variavel booleana para identificar se o programa terminou de ser executado
    bool redraw = true; // Enquanto essa variavel for verdadeira, ira ser desenhado algo na tela
    bool desenha = true;
    int draw[5]= {0, 0, 0, 0, 0};
    int pos_x = WIDTH / 2;
    int pos_y = HEIGHT / 2;
    int timer_tiros = 0;
    int timer_energy = 0;
    int timer_heats = 0;
    int timer_tamenho_heat = 0;
    int timer_componente = 0;
    int count = 0;


    Gamer(gamer);
    Zone zone[LINHAS][COLUNAS];
    Bullet bullets[NUM_BULLETS+1];
    Energy energy[NUM_ENERGYS+1];
    Heat heats[NUM_HEAT+1];
    Zombie zombie[NUM_ZOMBIES];
    Battery battery[NUM_BATTERY];


    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_TIMER *timer = NULL;
    ALLEGRO_FONT *font18 = NULL;
    ALLEGRO_FONT *font_maior = NULL;
    ALLEGRO_BITMAP *resistor = NULL;
    ALLEGRO_BITMAP *capacitor = NULL;
    ALLEGRO_BITMAP *indutor = NULL;
    ALLEGRO_BITMAP *diodo = NULL;
    ALLEGRO_BITMAP *bateria = NULL;
    ALLEGRO_BITMAP *protoboard = NULL;
    ALLEGRO_BITMAP *fogo = NULL;
    ALLEGRO_BITMAP *zombie_bitmap = NULL;
    ALLEGRO_BITMAP *ataque_eletromagnetico = NULL;
    ALLEGRO_BITMAP *energia_capacitor = NULL;
    ALLEGRO_BITMAP *logo = NULL;


    if(!al_init())										//initialize Allegro
        return -1;

    display = al_create_display(WIDTH, HEIGHT);			//create our display object

    if(!display)										//test display object
        return -1;

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

    resistor = al_load_bitmap("Resistor.png");
    capacitor = al_load_bitmap("Capacitor.png");
    indutor = al_load_bitmap("Indutor.png");
    diodo = al_load_bitmap("Diodo.png");
    bateria = al_load_bitmap("Bateria.png");
    protoboard = al_load_bitmap("Protoboard.png");
    fogo = al_load_bitmap("fogo_resistor/fire1.png");
    zombie_bitmap = al_load_bitmap("Zombie.png");
    ataque_eletromagnetico = al_load_bitmap("eletromagnetismo.jpg");
    energia_capacitor = al_load_bitmap("energia_capacitor.png");
    logo = al_load_bitmap("logo_EvsZ.jpg");

    al_convert_mask_to_alpha(resistor, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(capacitor, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(indutor, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(diodo, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(bateria, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(zombie_bitmap, al_map_rgb(255, 255, 255));
    al_convert_mask_to_alpha(ataque_eletromagnetico, al_map_rgb(255, 255, 255));


    event_queue = al_create_event_queue();
    timer = al_create_timer(1.0 / FPS);

    InitGamer(gamer);
    InitZone(zone, LINHAS, COLUNAS);
    InitBullet(bullets, NUM_BULLETS+1);
    InitHeat(heats, NUM_HEAT+1);
    InitZombie(zombie, NUM_ZOMBIES);
    InitEnergy(energy, NUM_ENERGYS+1);
    InitBattery(battery, NUM_BATTERY);

    font18 = al_load_font("arial.ttf", 18, 0);
    font_maior = al_load_font("arial.ttf", 24, 0);

    al_register_event_source(event_queue, al_get_mouse_event_source());
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    al_register_event_source(event_queue, al_get_display_event_source(display));

    al_hide_mouse_cursor(display);
    al_start_timer(timer);

    while(!done)
    {
        ALLEGRO_EVENT ev;
/*===============================================================================================================================================*/
        if(state == MENU)
        {
            al_wait_for_event(event_queue, &ev);

            al_draw_bitmap(logo, WIDTH / 2 - 145, HEIGHT - 500, 0);
            al_draw_text(font18, al_map_rgb(255, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Os Zombies querem roubar seu diploma, proteja-o");
            al_draw_text(font18, al_map_rgb(0, 255, 255), WIDTH / 2, HEIGHT / 2 + 100, ALLEGRO_ALIGN_CENTRE, "Pressione Space para jogar");
            al_draw_text(font_maior, al_map_rgb(0, 255, 0), WIDTH / 2, HEIGHT / 2 + 150, ALLEGRO_ALIGN_CENTRE, "Tecla 1 = Resistor Tecla 2 = Capacitor  Tecla 3 = Indutor  Tecla 4 = Diodo");
            if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
                if(ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
                    state = PLAYING;

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
                done = true;

            al_flip_display();

        }
/*===============================================================================================================================================*/
        if(state == PLAYING)
        {
            al_wait_for_event(event_queue, &ev);

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
                done = true;

            if(ev.type == ALLEGRO_EVENT_TIMER)
            {
                redraw = true;


                    for(int i = 0; i < LINHAS; i++)
                        for(int j=0; j<COLUNAS; j++)
                            if(zone[i][j].x < pos_x &&
                                    zone[i][j].x + ZONEX > pos_x &&
                                    zone[i][j].y < pos_y &&
                                    zone[i][j].y + ZONEY > pos_y)
                                if(desenha)
                                    if(zone[i][j].draw == 0)
                                    {
                                        if(keys[KEY_1] && gamer.energy >= 100)
                                        {
                                            zone[i][j].draw = draw[1];
                                            gamer.energy -=100;
                                            draw[1] = 0;
                                        }
                                        if(keys[KEY_2] && gamer.energy >= 50)
                                        {
                                            zone[i][j].draw = draw[2];
                                            gamer.energy -=50;
                                            draw[2] = 0;
                                        }
                                        if(keys[KEY_3] && gamer.energy >= 100)
                                        {
                                            zone[i][j].draw = draw[3];
                                            gamer.energy -=100;
                                            draw[3] = 0;
                                        }
                                        if(keys[KEY_4] && gamer.energy >= 100)
                                        {
                                            zone[i][j].draw = draw[4];
                                            gamer.energy -=100;
                                            draw[4] = 0;
                                        }
                                    }
                timer_battery_speed++;
                timer_battery_start++;
                if(timer_battery_start >= 360) // diminui a frequencia com que nasce uma bateria nova
                {
                    StartBattery(battery, NUM_BATTERY);
                    timer_battery_start = 0;
                }

                if(timer_battery_speed >= 2) // reduz um pouco a velocidade da bateria
                {
                    UpdateBattery(battery, NUM_BATTERY);
                    timer_battery_speed = 0;
                }


                timer_heats++;

                for(int i=0; i<LINHAS; i++)
                    for(int j=0; j<COLUNAS; j++)
                        if(zone[i][j].draw == 1)
                            if(timer_heats >= 200)
                            {
                                FireHeat(heats, NUM_HEAT+1, zone);
                                timer_heats = 0;
                            }

                timer_energy++;
                timer_energy_cap_death++;

                for(int i=0; i<LINHAS; i++)
                    for(int j=0; j<COLUNAS; j++)
                        if(zone[i][j].draw == 2)
                        {
                            if(timer_energy >= 420)
                            {
                                CreateEnergy(energy, NUM_ENERGYS+1, zone);
                                timer_energy = 0;
                            }

                        }

                timer_tiros++;

                for(int i=0; i<LINHAS; i++)
                    for(int j=0; j<COLUNAS; j++)
                        if(zone[i][j].draw == 3)
                            if(timer_tiros >= 200) // faz os Electronics atirarem numa velocidade constante
                            {
                                FireBullet(bullets, NUM_BULLETS+1, zone);
                                timer_tiros = 0;
                            }
                timer_zombie_start++;
                timer_zombie_speed++;
                timer_dificuldade++;
                if(timer_zombie_start >= 3)
                {
                    StartZombie(zombie, NUM_ZOMBIES);
                    timer_zombie_start = 0;
                }
                if(dificuldade >20 && dificuldade <=500)
                {
                if(timer_dificuldade >= 300)
                    {
                        dificuldade -= 30;
                        timer_dificuldade = 0;
                    }
                }
                if(timer_zombie_speed >= 3)
                {
                    UpdateZombie(zombie, NUM_ZOMBIES);
                    timer_zombie_speed = 0;
                }


                UpdateBullet(bullets, NUM_BULLETS+1);
                CollideBullet(bullets, NUM_BULLETS, zombie, NUM_ZOMBIES, gamer);
                CollideHeat(heats, NUM_HEAT, zombie, NUM_ZOMBIES, gamer, timer_tamenho_heat);
                CollideZone(zone, LINHAS, COLUNAS, zombie, NUM_ZOMBIES);
                CollideZoneDiodo(zone, LINHAS, COLUNAS, zombie, NUM_ZOMBIES);


                for(int i = 0; i < NUM_ENERGYS; i++)
                    if(energy[i].live)
                        if(energy[i].x - energy[i].boundx < pos_x &&
                                energy[i].x + energy[i].boundx > pos_x &&
                                energy[i].y - energy[i].boundy < pos_y &&
                                energy[i].y + energy[i].boundy > pos_y)
                        {
                            energy[i].live = false;
                            gamer.energy += 25;
                        }


            }
            else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                switch(ev.keyboard.keycode)
                {
                case ALLEGRO_KEY_ESCAPE:
                    done = true;
                    break;
                case ALLEGRO_KEY_1:
                    keys[KEY_1] = true;
                    break;
                case ALLEGRO_KEY_2:
                    keys[KEY_2] = true;
                    break;
                case ALLEGRO_KEY_3:
                    keys[KEY_3] = true;
                    break;
                case ALLEGRO_KEY_4:
                    keys[KEY_4] = true;
                    break;
                }
            }
            else if(ev.type == ALLEGRO_EVENT_KEY_UP)
            {
                switch(ev.keyboard.keycode)
                {
                case ALLEGRO_KEY_ESCAPE:
                    done = true;
                    break;
                case ALLEGRO_KEY_1:
                    keys[KEY_1] = false;
                    break;
                case ALLEGRO_KEY_2:
                    keys[KEY_2] = false;
                    break;
                case ALLEGRO_KEY_3:
                    keys[KEY_3] = false;
                    break;
                case ALLEGRO_KEY_4:
                    keys[KEY_4] = false;
                    break;
                }
            }
            else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
            {
                if (ev.mouse.button & 2)
                    done = true;
            }
            else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES)
            {
                pos_x = ev.mouse.x;
                pos_y = ev.mouse.y;
                CaptureBattery(battery, NUM_BATTERY, ev.mouse.x, ev.mouse.y, gamer);
            }

            timer_componente++;

            if(timer_componente >= 10)
            {
                if(keys[KEY_1])
                    draw[1] = 1;
                if(keys[KEY_2])
                    draw[2] = 2;
                if(keys[KEY_3])
                    draw[3] = 3;
                if(keys[KEY_4])
                    draw[4] = 4;
            }

            if(redraw && al_is_event_queue_empty(event_queue))
            {
                redraw = false;


                DrawZone(zone, LINHAS, COLUNAS, resistor, capacitor, indutor, diodo);
                DrawBullet(bullets, NUM_BULLETS+1, ataque_eletromagnetico);
                DrawEnergy(energy, NUM_ENERGYS+1, energia_capacitor);
                DrawZombie(zombie, NUM_ZOMBIES, zombie_bitmap);
                DrawBattery(battery, NUM_BATTERY, bateria);
                timer_tamenho_heat++;
                DrawHeat(heats, NUM_HEAT+1, timer_tamenho_heat, fogo);
                if(timer_tamenho_heat > 80)
                    for(int i=0; i<NUM_HEAT; i++)
                    {
                        heats[i].live = false;
                        timer_tamenho_heat = 0;
                    }


                al_draw_filled_rectangle(pos_x, pos_y, pos_x + 10, pos_y + 10, al_map_rgb(0, 0, 0));
                count++;
                al_draw_textf(font18, al_map_rgb(255, 0, 0), WIDTH*13/16, 85, 0, "Time: %i", count/60);
                al_draw_textf(font18, al_map_rgb(255, 0, 0), WIDTH*13/300, 85, 0, "Energy: %i", gamer.energy);
                al_draw_textf(font18, al_map_rgb(255, 0, 0), WIDTH*13/25, 85, 0, "Score: %i", gamer.score);
                al_flip_display();
                al_draw_bitmap(protoboard, 0, 0, 0);
            }

        }
    /*================================================================================================================================================*/
        if(state == GAMEOVER)
        {
            al_wait_for_event(event_queue, &ev);
            al_clear_to_color(al_map_rgb(0,0,0));
            al_draw_text(font18, al_map_rgb(255, 168, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Os Zombies roubaram seu diploma");
            al_draw_text(font18, al_map_rgb(255, 255, 255), WIDTH / 2, HEIGHT / 2 +40, ALLEGRO_ALIGN_CENTRE, "Pressione Space para sair");
            al_draw_textf(font_maior, al_map_rgb(255, 0, 0), WIDTH*13/28, 85, 0, "Score: %i", gamer.score);
            al_flip_display();

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
                done = true;

            if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
                if(ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
            {
                al_destroy_event_queue(event_queue);
                al_destroy_timer(timer);
                al_destroy_font(font18);
                al_destroy_bitmap(resistor);
                al_destroy_bitmap(capacitor);
                al_destroy_bitmap(indutor);
                al_destroy_bitmap(diodo);
                al_destroy_bitmap(bateria);
                al_destroy_bitmap(protoboard);
                al_destroy_bitmap(zombie_bitmap);
                al_destroy_display(display);

            }




        }
    }


    return 0;
}
示例#6
0
int main(void){
	//allegro variables
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_EVENT_QUEUE *event_queue = NULL;
	ALLEGRO_BITMAP *bg = NULL;
	ALLEGRO_TIMER *timer;

	//program init
	if(!al_init())										//initialize Allegro
		return -1;

	display = al_create_display(WIDTH, HEIGHT);			//create our display object

	if(!display)										//test display object
		return -1;

	//==============================================
	//ADDON INSTALL
	//==============================================
	al_install_keyboard();
	al_init_image_addon();
	al_init_font_addon();
	al_init_ttf_addon();
	al_init_primitives_addon();

    // FONT DEL PROGRAMA.
	ALLEGRO_FONT *font = al_load_ttf_font("Sarpanch-SemiBold.ttf",30,0 );

    // VARIABLES DEL JUEGO ========================
    bg = al_load_bitmap("img/sp1.jpg");

    SpaceShip nave_jugador;
    Enemy enemies1[10];
    Enemy enemies2[10];
    Enemy enemies3[10];
    Enemy enemies4[10];
    Enemy jefe1[10];
    Bullet bullets[5];


    char vidas_char[2];


    // INICIALIZAR OBJETOS====================
    InitShip(nave_jugador);
    InitBullet(bullets, NUM_BULLETS);
    InitEnemies(enemies1,ENM1,10,0,220,WIDTH/2);
    InitEnemies(enemies2,ENM2,8,50,180,WIDTH/2+100);
    InitEnemies(enemies3,ENM3,6,100,140,WIDTH);
    InitEnemies(enemies4,ENM4,4,150,100,(WIDTH/2)+100);
    InitEnemies(jefe1,JEFE,2,200,60);
    //DrawEnemies(enemies,NUM_ENEMIES);

	//==============================================
	//TIMER INIT AND STARTUP
	//==============================================
	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / 60);

	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_keyboard_event_source());

	al_start_timer(timer);

    //AnimacionEntrada(enemies,NUM_ENEMIES);
    printf("ya");
    int animacion=1;
    int movimientos=0;
	while(!done){

		ALLEGRO_EVENT ev;
		al_wait_for_event(event_queue, &ev);

        // INFORMACION DEL JUEGO Y BG
        // DrawWindowStatus();
		al_draw_bitmap(bg, 0, 0, 0);
        al_draw_bitmap(nave_jugador.image, 5,440,0);
        al_draw_text(font, al_map_rgb(255,255,255), 40,430, 0, "X");
        sprintf(vidas_char,"%d",vidas);
        al_draw_text(font, al_map_rgb(255,255,255), 60,430, 0, vidas_char);
        al_draw_text(font, al_map_rgb(255,255,255), WIDTH/2 - 30, 0,ALLEGRO_ALIGN_CENTRE, "Score");
        char vartext[10];
        sprintf(vartext,"%d",score);
        al_draw_text(font, al_map_rgb(255,255,255), WIDTH/2 + 40, 0, 0, vartext);


		//==============================================
		//INPUT
		//==============================================
        if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_DOWN){
			switch(ev.keyboard.keycode){
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = true;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = true;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = true;
				FireBullet(bullets, NUM_BULLETS, nave_jugador);
				break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
        {
			switch(ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = false;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = false;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = false;
				break;
			}
		}


		//==============================================
		//GAME UPDATE
		//==============================================
		else if(ev.type == ALLEGRO_EVENT_TIMER){
			render = true;

			if(keys[LEFT])
				MoveShipLeft(nave_jugador);
			else if(keys[RIGHT])
				MoveShipRight(nave_jugador);

            UpdateBullet(bullets, NUM_BULLETS);

            CollideBullet(bullets,NUM_BULLETS,enemies1,NUM_ENEMIES);
            CollideBullet(bullets,NUM_BULLETS,enemies2,8);
            CollideBullet(bullets,NUM_BULLETS,enemies3,6);
            CollideBullet(bullets,NUM_BULLETS,enemies4,4);
            CollideBullet(bullets,NUM_BULLETS,jefe1,2);
		}

		//==============================================
		//RENDER
		//==============================================
		if(render && al_is_event_queue_empty(event_queue))
		{
			if (animacion==1){
                printf("si primero");
                movEnemies(enemies1,10,1);
                movEnemies(enemies2, 8,1);
                movEnemies(enemies3,6,1);
                movEnemies(enemies4,4,1);
                movEnemies(jefe1,2,1);
                animacion=0;
                movimientos=1;
			}


			render = false;

            // Dibujar nave
			al_draw_bitmap(nave_jugador.image, nave_jugador.x - nave_jugador.w / 2, nave_jugador.y - nave_jugador.h / 2, 0);
			// Dibujar Balas
			DrawBullet(bullets, NUM_BULLETS);

            // Dibuja los enemigos.
            DrawEnemies(enemies1,10);
            DrawEnemies(enemies2,8);
            DrawEnemies(enemies3,6);
            DrawEnemies(enemies4,4);
            DrawEnemies(jefe1,2);

			al_flip_display();
			al_clear_to_color(al_map_rgb(0,0,0));
			if (movimientos){
                movimientos=0;
                movEnemies(enemies1,10,2);
                movEnemies(enemies2,8,3);
			}
		}

	}


    for (int i =0;i<10;i++)

    al_destroy_bitmap(enemies1[i].image);

	al_destroy_event_queue(event_queue);
	al_destroy_timer(timer);
	al_destroy_display(display);						//destroy our display object

	return 0;
}
示例#7
0
// run over the glyphs collecting information before calling client
// with APPDrawString
void scDrawLine::Draw()
{
	caseState	prevCase = noCase;
	caseState	curCase  = noCase;
	
	CharRecord chRec;

	SetCharData( );

	if ( !fOffset && scCachedStyle::GetParaStyle().GetNumbered() )
		DrawBullet();

	scCachedStyle& cachedStyle = scCachedStyle::GetCurrentCache();

	for ( ; fToDraw > 0; ) {

			// if there is a spec change dump any characters in the buffer
		if ( fOffset >= fSpecRec->offset()	) {
			cachedStyle = UpdateSpec(); 
			prevCase = noCase;
		}

#ifdef jis4051
		if ( fChRec->flags.GetRenMoji() > 0 )
			ProcessRenMoji();
		else 
#endif
		{  
			UCS2 ch = CMctToAPP( fChRec->character );

			switch ( fChRec->character ) {
				case scField:
					DrawField();
					break;

				case scRulePH:
					DrawRule();
					break;

				case scFillSpace:
				case scTabSpace:
					DrawTabSpace();
					break;
					
				default:
					if (ch)
					{
						chRec.Set( CMctToAPP( ch ), fChRec->flags, fChRec->escapement );
						SetCharArray( chRec, LETTERSPACE( fChRec ) );
					}
					break;
	
				case scFixRelSpace:
					chRec.Set( CMctToAPP( ch ), SCRLUCompGS( cachedStyle.GetGlyphWidth() , (RLU)fChRec->escapement ) );
					SetCharArray( chRec, false );
					break;
	
				case scFixAbsSpace:
					chRec.Set( CMctToAPP( ch ), fChRec->escapement );
					SetCharArray( chRec, false );
					break;
			}	
			IncrementBackingStore();
		}
		prevCase = curCase;
	}

	if ( fEndOffset == fPara.GetContentSize() ) {
		UCS2 ch = fCount ? (fChRec-1)->character : (UCS2)0;

		switch ( ch ) {
			case scHardReturn:
			case scVertTab:
				break;
			default:
				if ( fPara.Next() )
					fStreamPosition = eEndOfPara;
				else
					fStreamPosition = eEndOfStream;
				break;
		}
	}

	if ( fCount || fStreamPosition != eNoEnd ) {
		if ( fCount ) {
			scCharFlags flags	= (fChRec-1)->flags;

			if ( flags.IsHyphPresent() )
				ProcessHyphen( flags );
		}
			
		if ( fStreamPosition == eEndOfPara ) {
			if ( CMctToAPP( scParaEnd ) ) { 
				chRec.Set( CMctToAPP( scParaEnd ),	scCachedStyle::GetCurrentCache().GetPtSize() );
				SetCharArray( chRec, false );
			}
		}
		else if ( fStreamPosition == eEndOfStream ) {
			if ( CMctToAPP( scEndStream ) ) {
				chRec.Set( CMctToAPP( scEndStream ),  scCachedStyle::GetCurrentCache().GetPtSize() );
				SetCharArray( chRec, false );
			}
		}
				
		OutputLine();
	}
}
示例#8
0
void NETHER::draw_game(bool shadows)
{
	{
		MINY=-8*zoom;
		MINX=-(10+viewp.z*4)*zoom;
		MAXY=(9+viewp.z*4)*zoom;
		MAXX=8*zoom;
	}

	if (!explosions.EmptyP()) {
		int minstep=128;
		List<EXPLOSION> l;
		EXPLOSION *n;
		float offs=0.0,r;

		l.Instance(explosions);
		l.Rewind();
		while(l.Iterate(n)) {
			if (n->size==2 && n->step<minstep) minstep=n->step;
		} /* while */ 

		r=(128-minstep)/256.0;
		offs=sin(minstep)*r;

		gluLookAt(viewp.x+camera.x*zoom+offs,viewp.y+camera.y*zoom+offs,viewp.z+camera.z*zoom,viewp.x+offs,viewp.y+offs,viewp.z,0,0,1);
	} else {
		gluLookAt(viewp.x+camera.x*zoom,viewp.y+camera.y*zoom,viewp.z+camera.z*zoom,viewp.x,viewp.y,viewp.z,0,0,1);
	} /* if */ 

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

	/* Draw: */ 

	/* Draw the map: */ 
	drawmap(shadows);

	/* Draw the robots and bullets: */ 
	{
		int i;
		List<ROBOT> l;
		List<BULLET> l2;
		ROBOT *r;
		BULLET *b; 

		for(i=0;i<2;i++) {
			l.Instance(robots[i]);
			l.Rewind();
			while(l.Iterate(r)) {
				if (r->pos.y>=(viewp.y+MINY) &&
					r->pos.y<=(viewp.y+MAXY) &&
					r->pos.x>=(viewp.x+MINX) &&
					r->pos.x<=(viewp.x+MAXX)) {
					glPushMatrix();
					glTranslatef(r->pos.x,r->pos.y,r->pos.z);
					DrawRobot(r,i,shadows);
					glPopMatrix();
				} /* if */ 
			} /* while */ 
		} /* for */ 

		l2.Instance(bullets);
		l2.Rewind();
		while(l2.Iterate(b)) {
			if (b->pos.y>=(viewp.y+MINY) &&
				b->pos.y<=(viewp.y+MAXY) &&
				b->pos.x>=(viewp.x+MINX) &&
				b->pos.x<=(viewp.x+MAXX)) {
				glPushMatrix();
				glTranslatef(b->pos.x,b->pos.y,b->pos.z);
				DrawBullet(b,shadows);
				glPopMatrix();
			} /* if */ 
		} /* while */ 
	}

	/* Draw the ship: */ 
	glPushMatrix();
	glTranslatef(shipp.x,shipp.y,shipp.z);
	if (!shadows) ship->draw(0.7,0.7,0.7);
	glPopMatrix();

	if (shadows) {
		float sx,sy;
		float x[2],y[2];
		float minz;
		Vector light;

		light=lightposv;
		light=light/light.z;

		sx=shipp.x-light.x*shipp.z;
		sy=shipp.y-light.y*shipp.z;

		if (controlled==0) {
			x[0]=sx+ship->shdw_cmc.x[0];
			x[1]=sx+ship->shdw_cmc.x[1];
			y[0]=sy+ship->shdw_cmc.y[0];
			y[1]=sy+ship->shdw_cmc.y[1];
			minz=MapMaxZ(x,y);
		} else {
			minz=controlled->pos.z;
		} /* if */ 

		glPushMatrix();
		glTranslatef(sx,sy,minz+0.05);
		if (shadows) ship->DrawShadow(0,0,0,0.5);
		glPopMatrix();
	} 

	/* Draw the extras: */ 
	
	/* Draw nuclear explosions: */ 
	if (!shadows) {
		List<EXPLOSION> l;
		EXPLOSION *n;
		float a,r;

		l.Instance(explosions);
		l.Rewind();
		while(l.Iterate(n)) {
			a=(128.0f-n->step)/80.0f;
			r=1.0;
			if (n->size==0) {
				r=(float(n->step)/512.0f)+0.1;
			} /* if */ 
			if (n->size==1) {
				r=(float(n->step)/96.0f)+0.5;
			} /* if */ 
			if (n->size==2) {
				r=(float(n->step)/48.0f)+1.0;
			} /* if */ 
			if (a<0) a=0;
			if (a>1) a=1;

			glPushMatrix();
			glTranslatef(n->pos.x,n->pos.y,n->pos.z);		
			glColor4f(1.0f,0.5f,0.0,a);
			glDepthMask(GL_FALSE);
			glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
			glEnable(GL_BLEND);
			glutSolidSphere(r,8,8);
			glDisable(GL_BLEND);
			glDepthMask(GL_TRUE);
			glPopMatrix();
		} /* while */ 
	}

	/* Draw the particles: */ 
	if (!shadows) {
		List<PARTICLE> l;
		PARTICLE *p;

		l.Instance(particles);
		l.Rewind();
		while(l.Iterate(p)) {
			if (p->pos.y>=(viewp.y+MINY) &&
				p->pos.y<=(viewp.y+MAXY) &&
				p->pos.x>=(viewp.x+MINX) &&
				p->pos.x<=(viewp.x+MAXX)) DrawParticle(p);
		} /* if */ 

	} /* if */ 

} /* NETHER::draw_screen */ 
int main(void)
{
	//primitive variable
	bool done = false;
	bool redraw = true;
	const int FPS = 60;
	bool isGameOver = false;

	//object variables
	SpaceShip ship;
	Bullet bullets[NUM_BULLETS];
	Comet comets[NUM_COMETS];

	//Allegro variables
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_EVENT_QUEUE *event_queue = NULL;
	ALLEGRO_TIMER *timer = NULL;
	ALLEGRO_FONT *font18 = NULL;

	//Initialization Functions
	if(!al_init())										//initialize Allegro
		return -1;

	display = al_create_display(WIDTH, HEIGHT);			//create our display object

	if(!display)										//test display object
		return -1;

	al_init_primitives_addon();
	al_install_keyboard();
	al_init_font_addon();
	al_init_ttf_addon();

	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / FPS);

	srand(time(NULL));
	InitShip(ship);
	InitBullet(bullets, NUM_BULLETS);
	InitComet(comets, NUM_COMETS);
	
	font18 = al_load_font("arial.ttf", 18, 0);

	al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_display_event_source(display));

	al_start_timer(timer);
	while(!done)
	{
		ALLEGRO_EVENT ev;
		al_wait_for_event(event_queue, &ev);

		if(ev.type == ALLEGRO_EVENT_TIMER)
		{
			redraw = true;
			if(keys[UP])
				MoveShipUp(ship);
			if(keys[DOWN])
				MoveShipDown(ship);
			if(keys[LEFT])
				MoveShipLeft(ship);
			if(keys[RIGHT])
				MoveShipRight(ship);

			if(!isGameOver)
			{
				UpdateBullet(bullets, NUM_BULLETS);
				StartComet(comets, NUM_COMETS);
				UpdateComet(comets, NUM_COMETS);
				CollideBullet(bullets, NUM_BULLETS, comets, NUM_COMETS, ship);
				CollideComet(comets, NUM_COMETS, ship);

				if(ship.lives <= 0)
					isGameOver = true;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch(ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = true;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = true;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = true;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = true;
				FireBullet(bullets, NUM_BULLETS, ship);
				break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch(ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = false;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = false;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = false;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = false;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = false;
				break;
			}
		}

		if(redraw && al_is_event_queue_empty(event_queue))
		{
			redraw = false; 

			if(!isGameOver)
			{
				DrawShip(ship);
				DrawBullet(bullets, NUM_BULLETS);
				DrawComet(comets, NUM_COMETS);

				al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "Player has %i lives left. Player has destroyed %i objects", ship.lives, ship.score);
			}
			else
			{
				al_draw_textf(font18, al_map_rgb(0, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over. Final Score: %i", ship.score);
			}
		
			al_flip_display();
			al_clear_to_color(al_map_rgb(0,0,0));
		}
	}

	al_destroy_event_queue(event_queue);
	al_destroy_timer(timer);
	al_destroy_font(font18);
	al_destroy_display(display);						//destroy our display object

	return 0;
}
示例#10
0
  void Bullet::Draw() {
	DrawBullet(*this);
  }
int main(void)
{
	// don't forget to put allegro-5.0.10-monolith-md-debug.lib
	//primitive variable
	bool done = false;
	bool redraw = true;
	const int FPS = 60;
	bool isGameOver = false;

	//object variables
	spaceShip ship;
	Bullet bullets[NUM_BULLETS];
	Comet comets[NUM_COMETS];
	Explosion explosions[NUM_EXPLOSIONS];

	//Allegro variables
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_EVENT_QUEUE *event_queue = NULL;
	ALLEGRO_TIMER *timer = NULL;
	ALLEGRO_FONT *font18 = NULL;
	ALLEGRO_BITMAP *shipImage;
	ALLEGRO_BITMAP *cometImage;
	ALLEGRO_BITMAP *expImage;
	ALLEGRO_SAMPLE * sample = NULL;
	ALLEGRO_SAMPLE * sample2 = NULL;
	ALLEGRO_SAMPLE * sample3 = NULL;
	ALLEGRO_SAMPLE_INSTANCE * instance1 = NULL;
	ALLEGRO_SAMPLE_INSTANCE * instance2 = NULL;
	ALLEGRO_SAMPLE_INSTANCE * instance3 = NULL;

	//Initialization Functions
	if(!al_init())										//initialize Allegro
		return -1;

	display = al_create_display(WIDTH, HEIGHT);			//create our display object

	if(!display)										//test display object
		return -1;

	al_init_primitives_addon();
	al_install_keyboard();
	al_init_font_addon();
	al_init_ttf_addon();
	al_init_image_addon();
	al_install_audio(); // always initialize audio before the codecs
	al_init_acodec_addon();
	
	al_reserve_samples(10); // reserves numbers of samples/ channels or voices

	sample = al_load_sample("chirp.ogg");
	sample2 = al_load_sample("static.ogg");
	sample3 = al_load_sample("JSS - Our Song.ogg");

	instance1 = al_create_sample_instance(sample);
	instance2 = al_create_sample_instance(sample2);
	instance3 = al_create_sample_instance(sample3);	

	al_set_sample_instance_playmode(instance3, ALLEGRO_PLAYMODE_LOOP);

	al_attach_sample_instance_to_mixer(instance1, al_get_default_mixer());
    al_attach_sample_instance_to_mixer(instance2, al_get_default_mixer());
	al_attach_sample_instance_to_mixer(instance3, al_get_default_mixer());

	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / FPS);

	cometImage = al_load_bitmap("asteroid-1-96.png");
	//above does not need convert_mask_to_alpha because it is tranparent background in sprite sheet
	shipImage = al_load_bitmap("Spaceship_by_arboris.png");
	al_convert_mask_to_alpha(shipImage,al_map_rgb(255,0,255));

	expImage = al_load_bitmap("explosion_3_40_128.png");

	srand(time(NULL));
	InitShip(ship, shipImage);
	InitBullet(bullets, NUM_BULLETS);
	InitComet(comets, NUM_COMETS, cometImage);
	InitExplosions(explosions, NUM_EXPLOSIONS, expImage);

	font18 = al_load_font("arial.ttf", 18, 0);

	al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_display_event_source(display));

	al_play_sample_instance(instance3);

	al_start_timer(timer);
	while(!done)
	{
		ALLEGRO_EVENT ev;
		al_wait_for_event(event_queue, &ev);

		if(ev.type == ALLEGRO_EVENT_TIMER)
		{
			redraw = true;
			int soundX = ship.x;
			int soundY = ship.y;
			
			if(keys[UP])
				MoveShipUp(ship);
			else if(keys[DOWN])
				MoveShipDown(ship);
			else
				ResetShipAnimation(ship,1);
			if(keys[LEFT])
				MoveShipLeft(ship);
			else if(keys[RIGHT])
				MoveShipRight(ship);
			else
				ResetShipAnimation(ship, 2);
			
			if(soundX != ship.x || soundY != ship.y){
				//al_play_sample(sample, 1,0,1, ALLEGRO_PLAYMODE_ONCE, NULL);
				al_play_sample_instance(instance1);
			}
			if (ship.x -10 < 0 || ship.x + 10 > WIDTH || ship.y - 10 < 0 || ship.y + 10 > HEIGHT){
				al_play_sample_instance(instance2);
			}

			if(!isGameOver)
			{
				UpdateExplosions(explosions, NUM_EXPLOSIONS);
				UpdateBullet(bullets, NUM_BULLETS);
				StartComet(comets, NUM_COMETS);
				UpdateComet(comets, NUM_COMETS);
				CollideBullet(bullets, NUM_BULLETS, comets, NUM_COMETS, ship, explosions, NUM_EXPLOSIONS);
				CollideComet(comets, NUM_COMETS, ship, explosions, NUM_BULLETS);

				if(ship.lives <= 0)
					isGameOver = true;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch(ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = true;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = true;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = true;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = true;
				FireBullet(bullets, NUM_BULLETS, ship);
				break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch(ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = false;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = false;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = false;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = false;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = false;
				break;
			}
		}

		if(redraw && al_is_event_queue_empty(event_queue))
		{
			redraw = false; 

			if(!isGameOver)
			{
				DrawShip(ship);
				if(al_get_sample_instance_playing(instance1)){
					al_draw_text(font18, al_map_rgb(255,255,255),5,30,0, "Instance 1 is playing");
				}
				if(al_get_sample_instance_playing(instance2)){
					al_draw_text(font18, al_map_rgb(255,255,255),WIDTH - 5,30,ALLEGRO_ALIGN_RIGHT, "Instance 2 is playing");
				}
				if(al_get_sample_instance_playing(instance3)){
					al_draw_textf(font18, al_map_rgb(255,255,255),5,HEIGHT - 30,0, "Instance 3 is playing: %.1f %%", al_get_sample_instance_position(instance3) / (float)al_get_sample_instance_length(instance3) * 100);
				}

				DrawBullet(bullets, NUM_BULLETS);
				DrawComet(comets, NUM_COMETS);
				DrawExplosions(explosions, NUM_EXPLOSIONS);

				al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "Player has %i lives left. Player has destroyed %i objects", ship.lives, ship.score);
			}
			else
			{
				al_draw_textf(font18, al_map_rgb(0, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over. Final Score: %i", ship.score);
			}
		
			al_flip_display();
			al_clear_to_color(al_map_rgb(0,0,0));
		}
	}

	al_destroy_sample_instance(instance1);
	al_destroy_sample_instance(instance2);
	al_destroy_sample_instance(instance3);
	al_destroy_sample(sample);
	al_destroy_sample(sample2);
	al_destroy_sample(sample3);
	al_destroy_bitmap(expImage);
	al_destroy_bitmap(shipImage);
	al_destroy_bitmap(cometImage);
	al_destroy_event_queue(event_queue);
	al_destroy_timer(timer);
	al_destroy_font(font18);
	al_destroy_display(display);						//destroy our display object

	return 0;
}
示例#12
0
int main(void)
{
	//primitive variable
	const int FPS = 60;
	bool done = false;
	bool redraw = true;
	bool isGameOver = false;

	//object variables
	struct SpaceShip ship;
	struct Bullet bullets[NUM_BULLETS];
	struct Comet comets[NUM_COMETS];

	//Allegro variables
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_EVENT_QUEUE *event_queue = NULL;
	ALLEGRO_TIMER *timer = NULL;
	ALLEGRO_BITMAP *shipImage;
	ALLEGRO_BITMAP *cometImage;
	ALLEGRO_BITMAP *bulletImage;

	ALLEGRO_FONT *font = NULL;
	ALLEGRO_PATH *font_path;

	//Initialization Functions
	if (!al_init())										//initialize Allegro
		return -1;

	display = al_create_display(WIDTH, HEIGHT);		//create our display object

	if (!display)										//test display object
		return -1;

	// inicio inicializacao fontes
	al_init_image_addon();

	font_path = get_resources_path();
	al_set_path_filename(font_path, "a4_font.tga");

	al_init_font_addon();
	al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
	font = al_load_bitmap_font(al_path_cstr(font_path, '/'));
	if (!font)
	{
		return 1;
	}

	al_destroy_path(font_path);
	// termino inicializacao fontes

	al_init_primitives_addon();
	al_install_keyboard();
	al_init_image_addon();

	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / FPS);

	shipImage = al_load_bitmap("spaceship_by_arboris.png");
	al_convert_mask_to_alpha(shipImage, al_map_rgb(255, 0, 255));

	bulletImage = al_load_bitmap("bullets_by_arboris.png");
	al_convert_mask_to_alpha(bulletImage, al_map_rgb(255, 0, 255));

	cometImage = al_load_bitmap("asteroid-1-96.png");

	srand(time(NULL ));

	//Game Init
	ship = InitShip(shipImage);

	// init bullets
	int i = 0;
	while (i < NUM_BULLETS)
	{
		bullets[i].ID = BULLET;
		bullets[i].speed = 10;
		bullets[i].live = false;

		bullets[i].maxFrame = 143;
		bullets[i].curFrame = 0;
		bullets[i].frameCount = 0;
		bullets[i].frameDelay = 2;
		bullets[i].frameWidth = 20;
		bullets[i].frameHeight = 10;
		bullets[i].animationColumns = 1;

		bullets[i].animationRow = 1;

		bullets[i].image = bulletImage;

		i++;
	}

	// init comets
	i = 0;
	while (i < NUM_COMETS)
	{
		comets[i].ID = ENEMY;
		comets[i].live = false;
		comets[i].speed = 5;
		comets[i].boundx = 35;
		comets[i].boundy = 35;

		comets[i].maxFrame = 143;
		comets[i].curFrame = 0;
		comets[i].frameCount = 0;
		comets[i].frameDelay = 2;
		comets[i].frameWidth = 96;
		comets[i].frameHeight = 96;
		comets[i].animationColumns = 21;

		if (rand() % 2)
			comets[i].animationDirection = 1;
		else
			comets[i].animationDirection = -1;

		comets[i].image = cometImage;

		i++;
	}

	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_start_timer(timer);

	while (!done)
	{
		ALLEGRO_EVENT ev;
		al_wait_for_event(event_queue, &ev);

		if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}
		else if (ev.type == ALLEGRO_EVENT_TIMER)
		{
			redraw = true;
			if (keys[UP])
				ship = MoveShipUp(ship);
			else if (keys[DOWN])
				ship = MoveShipDown(ship);
			else
				ship = ResetShipAnimation(ship, 1);

			if (keys[LEFT])
				ship = MoveShipLeft(ship);
			else if (keys[RIGHT])
				ship = MoveShipRight(ship);
			else
				ship = ResetShipAnimation(ship, 2);

			if (!isGameOver)
			{
				//UpdateBullet
				int i = 0;
				while (i < NUM_BULLETS)
				{
					if (bullets[i].live)
					{
						bullets[i].x += bullets[i].speed;
						if (bullets[i].x > WIDTH)
							bullets[i].live = false;
					}

					i++;
				}

				// start comet
				i = 0;
				while (i < NUM_COMETS)
				{
					if (!comets[i].live)
					{
						if (rand() % 500 == 0)
						{
							comets[i].live = true;
							comets[i].x = WIDTH;
							comets[i].y = 30 + rand() % (HEIGHT - 60);

							break;
						}
					}

					i++;
				}

				// UpdateComet(Comet comets[], int size)
				i = 0;
				while (i < NUM_COMETS)
				{
					if (comets[i].live)
					{
						if (++comets[i].frameCount >= comets[i].frameDelay)
						{
							comets[i].curFrame += comets[i].animationDirection;
							if (comets[i].curFrame >= comets[i].maxFrame)
								comets[i].curFrame = 0;
							else if (comets[i].curFrame <= 0)
								comets[i].curFrame = comets[i].maxFrame - 1;

							comets[i].frameCount = 0;
						}

						comets[i].x -= comets[i].speed;
					}

					i++;
				}

				// collide bullet
				i = 0;
				while (i < NUM_BULLETS)
				{
					if (bullets[i].live)
					{
						int j = 0;
						while (j < NUM_COMETS)
						{
							if (comets[j].live)
							{
								if (bullets[i].x
										> (comets[j].x - comets[j].boundx)
										&& bullets[i].x
												< (comets[j].x
														+ comets[j].boundx)
										&& bullets[i].y
												> (comets[j].y
														- comets[j].boundy)
										&& bullets[i].y
												< (comets[j].y
														+ comets[j].boundy))
								{
									bullets[i].live = false;
									comets[j].live = false;

									ship.score++;
								}
							}

							j++;
						}
					}

					i++;
				}

				// collide comet
				i = 0;
				while (i < NUM_COMETS)
				{
					if (comets[i].live)
					{
						if (comets[i].x - comets[i].boundx
								< ship.x + ship.boundx
								&& comets[i].x + comets[i].boundx
										> ship.x - ship.boundx
								&& comets[i].y - comets[i].boundy
										< ship.y + ship.boundy
								&& comets[i].y + comets[i].boundy
										> ship.y - ship.boundy)
						{
							ship.lives--;
							comets[i].live = false;
						}
						else if (comets[i].x < 0)
						{
							comets[i].live = false;
							ship.lives--;
						}
					}

					i++;
				}

				if (ship.lives <= 0)
					isGameOver = true;
			}
		}
		else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch (ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = true;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = true;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = true;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = true;

				//FireBullet
				int i = 0;
				while (i < NUM_BULLETS)
				{
					if (!bullets[i].live)
					{
						bullets[i].x = ship.x + 17;
						bullets[i].y = ship.y;
						bullets[i].live = true;
						break;
					}

					i++;
				}

				break;
			}
		}
		else if (ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch (ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_UP:
				keys[UP] = false;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = false;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = false;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = false;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = false;
				break;
			}
		}

		if (redraw && al_is_event_queue_empty(event_queue))
		{
			redraw = false;

			if (!isGameOver)
			{
				DrawShip(ship);
				DrawBullet(bullets, NUM_BULLETS);
				DrawComet(comets, NUM_COMETS);
				al_draw_textf(font, al_map_rgb(255, 0, 255), 5, 5, 0,
						"Player has %i lives left. Player has destroyed %i objects",
						ship.lives, ship.score);
			}
			else
			{
				al_draw_textf(font, al_map_rgb(0, 255, 255), WIDTH / 2,
						HEIGHT / 2, ALLEGRO_ALIGN_CENTRE,
						"Game Over. Final Score: %i", ship.score);
			}

			al_flip_display();
			al_clear_to_color(al_map_rgb(0, 0, 0));
		}
	}

	al_destroy_bitmap(bulletImage);
	al_destroy_bitmap(cometImage);
	al_destroy_bitmap(shipImage);
	al_destroy_event_queue(event_queue);
	al_destroy_timer(timer);
	al_destroy_font(font);
	al_destroy_display(display);					//destroy our display object

	return 0;
}