天天跑酷1小游戏

/****************************
天天酷跑开发日志
1.创建项目
2.导入素材
3.游戏界面
 实际的开发流程
	对于初学者.最好的方式,建议从用户界面入手


	选择图形库或者其他引擎
	天天酷跑,是基于"easyx"图形库的
	1)创建游戏窗口
	2)设计游戏背景
		a.背景不同的速度同时移动
		b.循环滚动
	3)实现游戏背景
		a.加载背景资源
		b.渲染(背景知识:坐标)
		遇到问题:背景图片的png格式图片出现黑色
4.实现玩家的奔跑
5.实现玩家的跳跃


*/
#include<stdio.h>
#include <graphics.h>
#include "tools.h"
#include<conio.h>
#define WIN_WIDTH 1012
#define WIN_HEIGHT 396
//背景图片
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;//当前窗口是否有小乌龟
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;

}
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 {//不跳跃
		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() % 301;
		}
	}
	if (torToiseExist) {
		torToiseX -= bgSpeed[2];
		if (torToiseX < -imgTortoise.getwidth()) {
			torToiseExist = false;

		}

	}
}
//渲染"游戏背景"
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 keyEvent() {
	if (_kbhit()) {//如果有按键按下,_kbhit()返回 true
		char ch = _getch();//_getch()不需要按下回车即可直接读取
		if (ch == ' ') {
			jump();
		}

	}

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

		}
		//Sleep(30);
	}
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值