• c++编写天天酷跑游戏


    素材加Q群:723550115

    天天酷跑2.0版本:2.0

    游戏背景设置




    1. Start importing material (background picture)
    #include 
    
    • 1

    Create a graph window and define macros for the window

     #define WIN_WINDTH 1012
     #define WIN_HEIGHT 396
    
    initgraph(WIN_WINDTH, WIN_HEIGHT);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. Import game background (scroll cycle)

      #include 
      
      
      	//global variable
          IMAGE imgsBgs[3];//define imgae variable
      
      void init()
      {
      	initgraph(WIN_WINDTH, WIN_HEIGHT);
      
          char name[64];
          
          //Load background resources
          for (int i = 0;i < 3;i++)
              
          {
              
              sprintf(name,"res/bg%03d.png",i+1);//generate file name
              
               //loadimage(&imgBgs[i],"file name");
              loadimage(&imgBgs[i],name);
      
          }
          
         
          
      
      }
      
      
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
       ```c++
       int main()
       {
            init ();
            updataBg();
       
           return 0;
       }
       ```
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    2. Local modularization
      Game background coordinates

      void updataBg()
      {
      
      	putimage(0,0,&imgBgs[0]);
          putimage(0,119,&imgBgs[1]);
          putimage(0,330,&imgBgs[2]);
          
          
          
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10

    The picture is the Y coordinate of motion, and the definition amount is constantly changed to keep the last y coordinate

    int bgX[3];//X coordinate global variable of background picture
    
    • 1

    change initialization

    #include 
     
    
    
    void init()
    {
    	initgraph(WIN_WINDTH, WIN_HEIGHT);
    
        char name[64];
        
        //Load background resources
        for (int i = 0;i < 3;i++)
            
        {
            
            sprintf(name,"res/bg%03d.png",i+1);//generate file name
             //loadimage(&imgBgs[i],"file name");
            loadimage(&imgBgs[i],name);
            bgX[i] = 0;
        }
       
    }
    
       void updataBg()
       {
       
           putimage(bgX[0],0,&imgBgs[0]);
           putimage(bgX[1],119,&imgBgs[1]);
           putimage(bgX[2],330,&imgBgs[2]);          
       }
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    背景变黑了




    Call tool H transparent map
    Easy background black

    add tools file

    #include "tools.h"
    
    • 1

    **Change the put image code **

    **putimagePNG is Internet GPl code **

     void updataBg()
       {
       
           putimagePNG(bgX[0],0,&imgBgs[0]);
           putimagePNG(bgX[1],110,&imgBgs[1]);
           putimagePNG(bgX[2],330,&imgBgs[2]);          
       }
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    添加带有一些lib和Header文件的“tool.h”文件
    #include 
    #pragma comment(lib,"winmm.lib")
    
    • 1
    • 2
    实现循环滚动的效果
    int main()
    {
         init ();
        
        while(1)
        {
              
            updataBg();
            
    
        }
       
    
        return 0;
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    更改坐标

    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
    }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    int bgSpeed[3] = { 1,2,4};
    
    • 1
    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
       // same speed
       bgX[i] -= bgSpeed[1];
    }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    int main()
    {
         init ();
        
        while(1)
        {
              
            updataBg();
            
            fly();
            
            // add Frame waiting
            Sleep(20);
    
        }
       
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    导致记忆冲突??

    使用优化版本

    void updataBg()
    {
        putimagePNG2(bgX[0],0, &imgBgs[0]);
        putimagePNG2(bgX[1],119, & imgBgs[1]);
        putimagePNG2(bgX[2],330, & imgBgs[2]);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
        if(bgX[i] < -WIN_WINDTH)
        {
            bgX[i] = 0;
        }
        
    }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
     int main()
    {
         init ();
        
        while(1)
        {
           BeginBatchDraw();//double buffer mechanical start
            
           
            updataBg();
            
            
            //FlushBatshDraw
           EndBatshDraw();//ouble buffer mechanical end
            
            fly();
            
            // add Frame waiting
            Sleep(30);
    
        }
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    Hero appera

    define goba l variable

    IMAGE imgHeros[12];
    
    • 1

    加载玩家run图片hero

    
    void init()
    {
    	initgraph(WIN_WINDTH, WIN_HEIGHT);
    
        char name[64];
        
        //Load background resources
        for (int i = 0;i < 3;i++)
            
        {
            
            sprintf(name,"res/bg%03d.png",i+1);//generate file name
             //loadimage(&imgBgs[i],"file name");
            loadimage(&imgBgs[i],name);
            bgX[i] = 0;
        }
        //加载玩家奔跑帧数采
        
        for (int i = 0; i < 12;i++)
        {
    		//res/hero1.png ~ hero12.png
            sprintf(name,"res/hero%d",i + 1);
            loadimage(&imgHeros[i],name);
        
        }
        
       
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    让人物跑起来

     int main()
    {
         init ();
        
        while(1)
        {
           BeginBatchDraw();//double buffer mechanical start
            
           
            updataBg();
            //打印奔跑的hero
            putimagePNG2(heroX,heroY,&imgHeros[heroIndex]);
            
            //    FlushBatchDraw();
           EndBatshDraw();//ouble buffer mechanical end
            
            fly();
            
            // add Frame waiting
            Sleep(30);
    
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    定义玩家的x坐标

    int heroX,heroY;
    
    • 1
    void init()
    {
    	initgraph(WIN_WINDTH, WIN_HEIGHT);
    
        char name[64];
        
        //Load background resources
        for (int i = 0;i < 3;i++)
            
        {
            
            sprintf(name,"res/bg%03d.png",i+1);//generate file name
             //loadimage(&imgBgs[i],"file name");
            loadimage(&imgBgs[i],name);
            bgX[i] = 0;
        }
        //加载玩家奔跑帧数采
        
        for (int i = 0; i < 12;i++)
        {
    		//res/hero1.png ~ hero12.png
            sprintf(name,"res/hero%d.png",i + 1);
            loadimage(&imgHeros[1],name);
        
        }
        heroX = WIN_WIDTH * 0.5 - imgHeros[0].getwidth() *0.5;
        heroY = 345 - imgHeros[0].getheight();
        
        heroIndex = 0;
       
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    int heroIndex;//玩家显示的是第几章frame
    
    • 1
    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
        if(bgX[i] < -WIN_WINDTH)
        {
            bgX[i] = 0;
        }
        
    }
       heroIndex = (heroIndex + 1)%12;  
        
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    实现玩家跳跃

    用户输入检测函数

    #include 
    #include 
    #include 
    void keyEvent()
    {
        char ch;
     	if(_kbhit())//如果有按键输入
        {
            
    	 ch = _getch();//_getch() 不需要按下回车 直接读取
          
            if(ch == '')
            {
    			jump();
            }
            
        }
     
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    跳跃函数

    #include 
    #include 
    #include 
    
    void jump()
    {
    
        	heroJump = true;
        
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    //跳跃开关

    bool heroJump;
        
      //初始化 
        void init()
    {
        //。。。
      heroJump = false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    开始跳跃 实现

    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
        if(bgX[i] < -WIN_WINDTH)
        {
            bgX[i] = 0;
        }
        
    }
       heroIndex = (heroIndex + 1)%12;  
        
        //实现跳跃
        if(heroJump)
        {
            
            
    		if(heroY > JumpHeightMax)
            {
    				//上升
                 heroJumpOff = 4;
                
            }
            
          heroY += heroJumpOff;
            
        if(heroY > 345 - imgHeros[0].getheight())
      {
    		heroJump = false;
          heroJumpOff = -4;
      }
            
            
                
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    调用跳跃

    int main()
    {
       // test();
    
        init();
    
        while (1)
        {
            
     
            keyEvent();
            
            BeginBatchDraw();//double buffer mechanical start
    
    
            updataBg();
    
            putimagePNG2(heroX, heroY, &imgHeros[heroIndex]);
    
            FlushBatchDraw();
           
            //EndBatshDraw();//ouble buffer mechanical end
    
            fly();
    
            // add Frame waiting
            Sleep(20);
    
        }
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    跳跃的最高度

    全局变量

    int jumpHeightMax;
    
    void init()
    
    {
    
    //...
    
    jumpHeightMax =  345 - imgHeros[0].getheight() - 120;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    定义跳跃偏移量

    int heroJumpOff;
    void init()
    {
    ///....
    heroJumpOff = -4;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    跳的时候腿不要动

    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
        if(bgX[i] < -WIN_WINDTH)
        {
            bgX[i] = 0;
        }
        
    }
     
        
        //实现跳跃
        if(heroJump)
        {
            
            
    		if(heroY < JumpHeightMax)
            {
    				//上升
                 heroJumpOff = 4;
                
            }
            
          heroY += heroJumpOff;
            
         if(heroY > 345 - imgHeros[0].getheight())
         {
     		heroJump = false;
          heroJumpOff = -4;
        }          
        }
        else {//不跳跃的时候
              heroIndex = (heroIndex + 1)%12;  
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    优化帧等待

    int main()
    {
       // test();
    
        init();
    
        int timer;//调用时间
        while (1)
        {
            
     
            keyEvent();
            
          timer += getDelay();
            
            if(timer > 30)
            {
                timer = 0;
    			
            BeginBatchDraw(); 
            updataBg();
            putimagePNG2(heroX, heroY,                                            &imgHeros[heroIndex]);
            FlushBatchDraw();
         
            fly();
            }
           
            //Sleep(20);
    
        }
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    getDelay() function

    int updata; //表示是否要刷新画面
    
    void init()
    {
    
    //...
    updata = true;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    int main()
    {
       // test();
    
        init();
    
        int timer;//调用时间
        while (1)
        {
            
     
            keyEvent();
            
          timer += getDelay();
            
            if(timer > 30)
            {
                timer = 0;           
                updata = true;			
          
            }
           
            if(updata)
            {
             BeginBatchDraw(); 
            updataBg();
            putimagePNG2(heroX, heroY,                            &imgHeros[heroIndex]);
            FlushBatchDraw();
         
            fly();
            }
            //Sleep(20);
    
        }
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    更新jump

    void jump()
    {
    
        heroJump = true;
        updata = true;
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
     
            if(updata)
            {
                updata = false;//不能一直刷
             	BeginBatchDraw(); 
            	updataBg();
           	 	putimagePNG2(heroX, heroY,                           	 &imgHeros[heroIndex]);
            	FlushBatchDraw();
         
             	fly();
            }
            //Sleep(20);
     
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    new main

    int main()
    {
       // test();
    
        init();
    
        int timer;//调用时间
        while (1)
        {
            
     
            keyEvent();
            
          timer += getDelay();
            
            if(timer > 30)
            {
                timer = 0;           
                updata = true;			
          
            }
           
            if(updata)
            {
              updata = false;//不能一直刷
             BeginBatchDraw(); 
            updataBg();
            putimagePNG2(heroX, heroY,   &imgHeros[heroIndex]);
            FlushBatchDraw();
         
            fly();
            }
            //Sleep(20);
    
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    实现障碍物的出现

    定义图片

    IMAGE imgTortoise;//小乌龟
    int torToiseX,torToiseY;//小乌龟的水平坐标
    bool torToiseExits;//当前窗口是否有小乌龟
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    初始化

    void init()
    {
     // ..
        loadimage(&imgTortoise,"res/t1.png");
        torTooiseExist = false;
        torToiseY = 345 - imgTortoise.getheight() +5;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    显示小乌龟

    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
        if(bgX[i] < -WIN_WINDTH)
        {
            bgX[i] = 0;
        }
        
    }
     
        
        //实现跳跃
        if(heroJump)
        {
            
            
    		if(heroY > JumpHeightMax)
            {
    				//上升
                 heroJumpOff = 4;
                
            }
            
          heroY += heroJumpOff;
            
         if(heroY > 345 - imgHeros[0].getheight())
         {
     		heroJump = false;
          heroJumpOff = -4;
        }          
        }
        else {//不跳跃的时候
              heroIndex = (heroIndex + 1)%12;  
        }
    
       //创建小乌龟
        static int frameCount = 0;
        frameCount++;
        if(frameCount > 100)
        {
            frameCount = 0;
            if(!torToiseExist)
            {
    			torToiseExist = true;
                torToiseY = WIN_WIDTH;
            }
    	       
            
        }
               
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    int main()
    {
       // test();
    
        init();
    
        int timer;//调用时间
        while (1)
        {
            
     
            keyEvent();
            
          timer += getDelay();
            
            if(timer > 30)
            {
                timer = 0;           
                updata = true;			
          
            }
           
            if(updata)
            {
              updata = false;//不能一直刷
             BeginBatchDraw(); 
            updataBg();
            putimagePNG2(heroX, heroY,                            &imgHeros[heroIndex]);
                updataEnemy();
                
            FlushBatchDraw();
         
            fly();
            }
            //Sleep(20);
    
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    void updataEnemy()
    {
        //渲染小乌龟
        if(torTooiseExist){
    		putimagePNG2(torToiseX,torToiseY,WIN_WIDTH,&imgTortoise);
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    更新小乌龟的位置

    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
        if(bgX[i] < -WIN_WINDTH)
        {
            bgX[i] = 0;
        }
        
    }
     
        
        //实现跳跃
        if(heroJump)
        {
            
            
    		if(heroY<  JumpHeightMax)
            {
    				//上升
                 heroJumpOff = 4;
                
            }
            
          heroY += heroJumpOff;
            
         if(heroY > 345 - imgHeros[0].getheight())
         {
     		heroJump = false;
          heroJumpOff = -4;
        }          
        }
        else {//不跳跃的时候
              heroIndex = (heroIndex + 1)%12;  
        }
    
       //创建小乌龟
        static int frameCount = 0;
        frameCount++;
        if(frameCount > 100){
            frameCount = 0;
            if(!torToiseExist)
            {
    			torToiseExist = true;
                torToiseX = WIN_WIDTH;
            } 
        }
            
        torTOiseX -+ bgSpeed[2];
        if(torToiseExist)
        {
            
        if(torToiseX < - imgTortoise.getwidth())
        {
            torToiseExits = false;
        }
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    使小乌龟出现为随机值

    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
        if(bgX[i] < -WIN_WINDTH)
        {
            bgX[i] = 0;
        }
        
    }
     
        
        //实现跳跃
        if(heroJump)
        {
            
            
    		if(heroY > JumpHeightMax)
            {
    				//上升
                 heroJumpOff = 4;
                
            }
            
          heroY += heroJumpOff;
            
         if(heroY > 345 - imgHeros[0].getheight())
         {
     		heroJump = false;
          heroJumpOff = -4;
        }          
        }
        else {//不跳跃的时候
              heroIndex = (heroIndex + 1)%12;  
        }
    
       //创建小乌龟
        static int frameCount = 0;
        static int torToiseFre = 100;
        frameCount++;
        if(frameCount > torToiseFre ){
            frameCount = 0;
            if(!torToiseExist)
            {
    			torToiseExist = true;
                torToiseX = WIN_WIDTH;
                
                torToiseFre = 200 + rand()%300 ;
            } 
        }
            
        torTOiseX -+ bgSpeed[2];
        if(torToiseExist)
        {
            
        if(torToiseX < - imgTortoise.getwidth())
        {
            torToiseExits = false;
        }
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    使用结构体封装障碍物

    #include 
    using namespace std;
    #define OBSTACLE_COUNT 10
    typedef  enum {
    
        TORTOISE,//0
        LION,//1
        OBSTACLE_TYPE_COUNT//2
        
    }obstacle_type;
    
    //IMAGE obstacleImgs[3][12];
    //二维
    vector>  obstacleImgs;//存放所有障碍的图片
    
    
    typedef struct obstacle//障碍物
    {
    obstacle_type type;//障碍物的类型
       int x,y;	//障碍物的坐标
       int speed;
       int power;//受到的伤害 
       bool exist; 
       int imgIndex;//显示当前图片的序号
       
    }obstacle_t;
    
    obstacle_t obstacles[ OBSTACLE_COUNT];//障碍物的个数
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    改用结构体后的代码

    before init()

    void init()
    {
    	initgraph(WIN_WINDTH, WIN_HEIGHT);
    
        char name[64];
        
        //Load background resources
        for (int i = 0;i < 3;i++)
            
        {
            
            sprintf(name,"res/bg%03d.png",i+1);//generate file name
             //loadimage(&imgBgs[i],"file name");
            loadimage(&imgBgs[i],name);
            bgX[i] = 0;
        }
        //加载玩家奔跑帧数采
        
        for (int i = 0; i < 12;i++)
        {
    		//res/hero1.png ~ hero12.png
            sprintf(name,"res/hero%d.png",i + 1);
            loadimage(&imgHeros[1],name);
        
        }
        heroX = WIN_WIDTH * 0.5 - imgHeros[0].getwidth() *0.5;
        heroY = 345 - imgHeros[0].getheight();
        heroIndex = 0;
        heroJump = false;
        jumpHeightMax =  345 - imgHeros[0].getheight() - 120;
        updata = true;
         loadimage(&imgTortoise,"res/t1.png");
        torTooiseExist = false;
        torToiseY = 345 - imgTortoise.getheight() +5;
        heroJumpOff = -4;
        
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    after

    void init()
    {
    	initgraph(WIN_WINDTH, WIN_HEIGHT);
    
        char name[64];
        
        //Load background resources
        for (int i = 0;i < 3;i++)
            
        {
            
            sprintf(name,"res/bg%03d.png",i+1);//generate file name
             //loadimage(&imgBgs[i],"file name");
            loadimage(&imgBgs[i],name);
            bgX[i] = 0;
        }
        //加载玩家奔跑帧数采
        
        for (int i = 0; i < 12;i++)
        {
    		//res/hero1.png ~ hero12.png
            sprintf(name,"res/hero%d.png",i + 1);
            loadimage(&imgHeros[1],name);
        
        }
        heroX = WIN_WIDTH * 0.5 - imgHeros[0].getwidth() *0.5;
        heroY = 345 - imgHeros[0].getheight();
        heroIndex = 0;
        heroJump = false;
        jumpHeightMax =  345 - imgHeros[0].getheight() - 120;
        updata = true;
        heroJumpOff = -4;
        
        
        
        IMAGE imgTort ;  
       loadimage(&imgTort,"res/t1.png");
        vectorimgTortArray;
        imgTortArray.push_back(imgTort);
         
        obstacleImgs.push_back(imgTortArray);
        
        
        //lion
        IMAGE imgLion;
         vectorimgLionArry;
        for (int i = 0 ; i < 6;i++)
        {
    		sprintf(name,"res/p%d.png",i+1);
            loadimage(&mgLion,name);
            imgLionArry.push_back(imgLion);
                
        }
        
        obstacleImgs.push_back(imgLionArray);
        
        
        
        //对障碍物池进行初始化
        for(int i = 0;i < OBSTALE_COUNT;i++)
        {
    		obstacles[i].exist = false;//都不允许出场
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    更爱渲染小乌龟

    void updataEnemy()
    {
        //渲染小乌龟
        
      for(int i = 0;i < OBSTACLE_COUNT;i++)
      {
          if(obstacles[i].exist){
    		putimagePNG2(obstacles[i].x,obstacles[i].y,WIN_WIDTH, &obstacleImgs[obstacles[i].type] [obstacles[i].imgIndex])
                
          }
      }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    修改fly

    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
        if(bgX[i] < -WIN_WINDTH)
        {
            bgX[i] = 0;
        }
        
    }
     
        
        //实现跳跃
        if(heroJump)
        {
            
            
    		if(heroY < JumpHeightMax)
            {
    				//上升
                 heroJumpOff = 4;
                
            }
            
          heroY += heroJumpOff;
            
         if(heroY > 345 - imgHeros[0].getheight())
         {
     		heroJump = false;
          heroJumpOff = -4;
        }          
        }
        else {//不跳跃的时候
              heroIndex = (heroIndex + 1)%12;  
        }
    
       //创建障碍物
        static int frameCount = 0;
        static int enemyFre = 50;
        frameCount++;
        if(frameCount > enemyFre ){
            frameCount = 0;
            enemyFre = 50 + rand()%50 ;
            createObstacle();
        }
            
        
        
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    创建障碍物

    void createObstacle()
    {
        int i;
    	for( i = 0 ;i < OBSTACLE_COUNT:i++)
        {
    			if(obstacles[i].exist == false){
    					break;
                }     
        }
        
        if(i  >= OBSTACLE_COUNT){
            return;
        }
        
        obstacles[i].exist = true;
        obstacles[i].imgIndex = 0;
        obstacles[i].type =        (obstacle_type)(rand()%OBSTACLE_TYPE_COUNT);
        
        
        obstacles[i].x = WIN_WIDTH;
        obstacles[i].y = 345 +5 - obstacleImgs[obstacles[i].type][0].getheirgt();
        
        if(obstacles[i].type == TORTOISE)
        {
    			obstacles[i].speed = 0;
            	obstacles[i].power = 5;
            		
        }else if(obstacles[i].type == LION)
        {
    		obstacles[i].speed = 4;
            obstacles[i].power = 20;
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    更新障碍物坐标

    void fly()
    {
    for(int i = 0 ;i <3 ;i++)
    {
        // same speed
        bgX[i] -= 2;
        if(bgX[i] < -WIN_WINDTH)
        {
            bgX[i] = 0;
        }
        
    }
     
        
        //实现跳跃
        if(heroJump)
        {
            
            
    		if(heroY > JumpHeightMax)
            {
    				//上升
                 heroJumpOff = 4;
                
            }
            
          heroY += heroJumpOff;
            
         if(heroY > 345 - imgHeros[0].getheight())
         {
     		heroJump = false;
          heroJumpOff = -4;
        }          
        }
        else {//不跳跃的时候
              heroIndex = (heroIndex + 1)%12;  
        }
    
       //创建障碍物
        static int frameCount = 0;
        static int enemyFre = 50;
        frameCount++;
        if(frameCount > enemyFre ){
            frameCount = 0;
            enemyFre = 50 + rand()%50 ;
            createObstacle();
        }
            
        //更新所有障碍物的坐标
        for(int i = 0;i < OBSTACLE_COUNT;i++)
        {
    		if(obstacles[i].exist)
            {
    		obstacles[i].x -= obstacles[i].speed + bgSpeed[2];
                
                
                if(obstacles[i].x < -obstaceImgs[obstacles[i].type][0].getwidth() *2)//跑出screen
                {
                    obstacles[i].exist = false;    
                
           		 }
                int len = obstacleImgs[obstacles[i].type].size();
                
                obstacles[i].imgIndex = (obstacles[i].imgIndex+1) % len;
                
        }
        
     
    }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    int main()
    {
        // test();
    
        init();
    
        int timer   = 0;//调用时间
    
        while (1)
        {
    
    
            keyEvent();
    
            timer += getDelay();
    
            if (timer > 30)
            {
                timer = 0;
                updata = true;
    
            }
    
            if (updata)
            {
                updata = false;//不能一直刷
                BeginBatchDraw();
                updataBg();
                putimagePNG2(heroX, heroY, &imgHeros[heroIndex]);
                updataEnemy();
                FlushBatchDraw();
    
                fly();
            }
            //Sleep(20);
    
        }
        return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    实现人物下蹲




    下蹲图片

    IMAGE imgHeroDown[12];
    
    • 1

    初始化

    void init()
    {
    	initgraph(WIN_WINDTH, WIN_HEIGHT);
    
        char name[64];
        
        //Load background resources
        for (int i = 0;i < 3;i++)
            
        {
            
            sprintf(name,"res/bg%03d.png",i+1);//generate file name
             //loadimage(&imgBgs[i],"file name");
            loadimage(&imgBgs[i],name);
            bgX[i] = 0;
        }
        //加载玩家奔跑帧数采
        
        for (int i = 0; i < 12;i++)
        {
    		//res/hero1.png ~ hero12.png
            sprintf(name,"res/hero%d.png",i + 1);
            loadimage(&imgHeros[1],name);
        
        }
        heroX = WIN_WIDTH * 0.5 - imgHeros[0].getwidth() *0.5;
        heroY = 345 - imgHeros[0].getheight();
        heroIndex = 0;
        heroJump = false;
        jumpHeightMax =  345 - imgHeros[0].getheight() - 120;
        updata = true;
        heroJumpOff = -4;
        
        
        
        IMAGE imgTort ;  
       loadimage(&imgTort,"res/t1.png");
        vectorimgTortArray;
        imgTortArrat.push_back(imgTort);
         
        obstacleImgs.push_back(imgTortArray);
        
        
        //lion
        IMAGE imgLion;
         vectorimgLionArry;
        for (int i = 0 ; i < 6;i++)
        {
    		sprintf(name,"res/p%d.png",i+1);
            loadimage(imgLion,name);
            imgLionArry.push_back(imgLion);
                
        }
        
        obstacleImgs.push_back(imgLionArray);
        
        
        
        //对障碍物池进行初始化
        for(int i = 0;i < OBSTALE_COUNT;i++)
        {
    		obstacles[i].exist = false;//都不允许出场
        }
        
        //加载人物下蹲图
        
        for (int i = 0; i < 12; i++)
        {
            // res/hero1.png ~ hero12.png
            sprintf(name, "res/g%02d.png", i + 1);
            loadimage(&imgHeroDown[i], name);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    按键输入

    void keyEvent()
    {
        char ch;
        if (_kbhit())//如果有按键输入
        {
    
            ch = _getch();//_getch() 不需要按下回车 直接读取
    
            if (ch == ' ')
            {
                jump();
            }
    
            else if (ch == 'a')
            {
                down();
            }
        }
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    bool heroDown;
    
    • 1

    初始化

    void init()
    {
        initgraph(WIN_WINDTH, WIN_HEIGHT);
    
        char name[64];
    
        //Load background resources
        for (int i = 0; i < 3; i++)
    
        {
            
            sprintf(name, "res/bg%03d.png", i + 1);//generate file name
            //loadimage(&imgBgs[i],"file name");
            loadimage(&imgsBgs[i], name);
            bgX[i] = 0;
        }
    
        for (int i = 0;i < 12;i++)
        {
            // res/hero1.png ~ hero12.png
            sprintf(name, "res/hero%d.png", i + 1);
            loadimage(&imgHeros[i], name);
    
        }
    
        heroX = WIN_WINDTH * 0.5 - imgHeros[0].getwidth() * 0.5;
        heroY = 345 - imgHeros[0].getheight();
    
        heroJump = false;
    
        jumpHeightMax = 345 - imgHeros[0].getheight() - 120;
    
        updata = true;
    
        heroJumpOff = -4;
    
    
        //IMAGE imgTort;
        //loadimage(&imgTort, "res/t1.png");
        //vectorimgTortArray;
        //imgTortArray.push_back(imgTort);
    
        //obstacleImgs.push_back(imgTortArray);
    
    
        //lion
        IMAGE imgLion;
        vector imgLionArry;
        for (int i = 0; i < 6; i++)
        {
            sprintf(name, "res/p%d.png", i + 1);
            loadimage(&imgLion, name);
            imgLionArry.push_back(imgLion);
    
        }
    
        obstacleImgs.push_back(imgLionArry);
    
    
    
        //对障碍物池进行初始化
        for (int i = 0; i < OBSTACLE_COUNT; i++)
        {
            obstacles[i].exist = false;//都不允许出场
        }
    
    
         for (int i = 0; i < 12; i++)
        {
            // res/hero1.png ~ hero12.png
            sprintf(name, "res/g%02d.png", i + 1);
            loadimage(&imgHeroDown[i], name);
    
        }
    
        heroDown = false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    void down()
    {
        updata = true;
        heroDown = true;
        heroIndex = 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    change main

    int main()
    {
        // test();
    
        init();
    
        int timer   = 0;//调用时间
    
        while (1)
        {
    
    
            keyEvent();
    
            timer += getDelay();
    
            if (timer > 30)
            {
                timer = 0;
                updata = true;
    
            }
    
            if (updata)
            {
                updata = false;//不能一直刷
                BeginBatchDraw();
                updataBg();
                updataHero();
                updataEnemy();
                FlushBatchDraw();
    
                fly();
            }
            //Sleep(20);
    
        }
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    void fly()
    {
        for (int i = 0; i < 3; i++)
        {
            // same speed
            bgX[i] -= 2;
            if (bgX[i] < -WIN_WINDTH)
            {
                bgX[i] = 0;
            }
    
        }
    
        //实现跳跃
        if (heroJump)
        {
    
    
            if (heroY < jumpHeightMax)
            {
                //上升
                heroJumpOff = 4;
    
            }
    
            heroY += heroJumpOff;
    
            if (heroY > 345 - imgHeros[0].getheight())
            {
                heroJump = false;
                heroJumpOff = -4;
            }
        }
        else if (heroDown)
        {
            heroIndex++;
    
            if (heroIndex >= 12)
            {
                heroIndex = 0;
                heroDown = false;
            }
            //heroIndex = (heroIndex + 1) % 12;
        }
    
        else
        {//不跳跃的时候
            heroIndex = (heroIndex + 1) % 12;
        }
    
        //创建障碍物
        static int frameCount = 0;
        static int enemyFre = 50;
        frameCount++;
        if (frameCount > enemyFre) {
            frameCount = 0;
            enemyFre = 50 + rand() % 50;
            createObstacle();
        }
    
        //更新所有障碍物的坐标
        for (int i = 0; i < OBSTACLE_COUNT; i++)
        {
            if (obstacles[i].exist)
            {
                obstacles[i].x -= (obstacles[i].speed + bgSpeed[2]);
    
    
                if (obstacles[i].x < (- obstacleImgs[obstacles[i].type][0].getwidth() * 2) )//跑出screen
                {
                    obstacles[i].exist = false;
    
                }
                int len = obstacleImgs[obstacles[i].type].size();
    
                obstacles[i].imgIndex = (obstacles[i].imgIndex + 1) % len;
    
            }
    
    
        }
    
        
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    new fly

     void fly()
    {
        for (int i = 0; i < 3; i++)
        {
            // same speed
            bgX[i] -= 2;
            if (bgX[i] < -WIN_WINDTH)
            {
                bgX[i] = 0;
            }
    
        }
    
        //实现跳跃
        if (heroJump)
        {
    
    
            if (heroY < jumpHeightMax)
            {
                //上升
                heroJumpOff = 4;
    
            }
    
            heroY += heroJumpOff;
    
            if (heroY > 345 - imgHeros[0].getheight())
            {
                heroJump = false;
                heroJumpOff = -4;
            }
        }
        else if (heroDown)
        {
            static int count = 0;
            count++;
            int delays[2] = { 4,10 };
            if (count >= delays[heroIndex])
            {
                count = 0;
                heroIndex++;
    
                if (heroIndex >= 2){
                    heroIndex = 0;
                    heroDown = false;
                }
    
            }
    
        }
    
        else
        {//不跳跃的时候
            heroIndex = (heroIndex + 1) % 12;
        }
    
        //创建障碍物
        static int frameCount = 0;
        static int enemyFre = 50;
        frameCount++;
        if (frameCount > enemyFre) {
            frameCount = 0;
            enemyFre = 50 + rand() % 50;
            createObstacle();
        }
    
        //更新所有障碍物的坐标
        for (int i = 0; i < OBSTACLE_COUNT; i++)
        {
            if (obstacles[i].exist)
            {
                obstacles[i].x -= (obstacles[i].speed + bgSpeed[2]);
    
    
                if (obstacles[i].x < (- obstacleImgs[obstacles[i].type][0].getwidth() * 2) )//跑出screen
                {
                    obstacles[i].exist = false;
    
                }
                int len = obstacleImgs[obstacles[i].type].size();
    
                obstacles[i].imgIndex = (obstacles[i].imgIndex + 1) % len;
    
            }
    
    
        }
    
        
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    实现柱子

    typedef  enum {
    
        TORTOISE,//0
        LION,//1
        HOOK1,
        HOOK2,//柱子
        HOOK3,
        HOOK4,
        OBSTACLE_TYPE_COUNT//2
    
    }obstacle_type;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    void init()
    {
        initgraph(WIN_WINDTH, WIN_HEIGHT);
    
        char name[64];
    
        //Load background resources
        for (int i = 0; i < 3; i++)
    
        {
            
            sprintf(name, "res/bg%03d.png", i + 1);//generate file name
            //loadimage(&imgBgs[i],"file name");
            loadimage(&imgsBgs[i], name);
            bgX[i] = 0;
        }
    
        for (int i = 0;i < 12;i++)
        {
            // res/hero1.png ~ hero12.png
            sprintf(name, "res/hero%d.png", i + 1);
            loadimage(&imgHeros[i], name);
    
        }
    
        heroX = WIN_WINDTH * 0.5 - imgHeros[0].getwidth() * 0.5;
        heroY = 345 - imgHeros[0].getheight();
    
        heroJump = false;
    
        jumpHeightMax = 345 - imgHeros[0].getheight() - 120;
    
        updata = true;
    
        heroJumpOff = -4;
    
    
        IMAGE imgTort;
        loadimage(&imgTort, "res/t1.png");
        vectorimgTortArray;
        imgTortArray.push_back(imgTort);
    
        obstacleImgs.push_back(imgTortArray);
    
    
        //lion
        IMAGE imgLion;
       
        for (int i = 0; i < 6; i++)
        { 
            vector imgLionArry;
            sprintf(name, "res/p%d.png", i + 1);
            loadimage(&imgLion, name);
            imgLionArry.push_back(imgLion);
            obstacleImgs.push_back(imgLionArry);
        }
    
        
    
    
    
        //对障碍物池进行初始化
        for (int i = 0; i < OBSTACLE_COUNT; i++)
        {
            obstacles[i].exist = false;//都不允许出场
        }
    
    
        loadimage(&imgHeroDown[0], "res/d1.png");
        loadimage(&imgHeroDown[1], "res/d2.png");
        //for (int i = 0; i < 12; i++)
        //{
        //    // res/hero1.png ~ hero12.png
        //    sprintf(name, "res/g%02d.png", i + 1);
        //    loadimage(&imgHeroDown[i], name);
    
        //}
    
        heroDown = false;
    
        IMAGE imgH;
       
        for (int i = 0; i < 4; i++)
        {
            vector imgHookArray;
            sprintf_s(name, sizeof(name), "res/h%d.png", i + 1);
            loadimage(&imgH, name,63,260,true);
            imgHookArray.push_back(imgH);
            obstacleImgs.push_back(imgHookArray);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    void createObstacle()
    {  
        srand((unsigned)time(NULL));
        int i;
       for( i = 0; i < OBSTACLE_COUNT;i++)
        {
            if (obstacles[i].exist == false) {
                break;
            }
        }
    
        if (i >= OBSTACLE_COUNT) {
            return;
        }
    
        obstacles[i].exist = true;
        obstacles[i].imgIndex = 0;
        //obstacles[i].type = (obstacle_type)(rand() % OBSTACLE_TYPE_COUNT);
         obstacles[i].type = (obstacle_type)(rand() % 3);
        obstacles[i].y =  345 + 5 -  obstacleImgs[obstacles[i].type][0].getheight();
         obstacles[i].x = WIN_WINDTH; 
         if (obstacles[i].type == HOOK1)
         {
             obstacles[i].type = (obstacle_type)((int)(obstacles[i].type) + rand() % 4);
    
    
         }
       
        if (obstacles[i].type == TORTOISE)
        {
            obstacles[i].speed = 0;
            obstacles[i].power = 5;
    
        }
          else if (obstacles[i].type == LION)
        {
          
            obstacles[i].speed = 4;
            obstacles[i].power = 20;
        }
        //&&而且
          else if (obstacles[i].type >= HOOK1&&obstacles[i].type <= HOOK4)
        {
            obstacles[i].power = 20;
            obstacles[i].speed = 0;
            obstacles[i].y = 0;
        }
    
    
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    #include 
    #include 
    #include 
    #include 
    #include 
    #include "tools.h"
    #include 
    #include 
     
    #define OBSTACLE_COUNT 5
    #define WIN_WINDTH 1012
    #define WIN_HEIGHT 396
    
    using namespace std;
    
    
    typedef  enum {
    
        TORTOISE,//0
        LION,
        HOOK1,
        HOOK2,
        HOOK3,
        HOOK4,
        OBSTACLE_TYPE_COUNT
    
    }obstacle_type;
    
    //IMAGE obstacleImgs[3][12];
    //二维
    
    vector<vector<IMAGE>>  obstacleImgs;//存放所有障碍的图片
    
    Button btnStart;
    typedef struct obstacle//障碍物
    {
        obstacle_type type;//障碍物的类型
        int x, y;	//障碍物的坐标
        int speed;
        int power;//受到的伤害 
        bool exist;
        int imgIndex;//显示当前图片的序号
        bool hited;//表示是否已经发生碰撞
        bool passed;//表示障碍物是否被通过
    }obstacle_t;
    
    obstacle_t obstacles[OBSTACLE_COUNT];//障碍物的个数
    
    //global variable
    IMAGE imgsBgs[3];
    
    int bgX[3];
    
    int updata; //表示是否要刷新画面
    
    int bgSpeed[3] = { 1,2,4 };
    
    int jumpHeightMax;
    
    IMAGE imgHeros[12];
    
    IMAGE imgBackground;
    
    int heroBlood;
    
    int heroIndex;
    
    int heroX, heroY;
    
    bool heroJump;
    
    bool heroJump2;//二段跳
    
    bool heroGun;
    
    bool heroDown;
    
    IMAGE imgHeroDown[2];
    
    IMAGE imgHeroGun[12];
    
    int heroJumpOff;
    
    int lastObsIndex;
    
    int score;
    
    IMAGE imgSZ[10];
    
    
    void daoJiShi() 
    {
       
        IMAGE img[6];
        char name[64];
        for (int i = 0; i < 6; i++) {
            sprintf(name, "res/%d.png", i);
            loadimage(&img[i], name,300,250);
        }
    
        for (int i = 5; i >= 0; i--) {
            BeginBatchDraw();
            cleardevice();
            putimage(355,85, &img[i]);
            EndBatchDraw();
            mciSendString("play res/hit.mp3", 0, 0, 0);
            Sleep(1000);
        }
        cleardevice();
        mciSendString("stop res/hit.mp3", 0, 0, 0);
    }
    
    void createObstacle()
    {  
        srand((unsigned)time(NULL));
        int i;
       for( i = 0; i < OBSTACLE_COUNT;i++)
        {
            if (obstacles[i].exist == false) {
                break;
            }
        }
    
        if (i >= OBSTACLE_COUNT) {
            return;
        }
        obstacles[i].hited = false;
        obstacles[i].exist = true;
        obstacles[i].imgIndex = 0;
        //obstacles[i].type = (obstacle_type)(rand() % OBSTACLE_TYPE_COUNT);
         obstacles[i].type = (obstacle_type)(rand() % 3);
        obstacles[i].y =  345 + 5 -  obstacleImgs[obstacles[i].type][0].getheight();
         obstacles[i].x = WIN_WINDTH; 
         obstacles[i].passed = false;
    
         if (   lastObsIndex >= 0 
             &&obstacles[lastObsIndex].type >= HOOK1 
             && obstacles[lastObsIndex ].type<= HOOK4
             &&obstacles[i].type == LION
             &&obstacles[lastObsIndex].x>(WIN_WINDTH-500)){
             obstacles[i].type == TORTOISE;
         }
         lastObsIndex = i;//更新
         if (obstacles[i].type == HOOK1)
         {
             obstacles[i].type = (obstacle_type)((int)(obstacles[i].type) + rand() % 4);
    
    
         }
       
        if (obstacles[i].type == TORTOISE)
        {
            obstacles[i].speed = 0;
            obstacles[i].power = 5;
    
        }
          else if (obstacles[i].type == LION)
        {
          
            obstacles[i].speed = 4;
            obstacles[i].power = 20;
        }
        //&&而且
          else if (obstacles[i].type >= HOOK1&&obstacles[i].type <= HOOK4)
        {
            obstacles[i].power = 20;
            obstacles[i].speed = 0;
            obstacles[i].y = 0;
        }
    
    
    }
    
    void updataEnemy()
    {
        
    
        for (int i = 0; i <  OBSTACLE_COUNT; i++)
        {
            if (obstacles[i].exist) {
                 putimagePNG2(obstacles[i].x, obstacles[i].y, WIN_WINDTH, &obstacleImgs[obstacles[i].type] [obstacles[i].imgIndex]);
    
            }
        }
    
    }
    
    void init()
    {
        initgraph(WIN_WINDTH, WIN_HEIGHT, EW_SHOWCONSOLE);  
    
        char name[64];
    
        //载入背景图片
        for (int i = 0; i < 3; i++)
    
        {
            
            sprintf(name, "res/bg%03d.png", i + 1);//generate file name
            //loadimage(&imgBgs[i],"file name");
            loadimage(&imgsBgs[i], name);
            bgX[i] = 0;
        }
    
    
        //载入人物1
        for (int i = 0;i < 12;i++)
        {
            // res/hero1.png ~ hero12.png
            sprintf(name, "res/hero%d.png", i + 1);
            loadimage(&imgHeros[i], name);
    
        }
        
    
    
        heroX = WIN_WINDTH * 0.5 - imgHeros[0].getwidth() * 0.5;
        heroY = 345 - imgHeros[0].getheight();
    
        heroJump = false;
    
        heroGun = false;
    
        jumpHeightMax = 345 - imgHeros[0].getheight() - 150;
    
        updata = true;
    
        heroJumpOff = -4;
    
        //乌龟
        IMAGE imgTort;
        loadimage(&imgTort, "res/t1.png");
        vector<IMAGE>imgTortArray;
        imgTortArray.push_back(imgTort);
    
        obstacleImgs.push_back(imgTortArray);
    
    
        //狮子
       
         IMAGE imgLion;
         vector<IMAGE> imgLionArry;
    
        for (int i = 0; i < 6; i++)
        {   
     
            sprintf(name, "res/p%d.png", i + 1);
            loadimage(&imgLion, name);
            imgLionArry.push_back(imgLion);
           
        } 
        obstacleImgs.push_back(imgLionArry);
    
        
    
    
    
        //对障碍物池进行初始化
        for (int i = 0; i < OBSTACLE_COUNT; i++)
        {
            obstacles[i].exist = false;//都不允许出场
        }
    
    
        loadimage(&imgHeroDown[0], "res/d1.png");
        loadimage(&imgHeroDown[1], "res/d2.png");
       
       for (int i = 0; i < 12; i++)
        {
            // res/hero1.png ~ hero12.png
            sprintf(name, "res/g%02d.png", i + 1);
            loadimage(&imgHeroGun[i], name);
    
        }
      
    
        heroDown = false;
    
        IMAGE imgH;
       
        for (int i = 0; i < 4; i++)
        {
            vector<IMAGE> imgHookArray;
            sprintf_s(name, sizeof(name), "res/h%d.png", i + 1);
            loadimage(&imgH, name,63,260,true);
            imgHookArray.push_back(imgH);
            obstacleImgs.push_back(imgHookArray);
        }
    
        initButton(&btnStart, "res/btn-normal.jpg", "res/btn-press.jpg" ,131, 58, 0);
        btnStart.x = 436;
        btnStart.y = 259;
    
        loadimage(&imgBackground, "res/over.png");
        heroBlood = 100;
    
        //预加载音效
        preLoadSound("res/hit.mp3");
    
        //mciSendString(" play res/bg.mp3 repeat", 0, 0, 0);
    
        lastObsIndex = -1;
    
        score = 0; 
    
        for (int i = 0; i < 10; i++)
        {
            sprintf(name, "res/sz/%d.png", i);
            loadimage(&imgSZ[i], name);
        }
    }
    
     
    
    void checkHit()
    { 
        for (int i = 0; i < OBSTACLE_COUNT; i++)
         {
             if (obstacles[i].exist&& obstacles[i].hited == false)
            {
    
                int a1x, a1y, a2x, a2y;
                int off = 20;
                if (!heroDown)//非下蹲 奔跑 跳跃
                {
                    a1x = heroX + off ;
                    a1y = heroY + off;
    
                    a2x = heroX + imgHeros[heroIndex].getwidth() -  off;
                    a2y = heroY + imgHeros[heroIndex].getheight();
    
    
                }
                else { //下蹲状态
                    a1x = heroX + off;
                    a1y = 345  - imgHeroDown[heroIndex].getheight();
    
                    a2x = heroX + imgHeroDown[heroIndex].getwidth() - off ;
                    a2y =345;
                }
                
                IMAGE img = obstacleImgs[obstacles[i].type][obstacles[i].imgIndex];
    
                int b1x = obstacles[i].x + off;
                int b1y = obstacles[i].y + off;
                int b2x = obstacles[i].x + img.getwidth() - off;  
                int b2y = obstacles[i].y + img.getheight() - 10;
    
                if (rectIntersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y))
                {
                    //mciSendString("play res/hit.mp3", 0, 0, 0);
                    heroBlood -= obstacles[i].power;
                   printf("血量剩余 %d\n",heroBlood);
                   playSound("res/hit.mp3");
                   obstacles[i].hited = true;
                }
            }
        }
    
    }
    
    
    
    
    void fly()
    {
        srand((unsigned)time(NULL));
        for (int i = 0; i < 3; i++)
        {
            // same speed
            bgX[i] -= 2;
            if (bgX[i] < -WIN_WINDTH)
            {
                bgX[i] = 0;
            }
    
        }
    
        //实现跳跃
        if (heroJump)
        {
    
    
            if (heroY < jumpHeightMax)
            {
                //上升
                heroJumpOff = 4;
    
            }
    
            heroY += heroJumpOff;
    
            if (heroY > 345 - imgHeros[0].getheight())
            {
                heroJump = false;
                heroJumpOff = -4;
            }
            
    
        }
        else if (heroDown)
        {
            static int count = 0;
            count++; 
            int delays[2] = { 8,30 };
            if (count >= delays[heroIndex])
            {
                count = 0;
                heroIndex++;
    
                if (heroIndex >= 2){
                    heroIndex = 0;
                    heroDown = false;
                }
    
            }
    
        }
        else if (heroGun)
        {
           
                heroIndex++;
                Sleep(5);
    
                if (heroIndex >= 12) {
                   
                    heroGun = false;
                }
    
        }
        else
        {//不跳跃的时候
            heroIndex = (heroIndex + 1) % 12;
        }
    
        //创建障碍物
        static int frameCount = 0;
        static int enemyFre = 50;
        frameCount++;
        if (frameCount > enemyFre) {
            frameCount = 0;
            enemyFre = 50 + rand() % 50;
            createObstacle();
        }
    
        //更新所有障碍物的坐标
        for (int i = 0; i < OBSTACLE_COUNT; i++)
        {
            if (obstacles[i].exist)
            {
                obstacles[i].x -= (obstacles[i].speed + bgSpeed[2]);
    
    
                if (obstacles[i].x < (-(obstacleImgs[obstacles[i].type][0].getwidth() * 2) ) )//跑出screen
                {
                    obstacles[i].exist = false;
    
                }
                int len = obstacleImgs[obstacles[i].type].size();
    
                obstacles[i].imgIndex = (obstacles[i].imgIndex + 1) % len;
    
            }
    
            //碰撞检测
            checkHit();
    
        }      
    
    }
    
    
    void updataBg()
    {
        putimagePNG2(bgX[0], 0, &imgsBgs[0]);
        putimagePNG2(bgX[1], 119, &imgsBgs[1]);
        putimagePNG2(bgX[2], 330, &imgsBgs[2]);
    }
    
    
    void down()
    {
        updata = true;
        heroDown = true;
        heroIndex = 0;
    }
    
    void jump()
    {
    
        heroJump = true;
        updata = true;
    
    }
    
    void gun()
    {
        heroGun = true;
        updata = true;
    }
    
    
    
    void keyEvent()
    {
        char ch;
        if (_kbhit())//如果有按键输入
        {
    
            ch = _getch();//_getch() 不需要按下回车 直接读取
    
            if (ch == 'w')
            {
                jump();
            }
            else if (ch == 's')
            {
                down();
            }
            else if (ch == 'd')
            {
                gun();//滚
            }
             
           
        }
    
    
    }
    
    
    
    void updataHero()
    {
        if (!heroDown && !heroGun)
        {
            putimagePNG2(heroX, heroY,
                &imgHeros[heroIndex]);
        }else if(heroDown) {
            int y = 345 - (imgHeroDown[heroIndex].getheight());
                putimagePNG2(heroX, y, &imgHeroDown[heroIndex]);
    
        }
        else if (heroGun) {
            int y = 345 - (imgHeroGun[heroIndex].getheight());
            putimagePNG2(heroX, y, &imgHeroGun[heroIndex]);
        }
    }
    
    void welcome()
    {
        mciSendString("play res/bg.mp3", 0, 0, 0);
        
        for (;;)
        {
            MOUSEMSG m = GetMouseMsg();
            FlushMouseMsgBuffer(); //不能少,后缀快速拖动顶部的标题按钮,讲导致鼠标消息太多
            switch (m.uMsg)
            {
            case WM_MOUSEMOVE:
                if (checkButtonSelect(&btnStart, &m)) {
                    btnStart.pressed = true;
                    drawButton(&btnStart);
                }
                else {
                    btnStart.pressed = false;
                    drawButton(&btnStart);
                }
                break;
    
            case WM_LBUTTONDOWN:
                        if (checkButtonSelect(&btnStart, &m))
                        {
                                btnStart.pressed = true;
                                drawButton(&btnStart);
                                break;                     
                        }
                            
                             
            case WM_LBUTTONUP:
                if ( checkButtonSelect(&btnStart, &m) )
                {
    
                    if (btnStart.pressed)
                    {
                        mciSendString("stop res/bg.mp3", 0, 0, 0);
                       // daoJiShi();
                        return;
                    }
                }
    
                break;
            }
    
        }
    
    }
    void updataBloodBar()
    {
        drawBloodBar(10, 10, 200, 10, 2, BLUE, DARKGRAY, RED,heroBlood/100.0);
    }
    
    void   checkOver()
    {
        if (heroBlood <= 0)
        {
          
           // cleardevice();
            loadimage(0, "res/over.png");
            FlushBatchDraw();
            mciSendString("stop res/bg.mp3", 0, 0, 0);
            system("pause");
            
             heroBlood = 100;
             score = 0; 
            mciSendString("play res/bg.mp3 repeat", 0, 0, 0);
      
         }  
    }
    
    void checkScore()
    {
        for (int i = 0; i < OBSTACLE_COUNT; i++)
        {
            if (obstacles[i].exist &&
                    obstacles[i].passed == false && 
                    obstacles[i].hited ==false && 
                    obstacles[i].x + obstacleImgs[obstacles[i].type][0].getwidth() < heroX)  {
          
    
                score++;//加分
            obstacles[i].passed = true;
            printf("score :%d \n", score);
            }
    
        }
     }
    
    void  updataScore()
    {
        int x = 20, y = 25;
        char str[8];
        sprintf(str, "%d", score);
        for (int i = 0; str[i]; i++)
        {
            int sz = str[i] - '0';
            putimagePNG(x,y, &imgSZ[sz]);
    
            x += imgSZ[sz].getwidth() + 5;
        }
    }
    
    
    int main()
    {
        // test();
    
        init();
       // putimage(0,0,&imgBackground);
      // drawButton(&btnStart);
    
       // welcome();
    
    
       
        int timer   = 0;//调用时间
    
        while (1)
        {
    
    
            keyEvent();
    
            timer += getDelay();
    
            if (timer > 30)
            {
                timer = 0;
                updata = true;
    
            }
    
            if (updata)
            {
                updata = false;//不能一直刷
                BeginBatchDraw();
                updataBg();
                updataHero();
                updataEnemy();
                updataBloodBar();
    
                updataScore();
                FlushBatchDraw();
    
                checkOver();
    
                checkScore();
    
                fly();
    
            }
           
    
        }
        return 0;
    }
    
    
    
    
    int test()
    {
        IMAGE imgsBg[3];
        initgraph(WIN_WINDTH, WIN_HEIGHT);
        char name[64];
    
        //Load background resources
        for (int i = 0; i < 3; i++)
    
        {
    
            sprintf(name, "res/bg%03d.png", i + 1);//generate file name
            //loadimage(&imgBgs[i],"file name");
            loadimage(&imgsBgs[i], name);
            bgX[i] = 0;
        }
        putimagePNG2(0, 0, &imgsBgs[0]);
        putimagePNG2(0, 119, &imgsBgs[1]);
        putimagePNG2(0, 330, &imgsBgs[2]);
        Sleep(10000);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560
    • 561
    • 562
    • 563
    • 564
    • 565
    • 566
    • 567
    • 568
    • 569
    • 570
    • 571
    • 572
    • 573
    • 574
    • 575
    • 576
    • 577
    • 578
    • 579
    • 580
    • 581
    • 582
    • 583
    • 584
    • 585
    • 586
    • 587
    • 588
    • 589
    • 590
    • 591
    • 592
    • 593
    • 594
    • 595
    • 596
    • 597
    • 598
    • 599
    • 600
    • 601
    • 602
    • 603
    • 604
    • 605
    • 606
    • 607
    • 608
    • 609
    • 610
    • 611
    • 612
    • 613
    • 614
    • 615
    • 616
    • 617
    • 618
    • 619
    • 620
    • 621
    • 622
    • 623
    • 624
    • 625
    • 626
    • 627
    • 628
    • 629
    • 630
    • 631
    • 632
    • 633
    • 634
    • 635
    • 636
    • 637
    • 638
    • 639
    • 640
    • 641
    • 642
    • 643
    • 644
    • 645
    • 646
    • 647
    • 648
    • 649
    • 650
    • 651
    • 652
    • 653
    • 654
    • 655
    • 656
    • 657
    • 658
    • 659
    • 660
    • 661
    • 662
    • 663
    • 664
    • 665
    • 666
    • 667
    • 668
    • 669
    • 670
    • 671
    • 672
    • 673
    • 674
    • 675
    • 676
    • 677
    • 678
    • 679
    • 680
    • 681
    • 682
    • 683
    • 684
    • 685
    • 686
    • 687
    • 688
    • 689
    • 690
    • 691
    • 692
    • 693
    • 694
    • 695
    • 696
    • 697
    • 698
    • 699
    • 700
    • 701
    • 702
    • 703
    • 704
    • 705
    • 706
    • 707
    • 708
    • 709
    • 710
    • 711
    • 712
    • 713
    • 714
    • 715
    • 716
    • 717
    • 718
    • 719
    • 720
    • 721
    • 722
    • 723
    • 724
    • 725
    • 726
    • 727
    • 728
    • 729
    • 730
    • 731
    • 732
    • 733
  • 相关阅读:
    Brocade FOS下载 博客光交换机固件升级
    Photoshop (PS)下载安装
    一文了解Linux上TCP的几个内核参数调优
    【WSL报错】执行:wsl --list --online;错误:0x80072ee7
    Redux 学习笔记
    GET请求和POST请求区别
    Python中的数据常见问题
    Windows安全中心内存完整性无法打开问题的处理方法
    C# 模式匹配完全指南
    Informatica使用操作流程及Expression(表达式转换)案例2
  • 原文地址:https://blog.csdn.net/m0_72703340/article/details/131776463