Untitled
raw download clone
C
views 16
,
size 15957 b
#include <stdio.h>
#include <stdlib.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>

#define GAME_TERMINATE 666

ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_BITMAP *image = NULL;   //Setting
ALLEGRO_EVENT event;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_TIMER *timer2 = NULL;
ALLEGRO_TIMER *timer3 = NULL;
ALLEGRO_FONT *font = NULL;

const float FPS = 60;
const int width = 800;
const int height = 600;
bool judge_next_window = false;
int window = 1;
int launchskill1 = 0;
int launchskill2 = 0;
typedef struct
{
    int x;
    int y;
    int hp;
    int mp;
    int atk;
    int dir;
    ALLEGRO_BITMAP *image_path;
} Character;

Character player;
Character minions;  //Setting

typedef struct
{
    double x;
    double y;
    double dir;
    int go;
} skill1_location;

skill1_location num1_skill1;

void show_err_msg(int msg);
void game_init();
void game_begin();
int process_event();
int game_run();
void game_destroy();
void showplayerhp();
void showplayermp();

int main(int argc, char *argv[])
{
    int msg = 0;

    game_init();
    game_begin();
    printf("Game on\n");

    while (msg != GAME_TERMINATE)
    {
        msg = game_run();
        if (msg == GAME_TERMINATE)
            printf("See you, world\n");
    }
    game_destroy();
    return 0;
}

void show_err_msg(int msg)
{
    fprintf(stderr, "unexpected msg: %d", msg);
    game_destroy();
    exit(9);
}

void game_init()
{
    if (!al_init())
    {
        show_err_msg(-1);
    }

    display = al_create_display(width, height);
    event_queue = al_create_event_queue();

    if (display == NULL || event_queue == NULL)
    {
        show_err_msg(-1);
    }

    // Initialize Allegro settings
    al_init_primitives_addon();
    al_install_keyboard();
    al_install_mouse();
    al_init_image_addon();
    al_init_acodec_addon(); // Music
    al_init_font_addon();
    al_init_ttf_addon();

    // Register event
    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_mouse_event_source());
}

void game_begin()
{
    al_clear_to_color(al_map_rgb(100, 100, 100));
    font = al_load_ttf_font("pirulen.ttf", 12, 0);
    al_draw_text(font, al_map_rgb(255, 255, 255), width / 2, (height / 2) + 220, ALLEGRO_ALIGN_CENTRE, "Press 'Enter' to start");
    al_draw_rectangle((width / 2) - 150, 510, (width / 2) + 150, 550, al_map_rgb(255, 255, 255), 0);
    al_flip_display();
}

int process_event()
{
    al_wait_for_event(event_queue, &event);

    if (event.timer.source == timer3)   //Setting
    {
        // Player Fall
        if (player.y + 30 < (height / 4) * 3)
            player.y += 10;

        // Mon1 Movement
        if (minions.x + 50 == width)
            minions.dir = 0;
        else if (minions.x == 0)
            minions.dir = 1;
        else if (minions.x % 150 == 0)
            minions.dir = rand() % 2;
        if (minions.dir == 0)   //Setting
        {
            minions.x -= 10;
            minions.image_path = al_load_bitmap("Minions_L.png");
        }
        else    //Setting
        {
            minions.x += 10;
            minions.image_path = al_load_bitmap("Minions_R.png");
        }
    }

    // Keyboard
    if (event.type == ALLEGRO_EVENT_KEY_UP)
    {
        switch (event.keyboard.keycode)
        {
        // Control
        case ALLEGRO_KEY_W:
            if (player.y + 30 < (height / 4) * 3)
                break;
            player.y -= 120;
            break;
        case ALLEGRO_KEY_SPACE:
            if (player.y + 30 < (height / 4) * 3)
                break;
            player.y -= 120;
            break;
        case ALLEGRO_KEY_A:
            player.x -= 45;
            break;
        case ALLEGRO_KEY_D:
            player.x += 45;
            break;

        // For Start Menu
        case ALLEGRO_KEY_ESCAPE:
            return GAME_TERMINATE;

        case ALLEGRO_KEY_ENTER:
            judge_next_window = true;
            break;
        }
    }

    // Mouse
    if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
    {
        switch (event.mouse.button)
        {
        case 1:
            if (player.mp >= 2)
            {
                player.mp -= 2;
                launchskill1 = 1;
                num1_skill1.dir = (double)(player.y - event.mouse.y) / (event.mouse.x - player.x);
                printf("num1_skill1.dir = %f\n", num1_skill1.dir);
                if (event.mouse.x >= player.x)
                    num1_skill1.go = 1;
                else
                    num1_skill1.go = 0;
            }
            break;
        case 2:
            if (player.mp >= 4)
            {
                player.mp -= 4;
                launchskill2 = 1;
            }
            break;
        }
    }

    // Shutdown our program
    else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        return GAME_TERMINATE;

    return 0;
}

