c++编写天天酷跑游戏

这篇具有很好参考价值的文章主要介绍了c++编写天天酷跑游戏。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

素材加Q群:723550115

天天酷跑2.0版本:2.0

游戏背景设置




  1. Start importing material (background picture)
#include <graphics.h>

Create a graph window and define macros for the window

 #define WIN_WINDTH 1012
 #define WIN_HEIGHT 396

initgraph(WIN_WINDTH, WIN_HEIGHT);

  1. Import game background (scroll cycle)

    #include <graphics.h>
    
    
    	//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);
    
        }
        
       
        
    
    }
    
    
    
    
     ```c++
     int main()
     {
          init ();
          updataBg();
     
         return 0;
     }
     ```
    
  2. Local modularization
    Game background coordinates

    void updataBg()
    {
    
    	putimage(0,0,&imgBgs[0]);
        putimage(0,119,&imgBgs[1]);
        putimage(0,330,&imgBgs[2]);
        
        
        
    }
    

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

change initialization

#include <graphics.h>
 


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]);          
   }
    


背景变黑了




Call tool H transparent map
Easy background black

add tools file

#include "tools.h"

**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]);          
   }
    
添加带有一些lib和Header文件的“tool.h”文件
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
实现循环滚动的效果
int main()
{
     init ();
    
    while(1)
    {
          
        updataBg();
        

    }
   

    return 0;
}



更改坐标

void fly()
{
for(int i = 0 ;i <3 ;i++)
{
    // same speed
    bgX[i] -= 2;
}

}
int bgSpeed[3] = { 1,2,4};
void fly()
{
for(int i = 0 ;i <3 ;i++)
{
   // same speed
   bgX[i] -= bgSpeed[1];
}

}
int main()
{
     init ();
    
    while(1)
    {
          
        updataBg();
        
        fly();
        
        // add Frame waiting
        Sleep(20);

    }
   

    return 0;
}

导致记忆冲突??

使用优化版本

void updataBg()
{
    putimagePNG2(bgX[0],0, &imgBgs[0]);
    putimagePNG2(bgX[1],119, & imgBgs[1]);
    putimagePNG2(bgX[2],330, & imgBgs[2]);
}
void fly()
{
for(int i = 0 ;i <3 ;i++)
{
    // same speed
    bgX[i] -= 2;
    if(bgX[i] < -WIN_WINDTH)
    {
        bgX[i] = 0;
    }
    
}

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

Hero appera

define goba l variable

IMAGE imgHeros[12];

加载玩家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);
    
    }
    
   
}

让人物跑起来

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

定义玩家的x坐标

int heroX,heroY;
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;
   
}
int heroIndex;//玩家显示的是第几章frame
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;  
    

}

实现玩家跳跃

用户输入检测函数

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void keyEvent()
{
    char ch;
 	if(_kbhit())//如果有按键输入
    {
        
	 ch = _getch();//_getch() 不需要按下回车 直接读取
      
        if(ch == '')
        {
			jump();
        }
        
    }
 

}

跳跃函数

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void jump()
{

    	heroJump = true;
    

}


//跳跃开关

bool heroJump;
    
  //初始化 
    void init()
{
    //。。。
  heroJump = false;
}

开始跳跃 实现

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

}

调用跳跃

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

跳跃的最高度

全局变量

int jumpHeightMax;

void init()

{

//...

jumpHeightMax =  345 - imgHeros[0].getheight() - 120;

}

定义跳跃偏移量

int heroJumpOff;
void init()
{
///....
heroJumpOff = -4;

}

跳的时候腿不要动

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

}

优化帧等待

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

getDelay() function

int updata; //表示是否要刷新画面

