Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_acodec.h>
- #include <allegro5/allegro_audio.h>
- #include <allegro5/allegro_color.h>
- #include <allegro5/allegro_font.h>
- #include <allegro5/allegro_image.h>
- #include <allegro5/allegro_primitives.h>
- #include <allegro5/allegro_native_dialog.h>
- #include <allegro5/allegro_ttf.h>
- #include <iostream>
- #include <windows.h>
- #include <stdio.h>
- #include <cmath>
- #include <time.h>
- using namespace std;
- int screen_width = 800;
- int screen_height = 600;
- int main()
- {
- enum Direction { DOWN, LEFT, RIGHT, UP };
- const float frameFPS = 10.0;
- if (!al_init())
- {
- fprintf(stderr, "Can't load allegro\n");
- exit(1);
- }
- ALLEGRO_DISPLAY* display = al_create_display(screen_width, screen_height);
- if (!display) {
- fprintf(stderr, "Can't load display\n");
- exit(1);
- }
- al_set_window_position(display, 200, 200);
- al_set_window_title(display, "Have Time ? Let's PLAY !");
- al_init_primitives_addon();
- al_install_keyboard();
- al_install_mouse();
- al_init_image_addon();
- al_init_font_addon();
- al_init_ttf_addon();
- al_install_audio();
- al_init_acodec_addon();
- ALLEGRO_TIMER* timer = al_create_timer(1.0 / 10);
- //snake
- ALLEGRO_TIMER* timerS = al_create_timer(1.0 / 10);
- ALLEGRO_TIMER* timerK = al_create_timer(1.0 / 50);
- ALLEGRO_TIMER* timerF = al_create_timer(1.0 / 60);
- ALLEGRO_TIMER* frameTimer = al_create_timer(1.0 / frameFPS);
- ALLEGRO_TIMER* game_time = al_create_timer(1);
- ALLEGRO_KEYBOARD_STATE keyState;
- ALLEGRO_FONT* introF1 = al_load_font("C:/Users/Windica/source/repos/Menu/Fonts/intro1.ttf", 100, NULL);
- if (!introF1) {
- fprintf(stderr, "Can't load font\n");
- exit(1);
- }
- ALLEGRO_FONT* introF2 = al_load_ttf_font("C:/Users/Windica/source/repos/snake3/snake.ttf", 140, NULL);
- ALLEGRO_FONT* menu_selectF = al_load_font("C:/Users/Windica/source/repos/Menu/Fonts/intro2.ttf", 30, NULL);
- ALLEGRO_FONT* menuF = al_load_font("C:/Users/Windica/source/repos/Menu/Fonts/intro2.ttf", 50, NULL);
- ALLEGRO_FONT* click = al_load_font("C:/Users/Windica/source/repos/Menu/Fonts/intro2.ttf", 30, NULL);
- //Snake
- ALLEGRO_FONT* font = al_load_ttf_font("C:/Users/Windica/source/repos/snake3/snake.ttf", 50, NULL);
- ALLEGRO_FONT* font1 = al_load_ttf_font("C:/Users/Windica/source/repos/snake3/snake.ttf", 180, NULL);
- ALLEGRO_SAMPLE* menuSample = al_load_sample("C:/Users/Windica/source/repos/Menu/Sounds/Menu.wav");
- if (!menuSample) {
- fprintf(stderr, "Can't load intro sound\n");
- exit(1);
- }
- ALLEGRO_SAMPLE_ID menuSampleID;
- //Snake
- ALLEGRO_SAMPLE* eat = al_load_sample("C:/Users/Windica/source/repos/snake3/eat.wav");
- ALLEGRO_SAMPLE* mushroom_eat = al_load_sample("C:/Users/Windica/source/repos/snake3/mushroom_eat.wav");
- ALLEGRO_SAMPLE* snakeSample = al_load_sample("C:/Users/Windica/source/repos/snake3/background_music.wav");
- ALLEGRO_SAMPLE* fail = al_load_sample("C:/Users/Windica/source/repos/snake3/fail.wav");
- ALLEGRO_SAMPLE* birdUP = al_load_sample("C:/Users/Windica/source/repos/menu/Sounds/birdupS.wav");
- ALLEGRO_SAMPLE* birdSCORE = al_load_sample("C:/Users/Windica/source/repos/menu/Sounds/birdscoreS.wav");
- ALLEGRO_SAMPLE_ID snakeSampleID;
- ALLEGRO_SAMPLE_ID birdUPID;
- al_reserve_samples(7);
- ALLEGRO_BITMAP* do_not_disturb = al_load_bitmap("C:/Users/Windica/source/repos/Menu/Images/do_not_disturb.png");
- al_convert_mask_to_alpha(do_not_disturb, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* background = al_load_bitmap("C:/Users/Windica/source/repos/Menu/Images/background.png");
- ALLEGRO_BITMAP* bird = al_load_bitmap("C:/Users/Windica/source/repos/Menu/Images/bird.png");
- al_convert_mask_to_alpha(bird, al_map_rgb(255, 255, 255));
- //Snake
- ALLEGRO_BITMAP* headDOWN = al_load_bitmap("C:/Users/Windica/source/repos/snake3/headDOWN.png");
- al_convert_mask_to_alpha(headDOWN, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* headLEFT = al_load_bitmap("C:/Users/Windica/source/repos/snake3/headLEFT.png");
- al_convert_mask_to_alpha(headLEFT, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* headRIGHT = al_load_bitmap("C:/Users/Windica/source/repos/snake3/headRIGHT.png");
- al_convert_mask_to_alpha(headRIGHT, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* headUP = al_load_bitmap("C:/Users/Windica/source/repos/snake3/headUP.png");
- al_convert_mask_to_alpha(headUP, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* bodyDOWN = al_load_bitmap("C:/Users/Windica/source/repos/snake3/bodyDOWN.png");
- al_convert_mask_to_alpha(bodyDOWN, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* bodyLEFT = al_load_bitmap("C:/Users/Windica/source/repos/snake3/bodyLEFT.png");
- al_convert_mask_to_alpha(bodyLEFT, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* bodyRIGHT = al_load_bitmap("C:/Users/Windica/source/repos/snake3/bodyRIGHT.png");
- al_convert_mask_to_alpha(bodyRIGHT, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* bodyUP = al_load_bitmap("C:/Users/Windica/source/repos/snake3/bodyUP.png");
- al_convert_mask_to_alpha(bodyUP, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* logo = al_load_bitmap("C:/Users/Windica/source/repos/snake3/snake_logo.png");
- al_convert_mask_to_alpha(logo, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* rabbit = al_load_bitmap("C:/Users/Windica/source/repos/snake3/rabbit.png");
- al_convert_mask_to_alpha(rabbit, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* gold_rabbit = al_load_bitmap("C:/Users/Windica/source/repos/snake3/goldrabbit.png");
- al_convert_mask_to_alpha(gold_rabbit, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* mushroom = al_load_bitmap("C:/Users/Windica/source/repos/snake3/mushroom.png");
- al_convert_mask_to_alpha(mushroom, al_map_rgb(255, 255, 255));
- ALLEGRO_BITMAP* frog = al_load_bitmap("C:/Users/Windica/source/repos/snake3/frog.png");
- al_convert_mask_to_alpha(frog, al_map_rgb(255, 255, 255));
- ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
- 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_timer_event_source(timerS));
- al_register_event_source(event_queue, al_get_timer_event_source(timerK));
- al_register_event_source(event_queue, al_get_timer_event_source(timerF));
- al_register_event_source(event_queue, al_get_timer_event_source(frameTimer));
- al_register_event_source(event_queue, al_get_timer_event_source(game_time));
- al_register_event_source(event_queue, al_get_display_event_source(display));
- al_start_timer(timer);
- al_start_timer(timerS);
- al_start_timer(frameTimer);
- al_start_timer(game_time);
- srand(time(NULL));
- bool doneM = false;
- bool intro = true;
- bool menu = false;
- bool drawM = true;
- bool snake = false;
- bool kwadrat = false;
- bool flip = false;
- int Mx = 0;
- int My = 0;
- int timeI = 0;
- while (!doneM) {
- ALLEGRO_EVENT events;
- al_wait_for_event(event_queue, &events);
- if (events.type == ALLEGRO_EVENT_KEY_UP)
- {
- switch (events.keyboard.keycode) {
- case ALLEGRO_KEY_ESCAPE:
- doneM = true;
- break;
- case ALLEGRO_KEY_ENTER:
- break;
- }
- }
- else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
- doneM = true;
- else if (events.type == ALLEGRO_EVENT_MOUSE_AXES)
- {
- Mx = events.mouse.x;
- My = events.mouse.y;
- }
- else if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
- if (events.mouse.button & 1) {
- if (menu == true && Mx >= 360 && Mx < 500 && My >= 160 && My < 210)
- {
- al_stop_sample(&menuSampleID);
- snake = true;
- menu = false;
- }
- if (menu == true && Mx >= 360 && Mx < 500 && My >= 260 && My < 310)
- {
- al_stop_sample(&menuSampleID);
- kwadrat = true;
- menu = false;
- }
- if (menu == true && Mx >= 360 && Mx < 500 && My >= 360 && My < 410)
- {
- al_stop_sample(&menuSampleID);
- flip = true;
- menu = false;
- }
- }
- }
- else if (events.type == ALLEGRO_EVENT_TIMER) {
- if (events.timer.source == timer) {
- drawM = true;
- if (drawM == true)
- {
- if (intro == true)
- {
- al_play_sample(menuSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &menuSampleID);
- for (int i = 0; i < 8500; i++) {
- al_draw_text(introF2, al_map_rgb(255, 0, 255), 130, 200, NULL, "HAVE TIME ?");
- al_flip_display();
- }
- al_draw_text(introF1, al_map_rgb(255, 0, 255), 130, 200, NULL, "LET'S PLAY !");
- al_flip_display();
- al_clear_to_color(al_map_rgb(0, 0, 0));
- do {
- al_get_keyboard_state(&keyState);
- al_draw_text(introF1, al_map_rgb(255, 0, 255), 130, 200, NULL, "LET'S PLAY !");
- al_draw_text(click, al_map_rgb(255, 0, 255), 170, 400, NULL, "Press the button to START !");
- al_wait_for_event(event_queue, &events);
- al_flip_display();
- al_clear_to_color(al_map_rgb(0, 0, 0));
- } while (events.type != ALLEGRO_EVENT_KEY_DOWN);
- intro = false;
- menu = true;
- }
- if (menu == true)
- {
- al_draw_bitmap(do_not_disturb, 740, 510, NULL);
- al_draw_text(menuF, al_map_rgb(255, 0, 255), 190, 50, NULL, "GAME SELECT MENU");
- al_draw_text(menu_selectF, al_map_rgb(255, 0, 255), 380, 170, NULL, "SNAKE");
- al_draw_text(menu_selectF, al_map_rgb(255, 0, 255), 380, 270, NULL, "CHILL");
- al_draw_text(menu_selectF, al_map_rgb(255, 0, 255), 380, 370, NULL, "Flappy Bird");
- }
- if (snake == true)
- {
- al_set_window_position(display, 200, 200);
- al_set_window_title(display, "HTLP - Snake");
- bool doneS = false, activeS = false;
- bool drawS = true;
- int x = 0, y = 0, moveSpeed = 5;
- al_play_sample(snakeSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &snakeSampleID);
- int eventT = 0;
- int timeS = 0;
- int timeF = 0;
- int dir = DOWN, prevdir = DOWN;
- int score = 1;
- int lastX;
- int lastY;
- char finalscore[100];
- int rabbitX = 40 * (rand() % 20);
- int rabbitY = 40 * (rand() % 15);
- int gold_rabbitX = 40 * (rand() % 20);
- int gold_rabbitY = 40 * (rand() % 15);
- int mushroomX = 40 * (rand() % 20);
- int mushroomY = 40 * (rand() % 15);
- int frogX = 40 * (rand() % 20);
- int frogY = 40 * (rand() % 15);
- int snakeT[150];
- for (int i = 0; i <= 149; i++) {
- snakeT[i] = 0;
- }
- int snakeX[150], snakeY[150];
- bool menuS = true;
- bool dead = false;
- bool drawgolden = false;
- while (!doneS) {
- lastX = x;
- lastY = y;
- ALLEGRO_EVENT events;
- al_wait_for_event(event_queue, &events);
- if (events.type == ALLEGRO_EVENT_KEY_UP)
- {
- al_get_keyboard_state(&keyState);
- switch (events.keyboard.keycode) {
- case ALLEGRO_KEY_ESCAPE:
- al_stop_sample(&snakeSampleID);
- doneS = true;
- snake = false;
- menu = true;
- break;
- case ALLEGRO_KEY_ENTER:
- if (menuS) menuS = false, score = 1, timeS = 0, x = 0, y = 0;
- break;
- }
- }
- else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
- doneS = true;
- snake = false;
- doneM = true;
- break;
- }
- if (events.type == ALLEGRO_EVENT_TIMER) {
- if (events.timer.source == game_time) {
- timeS++;
- eventT++;
- }
- if (events.timer.source == timer) {
- al_get_keyboard_state(&keyState);
- if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT) && dir != LEFT)
- dir = RIGHT;
- else if (al_key_down(&keyState, ALLEGRO_KEY_LEFT) && dir != RIGHT)
- dir = LEFT;
- else if (al_key_down(&keyState, ALLEGRO_KEY_UP) && dir != DOWN)
- dir = UP;
- else if (al_key_down(&keyState, ALLEGRO_KEY_DOWN) && dir != UP)
- dir = DOWN;
- else if (al_key_down(&keyState, ALLEGRO_KEY_A))
- score++;
- else if (al_key_down(&keyState, ALLEGRO_KEY_ENTER) && menu == true)
- menu = false, score = 1, timeS = 0, x = 0, y = 0;
- if (menu == false) {
- if (score != 0) {
- for (int i = score; i > 0; i--) {
- snakeX[i] = snakeX[i - 1];
- snakeY[i] = snakeY[i - 1];
- }
- snakeX[0] = lastX;
- snakeY[0] = lastY;
- }
- }
- switch (dir) {
- case RIGHT: x = x + 40;
- break;
- case LEFT: x = x - 40;
- break;
- case UP: y = y - 40;
- break;
- case DOWN: y = y + 40;
- break;
- }
- if (x == rabbitX && y == rabbitY) {
- al_play_sample(eat, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
- score++;
- rabbitX = 40 * (rand() % 20);
- rabbitY = 40 * (rand() % 15);
- if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
- rabbitX = 40 * (rand() % 20);
- rabbitY = 40 * (rand() % 15);
- }
- snakeT[score] = 1;
- }
- if (x == frogX && y == frogY) {
- al_play_sample(eat, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
- score--;
- frogX = 40 * (rand() % 20);
- frogY = 40 * (rand() % 15);
- if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
- frogX = 40 * (rand() % 20);
- frogY = 40 * (rand() % 15);
- }
- snakeT[score] = 0;
- }
- if (x == mushroomX && y == mushroomY) {
- al_play_sample(mushroom_eat, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
- score = score - 5;
- mushroomX = 40 * (rand() % 20);
- mushroomY = 40 * (rand() % 15);
- if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || mushroomX == frogX && mushroomY == frogY) {
- mushroomX = 40 * (rand() % 20);
- mushroomY = 40 * (rand() % 15);
- }
- snakeT[score] = 0;
- }
- if (x == gold_rabbitX && y == gold_rabbitY) {
- al_play_sample(eat, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
- score = score + 3;
- drawgolden = false;
- gold_rabbitX = 40 * (rand() % 20);
- gold_rabbitY = 40 * (rand() % 15);
- if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
- gold_rabbitX = 40 * (rand() % 20);
- gold_rabbitY = 40 * (rand() % 15);
- }
- snakeT[score] = 1;
- }
- if (eventT % 6 == 0) {
- eventT++;
- mushroomX = 40 * (rand() % 20);
- mushroomY = 40 * (rand() % 15);
- if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
- mushroomX = 40 * (rand() % 20);
- mushroomY = 40 * (rand() % 15);
- }
- }
- if (eventT % 15 == 0) {
- eventT++;
- drawgolden = true;
- gold_rabbitX = 40 * (rand() % 20);
- gold_rabbitY = 40 * (rand() % 15);
- if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
- gold_rabbitX = 40 * (rand() % 20);
- gold_rabbitY = 40 * (rand() % 15);
- }
- }
- if (eventT % 5 == 0) {
- drawgolden = false;
- }
- if (menuS == false) {
- for (int i = 1; i < score; i++) {
- if (x == snakeX[i] && y == snakeY[i] && menuS == false) {
- al_play_sample(fail, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
- sprintf_s(finalscore, "%d", score - 1);
- al_show_native_message_box(display, "GAME OVER", "GAME OVER - your score", finalscore, NULL, ALLEGRO_MESSAGEBOX_ERROR);
- dead = true;
- al_rest(2.0);
- }
- }
- if (x < 0 || x >= screen_width || y < 0 || y >= screen_height && menuS == false) {
- al_play_sample(fail, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
- sprintf_s(finalscore, "%d", score - 1);
- al_show_native_message_box(display, "GAME OVER", "GAME OVER - your score", finalscore, NULL, ALLEGRO_MESSAGEBOX_ERROR);
- dead = true;
- al_rest(2.0);
- }
- }
- drawS = true;
- }
- }
- prevdir = dir;
- if (dead && menuS == false) {
- menuS = true;
- timeF = timeS;
- x = 0, y = 0;
- for (int i = 0; i <= 149; i++) {
- snakeT[i] = 0;
- }
- dead = false;
- dir = DOWN;
- }
- if (drawS == true) {
- drawS = false;
- if (menuS) {
- x = 0, y = 0;
- for (int i = 0; i <= 149; i++) {
- snakeT[i] = 0;
- }
- al_draw_bitmap(logo, 0, 200, NULL);
- al_draw_bitmap(logo, 550, 200, NULL);
- al_draw_text(font1, al_map_rgb(255, 0, 0), 230, 10, 0, "SNAKE");
- al_draw_text(font, al_map_rgb(100, 50, 250), 225, 220, 0, "Press Enter to Start");
- al_draw_text(font, al_map_rgb(100, 50, 250), 245, 320, 0, "Press Esc to Exit");;
- al_draw_text(font, al_map_rgb(250, 0, 250), 0, 430, 0, "rabbit = +1");
- al_draw_text(font, al_map_rgb(250, 0, 250), 0, 470, 0, "Gold rabbit = +3");
- al_draw_text(font, al_map_rgb(250, 0, 250), 0, 510, 0, "frog = -1");
- al_draw_text(font, al_map_rgb(250, 0, 250), 0, 550, 0, "mushroom = -5");
- al_draw_textf(font, al_map_rgb(250, 0, 250), 650, 550, 0, "Score: %i", score - 1);
- }
- else {
- al_draw_bitmap(mushroom, mushroomX, mushroomY, NULL);
- al_draw_bitmap(rabbit, rabbitX, rabbitY, NULL);
- al_draw_bitmap(frog, frogX, frogY, NULL);
- if (drawgolden == true) {
- al_draw_bitmap(gold_rabbit, gold_rabbitX, gold_rabbitY, NULL);
- }
- switch (prevdir) {
- case RIGHT: for (int i = 0; i < score; i++) {
- al_draw_bitmap(bodyRIGHT, snakeX[i], snakeY[i], NULL);
- }
- break;
- case LEFT: for (int i = 0; i < score; i++) {
- al_draw_bitmap(bodyRIGHT, snakeX[i], snakeY[i], NULL);
- }
- break;
- case UP: for (int i = 0; i < score; i++) {
- al_draw_bitmap(bodyUP, snakeX[i], snakeY[i], NULL);
- }
- break;
- case DOWN: for (int i = 0; i < score; i++) {
- al_draw_bitmap(bodyUP, snakeX[i], snakeY[i], NULL);
- }
- break;
- }
- switch (prevdir) {
- case RIGHT: al_draw_bitmap(headRIGHT, x, y, NULL);
- break;
- case LEFT: al_draw_bitmap(headLEFT, x, y, NULL);
- break;
- case UP: al_draw_bitmap(headUP, x, y, NULL);
- break;
- case DOWN: al_draw_bitmap(headDOWN, x, y, NULL);
- break;
- }
- al_draw_textf(font, al_map_rgb(250, 0, 250), 5, 5, 0,
- "Score: %i", score - 1);
- al_draw_textf(font, al_map_rgb(250, 0, 250), 655, 5, 0,
- "Time: %i", timeS);
- }
- al_flip_display();
- al_clear_to_color(al_map_rgb(0, 204, 102));
- }
- }
- al_stop_sample(&snakeSampleID);
- al_play_sample(menuSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &menuSampleID);
- }
- if (kwadrat == true)
- {
- ALLEGRO_BITMAP* kwadratG;
- const int KWADRAT_SIZE = 32;
- float kwadrat_x = screen_height / 2.0 - KWADRAT_SIZE / 2.0;
- float kwadrat_y = screen_width / 2.0 - KWADRAT_SIZE / 2.0;
- float kwadrat_dx = -4.0, kwadrat_dy = 4.0;
- bool redraw = true;
- kwadratG = al_create_bitmap(KWADRAT_SIZE, KWADRAT_SIZE);
- int x = rand() % 255 + 1;
- int y = rand() % 255 + 1;
- int z = rand() % 255 + 1;
- al_set_target_bitmap(kwadratG);
- al_clear_to_color(al_map_rgb(x, y, z));
- al_set_target_bitmap(al_get_backbuffer(display));
- al_clear_to_color(al_map_rgb(0, 0, 0));
- bool doneK = false;
- al_start_timer(timerK);
- while (!doneK)
- {
- al_wait_for_event(event_queue, &events);
- if (events.type == ALLEGRO_EVENT_TIMER) {
- if (kwadrat_x < 0 || kwadrat_x > screen_width - KWADRAT_SIZE) {
- kwadrat_dx = -kwadrat_dx;
- al_set_target_bitmap(kwadratG);
- int x = rand() % 255 + 1;
- int y = rand() % 255 + 1;
- int z = rand() % 255 + 1;
- al_clear_to_color(al_map_rgb(x, y, z));
- al_set_target_bitmap(al_get_backbuffer(display));
- }
- if (kwadrat_y < 0 || kwadrat_y > screen_height - KWADRAT_SIZE) {
- kwadrat_dy = -kwadrat_dy;
- al_set_target_bitmap(kwadratG);
- int x = rand() % 255 + 1;
- int y = rand() % 255 + 1;
- int z = rand() % 255 + 1;
- al_clear_to_color(al_map_rgb(x, y, z));
- al_set_target_bitmap(al_get_backbuffer(display));
- }
- kwadrat_x += kwadrat_dx;
- kwadrat_y += kwadrat_dy;
- redraw = true;
- }
- else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
- doneK = true;
- kwadrat = false;
- doneM = true;
- }
- if (events.type == ALLEGRO_EVENT_KEY_UP)
- {
- al_get_keyboard_state(&keyState);
- switch (events.keyboard.keycode) {
- case ALLEGRO_KEY_ESCAPE:
- doneK = true;
- kwadrat = false;
- menu = true;
- break;
- case ALLEGRO_KEY_ENTER:
- break;
- }
- }
- if (redraw && al_is_event_queue_empty(event_queue)) {
- redraw = false;
- al_clear_to_color(al_map_rgb(0, 0, 0));
- al_draw_bitmap(kwadratG, kwadrat_x, kwadrat_y, 0);
- al_flip_display();
- }
- }
- al_destroy_bitmap(kwadratG);
- al_play_sample(menuSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &menuSampleID);
- }
- if (flip == true) {
- bool key[ALLEGRO_KEY_MAX];
- struct przeszkoda {
- int x, y;
- const int gap = 100;
- };
- struct ptaszek {
- float x, y;
- };
- const int ile_przeszkod = 3;
- struct przeszkoda p[ile_przeszkod];
- struct ptaszek pt;
- float v;
- al_flip_display();
- al_start_timer(timerF);
- bool przerysuj = true;
- bool wyjdz = false;
- for (int i = 0; i < ile_przeszkod; i++) {
- p[i].x = i * screen_width / ile_przeszkod;
- p[i].y = rand() % (screen_height - 60);
- }
- pt.x = screen_width / 4;
- pt.y = screen_height / 2;
- int scoreF = 0;
- v = 0;
- while (!wyjdz)
- {
- al_wait_for_event(event_queue, &events);
- if (events.type == ALLEGRO_EVENT_TIMER) {
- przerysuj = true;
- if (key[ALLEGRO_KEY_SPACE]) {
- v = -8;
- }
- for (int i = 0; i < ile_przeszkod; i++) {
- p[i].x = p[i].x - 2;
- if (p[i].x < 0) {
- p[i].x = screen_width;
- }
- }
- v += 1;
- pt.y += v;
- if (pt.y > screen_height) {
- v = -12;
- }
- }
- else if (events.type == ALLEGRO_EVENT_KEY_DOWN) {
- key[events.keyboard.keycode] = true;
- }
- else if (events.type == ALLEGRO_EVENT_KEY_UP) {
- key[events.keyboard.keycode] = false;
- if (events.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
- wyjdz = true;
- menu = true;
- flip = false;
- }
- }
- if (przerysuj && al_is_event_queue_empty(event_queue)) {
- przerysuj = false;
- al_clear_to_color(al_map_rgb(255, 255, 255));
- for (int i = 0; i < ile_przeszkod; i++) {
- al_draw_filled_rectangle(p[i].x, 0, p[i].x + 60, screen_height, al_map_rgb(0, 0, 255));
- al_draw_filled_rectangle(p[i].x, p[i].y, p[i].x + 60, p[i].y + 200, al_map_rgb(255, 255, 255));
- if (pt.x >= p[i].x && pt.x < p[i].x + 1) {
- scoreF++;
- al_play_sample(birdSCORE, 0.6, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
- }
- }
- al_draw_textf(font, al_map_rgb(255, 0, 0), 5, 5, 0, "Score: %i", scoreF);
- al_draw_bitmap(bird, pt.x, pt.y, NULL);
- al_flip_display();
- }
- }
- al_play_sample(menuSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &menuSampleID);
- }
- al_flip_display();
- al_draw_bitmap(background, 0, 0, 0);
- }
- }
- }
- }
- al_destroy_display(display);
- al_destroy_timer(timer);
- al_destroy_event_queue(event_queue);
- al_destroy_font(introF1);
- al_destroy_font(menuF);
- al_destroy_sample(menuSample);
- al_destroy_font(menu_selectF);
- //snake
- al_destroy_timer(timerS);
- al_destroy_timer(game_time);
- al_destroy_timer(frameTimer);
- al_destroy_font(font);
- al_destroy_font(font1);
- al_destroy_bitmap(headUP);
- al_destroy_bitmap(headRIGHT);
- al_destroy_bitmap(headLEFT);
- al_destroy_bitmap(headDOWN);
- al_destroy_bitmap(bodyUP);
- al_destroy_bitmap(bodyDOWN);
- al_destroy_bitmap(bodyLEFT);
- al_destroy_bitmap(bodyRIGHT);
- al_destroy_bitmap(rabbit);
- al_destroy_bitmap(frog);
- al_destroy_sample(eat);
- al_destroy_sample(mushroom_eat);
- al_destroy_sample(snakeSample);
- //kwadrat
- al_destroy_timer(timerK);
- //flip
- al_destroy_timer(timerF);
- al_destroy_bitmap(bird);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement