国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

簡單的天天酷跑小游戲?qū)崿F(xiàn)

這篇具有很好參考價值的文章主要介紹了簡單的天天酷跑小游戲?qū)崿F(xiàn)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

初級函數(shù)實現(xiàn)人物,背景,小烏龜?shù)囊苿?/h2>

簡單的天天酷跑小游戲?qū)崿F(xiàn),visual studio,c++,游戲,c語言

#include <graphics.h>
#include <iostream>
#include <Windows.h>
#include "tools.h"
#include <mmsystem.h>
#include <conio.h>
#include <time.h>//時間頭文件
#include <cstdlib>//隨機數(shù)文件
#pragma comment(lib, "winmm.lib")
using namespace std;
/*

日志:

游戲界面,(游戲窗口(init),游戲背景(3重背景以不同的速度循環(huán)滾動(updateBg(渲染背景)))
實現(xiàn)人物(跳躍活動)

隨機出現(xiàn)道具

加分
*/
#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;//小烏龜?shù)乃阶鴺?biāo)
int tortoiseY;
bool tortoiseExise;//設(shè)置一次只顯示一個小烏龜
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);
	}
	//加載小烏龜?shù)膱D片
	for (int i = 0; i < tortoiseNum; i++) {
		sprintf_s(name, "res/t%d.png", i + 1);
		loadimage(&imgTortoise[i], name);
	}
	//設(shè)置小烏龜相關(guān)信息
	tortoiseExise = false;
	update1 = false;
	//設(shè)置人物位置
	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;
}
//玩家跳躍的開關(guān)
void jump() {
	hero_jump = true;
	update1 = true;
}
//設(shè)置背景圖片不同速度移動
void fly() {
	//三重背景回位
	for (int i = 0; i < BG_IMAGE; i++){
		bgX[i] -= bgXSpeed[i];
		if (bgX[i] < -WIN_WIDTH) {
			bgX[i] = 0;//設(shè)置回位
		}
	}
	//人物實現(xiàn)跳躍
	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;//小烏龜圖片幀

	//創(chuàng)建小烏龜
	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]);
	//實現(xiàn)玩家奔跑
	putimagePNG2(HERO_X, HERO_Y, &imgHeros[index]);
	
}
//渲染障礙物
void updateEmy() {
	if (tortoiseExise){
		//實現(xiàn)小烏龜圖片幀
		putimagePNG2(tortoiseX, tortoiseY,WIN_WIDTH, &imgTortoise[index1]);
	}
	
	
}
//處理按鍵事件
void keyEvent() {
	//鍵盤空格跳躍
	char c;
	if (_kbhit()) {
		c = _getch();
		if (c == ' '){
			jump();
		}
	}
	//鼠標(biāo)左鍵跳躍
	/*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();//設(shè)置雙緩沖
			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++提供的長度可變數(shù)組
#pragma comment(lib, "winmm.lib")
using namespace std;
/*

日志:

游戲界面,(游戲窗口(init),游戲背景(3重背景以不同的速度循環(huán)滾動(updateBg(渲染背景)))
實現(xiàn)人物(跳躍活動)

隨機出現(xiàn)道具

加分
*/
#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//小烏龜?shù)膱D片數(shù)量
#define lionNum     6//獅子的數(shù)量
#define pillarNum   4//柱子數(shù)量

int tortoiseX;//小烏龜?shù)乃阶鴺?biāo)
int tortoiseY;
bool tortoiseExise;//設(shè)置一次只顯示一個小烏龜
int index1 = tortoiseNum;

//設(shè)置障礙物枚舉
typedef enum {
	tortiose,
	lion,
	pillar
}obstract_type;

//IMAGE ObstractIMG[3][12];
//C++提供的長度可變數(shù)組
vector<vector<IMAGE>> ObstractIMG;//image ObstractIMG[][]
//設(shè)置障礙物的屬性
typedef struct obstract {
	obstract_type type;//障礙物類型
	int imgIndex;//當(dāng)前顯示的圖片序號
	int x, y;//障礙物的坐標(biāo)
	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);
	}
	//設(shè)置人物位置
	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;

	//加載小烏龜?shù)膱D片
	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);
	//設(shè)置各種障礙物的共同屬性
	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;
	//設(shè)置隨機出現(xiàn)障礙物的類型
	//枚舉類型最后一個就是這個枚舉的長度,強制轉(zhuǎn)化
	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;
	}
}
//玩家跳躍的開關(guān)
void jump() {
	hero_jump = true;
	update1 = true;
}
//設(shè)置背景圖片不同速度移動
void fly() {
	//三重背景回位
	for (int i = 0; i < BG_IMAGE; i++){
		bgX[i] -= bgXSpeed[i];
		if (bgX[i] < -WIN_WIDTH) {
			bgX[i] = 0;//設(shè)置回位
		}
	}
	//人物實現(xiàn)跳躍
	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;//人物圖片幀
	}
	

	//創(chuàng)建小烏龜
	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;
		}*/
		//障礙物出現(xiàn)的函數(shù)
		createObstract();
		torZhen1 = 100 + rand() % 70;
	}
	/*if (tortoiseExise){
		tortoiseX -= bgXSpeed[2];
		if (tortoiseX < -imgTortoise[0].getwidth()) {
			tortoiseExise = false;
		}
	}*/
	//更新障礙物的坐標(biāo)
	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]);
	//實現(xiàn)玩家奔跑
	putimagePNG2(HERO_X, HERO_Y, &imgHeros[index]);
	
}
//渲染障礙物
void updateEmy() {
	//if (tortoiseExise){
	//	//實現(xiàn)小烏龜圖片幀
	//	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();
		}
	}
	//鼠標(biāo)左鍵跳躍
	/*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();//設(shè)置雙緩沖
			updateBg();//渲染圖片
			updateEmy();
			EndBatchDraw();
			fly();
		}
	}
	system("pause");
	return 0;
}

代碼忒難分塊了(直接放最后的源碼了)

簡單的天天酷跑小游戲?qū)崿F(xiàn),visual studio,c++,游戲,c語言文章來源地址http://www.zghlxwxcb.cn/news/detail-788333.html

#include <graphics.h>
#include <iostream>
#include <Windows.h>
#include "tools.h"
#include <mmsystem.h>
#include <conio.h>
#include <vector>//C++提供的長度可變數(shù)組
#pragma comment(lib, "winmm.lib")
using namespace std;
/*

日志:

游戲界面,(游戲窗口(init),游戲背景(3重背景以不同的速度循環(huán)滾動(updateBg(渲染背景)))
實現(xiàn)人物(跳躍活動)

隨機出現(xiàn)道具

加分
*/
#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//小烏龜?shù)膱D片數(shù)量
#define lionNum     6//獅子的數(shù)量
#define pillarNum   4//柱子數(shù)量