void init()
{

//...
updata = true;

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

更新jump

void jump()
{

    heroJump = true;
    updata = true;

}


 
        if(updata)
        {
            updata = false;//不能一直刷
         	BeginBatchDraw(); 
        	updataBg();
       	 	putimagePNG2(heroX, heroY,                           	 &imgHeros[heroIndex]);
        	FlushBatchDraw();
     
         	fly();
        }
        //Sleep(20);
 
 

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

实现障碍物的出现

定义图片

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


初始化

void init()
{
 // ..
    loadimage(&imgTortoise,"res/t1.png");
    torTooiseExist = false;
    torToiseY = 345 - imgTortoise.getheight() +5;
}

显示小乌龟

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;
        }
	       
        
    }
           
}
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;
}
void updataEnemy()
{
    //渲染小乌龟
    if(torTooiseExist){
		putimagePNG2(torToiseX,torToiseY,WIN_WIDTH,&imgTortoise);
    }
    
}

更新小乌龟的位置

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

使小乌龟出现为随机值

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

使用结构体封装障碍物

#include <vector>
using namespace std;
#define OBSTACLE_COUNT 10
typedef  enum {

    TORTOISE,//0
    LION,//1
    OBSTACLE_TYPE_COUNT//2
    
}obstacle_type;

//IMAGE obstacleImgs[3][12];
//二维
vector<vector<IMAGE>>  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];//障碍物的个数


改用结构体后的代码

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

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");
    vector<IMAGE>imgTortArray;
    imgTortArray.push_back(imgTort);
     
    obstacleImgs.push_back(imgTortArray);
    
    
    //lion
    IMAGE imgLion;
     vector<IMAGE>imgLionArry;
    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;//都不允许出场
    }
    
}

更爱渲染小乌龟

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])
            
      }
  }
    
}

修改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();
    }
        
    
    
 
}

创建障碍物

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

更新障碍物坐标

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

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


实现人物下蹲




下蹲图片

IMAGE imgHeroDown[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(&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");
    vector<IMAGE>imgTortArray;
    imgTortArrat.push_back(imgTort);
     
    obstacleImgs.push_back(imgTortArray);
    
    
    //lion
    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(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);

    }
}

按键输入

void keyEvent()
{
    char ch;
    if (_kbhit())//如果有按键输入
    {

        ch = _getch();//_getch() 不需要按下回车 直接读取

        if (ch == ' ')
        {
            jump();
        }

        else if (ch == 'a')
        {
            down();
        }
    }


}
bool heroDown;

初始化

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");
    //vector<IMAGE>imgTortArray;
    //imgTortArray.push_back(imgTort);

    //obstacleImgs.push_back(imgTortArray);


    //lion
    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;//都不允许出场
    }


     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;
}
void down()
{
    updata = true;
    heroDown = true;
    heroIndex = 0;

}

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

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;

        }


    }

    

}

new fly文章来源地址https://www.toymoban.com/news/detail-579097.html

 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;

        }


    }

    

}

实现柱子

typedef  enum {

    TORTOISE,//0
    LION,//1
    HOOK1,
    HOOK2,//柱子
    HOOK3,
    HOOK4,
    OBSTACLE_TYPE_COUNT//2

}obstacle_type;

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");
    vector<IMAGE>imgTortArray;
    imgTortArray.push_back(imgTort);

    obstacleImgs.push_back(imgTortArray);


    //lion
    IMAGE imgLion;
   
    for (int i = 0; i < 6; i++)
    { 
        vector<IMAGE> 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<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);
    }
}
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;
    }


}
 
#include <graphics.h>
#include <stdlib.h>
#include <easyx.h>
#include <conio.h>
#include <stdio.h>
#include "tools.h"
#include <vector>
#include <time.h>
 
#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;
}

