简单的天天酷跑小游戏实现

初级函数实现人物,背景,小乌龟的移动

#include <graphics.h>
#include <iostream>
#include <Windows.h>
#include "tools.h"
#include <mmsystem.h>
#include <conio.h>
#include <time.h>//时间头文件
#include <cstdlib>//随机数文件
#pragma comment(lib, "winmm.lib")
using namespace std;
/*日志:游戏界面,(游戏窗口(init),游戏背景(3重背景以不同的速度循环滚动(updateBg(渲染背景)))
实现人物(跳跃活动)随机出现道具加分
*/
#define WIN_WIDTH   1012
#define WIN_HEIGHT  396#define BG_IMAGE    3//多少张背景图片
IMAGE imgBgs[BG_IMAGE];//背景图片
int bgX[3];//背景图片x的初始位置
int bgXSpeed[3] = { 1,2,3 };//人物图片
#define HERO_IMAGE  12
int index = HERO_IMAGE;
IMAGE imgHeros[HERO_IMAGE];
int HERO_X;
int HERO_Y;
bool hero_jump;//判断人物是否跳跃
int heroJumpMax;//人物跳跃的最大值
int heroJumpMaxoff; //人物跳跃的偏移量//小乌龟图片
#define tortoiseNum 7
IMAGE imgTortoise[tortoiseNum];
int tortoiseX;//小乌龟的水平坐标
int tortoiseY;
bool tortoiseExise;//设置一次只显示一个小乌龟
int index1 = tortoiseNum;
bool update1;void init() {initgraph(WIN_WIDTH, WIN_HEIGHT);char name[64];for (int i = 0; i < BG_IMAGE; i++){sprintf_s(name, "res/bg%03d.png", i + 1);//不够三位前面补零loadimage(&imgBgs[i], name);bgX[i] = 0;}//加载人物图片for (int i = 0; i < HERO_IMAGE; i++) {sprintf_s(name, "res/hero%d.png", i + 1);loadimage(&imgHeros[i], name);}//加载小乌龟的图片for (int i = 0; i < tortoiseNum; i++) {sprintf_s(name, "res/t%d.png", i + 1);loadimage(&imgTortoise[i], name);}//设置小乌龟相关信息tortoiseExise = false;update1 = false;//设置人物位置HERO_X = WIN_WIDTH * 0.5 - imgHeros[0].getwidth() * 0.5;HERO_Y = 295-imgHeros[0].getheight()*0.5;hero_jump = false;heroJumpMax= 295 - imgHeros[0].getheight() * 0.5 -120;heroJumpMaxoff = -4;
}
//玩家跳跃的开关
void jump() {hero_jump = true;update1 = true;
}
//设置背景图片不同速度移动
void fly() {//三重背景回位for (int i = 0; i < BG_IMAGE; i++){bgX[i] -= bgXSpeed[i];if (bgX[i] < -WIN_WIDTH) {bgX[i] = 0;//设置回位}}//人物实现跳跃if (hero_jump) {if (HERO_Y < heroJumpMax) {heroJumpMaxoff = 4;}HERO_Y += heroJumpMaxoff;if (HERO_Y > 295 - imgHeros[0].getheight() * 0.5){hero_jump = false;heroJumpMaxoff = -4;}}else {//跳跃的时候不会刷新图片帧index = (index + 1) % 12;//人物图片帧}index1 = (index1 + 1) % 7;//小乌龟图片帧//创建小乌龟static int torZhen = 0;static int torZhen1 = 100;torZhen++;if (torZhen > torZhen1) {torZhen = 0;if (!tortoiseExise){tortoiseExise = true;tortoiseX = WIN_WIDTH + imgTortoise[0].getwidth();tortoiseY = 300  + imgTortoise[0].getheight()*0.4;}torZhen1 = 100 + rand() % 300;}if (tortoiseExise){tortoiseX -= bgXSpeed[2];if (tortoiseX < -imgTortoise[0].getwidth()) {tortoiseExise = false;}}
}//渲染背景
void updateBg() {putimagePNG2(bgX[0],  0,  &imgBgs[0]);putimagePNG2(bgX[1], 119, &imgBgs[1]);putimagePNG2(bgX[2], 330, &imgBgs[2]);//实现玩家奔跑putimagePNG2(HERO_X, HERO_Y, &imgHeros[index]);}
//渲染障碍物
void updateEmy() {if (tortoiseExise){//实现小乌龟图片帧putimagePNG2(tortoiseX, tortoiseY,WIN_WIDTH, &imgTortoise[index1]);}}
//处理按键事件
void keyEvent() {//键盘空格跳跃char c;if (_kbhit()) {c = _getch();if (c == ' '){jump();}}//鼠标左键跳跃/*MOUSEMSG msg;msg = GetMouseMsg();if (msg.uMsg == WM_LBUTTONDOWN) {jump();}*/
}
int main(void) {init();int timer = 0;while (1) {keyEvent();timer += getDelay();//距离上一次相差多久时间if (timer>30){timer = 0;update1 = true;}if (update1) {update1 = false;BeginBatchDraw();//设置双缓冲fly();updateBg();//渲染图片updateEmy();EndBatchDraw();}}system("pause");return 0;
}

封装其他障碍物

#include <graphics.h>
#include <iostream>
#include <Windows.h>
#include "tools.h"
#include <mmsystem.h>
#include <conio.h>
#include <vector>//C++提供的长度可变数组
#pragma comment(lib, "winmm.lib")
using namespace std;
/*日志:游戏界面,(游戏窗口(init),游戏背景(3重背景以不同的速度循环滚动(updateBg(渲染背景)))
实现人物(跳跃活动)随机出现道具加分
*/
#define WIN_WIDTH    1012
#define WIN_HEIGHT   396
bool update1;
#define OBSTRACT_NUM 10#define BG_IMAGE    3//多少张背景图片
IMAGE imgBgs[BG_IMAGE];//背景图片
int bgX[3];//背景图片x的初始位置
int bgXSpeed[3] = { 1,2,4 };//人物图片
#define HERO_IMAGE  12
int index = HERO_IMAGE;
IMAGE imgHeros[HERO_IMAGE];
int HERO_X;
int HERO_Y;
bool hero_jump;//判断人物是否跳跃
int heroJumpMax;//人物跳跃的最大值
int heroJumpMaxoff; //人物跳跃的偏移量//小乌龟图片
#define tortoiseNum 7//小乌龟的图片数量
#define lionNum     6//狮子的数量
#define pillarNum   4//柱子数量int tortoiseX;//小乌龟的水平坐标
int tortoiseY;
bool tortoiseExise;//设置一次只显示一个小乌龟
int index1 = tortoiseNum;//设置障碍物枚举
typedef enum {tortiose,lion,pillar
}obstract_type;//IMAGE ObstractIMG[3][12];
//C++提供的长度可变数组
vector<vector<IMAGE>> ObstractIMG;//image ObstractIMG[][]
//设置障碍物的属性
typedef struct obstract {obstract_type type;//障碍物类型int imgIndex;//当前显示的图片序号int x, y;//障碍物的坐标int speed;int power;//杀伤力bool exist;
}obstract_t;
obstract_t obstracts[OBSTRACT_NUM];
void init() {initgraph(WIN_WIDTH, WIN_HEIGHT);char name[128];for (int i = 0; i < BG_IMAGE; i++){sprintf_s(name, "res/bg%03d.png", i + 1);//不够三位前面补零loadimage(&imgBgs[i], name);bgX[i] = 0;}update1 = false;//加载人物图片for (int i = 0; i < HERO_IMAGE; i++) {sprintf_s(name, "res/hero%d.png", i + 1);loadimage(&imgHeros[i], name);}//设置人物位置HERO_X = WIN_WIDTH * 0.5 - imgHeros[0].getwidth() * 0.5;HERO_Y = 295 - imgHeros[0].getheight() * 0.5;hero_jump = false;heroJumpMax = 295 - imgHeros[0].getheight() * 0.5 - 120;heroJumpMaxoff = -4;//加载小乌龟的图片IMAGE imgTortoise[tortoiseNum];for (int i = 0; i < tortoiseNum; i++) {sprintf_s(name, "res/t%d.png", i + 1);loadimage(&imgTortoise[i], name);}//使用封装//二维容器vector<IMAGE> imgTortArray1;for (int i = 0; i < tortoiseNum; i++){imgTortArray1.push_back(imgTortoise[i]);}ObstractIMG.push_back(imgTortArray1);//狮子图片IMAGE imgLion[lionNum];for (int i = 0; i < lionNum; i++) {sprintf_s(name, "res/p%d.png", i + 1);loadimage(&imgLion[i], name);}vector<IMAGE> imgTortArray2;for (int i = 0; i < lionNum; i++) {imgTortArray2.push_back(imgLion[i]);}ObstractIMG.push_back(imgTortArray2);//柱子图片IMAGE imgPillar[pillarNum];for (int i = 0; i < pillarNum; i++) {sprintf_s(name, "res/t%d.png", i + 1);loadimage(&imgPillar[i], name);}vector<IMAGE> imgTortArray3;for (int i = 0; i < pillarNum; i++) {imgTortArray3.push_back(imgPillar[i]);}ObstractIMG.push_back(imgTortArray3);//设置各种障碍物的共同属性for (int i = 0; i < OBSTRACT_NUM; i++){obstracts[i].exist = false;}}
void createObstract() {int i;for (i = 0; i < OBSTRACT_NUM; i++){if (obstracts[i].exist == false){break;}}if (i> OBSTRACT_NUM){return;}obstracts[i].exist = true;obstracts[i].imgIndex = 0;//设置随机出现障碍物的类型//枚举类型最后一个就是这个枚举的长度,强制转化obstracts[i].type =(obstract_type)(rand() % pillar);obstracts[i].x = WIN_WIDTH;if (obstracts[i].type==pillar) {obstracts[i].y = 0;}else {obstracts[i].y = 295;}if (obstracts[i].type==tortiose){obstracts[i].speed = 0;obstracts[i].power = 5;}else if (obstracts[i].type == lion) {obstracts[i].speed = 4;obstracts[i].power = 10;}else if (obstracts[i].type == pillar) {obstracts[i].speed = 0;obstracts[i].power = 20;}
}
//玩家跳跃的开关
void jump() {hero_jump = true;update1 = true;
}
//设置背景图片不同速度移动
void fly() {//三重背景回位for (int i = 0; i < BG_IMAGE; i++){bgX[i] -= bgXSpeed[i];if (bgX[i] < -WIN_WIDTH) {bgX[i] = 0;//设置回位}}//人物实现跳跃if (hero_jump) {if (HERO_Y < heroJumpMax) {heroJumpMaxoff = 4;}HERO_Y += heroJumpMaxoff;if (HERO_Y > 295 - imgHeros[0].getheight() * 0.5){hero_jump = false;heroJumpMaxoff = -4;}}else {//跳跃的时候不会刷新图片帧index = (index + 1) % 12;//人物图片帧}//创建小乌龟static int torZhen = 0;static int torZhen1 = 100;torZhen++;if (torZhen > torZhen1) {torZhen = 0;/*if (!tortoiseExise){tortoiseExise = true;tortoiseX = WIN_WIDTH + imgTortoise[0].getwidth();tortoiseY = 300  + imgTortoise[0].getheight()*0.4;}*///障碍物出现的函数createObstract();torZhen1 = 100 + rand() % 70;}/*if (tortoiseExise){tortoiseX -= bgXSpeed[2];if (tortoiseX < -imgTortoise[0].getwidth()) {tortoiseExise = false;}}*///更新障碍物的坐标for (int i = 0; i < OBSTRACT_NUM; i++){if (obstracts[i].exist) {obstracts[i].x -= obstracts[i].speed + bgXSpeed[2];if (obstracts[i].x < -ObstractIMG[obstracts[i].type][0].getwidth()*2) {obstracts[i].exist = false;}int len = ObstractIMG[obstracts[i].type].size();obstracts[i].imgIndex = (obstracts[i].imgIndex + 1) % len;}}
}//渲染背景
void updateBg() {putimagePNG2(bgX[0],  0,  &imgBgs[0]);putimagePNG2(bgX[1], 119, &imgBgs[1]);putimagePNG2(bgX[2], 330, &imgBgs[2]);//实现玩家奔跑putimagePNG2(HERO_X, HERO_Y, &imgHeros[index]);}
//渲染障碍物
void updateEmy() {//if (tortoiseExise){//	//实现小乌龟图片帧//	putimagePNG2(tortoiseX, tortoiseY,WIN_WIDTH, &imgTortoise[index1]);//}for (int  i = 0; i < OBSTRACT_NUM; i++){if (obstracts[i].exist) {putimagePNG2(obstracts[i].x, obstracts[i].y, WIN_WIDTH, &ObstractIMG[obstracts[i].type][obstracts[i].imgIndex]);}}}
//处理按键事件
void keyEvent() {//键盘空格跳跃char c;if (_kbhit()) {c = _getch();if (c == ' '){jump();}}//鼠标左键跳跃/*MOUSEMSG msg;msg = GetMouseMsg();if (msg.uMsg == WM_LBUTTONDOWN) {jump();}*/
}
int main(void) {init();int timer = 0;while (1) {keyEvent();timer += getDelay();//距离上一次相差多久时间if (timer>30){timer = 0;update1 = true;}if (update1) {update1 = false;BeginBatchDraw();//设置双缓冲updateBg();//渲染图片updateEmy();EndBatchDraw();fly();}}system("pause");return 0;
}

代码忒难分块了(直接放最好的源码了)

#include <graphics.h>
#include <iostream>
#include <Windows.h>
#include "tools.h"
#include <mmsystem.h>
#include <conio.h>
#include <vector>//C++提供的长度可变数组
#pragma comment(lib, "winmm.lib")
using namespace std;
/*日志:游戏界面,(游戏窗口(init),游戏背景(3重背景以不同的速度循环滚动(updateBg(渲染背景)))
实现人物(跳跃活动)随机出现道具加分
*/
#define WIN_WIDTH    1012
#define WIN_HEIGHT   396
bool update1;
#define OBSTRACT_NUM 10
#define WIN_NUM      5#define BG_IMAGE    3//多少张背景图片
IMAGE imgBgs[BG_IMAGE];//背景图片
int bgX[3];//背景图片x的初始位置
int bgXSpeed[3] = { 1,2,4 };//人物图片
#define HERO_IMAGE  12
int index = HERO_IMAGE;
IMAGE imgHeros[HERO_IMAGE];
//人物下蹲图片
IMAGE imgHerosDown[2];
int HERO_X;
int HERO_Y;
int heroBlood;
bool hero_jump;//判断人物是否跳跃
bool hero_down;//判断人物是否下蹲
int heroJumpMax;//人物跳跃的最大值
int heroJumpMaxoff; //人物跳跃的偏移量//小乌龟图片
#define tortoiseNum 7//小乌龟的图片数量
#define lionNum     6//狮子的数量
#define pillarNum   4//柱子数量int tortoiseX;//小乌龟的水平坐标
int tortoiseY;
bool tortoiseExise;//设置一次只显示一个小乌gui//设置障碍物枚举
typedef enum {tortiose,lion,pillar1,pillar2,pillar3,pillar4,pillar
}obstract_type;//IMAGE ObstractIMG[3][12];
//C++提供的长度可变数组
vector<vector<IMAGE>> ObstractIMG;//image ObstractIMG[][]
//设置障碍物的属性
typedef struct obstract {obstract_type type;//障碍物类型int imgIndex;//当前显示的图片序号int x, y;//障碍物的坐标int speed;int power;//杀伤力bool exist;bool hitH; //是否发生碰撞bool pass;//表示是否跨过障碍物
}obstract_t;
obstract_t obstracts[OBSTRACT_NUM];
//解决死亡陷阱
int lastObsIndex;
//记录分数
int scores;
//加分的图片数组
IMAGE imgScores[9];
void init() {initgraph(WIN_WIDTH, WIN_HEIGHT ,1);char name[128];preLoadSound("res/hit.mp3");mciSendString("play res/bg.mp3 repeat", 0, 0, 0);for (int i = 0; i < BG_IMAGE; i++){sprintf_s(name, "res/bg%03d.png", i + 1);//不够三位前面补零loadimage(&imgBgs[i], name);bgX[i] = 0;}update1 = false;//加载人物图片for (int i = 0; i < HERO_IMAGE; i++) {sprintf_s(name, "res/hero%d.png", i + 1);loadimage(&imgHeros[i], name);}//设置人物位置HERO_X = WIN_WIDTH * 0.5 - imgHeros[0].getwidth() * 0.5;HERO_Y = 345 - imgHeros[0].getheight();heroBlood = 100;hero_jump = false;hero_down = false;heroJumpMax = 295 - imgHeros[0].getheight() * 0.5 - 120;heroJumpMaxoff = -4;//lastObsIndex = -1;scores = 0;//加载小乌龟的图片IMAGE imgTortoise[tortoiseNum];for (int i = 0; i < tortoiseNum; i++) {sprintf_s(name, "res/t%d.png", i + 1);loadimage(&imgTortoise[i], name);}//使用封装//二维容器vector<IMAGE> imgTortArray;for (int i = 0; i < tortoiseNum; i++){imgTortArray.push_back(imgTortoise[i]);}ObstractIMG.push_back(imgTortArray);//狮子图片IMAGE imgLion[lionNum];for (int i = 0; i < lionNum; i++) {sprintf_s(name, "res/p%d.png", i + 1);loadimage(&imgLion[i], name);}vector<IMAGE> imgPArray;for (int i = 0; i < lionNum; i++) {imgPArray.push_back(imgLion[i]);}ObstractIMG.push_back(imgPArray);//柱子图片//我这里写循环会报错IMAGE imgPillar[pillarNum];vector<IMAGE> imgHArray;sprintf_s(name,sizeof(name) ,"res/h1.png");loadimage(&imgPillar[0], name, 63, 260, true);imgHArray.push_back(imgPillar[0]);ObstractIMG.push_back(imgHArray);vector<IMAGE> imgHArray1;sprintf_s(name, sizeof(name), "res/h2.png");loadimage(&imgPillar[1], name, 63, 260, true);imgHArray1.push_back(imgPillar[1]);ObstractIMG.push_back(imgHArray1);vector<IMAGE> imgHArray2;sprintf_s(name, sizeof(name),"res/h3.png");loadimage(&imgPillar[2], name, 63, 260, true);imgHArray2.push_back(imgPillar[2]);ObstractIMG.push_back(imgHArray2);vector<IMAGE> imgHArray3;sprintf_s(name, sizeof(name),"res/h4.png");loadimage(&imgPillar[3], name, 63, 260, true);imgHArray3.push_back(imgPillar[3]);ObstractIMG.push_back(imgHArray3);//设置各种障碍物的共同属性for (int i = 0; i < OBSTRACT_NUM; i++){obstracts[i].exist = false;}//加载分数图片for (int i = 0; i < 9; i++) {sprintf_s(name, "res/sz/%d.png", i);loadimage(&imgScores[i], name);}//设置人物下蹲素材loadimage(&imgHerosDown[0],"res/d1.png");loadimage(&imgHerosDown[1],"res/d2.png");
}
void createObstract() {int i;for (i = 0; i < OBSTRACT_NUM; i++){if (obstracts[i].exist == false){break;}}if (i> OBSTRACT_NUM){return;}obstracts[i].exist = true;obstracts[i].hitH = false;obstracts[i].pass = false;obstracts[i].imgIndex = 0;//设置随机出现障碍物的类型//枚举类型最后一个就是这个枚举的长度,强制转化obstracts[i].type =(obstract_type)(rand() % 3);if (lastObsIndex>=0 &&obstracts[lastObsIndex].type>=pillar1&&obstracts[lastObsIndex].type<=pillar4&&obstracts[i].type == lion&&obstracts[lastObsIndex].x>WIN_WIDTH -500) {obstracts[i].type = tortiose;}lastObsIndex = i;if (obstracts[i].type == pillar1){obstracts[i].type = (obstract_type)((int)(obstracts[i].type) + rand() % 4);}obstracts[i].x = WIN_WIDTH;obstracts[i].y = 345+5- ObstractIMG[obstracts[i].type][0].getheight();if (obstracts[i].type==tortiose){obstracts[i].speed = 0;obstracts[i].power = 5;}else if (obstracts[i].type == lion) {obstracts[i].speed = 4;obstracts[i].power = 10;}else if (obstracts[i].type >= pillar1 && obstracts[i].type <= pillar4) {obstracts[i].speed = 0;obstracts[i].power = 20;obstracts[i].y = 0;}
}
//计算障碍物
void checkHit() {for (int i = 0; i < OBSTRACT_NUM; i++) {if (obstracts[i].exist&& obstracts[i].hitH == false) {int a1x, a1y, a2x, a2y;int off = 30;if (!hero_down) {a1x = HERO_X + off;a1y = HERO_Y + off;a2x = HERO_X + imgHeros[index].getwidth() - off;a2y = HERO_Y + imgHeros[index].getheight();}else {a1x = HERO_X + off;a1y = 345 - imgHerosDown[index].getheight();a2x = HERO_X + imgHerosDown[index].getwidth() - off;a2y = 345;}IMAGE img = ObstractIMG[obstracts[i].type][obstracts[i].imgIndex];int b1x = obstracts[i].x +off;int b1y = obstracts[i].y +off;int b2x = obstracts[i].x + img.getwidth() - off;int b2y = obstracts[i].y +img.getheight() - 10;if (rectIntersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y)){heroBlood -= obstracts[i].power;//cout << "英雄血量:" << heroBlood << endl;playSound("res/hit.mp3");obstracts[i].hitH = true;}} }
}
//玩家跳跃的开关
void jump() {hero_jump = true;update1 = true;
}
//玩家下蹲
void down() {update1 = true;hero_down = true;index = 0;
}
//设置背景图片不同速度移动
void fly() {//三重背景回位for (int i = 0; i < BG_IMAGE; i++){bgX[i] -= bgXSpeed[i];if (bgX[i] < -WIN_WIDTH) {bgX[i] = 0;//设置回位}}//人物实现跳跃if (hero_jump) {if (HERO_Y < heroJumpMax) {heroJumpMaxoff = 4;}HERO_Y += heroJumpMaxoff;if (HERO_Y > 295 - imgHeros[0].getheight() * 0.5){hero_jump = false;heroJumpMaxoff = -4;}}else if (hero_down) {static int i = 0;int delayIndex[2] = { 4,30 };i++;if (i>=delayIndex[index]) {i = 0;index++;if (index >= 2) {hero_down = false;index = 0;}}}else {//跳跃的时候不会刷新图片帧index = (index + 1) % 12;//人物图片帧}//创建小乌龟static int torZhen = 0;static int torZhen1 = 100;torZhen++;if (torZhen > torZhen1) {torZhen = 0;/*if (!tortoiseExise){tortoiseExise = true;tortoiseX = WIN_WIDTH + imgTortoise[0].getwidth();tortoiseY = 300  + imgTortoise[0].getheight()*0.4;}*///障碍物出现的函数createObstract();torZhen1 = 100 + rand() % 70;}/*if (tortoiseExise){tortoiseX -= bgXSpeed[2];if (tortoiseX < -imgTortoise[0].getwidth()) {tortoiseExise = false;}}*///更新障碍物的坐标for (int i = 0; i < OBSTRACT_NUM; i++){if (obstracts[i].exist) {obstracts[i].x -= obstracts[i].speed + bgXSpeed[2];if (obstracts[i].x < -ObstractIMG[obstracts[i].type][0].getwidth()*2) {obstracts[i].exist = false;}int len = ObstractIMG[obstracts[i].type].size();obstracts[i].imgIndex = (obstracts[i].imgIndex + 1) % len;}}
}//渲染背景
void updateBg() {putimagePNG2(bgX[0],  0,  &imgBgs[0]);putimagePNG2(bgX[1], 119, &imgBgs[1]);putimagePNG2(bgX[2], 330, &imgBgs[2]);}
void updateHero() {if (!hero_down) {//实现玩家奔跑putimagePNG2(HERO_X, HERO_Y, &imgHeros[index]);}else {//实现玩家下蹲int y = 295 - imgHerosDown[index].getheight() * 0.5;putimagePNG2(HERO_X, 295, &imgHerosDown[index]);}
}
//渲染障碍物
void updateEmy() {//if (tortoiseExise){//	//实现小乌龟图片帧//	putimagePNG2(tortoiseX, tortoiseY,WIN_WIDTH, &imgTortoise[index1]);//}for (int  i = 0; i < OBSTRACT_NUM; i++){if (obstracts[i].exist) {putimagePNG2(obstracts[i].x, obstracts[i].y, WIN_WIDTH, &ObstractIMG[obstracts[i].type][obstracts[i].imgIndex]);}}//人物与障碍物的碰撞检测checkHit();
}
void updateBlood() {drawBloodBar(10, 10, 200, 10, 2, BLUE, DARKGRAY, RED, heroBlood / 100.0);
}
//检查游戏是否结束
void checkOver() {if (heroBlood<=0){loadimage(0, "res/over.png");FlushBatchDraw();mciSendString("stop res/bg.mp3",0,0,0);system("pause");mciSendString("play res/bg.mp3 repeat", 0, 0, 0);heroBlood = 100;scores = 0;}}
//计算分数
void checkScore() {for (int i = 0; i < OBSTRACT_NUM; i++){if (obstracts[i].exist && obstracts[i].pass==false&&obstracts[i].x+ObstractIMG[obstracts[i].type][0].getwidth()<HERO_X&&obstracts[i].hitH==false){if (obstracts[i].type>=pillar1&&obstracts[i].type<=pillar4){scores += 2;}scores++;cout << scores << endl;obstracts[i].pass = true;}}
}
//渲染分数
void updateScore() {char str[8];int x = 20;int y = 25;sprintf(str, "%d", scores);for (int i = 0; i < str[i]; i++){int sz = str[i] - '0';putimagePNG(x, y, &imgScores[sz]);x += imgScores[i].getwidth() + 5;}FlushBatchDraw();
}
//游戏审理
void checkWin() {if (scores>WIN_NUM){mciSendString("play res/win.mp3 repeat", 0, 0, 0);Sleep(1000);loadimage(0, "res/win.png");FlushBatchDraw();mciSendString("stop res/win.mp3", 0, 0, 0);system("pause");heroBlood = 100;scores = 0;mciSendString("play res/bg.mp3", 0, 0, 0);}
}
//处理按键事件
void keyEvent() {//键盘空格跳跃char c;if (_kbhit()) {c = _getch();if (c == ' '){jump();}else if (c == 'a') {down();}}//鼠标左键跳跃/*MOUSEMSG msg;msg = GetMouseMsg();if (msg.uMsg == WM_LBUTTONDOWN) {jump();}*/
}
int main(void) {init();loadimage(0, "res/over.png");FlushBatchDraw();system("pause");int timer = 0;while (1) {keyEvent();timer += getDelay();//距离上一次相差多久时间if (timer>30){timer = 0;update1 = true;}if (update1) {update1 = false;BeginBatchDraw();//设置双缓冲updateBg();//渲染图片updateBlood();//血条updateHero();updateEmy();checkOver();checkScore();//检查分数updateScore();//渲染分数checkWin();EndBatchDraw();fly();}}system("pause");return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/621314.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024最新最全【DDOS教学】,从零基础入门到精通,看完这一篇就够了

1、互联网安全现状 随着网络世界的高速发展&#xff0c;各行业数字化转型也在如火如荼的进行。但由于TCP/IP网络底层的安全性缺陷&#xff0c;钓鱼网站、木马程序、DDoS攻击等层出不穷的恶意攻击和高危漏洞正随时入侵企业的网络&#xff0c;如何保障网络安全成为网络建设中的刚…

【STK】手把手教你利用STK进行关联分析仿真01-STK/CAT模块介绍

关联分析工具(Conjunction Analysis Tool )主要用于分析航天发射或卫星在轨运行过程中与其他目标之间的接近情况,关联分析包括: 接近分析工具 Close Approach Tool CAT高级接近分析工具 AdvCAT激光接近分析工具 LaserCAT发射窗口分析工具 Launch Window Analysis今天主要介绍…

Mybatis记单词

&#xff08;一&#xff09;Mybatis 1.1 全局配置文件mybatis-config.xml 1、在resources文件夹下新建mybatis-config.xml和db.properties&#xff0c;mybatis-config.xml引入db.properties中关于数据库的配置&#xff1a; <properties resource"db.properties"…

单一附合导线网平差过程

1、导线网图示如下&#xff1a; 2、已知A、B、C、D点坐标和方位角、. 3、设定未知数及近似值。设待定点坐标、、、...、为未知数&#xff0c;则有2n个未知数。坐标近似值为由观测值推算而得的坐标值&#xff0c;分别为、、、...、。改正数分别为、...、。则有&#xff1a;&am…

html+css+Jquery 实现 文本域 文字数量限制、根据输入字数自适应高度

先看效果&#xff1a;初始的效果&#xff0c;样式多少有点问题&#xff0c;不重要&#xff01;重要的是功能&#xff01; 输入后&#xff1a; 根据文字长度&#xff0c;决定文本域长度 限制文字数量 话不多说&#xff0c;直接上代码&#xff01; <!DOCTYPE html> <h…

企业网盘助力数字化教育资源库建设

教育行业数字化是适应社会发展的必然选择&#xff0c;是教育行业的未来重要发展趋势。万事开头难&#xff0c;如何在数字化时代升级转型是教育行业团队正在面临的挑战。Zoho Workdrive企业网盘深耕智慧文件管理服务&#xff0c;为教育行业量身打造集中文件管理库&#xff0c;推…

服务器命令行换源

首先清除默认源 conda config --remove channels default随后添加阿里源 conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/

【嵌入式AI】CanMVk230开发板学习笔记(一)

嵌入式AI学习&#xff1a;CanMVk230开发板学习笔记 官方链接: k230快速入门 github固件下载地址&#xff1a; https://github.com/kendryte/k230_canmv/releases K230的相关软硬件资料&#xff0c;请参考 https://developer.canaan-creative.com/k230/dev/index.html https://g…

Qt屏蔽输出流技巧

Qt程序中调试会加入qDebug语句&#xff0c;但有时候想屏蔽掉&#xff0c;用下面的方法很方便实现。 在Qt项目文件添加以下定义&#xff1a; 屏蔽qInfo() DEFINES QT_NO_INFO_OUTPUT 屏蔽qDebug() DEFINES QT_NO_DEBUG_OUTPUT 屏蔽qWarning() DEFINES QT_NO_WARNING_OUT…

一键批量整理:将相同名称的文件归类至指定文件夹

随着电脑中的文件日益增多&#xff0c;文件管理成为了让人头疼的问题。相似的文件名&#xff0c;难以分类的内容&#xff0c;让你在寻找和整理时耗费大量时间。现在&#xff0c;有了我们的全新工具&#xff0c;这些烦恼全部消失。 第一步&#xff1a;进入文件批量改名高手主页面…

Day29 131分割回文串 93复原ip地址

131分割回文串 给定一个字符串 s&#xff0c;将 s 分割成一些子串&#xff0c;使每个子串都是回文串。 返回 s 所有可能的分割方案。 示例: 输入: "aab" 输出: [ ["aa","b"], ["a","a","b"] ] class Solution …

CnosDB的数据更新和删除

在时序数据中&#xff0c;可能会出现一些数据错误或者异常情况&#xff0c;这时候就需要能够对数据进行清洗修复。如果不支持更新操作&#xff0c;将会变得非常困难。另外&#xff0c;一些业务场景可能会需要对已有数据进行调整&#xff0c;比如设备信息发生变化等。支持数据更…

使用kibana来创建ElasticSearch的索引库与文档的命令

文章目录 &#x1f412;个人主页&#x1f3c5;JavaEE系列专栏&#x1f4d6;前言&#xff1a;&#x1f380;使用kibana来为ElasticSearch创建索引库&#x1f380;使用kibana来为ElasticSearch创建修改文档 &#x1f412;个人主页 &#x1f3c5;JavaEE系列专栏 &#x1f4d6;前言…

VSCode 正则表达式 匹配多行

VS Code 正则表达式匹配多行 (.|\n)*? //test.js const test {str: VS Code 正则表达式匹配多行VS Code 正则表达式匹配多行VS Code 正则表达式匹配多行VS Code 正则表达式匹配多行VS Code 正则表达式匹配多行VS Code 正则表达式匹配多行VS Code 正则表达式匹配多行VS Code …

代码随想录算法训练营day7|454.四数相加II 、383.赎金信、15.三数之和、18.四数之和

454.四数相加II 建议&#xff1a;本题是 使用map 巧妙解决的问题&#xff0c;好好体会一下 哈希法 如何提高程序执行效率&#xff0c;降低时间复杂度&#xff0c;当然使用哈希法 会提高空间复杂度&#xff0c;但一般来说我们都是舍空间 换时间&#xff0c; 工业开发也是这样。…

this关键字和static关键字

一、this关键字 关于java语言当中的this关键字&#xff1a; 1.this是一个关键字&#xff0c;翻译为&#xff1a;这个 2.this是一个引用&#xff0c;this是一个变量&#xff0c;this变量中保存了内存地址指向了了自身&#xff0c; this存储在JVM堆内存java对象内部。 3.创建…

koa2文件的上传下载功能

const Router require(“koa-router”); const upload new Router(); const bodyParser require(“koa-bodyparser”); const multer require("koa/multer"); const path require(“path”); const article require("…/utils/sql"); const { getCur…

(超详细)2-YOLOV5改进-添加SimAM注意力机制

1、在yolov5/models下面新建一个SimAM.py文件&#xff0c;在里面放入下面的代码 代码如下&#xff1a; import torch import torch.nn as nnclass SimAM(torch.nn.Module):def __init__(self, e_lambda1e-4):super(SimAM, self).__init__()self.activaton nn.Sigmoid()self…

在 C++/WinRT 中使用委托处理事件

本主题介绍了如何使用 C/WinRT 注册和撤销事件处理委托。 可以使用任何标准 C 函数类对象来处理事件。 使用 Visual Studio 添加事件处理程序&#xff08;WPF&#xff09; 一种将事件处理程序添加到项目的简便方法是使用 Visual Studio 中的 XAML 设计器用户界面 (UI)。 XAML …

STM32 1位共阳极数码管

数码管分为共阳极和共阴极&#xff0c;即多个二极管的同一端接到GND/Vss&#xff08;若一起接到GND&#xff0c;则称为共阴极。若一起接到Vss&#xff0c;则称为共阳极&#xff09; 把数码管上的每个二极管一次标号对应a,b,c,d,e,f,g,dp。我们知道发光二极管一端正一端负&#…