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

2024-01-13 21:04

本文主要是介绍简单的天天酷跑小游戏实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

#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.chinasem.cn/article/602775

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直