到了这里,关于c++编写天天酷跑游戏的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • python制作简单版天天酷跑,是不是你平日里摸鱼小游戏呀

    前言 嗨喽,大家好呀~这里是爱看美女的茜茜呐 天天酷跑是一款轻松好玩、简单时尚的跑酷类手机游戏。 那我们能不能用python模拟出这个一个游戏呢? 答案当然是可以的,今天我就给大家带来简易版的天天酷跑小游戏 开发环境: 版 本: python 3.8 编辑器:pycharm 2021.2 准备事

    2024年02月04日
    浏览(33)
  • 小学期笔记——天天酷跑3

    画笔的载体是图层   图层的载体是窗体 效果: ------------------- 效果: ---------------------- 实现一个接口可以理解成添加一个能力  接口可以理解为能力的集合 对于abstract(判断:没有方法体),尽量使用隐式声明的就不写 效果: 点击登录后出现: 使用JDBC插入一條數據, ur

    2024年02月15日
    浏览(41)
  • [C/C++]天天酷跑超详细教程-中篇

     个人主页:北·海  🎐CSDN新晋作者  🎉欢迎 👍点赞✍评论⭐收藏 ✨收录专栏:C/C++ 🤝希望作者的文章能对你有所帮助,有不足的地方请在评论区留言指正,大家一起学习交流!🤗 天天酷跑,一款童年游戏,主要是进行跳跃操作,和躲避障碍物,中篇主要实现人物的下蹲,随机

    2024年02月10日
    浏览(39)
  • unity——小球酷跑游戏制作

    所有变量与物体名的命名原则都是见名知意 一、创建一个unity项目 二、Create所需3Dobject 1.Player 2.walls 三、添加属性 : 1.添加在Player上 a.添加Rigidbody组件 b.添加new script组件,并命名为PlayMove,代码如下: 2.添加到walls上 a.首先create empty将wall包含 b.在Wall上添加new script组件,代码

    2023年04月24日
    浏览(31)
  • Unity 3D 设计小球酷跑游戏

    1. 分别创建有关Folder(Materials、Prefabs、Scenes、Script) 2. 创建小球Player()、Walls(WallUp、WallDown)。 3D Object Sphere (更改名为Player) 3D Object Cube (更改名为WallUp) 3D Object Cube (更改名为WallDown) Create Empty 更改名为Walls,并把WallUp、WallDown拖入到Walls子目录下。 3. 为Player添加组件(Add Compon

    2024年02月08日
    浏览(29)
  • 用unity做的简单小游戏------“小球酷跑“

    游戏介绍: 游戏由小球和障碍物组成,在所布置的游戏背景中进行游戏,如图所示。 游戏主要以控制小球移动躲过障碍物继续行走为玩法,是一个简单的不够完整的小游戏,下面将会简单地介绍在unity中如何制作出这个小游戏。 场景设置 : 先创造一个小球和两个方块,将两个

    2024年02月01日
    浏览(36)
  • c++游戏小技巧9:windows.h 句柄介绍+实例

    目录 1.前言 2.正文 1.COORD(结构体) 2.HANDLE(句柄) 3.HWND(句柄) 4.RECT(结构体) 5.CONSOLE_SCREEN_BUFFER_INFO(结构体) 6.point(结构体) 7.代码实例(四个) 1.键鼠操作 2.获得运行框相关信息 3.遍历windows全部可见窗体(执行结果应坤而异) 4.移动运行框 3.后文 (关于MFC的事情我在鸽一段

    2024年02月03日
    浏览(31)
  • 利用python编写小游戏,用python编写的游戏

    大家好,小编为大家解答利用python编写小游戏的问题。很多人还不知道用python编写的游戏,现在让我们一起来看看吧! 小朋友们好,大朋友们好! 我是猫妹,一名爱上Python编程的小学生。 欢迎和猫妹一起,趣味学Pythonpython简单新年祝福代码。 今日主题 你玩过游戏吗? 你喜

    2024年01月15日
    浏览(33)
  • python编写一个简单的游戏,python编写小游戏的代码

    大家好,本文将围绕如何用python编写一个简单的小游戏展开说明,用python做一个小游戏代码是一个很多人都想弄明白的事情,想搞清楚python编写小游戏详细教程需要先了解以下几个事情。 今天玩点别的吧都说 Python 除了生孩子什么都能干 咱们今天就用 Python 写个小游戏 贪吃蛇

    2024年02月02日
    浏览(49)
  • python编写小游戏详细教程,python编写小游戏的代码

    大家好,小编来为大家解答以下问题,python编写小游戏详细教程,python编写小游戏的代码,现在让我们一起来看看吧! 今天给大家带来十五个Python小游戏,找回童年的同时学习编程还可以摸鱼, 源码附上结尾领取。 一、接金币(1分) 普通难度:❤ 玩法介绍: 吃金币,控制

    2024年01月17日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包