int game_run()
{
    int error = 0;

    //First window
    if (window == 1)
    {
        if (!al_is_event_queue_empty(event_queue))
        {
            error = process_event();
            if (judge_next_window)
            {
                window = 2;

                player.x = width / 2;
                player.y = height / 2;
                minions.x = width - 50;
                minions.y = (height / 4) * 3 - 50;

                player.hp = 20;
                player.mp = 20;

                al_clear_to_color(al_map_rgb(0, 0, 0));
                al_draw_filled_rectangle(player.x, player.y, player.x + 30, player.y + 30, al_map_rgb(255, 0, 255));
                al_draw_filled_rectangle(0, (height / 4) * 3, width, (height / 4) * 3 + 1, al_map_rgb(255, 255, 255));

                // HP
                al_draw_text(font, al_map_rgb(255, 255, 255), (width / 2) - 350, 490, ALLEGRO_ALIGN_CENTRE, "Hp");
                al_draw_rectangle((width / 2) - 350, 510, (width / 2) - 50, 550, al_map_rgb(255, 255, 255), 0);
                showplayerhp();

                // Mp
                al_draw_text(font, al_map_rgb(255, 255, 255), (width / 2) + 50, 490, ALLEGRO_ALIGN_CENTRE, "Mp");
                al_draw_rectangle((width / 2) + 350, 510, (width / 2) + 50, 550, al_map_rgb(255, 255, 255), 0);
                showplayermp();

                al_flip_display();

                // Initialize Timer
                timer = al_create_timer(1.0);
                timer2 = al_create_timer(1.0 / 10.0);
                timer3 = al_create_timer(1.0 / 15.0);
                al_register_event_source(event_queue, al_get_timer_event_source(timer));
                al_register_event_source(event_queue, al_get_timer_event_source(timer2));
                al_register_event_source(event_queue, al_get_timer_event_source(timer3));
                al_start_timer(timer);
                al_start_timer(timer2);
                al_start_timer(timer3);
            }
        }
    }

    if (window == 2)
    {
        if (!al_is_event_queue_empty(event_queue))
        {
            error = process_event();
            al_clear_to_color(al_map_rgb(0, 0, 0));
            al_draw_filled_rectangle(player.x, player.y, player.x + 30, player.y + 30, al_map_rgb(255, 0, 255));   //player
            al_draw_filled_rectangle(0, (height / 4) * 3, width, (height / 4) * 3 + 1, al_map_rgb(255, 255, 255)); //floor
            if (minions.image_path != NULL) //Setting
                al_draw_bitmap(minions.image_path, minions.x-50, minions.y-50, 0);

            // Hp
            al_draw_text(font, al_map_rgb(255, 255, 255), (width / 2) - 350, 490, ALLEGRO_ALIGN_CENTRE, "Hp");
            al_draw_rectangle((width / 2) - 350, 510, (width / 2) - 50, 550, al_map_rgb(255, 255, 255), 0);
            showplayerhp();

            // Mp
            al_draw_text(font, al_map_rgb(255, 255, 255), (width / 2) + 50, 490, ALLEGRO_ALIGN_CENTRE, "Mp");
            al_draw_rectangle((width / 2) + 350, 510, (width / 2) + 50, 550, al_map_rgb(255, 255, 255), 0);
            showplayermp();

            // Skill1_num1
            if (launchskill1 == 1)
            {
                launchskill1 = 2;
                num1_skill1.x = player.x;
                num1_skill1.y = player.y;
                al_draw_filled_circle(num1_skill1.x, num1_skill1.y, 10, al_map_rgb(0, 255, 0));
            }

            if (launchskill1 == 2)
            {
                if (num1_skill1.go)
                {
                    num1_skill1.x += 1;
                    num1_skill1.y -= num1_skill1.dir;
                }
                else
                {
                    num1_skill1.x -= 1;
                    num1_skill1.y += num1_skill1.dir;
                }
                al_draw_filled_circle(num1_skill1.x, num1_skill1.y, 10, al_map_rgb(0, 255, 0));
            }

            al_flip_display();
        }
    }

    return error;
}

