/****************************
天天酷跑开发日志
1.创建项目
2.导入素材
3.游戏界面
实际的开发流程
对于初学者.最好的方式,建议从用户界面入手
选择图形库或者其他引擎
天天酷跑,是基于"easyx"图形库的
1)创建游戏窗口
2)设计游戏背景
a.背景不同的速度同时移动
b.循环滚动
3)实现游戏背景
a.加载背景资源
b.渲染(背景知识:坐标)
遇到问题:背景图片的png格式图片出现黑色
4.实现玩家的奔跑
5.实现玩家的跳跃
6.实现随机小乌龟
7.创建障碍物结构体数据类型
8.使用障碍物结构体后重新初始化
9.封装后多个障碍物的显示
10.实现玩家的下蹲技能
11.实现"柱子"障碍物
*/
#include<stdio.h>
#include <graphics.h>
#include "tools.h"
#include<conio.h>
#include<vector>
using namespace std;
#define WIN_WIDTH 1012
#define WIN_HEIGHT 396
#define OBSTACLE_COUNT 10
//背景图片
IMAGE imgBgs[3];
IMAGE imgHeros[12];
int heroX; // 玩家的x坐标
int heroY; //玩家的y坐标
int heroIndex;//玩家奔跑的图片帧序号
int bgX[3];
int bgSpeed[3] = { 1,2,4 };
bool heroJump;//表示玩家正在跳跃
int jumpHeighetMax;
int heroJumpOff;
int updata;
IMAGE imgTortoise;//小乌龟
int torToiseX; // 小乌龟的水平坐标
int torToiseY;//小乌龟的垂直坐标
bool torToiseExist;//当前窗口是否有小乌龟
typedef enum {
TPRTOISE,//乌龟
LION, // 狮子 1
HOOK1,
HOOK2,
HOOK3,
HOOK4,
OBSTACLE_TYPE_COUNT //2
}obstacle_type;
vector<vector<IMAGE>>obstacleImgs;//存放所有障碍物的图片 相当于 IMAGE obstacleImgs[3][12];
//IMAGE obstacleImgs[3][12];
typedef struct obstacle {
int type;//障碍物的类型
int imgIndex;//当前显示的图片的序号
int x, y;// 障碍物的坐标
int speed;
int power;//杀伤力
bool exist;
}obstacle_t;
obstacle_t obstacles[OBSTACLE_COUNT];
IMAGE imgHeroDown[2];
bool heroDown;//表示玩家是否处于下蹲状态
void init() {
//创建一个游戏窗口
initgraph(WIN_WIDTH, WIN_HEIGHT);
//加载背景资源
char name[64];
for (int i = 0; i < 3; i++) {
//"res/bg001.png" "res/bg002.png" "res/bg003.png"
sprintf(name, "res/bg%03d.png", i + 1);
loadimage(&imgBgs[i], name);
bgX[i] = 0;
}
//加载Hero奔跑的图片帧素材
for (int i = 0; i < 12; i++) {
//"res/hero1.png" ...."res/hero12.png"
sprintf(name, "res/hero%d.png", i + 1);
loadimage(&imgHeros[i], name);
}
//设置玩家的初始化位置
heroX = WIN_WIDTH * 0.5 - imgHeros[0].getwidth() * 0.5;
heroY = 345 - imgHeros[0].getheight();
heroIndex = 0;
heroJump = false;
jumpHeighetMax = 345 - imgHeros[0].getheight() - 120;
heroJumpOff = -4;
updata = true;
//加载小乌龟
/*loadimage(&imgTortoise, "res/t1.png");
torToiseExist = false;
torToiseY = 345 - imgTortoise.getheight() + 5;*/
IMAGE imgTort;
loadimage(&imgTort, "res/t1.png");
vector<IMAGE> imgTortArray;
imgTortArray.push_back(imgTort);
obstacleImgs.push_back(imgTortArray);
IMAGE imgLion[6];
vector<IMAGE>imgLionArray;
for (int i = 0; i < 6; i++) {
sprintf(name, "res/p%d.png", i + 1);
loadimage(&imgLion[i], name);
imgLionArray.push_back(imgLion[i]);
}
obstacleImgs.push_back(imgLionArray);
//初始化障碍物池
for (int i = 0; i < OBSTACLE_COUNT; i++) {
obstacles[i].exist = false;
}
//加载下蹲素材
loadimage(&imgHeroDown[0], "res/d1.png");
loadimage(&imgHeroDown[1], "res/d2.png");
heroDown = false;
IMAGE imgH;
for (int i = 0; i < 4; i++) {
vector<IMAGE>imgHookArry;
sprintf(name, "res/h%d.png", i + 1);
loadimage(&imgH, name,63,260,true);
imgHookArry.push_back(imgH);
obstacleImgs.push_back(imgHookArry);
}
}
void createObstaclr() {
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);
if (obstacles[i].type == HOOK1) {
obstacles[i].type += rand() % 4;
}
obstacles[i].x = WIN_WIDTH;
obstacles[i].y = 345 + 5 - obstacleImgs[obstacles[i].type][0].getheight();
if (obstacles[i].type == TPRTOISE) {
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].speed = 0;
obstacles[i].power = 20;
obstacles[i].y = 0;
}
}
void fly() {
for (int i = 0; i < 3; i++) {
bgX[i] -= bgSpeed[i];
if (bgX[i] < -WIN_WIDTH) {
bgX[i] = 0;
}
}
//实现跳跃
if (heroJump) {
if (heroY < jumpHeighetMax) {
heroJumpOff = 4;
}
heroY += heroJumpOff;
if (heroY > 345 - imgHeros[0].getheight()) {
heroJump = false;
heroJumpOff = -4;
}
}
else if(heroDown){//不跳跃
static int count = 0;
static int delays[2] = { 4,10 };
count++;
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;
createObstaclr();
}
/*if (torToiseExist) {
torToiseX -= bgSpeed[2];
if (torToiseX < -imgTortoise.getwidth()) {
torToiseExist = false;
}
}*/
//更新障碍物的坐标
for (int i = 0; i < OBSTACLE_TYPE_COUNT; i++) {
if (obstacles[i].exist) {
obstacles[i].x -= obstacles[i].speed + bgSpeed[2];
if (obstacles[i].x < -obstacleImgs[obstacles[i].type][0].getheight() * 2) {
obstacles[i].exist = false;
}
int len = obstacleImgs[obstacles[i].type].size();
obstacles[i].imgIndex = (obstacles[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 jump() {
heroJump = true;
updata = true;
}
void down() {
heroDown = true;
updata = true;
heroIndex = 0;
}
void keyEvent() {
if (_kbhit()) {//如果有按键按下,_kbhit()返回 true
char ch = _getch();//_getch()不需要按下回车即可直接读取
if (ch == ' ') {
jump();
}
else if (ch == 'a') {
down();
}
}
}
void updataEnemy() {
//渲染小乌龟
/*if (torToiseExist) {
putimagePNG2(torToiseX, torToiseY, WIN_WIDTH, &imgTortoise);
}*/
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]);
}
}
}
void updataHero() {
if (heroDown == false) {
putimagePNG2(heroX, heroY, &imgHeros[heroIndex]);
}
else {
int y = 345 - imgHeroDown[heroIndex].getheight();
putimagePNG2(heroX, y, &imgHeroDown[heroIndex]);
}
}
int main(void) {
init();
int timer = 0;
while (1) {
keyEvent();
timer += getDelay();
if (timer > 30) {
timer = 0;
updata = true;
}
if (updata) {
updata = false;
BeginBatchDraw();
updateBg();
//putimagePNG2(heroX, heroY, &imgHeros[heroIndex]);
updataHero();
updataEnemy();
EndBatchDraw();
fly();
}
//Sleep(30);
}
system("pause");
return 0;
}
天天跑酷222222222222
最新推荐文章于 2024-08-13 14:56:59 发布