C语言小游戏---画面愤怒的小鸟

学习B站 https://www.bilibili.com/video/av34118200?p=41

还没写完,1、撞柱子没死呢 2、没计分呢

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<graphics.h>
#include<conio.h>
#pragma comment(lib,"Winmm.lib")

int width, high; // screen dimention
IMAGE img_bk,img_bird,img_bird_1,img_bar_up,img_bar_up_1,img_bar_down,img_bar_down_1; // define image name
int bird_x, bird_y; // bird position
int bar_x, bar_up_y, bar_down_y; // bar position
int bird_fly_step,bird_down_step; // control step
int score; 

void startup() { // init data
    width = 432;
    high = 675;

    initgraph(width,high);  // draw screen
    // load images
    loadimage(&img_bk,"E:\\C程序练习\\project\\flightwar\\flappybird\\picture\\bk.jpeg");
    loadimage(&img_bird,"E:\\C程序练习\\project\\flightwar\\flappybird\\picture\\bird.jpg");
    loadimage(&img_bird_1,"E:\\C程序练习\\project\\flightwar\\flappybird\\picture\\bird_1.jpg");
    loadimage(&img_bar_up,"E:\\C程序练习\\project\\flightwar\\flappybird\\picture\\bar_up2.gif");
    loadimage(&img_bar_up_1,"E:\\C程序练习\\project\\flightwar\\flappybird\\picture\\bar_up1.gif");
    loadimage(&img_bar_down,"E:\\C程序练习\\project\\flightwar\\flappybird\\picture\\bar_down2.gif");
    loadimage(&img_bar_down_1,"E:\\C程序练习\\project\\flightwar\\flappybird\\picture\\bar_down1.gif");

    // bird position
    bird_x = 100;
    bird_y = 200;
    // bar position
    bar_x = 250;
    bar_up_y = -380;
    bar_down_y = 350;
    // control bird step
    bird_fly_step = 40;
    bird_down_step = 13;

    BeginBatchDraw();
    // play background music - repeat
    mciSendString("open E:\\C程序练习\\project\\flightwar\\flappybird\\picture\\background.mp3 alias bkmusic",NULL,0,NULL);
    mciSendString("play bkmusic repeat", NULL,0,NULL);
    
    score = 0;    
}

void show() { // show windows
    putimage(0,0,&img_bk);     // show background picture
    putimage(bird_x,bird_y,&img_bird_1,NOTSRCERASE); // show bird
    putimage(bird_x,bird_y,&img_bird,SRCINVERT);
    putimage(bar_x,bar_up_y,&img_bar_up_1,NOTSRCERASE); // show bar up
    putimage(bar_x,bar_up_y,&img_bar_up,SRCINVERT);
    putimage(bar_x,bar_down_y,&img_bar_down_1,NOTSRCERASE);  // show bar down
    putimage(bar_x,bar_down_y,&img_bar_down,SRCINVERT);    

    FlushBatchDraw();
}

void updateWithoutInput() {
    if(bird_y<high)  // bird auto go down
        bird_y+=bird_down_step;
    
    if(bird_x==bar_x) {  
        if(bird_y>bar_down_y-140 && bird_y<bar_down_y)
            score++;
        else exit(0);
    }

    if(bar_x>0)  // move bar
        bar_x-=13;
    else {
        bar_x = width;  // new bar

        int randPosition = rand() % (high-50);
        bar_up_y = randPosition - 670 ;
        bar_down_y = randPosition + 70 ;
    }
    Sleep(150);
}

void updateWithInput() {
    char input;
    if(kbhit()) { //runing while user push keyboard
        input = getch();
        if(input == ' ' && bird_y>20)   // control bird move up
        {
            bird_y = bird_y - bird_fly_step;
            // bird fly with music
            mciSendString("close   jpmusic",NULL,0,NULL);
            mciSendString("open E:\\C程序练习\\project\\flightwar\\flappybird\\picture\\Jump.mp3 alias jpmusic",NULL,0,NULL);
            mciSendString("play jpmusic",NULL,0,NULL);
        }
    }
}

void gameover() 
{
    EndBatchDraw(); // end batch picture
    getch(); 
    closegraph();  // close graph

}
int main() {
    startup(); // init data
    while(1) { // game loop run
        show(); // show windows
        updateWithoutInput(); //update don't need user
        updateWithInput(); //update need user
    }
    gameover();
    return 0;
}

 

 

posted @ 2020-01-13 17:11  yangly  阅读(391)  评论(0编辑  收藏  举报