void showplayerhp()
{
    if (player.hp >= 1)
        al_draw_filled_rectangle((width / 2) - 340, 520, (width / 2) - 340 + 10, 540, al_map_rgb(255, 0, 0)); //1
    if (player.hp >= 2)
        al_draw_filled_rectangle((width / 2) - 340 + 14, 520, (width / 2) - 340 + 10 + 14, 540, al_map_rgb(255, 0, 0)); //2
    if (player.hp >= 3)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 2, 520, (width / 2) - 340 + 10 + 14 * 2, 540, al_map_rgb(255, 0, 0)); //3
    if (player.hp >= 4)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 3, 520, (width / 2) - 340 + 10 + 14 * 3, 540, al_map_rgb(255, 0, 0)); //4
    if (player.hp >= 5)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 4, 520, (width / 2) - 340 + 10 + 14 * 4, 540, al_map_rgb(255, 0, 0)); //5
    if (player.hp >= 6)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 5, 520, (width / 2) - 340 + 10 + 14 * 5, 540, al_map_rgb(255, 0, 0)); //6
    if (player.hp >= 7)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 6, 520, (width / 2) - 340 + 10 + 14 * 6, 540, al_map_rgb(255, 0, 0)); //7
    if (player.hp >= 8)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 7, 520, (width / 2) - 340 + 10 + 14 * 7, 540, al_map_rgb(255, 0, 0)); //8
    if (player.hp >= 9)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 8, 520, (width / 2) - 340 + 10 + 14 * 8, 540, al_map_rgb(255, 0, 0)); //9
    if (player.hp >= 10)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 9, 520, (width / 2) - 340 + 10 + 14 * 9, 540, al_map_rgb(255, 0, 0)); //10
    if (player.hp >= 11)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 10, 520, (width / 2) - 340 + 10 + 14 * 10, 540, al_map_rgb(255, 0, 0)); //11
    if (player.hp >= 12)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 11, 520, (width / 2) - 340 + 10 + 14 * 11, 540, al_map_rgb(255, 0, 0)); //12
    if (player.hp >= 13)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 12, 520, (width / 2) - 340 + 10 + 14 * 12, 540, al_map_rgb(255, 0, 0)); //13
    if (player.hp >= 14)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 13, 520, (width / 2) - 340 + 10 + 14 * 13, 540, al_map_rgb(255, 0, 0)); //14
    if (player.hp >= 15)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 14, 520, (width / 2) - 340 + 10 + 14 * 14, 540, al_map_rgb(255, 0, 0)); //15
    if (player.hp >= 16)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 15, 520, (width / 2) - 340 + 10 + 14 * 15, 540, al_map_rgb(255, 0, 0)); //16
    if (player.hp >= 17)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 16, 520, (width / 2) - 340 + 10 + 14 * 16, 540, al_map_rgb(255, 0, 0)); //17
    if (player.hp >= 18)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 17, 520, (width / 2) - 340 + 10 + 14 * 17, 540, al_map_rgb(255, 0, 0)); //18
    if (player.hp >= 19)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 18, 520, (width / 2) - 340 + 10 + 14 * 18, 540, al_map_rgb(255, 0, 0)); //19
    if (player.hp >= 20)
        al_draw_filled_rectangle((width / 2) - 340 + 14 * 19, 520, (width / 2) - 340 + 10 + 14 * 19, 540, al_map_rgb(255, 0, 0)); //20
}