int tortoiseX;//小烏龜?shù)乃阶鴺?biāo)
int tortoiseY;
bool tortoiseExise;//設(shè)置一次只顯示一個小烏gui

//設(shè)置障礙物枚舉
typedef enum {
	tortiose,
	lion,
	pillar1,
	pillar2,
	pillar3,
	pillar4,
	pillar
}obstract_type;

//IMAGE ObstractIMG[3][12];
//C++提供的長度可變數(shù)組
vector<vector<IMAGE>> ObstractIMG;//image ObstractIMG[][]
//設(shè)置障礙物的屬性
typedef struct obstract {
	obstract_type type;//障礙物類型
	int imgIndex;//當(dāng)前顯示的圖片序號
	int x, y;//障礙物的坐標(biāo)
	int speed;
	int power;//殺傷力
	bool exist;
	bool hitH; //是否發(fā)生碰撞
	bool pass;//表示是否跨過障礙物
}obstract_t;
obstract_t obstracts[OBSTRACT_NUM];
//解決死亡陷阱
int lastObsIndex;
//記錄分?jǐn)?shù)
int scores;
//加分的圖片數(shù)組
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);
	}
	//設(shè)置人物位置
	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;
	//加載小烏龜?shù)膱D片
	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);
	//柱子圖片
	//我這里寫循環(huán)會報錯
	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);

	//設(shè)置各種障礙物的共同屬性
	for (int i = 0; i < OBSTRACT_NUM; i++){
		obstracts[i].exist = false;

	}
	//加載分?jǐn)?shù)圖片
	for (int i = 0; i < 9; i++) {
		sprintf_s(name, "res/sz/%d.png", i);
		loadimage(&imgScores[i], name);
	}
	//設(shè)置人物下蹲素材
	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;
	//設(shè)置隨機出現(xiàn)障礙物的類型
	//枚舉類型最后一個就是這個枚舉的長度,強制轉(zhuǎn)化
	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;
			}
		} 
	}
}
//玩家跳躍的開關(guān)
void jump() {
	hero_jump = true;
	update1 = true;
}
//玩家下蹲
void down() {
	update1 = true;
	hero_down = true;
	index = 0;
}
//設(shè)置背景圖片不同速度移動
void fly() {
	//三重背景回位
	for (int i = 0; i < BG_IMAGE; i++){
		bgX[i] -= bgXSpeed[i];
		if (bgX[i] < -WIN_WIDTH) {
			bgX[i] = 0;//設(shè)置回位
		}
	}
	//人物實現(xiàn)跳躍
	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;//人物圖片幀
	}
	

	//創(chuàng)建小烏龜
	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;
		}*/
		//障礙物出現(xiàn)的函數(shù)
		createObstract();
		torZhen1 = 100 + rand() % 70;
	}
	/*if (tortoiseExise){
		tortoiseX -= bgXSpeed[2];
		if (tortoiseX < -imgTortoise[0].getwidth()) {
			tortoiseExise = false;
		}
	}*/
	//更新障礙物的坐標(biāo)
	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) {
		//實現(xiàn)玩家奔跑
		putimagePNG2(HERO_X, HERO_Y, &imgHeros[index]);
	}
	else {
		//實現(xiàn)玩家下蹲
		int y = 295 - imgHerosDown[index].getheight() * 0.5;
		putimagePNG2(HERO_X, 295, &imgHerosDown[index]);
	}
}
//渲染障礙物
void updateEmy() {
	//if (tortoiseExise){
	//	//實現(xiàn)小烏龜圖片幀
	//	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);
}
//檢查游戲是否結(jié)束
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;
	}
	
}
//計算分?jǐn)?shù)
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;
		}
	}
}
//渲染分?jǐn)?shù)
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();
}
//游戲?qū)徖?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();
		}
	}
	//鼠標(biāo)左鍵跳躍
	/*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();//設(shè)置雙緩沖
			updateBg();//渲染圖片
			updateBlood();//血條
			updateHero();
			updateEmy();
			checkOver();
			checkScore();//檢查分?jǐn)?shù)
			updateScore();//渲染分?jǐn)?shù)
			checkWin();
			EndBatchDraw();
			fly();
			
		
			
		}
	}
	system("pause");
	return 0;
}

到了這里,關(guān)于簡單的天天酷跑小游戲?qū)崿F(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Android Studio實現(xiàn)五子棋小游戲

    Android Studio實現(xiàn)五子棋小游戲

    五子棋是一種兩人對弈的策略型棋類游戲,本次五子棋小游戲具有人機對戰(zhàn)和人人對戰(zhàn)兩種玩法。人機對戰(zhàn)可以單人挑戰(zhàn)AI,實時記錄比賽得分,AI是根據(jù)棋盤上每個點的得分進行決策,人人對戰(zhàn)采用輪流下棋方式進行對決,下棋過程中會記錄當(dāng)前棋子的顏色??梢栽谙缕暹^程

    2024年02月08日
    瀏覽(26)
  • 用C++實現(xiàn)簡單的小游戲

    用C++實現(xiàn)簡單的小游戲

    采用面向?qū)ο蟮木幊趟枷?在頭文件中引入acllic圖形庫,實現(xiàn)c++控制圖片以及生成可視化窗口 所需工具: acllib圖形庫下載地址:acl圖形庫下載地址 ?win32位項目的創(chuàng)建: 通過visual studio創(chuàng)建win32項目 三張圖片:tom.bmp,jerry.bmp,heart.bmp 1.貓和老鼠游戲,其中包含可以加分的紅心

    2024年02月05日
    瀏覽(18)
  • c++編寫天天酷跑游戲

    素材加Q群:723550115 Start importing material (background picture) Create a graph window and define macros for the window Import game background (scroll cycle) Local modularization Game background coordinates ? The picture is the Y coordinate of motion, and the definition amount is constantly changed to keep the last y coordinate change initialization

    2024年02月16日
    瀏覽(24)
  • python實現(xiàn)迷宮小游戲(附源碼 簡單易懂)

    python實現(xiàn)迷宮小游戲(附源碼 簡單易懂)

    需要源碼請點贊關(guān)注收藏后評論區(qū)留言~~~ 接下來用python實現(xiàn)一個控制臺的迷宮小游戲? 游戲規(guī)則如下 輸入exit退出游戲 輸入8為向上走 輸入5為向下走 輸入4為向左走 輸入6為向右走 游戲地圖如下 ? ? Y即為玩家,輸入對應(yīng)數(shù)字后可以變換位置,當(dāng)撞墻時位置不變 ? ? 部分源碼

    2024年02月11日
    瀏覽(23)
  • Android Studio實現(xiàn)連連看小游戲,比比看誰過關(guān)最快~

    Android Studio實現(xiàn)連連看小游戲,比比看誰過關(guān)最快~

    這是一款基于Android studio開發(fā)的連連看小游戲。主要實現(xiàn)的功能有: 難度設(shè)置 打亂重排 排行榜 計時器 背景音樂 消除音效 主要應(yīng)用的技術(shù)如下: Fragment碎片 Service服務(wù) Menu菜單 自定義view Java反射 handler消息機制 BroadcastReceiver 多線程 SQLiteOpenHelper SharedPreferences Bitmap ViewPager Me

    2023年04月08日
    瀏覽(68)
  • 使用Java實現(xiàn)一個簡單的貪吃蛇小游戲

    使用Java實現(xiàn)一個簡單的貪吃蛇小游戲

    基于java實現(xiàn)貪吃蛇小游戲,主要通過繪制不同的圖片并以一定速度一幀一幀地在窗體上進行展示。 開發(fā)工具:eclipse java工具包:jdk1.8 代碼地址:https://gitee.com/jay_musu/games-and-tools.git 創(chuàng)建一個新的項目,并命名。創(chuàng)建一個名為images的文件夾用來存放游戲相關(guān)圖片。然后再在項

    2024年02月11日
    瀏覽(28)
  • C#實現(xiàn)五子棋小游戲:簡單、有趣的編程項目
  • c語言及數(shù)據(jù)結(jié)構(gòu)實現(xiàn)簡單貪吃蛇小游戲

    c語言及數(shù)據(jù)結(jié)構(gòu)實現(xiàn)簡單貪吃蛇小游戲

    目錄 一·貪吃蛇簡單介紹: 二·貪吃蛇的實現(xiàn)的開始準(zhǔn)備: 2.1:歡迎界面的實現(xiàn): 2.2地圖的繪制: 2.3.1初始化蛇: 2.3.2初始化食物:? 三·貪吃蛇的運行操作: 3.1輔助信息的打?。?3.2蛇的下一步移動操作: 3.2.1判斷玩家按鍵情況: 3.2.2下一步遇到食物: 3.2.3下一步不是食物:

    2024年04月27日
    瀏覽(21)
  • [C/C++]天天酷跑游戲超詳細(xì)教程-上篇

    [C/C++]天天酷跑游戲超詳細(xì)教程-上篇

    ?個人主頁:北·海 ???CSDN新晉作者 ???歡迎 ??點贊?評論?收藏 ?收錄專欄:C/C++ ??希望作者的文章能對你有所幫助,有不足的地方請在評論區(qū)留言指正,大家一起學(xué)習(xí)交流!?? 天天酷跑,一款童年游戲,主要是進行跳躍操作,和躲避障礙物,該結(jié)主要實現(xiàn)背景圖的連續(xù)播

    2024年02月11日
    瀏覽(23)
  • 【Python游戲】超簡單~Python實現(xiàn)植物大戰(zhàn)僵尸小游戲,可以用于做畢業(yè)設(shè)計喲 | 附源碼

    【Python游戲】超簡單~Python實現(xiàn)植物大戰(zhàn)僵尸小游戲,可以用于做畢業(yè)設(shè)計喲 | 附源碼

    hello,大家好呀~ 今天給打擊整一個植物大戰(zhàn)僵尸 無廣告版本 哈哈 現(xiàn)在的小游戲很多都是有廣告,多少有點難受 今天給大家直接安排 遇到不懂的問題也可以私信小編或者↓ ↓ ↓ 源碼. 點擊藍(lán)色字體領(lǐng)取喲~(備注:蘇) 有很多的資源可以白嫖的哈,不定時會更新一下Pytho

    2024年02月05日
    瀏覽(28)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包