码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-01-13 17:53:33      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:char   void   mamicode   include   keyboard   ges   愤怒的小鸟   程序   class   

学习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;
}

 

 

技术图片

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

标签:char   void   mamicode   include   keyboard   ges   愤怒的小鸟   程序   class   

原文地址:https://www.cnblogs.com/lely/p/12188222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!