void showplayermp()
{
    if (player.mp >= 1)
        al_draw_filled_rectangle((width / 2) + 60, 520, (width / 2) + 60 + 10, 540, al_map_rgb(0, 0, 255)); //1
    if (player.mp >= 2)
        al_draw_filled_rectangle((width / 2) + 60 + 14, 520, (width / 2) + 60 + 10 + 14, 540, al_map_rgb(0, 0, 255)); //2
    if (player.mp >= 3)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 2, 520, (width / 2) + 60 + 10 + 14 * 2, 540, al_map_rgb(0, 0, 255)); //3
    if (player.mp >= 4)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 3, 520, (width / 2) + 60 + 10 + 14 * 3, 540, al_map_rgb(0, 0, 255)); //4
    if (player.mp >= 5)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 4, 520, (width / 2) + 60 + 10 + 14 * 4, 540, al_map_rgb(0, 0, 255)); //5
    if (player.mp >= 6)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 5, 520, (width / 2) + 60 + 10 + 14 * 5, 540, al_map_rgb(0, 0, 255)); //6
    if (player.mp >= 7)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 6, 520, (width / 2) + 60 + 10 + 14 * 6, 540, al_map_rgb(0, 0, 255)); //7
    if (player.mp >= 8)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 7, 520, (width / 2) + 60 + 10 + 14 * 7, 540, al_map_rgb(0, 0, 255)); //8
    if (player.mp >= 9)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 8, 520, (width / 2) + 60 + 10 + 14 * 8, 540, al_map_rgb(0, 0, 255)); //9
    if (player.mp >= 10)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 9, 520, (width / 2) + 60 + 10 + 14 * 9, 540, al_map_rgb(0, 0, 255)); //10
    if (player.mp >= 11)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 10, 520, (width / 2) + 60 + 10 + 14 * 10, 540, al_map_rgb(0, 0, 255)); //11
    if (player.mp >= 12)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 11, 520, (width / 2) + 60 + 10 + 14 * 11, 540, al_map_rgb(0, 0, 255)); //12
    if (player.mp >= 13)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 12, 520, (width / 2) + 60 + 10 + 14 * 12, 540, al_map_rgb(0, 0, 255)); //13
    if (player.mp >= 14)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 13, 520, (width / 2) + 60 + 10 + 14 * 13, 540, al_map_rgb(0, 0, 255)); //14
    if (player.mp >= 15)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 14, 520, (width / 2) + 60 + 10 + 14 * 14, 540, al_map_rgb(0, 0, 255)); //15
    if (player.mp >= 16)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 15, 520, (width / 2) + 60 + 10 + 14 * 15, 540, al_map_rgb(0, 0, 255)); //16
    if (player.mp >= 17)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 16, 520, (width / 2) + 60 + 10 + 14 * 16, 540, al_map_rgb(0, 0, 255)); //17
    if (player.mp >= 18)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 17, 520, (width / 2) + 60 + 10 + 14 * 17, 540, al_map_rgb(0, 0, 255)); //18
    if (player.mp >= 19)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 18, 520, (width / 2) + 60 + 10 + 14 * 18, 540, al_map_rgb(0, 0, 255)); //19
    if (player.mp >= 20)
        al_draw_filled_rectangle((width / 2) + 60 + 14 * 19, 520, (width / 2) + 60 + 10 + 14 * 19, 540, al_map_rgb(0, 0, 255)); //20
}

void game_destroy()
{
    al_destroy_event_queue(event_queue);
    al_destroy_display(display);
    al_destroy_bitmap(image);
}
close fullscreen
Login or Register to edit or fork this paste. It's free.