• 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;
    }

  • 相关阅读:
    GUI 监听事件 (两个按钮,实现同一个监听)
    GUI 监听事件
    GUI 练习
    GUI 之表格布局
    GUI 之边界布局
    GUI 之流布局
    [转帖]Linux 下解压 rar 文件
    Linux 启动、停止、重启jar包脚本
    关于linux下,ls vi等命令失效的解决方法(配置下环境变量出现问题)
    超好用的UnixLinux 命令技巧 大神为你详细解读
  • 原文地址:https://www.cnblogs.com/lely/p/12188222.html
Copyright © 2020-2023  润新知