#include "iGraphics.h"


#define SCREEN_WIDTH 864

#define SCREEN_HEIGHT 768


/*

function iDraw() is called again and again by the system.

*/

int bird_x = 300, bird_y = 300;

char bird_flying_animation[3][50] = {"assets/flappy_bird_images/bird1.png", 

                                    "assets/flappy_bird_images/bird2.png", 

                                    "assets/flappy_bird_images/bird3.png"};

int flying_animation_index = 0;


int gravity = -1;

int velocity = 0;

int force = 15;


int pipe_x = SCREEN_WIDTH, pipe_y = 0;

int pipe_width = 50;

int pipe_gap = 200;

int gap_position = 400;


bool game_over = false;


// Score related variables

bool score_updated = false;

int score = 0;


void iDraw()

{

    // place your drawing codes here

    iClear();


    iShowImage(0, 0, "assets/flappy_bird_images/bg.png");

    iShowImage(bird_x, bird_y, bird_flying_animation[flying_animation_index]);

    

    iSetColor(255, 255, 255);

    char score_text[20];

    score_text[0] = '\0'; // Initialize the string

    sprintf(score_text, "Score: %d", score);

    iText(10, SCREEN_HEIGHT - 30, score_text, GLUT_BITMAP_TIMES_ROMAN_24);


    iSetColor(0, 0, 255);


    iFilledRectangle(pipe_x, pipe_y, pipe_width, gap_position);

    iFilledRectangle(pipe_x, gap_position + pipe_gap, pipe_width, SCREEN_HEIGHT - pipe_gap - gap_position);



    if(game_over){

        iSetColor(255, 0, 0);

        iText(SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT / 2, "Game Over!", GLUT_BITMAP_TIMES_ROMAN_24);

        iText(SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT / 2 - 30, score_text, GLUT_BITMAP_TIMES_ROMAN_24);

        iText(SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT / 2 - 60, "Press 'r' to restart", GLUT_BITMAP_TIMES_ROMAN_24);

    }

}



/*

function iMouseMove() is called when the user moves the mouse.

(mx, my) is the position where the mouse pointer is.

*/

void iMouseMove(int mx, int my)

{

    // place your codes here

}


/*

function iMouseDrag() is called when the user presses and drags the mouse.

(mx, my) is the position where the mouse pointer is.

*/

void iMouseDrag(int mx, int my)

{

    // place your codes here

}


/*

function iMouse() is called when the user presses/releases the mouse.

(mx, my) is the position where the mouse pointer is.

*/

void iMouse(int button, int state, int mx, int my)

{

    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)

    {

        // place your codes here

    }

    if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)

    {

        // place your codes here

    }

}


/*

function iMouseWheel() is called when the user scrolls the mouse wheel.

dir = 1 for up, -1 for down.

*/

void iMouseWheel(int dir, int mx, int my)

{

    // place your code here

}


/*

function iKeyboard() is called whenever the user hits a key in keyboard.

key- holds the ASCII value of the key pressed.

*/

void iKeyboard(unsigned char key)

{

    switch (key)

    {

    case 'p':

        iPauseTimer(0);

        break;

    // place your codes for other keys here

    case 'r':

        iResumeTimer(0);

    case ' ':

        velocity = force;

    default:

        break;

    }

}


/*

function iSpecialKeyboard() is called whenver user hits special keys likefunction

keys, home, end, pg up, pg down, arraows etc. you have to use

appropriate constants to detect them. A list is:

GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3, GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,

GLUT_KEY_F7, GLUT_KEY_F8, GLUT_KEY_F9, GLUT_KEY_F10, GLUT_KEY_F11,

GLUT_KEY_F12, GLUT_KEY_LEFT, GLUT_KEY_UP, GLUT_KEY_RIGHT, GLUT_KEY_DOWN,

GLUT_KEY_PAGE_UP, GLUT_KEY_PAGE_DOWN, GLUT_KEY_HOME, GLUT_KEY_END,

GLUT_KEY_INSERT */

void iSpecialKeyboard(unsigned char key)

{

    if (key == GLUT_KEY_END)

    {

        exit(0);

    }

    if (key == GLUT_KEY_LEFT)

    {

    }

    if (key == GLUT_KEY_RIGHT)

    {

    }

    if (key == GLUT_KEY_UP)

    {

    }

    if (key == GLUT_KEY_DOWN)

    {

    }

}


void changeFlyingAnimation()

{

    flying_animation_index++;

    if (flying_animation_index >= 3)

    {

        flying_animation_index = 0;

    }

}


void idle(){

    if(game_over) return;


    // else we can continue the game

    // updating bird position and pipe position

    velocity += gravity; 

    bird_y += velocity;

    if (bird_y < 0)

    {

        bird_y = 0;

    }

    else if (bird_y > SCREEN_HEIGHT - 50)

    {

        bird_y = SCREEN_HEIGHT - 50;

    }


    if(bird_x >= pipe_x && bird_x <= pipe_x + pipe_width && (bird_y < gap_position || bird_y > gap_position + pipe_gap)){

        game_over = true;

        iPauseTimer(0);

    }else{

        pipe_x -= 5;

        if(pipe_x < -50){

            pipe_x = SCREEN_WIDTH;

            gap_position = rand() % (SCREEN_HEIGHT - pipe_gap);

            score_updated = false;

        }

    }


    if(bird_y <= 0 || bird_y >= SCREEN_HEIGHT - 50){

        game_over = true;

        iPauseTimer(0);

    }


    if(!score_updated && bird_x > pipe_x + pipe_width){

        score_updated = true;

        score += 10;

    }

}


int main(int argc, char *argv[])

{

    glutInit(&argc, argv);


    iSetTimer(100, changeFlyingAnimation);

    iSetTimer(10, idle);


    iInitialize(SCREEN_WIDTH, SCREEN_HEIGHT, "Flappy Bird!!!");


    return 0;

}

Comments

Popular posts from this blog

Phy 129 - WM, CT-03

Online 3 CSE 102; Array Resources.