Discuz! Board

 找回密码
 立即注册
查看: 414|回复: 0

小恐龙20230527

[复制链接]

27

主题

29

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

积分
14147
发表于 2023-5-27 11:37:03 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<bits/stdc++.h>
using namespace std;
int cnt;
int topScore;

void loadImage();
void releaseImage();
void addRandomNum(int n);
bool move(int direction);
void draw();

key_msg keyMsg;

//数据文件
const char* recordFile = "game_dino.txt";
//图片文件位置
PIMAGE pimg_tree[6];
PIMAGE pimg_dino[5];
PIMAGE pimg_bird[2];
PIMAGE pimg_ground[6];
PIMAGE pimg_cloud;
PIMAGE pimg_gameover;


struct dinosaur
{
	int state;//恐龙状态  0-空中 1-站立  2-下蹲 
	int act;//恐龙动作 0-不动  12-站立左右脚	34-下蹲左右脚
	float vy;//y方向速度 
	float g;//加速度 
	float x,y;//位置坐标
	float lx,ly,rx,ry;//恐龙左上坐标,右下坐标 
}dino; 

struct ground
{
	int  type;//路的类型 
	float x,y;//坐标 
}gro[4]; 

struct cloud
{
	float vx;//飘动速度 
	float x,y;//坐标 
}clo[3]; 


struct obstacle
{
	int type;//仙人掌的类型 0-2小  3-5大   6-8 h鸟/m鸟/l鸟 
	float x,y;//位置坐标 
	float lx,ly,rx,ry;//障碍左上坐标,右下坐标 
}obs[9]; 

float vx;//水平速度 
int bird_act;//翼龙动作 


const char* imgFiles[21] = 
{
	"resource\\tree_s1.png",
	"resource\\tree_s2.png",
	"resource\\tree_s3.png",
	"resource\\tree_b1.png",
	"resource\\tree_b2.png",
	"resource\\tree_b3.png",

	"resource\\dinosaur00.png",
	"resource\\dinosaur01.png",
	"resource\\dinosaur02.png",
	"resource\\dinosaur03.png",
	"resource\\dinosaur04.png",
	
	"resource\\bird01.png",
	"resource\\bird02.png",
	
	"resource\\ground_01.png",
	"resource\\ground_02.png",
	"resource\\ground_03.png",
	"resource\\ground_04.png",
	"resource\\ground_05.png",
	"resource\\ground_06.png",
	
	 "resource\\cloud.png",
	 "resource\\gameover.png"
};

void loadImage()//图片载入 
{
	//图 初始化 
	for (int i = 0; i < 6; i++) 
	{
		pimg_tree[i] = newimage();
		getimage(pimg_tree[i], imgFiles[i]);
	}
	for (int i = 0; i < 5; i++) 
	{
		pimg_dino[i] = newimage();
		getimage(pimg_dino[i], imgFiles[i+6]);
	}
	for (int i = 0; i < 2; i++) 
	{
		pimg_bird[i] = newimage();
		getimage(pimg_bird[i], imgFiles[i+11]);
	}
	for (int i = 0; i < 6; i++) 
	{
		pimg_ground[i] = newimage();
		getimage(pimg_ground[i], imgFiles[i+13]);
	}
	
	pimg_cloud = newimage();
	getimage(pimg_cloud, imgFiles[19]);
	pimg_gameover = newimage();
	getimage(pimg_gameover, imgFiles[20]);
}

void start()//初始化 
{
	dino.state=1;
	dino.g=0.4;
	dino.vy=0;
	dino.y=222;
	vx=-3;
	
	for(int i=0;i<4;i++)//地面 
	{
		gro[i].type=rand()%6;
		gro[i].x=0+i*300;
		gro[i].y=250; 
	} 
	for(int i=0;i<3;i++)//云  
	{
		clo[i].vx=-3;
		clo[i].x=rand()%100+i*300;
		clo[i].y=rand()%100+50; 
	}
	for(int i=0;i<3;i++)//障碍 
	{
		obs[i].type=rand()%5;
		obs[i].x=rand()%50+(i+1)*450;
		if(obs[i].type<3)	obs[i].y=235;//小仙人掌 
		else if(obs[i].type<6)	 obs[i].y=220;//大仙人掌  
	} 
	
	initgraph(900,300);//图形初始化 
	setbkcolor(WHITE);
	srand(time(0));//随机数初始化 
	loadImage();//图片载入 
	
	setcolor(DARKGRAY);
	setfont(25,0,"幼圆");//(字体高度,字体宽度(为0即为自适应),字形)设置字体为25,幼圆字体
	setbkmode(TRANSPARENT); //设置文字背景色为透明(默认为有背景色)
	setcaption("小恐龙历险记");//设置标题
} 

void draw()//图形绘制 
{
	for(int i=0;i<4;i++)//路面绘制 
	{
		putimage_withalpha(NULL, pimg_ground[gro[i].type],gro[i].x, gro[i].y);//首个路面号循环递增 
	}
	for(int i=0;i<3;i++)//云绘制 
	{
		putimage_withalpha(NULL, pimg_cloud, clo[i].x, clo[i].y);
	}
	for(int i=0;i<3;i++)//障碍物
	{
		if(obs[i].type<6)	putimage_withalpha(NULL, pimg_tree[obs[i].type], obs[i].x, obs[i].y);
		else if(obs[i].type<9)	putimage_withalpha(NULL, pimg_bird[bird_act], obs[i].x, obs[i].y);
	}
	putimage_withalpha(NULL, pimg_dino[dino.act], 20, dino.y);//绘制恐龙 ,最后绘制防遮挡 
	//游戏分数 
	xyprintf(650,30,"HI:%5d", topScore);		
	xyprintf(800,30,"%5d", cnt);
}


void dino_act()//恐龙状态
{
	switch(dino.state)
	{
		case 0://空中 
			if(keyMsg.key==key_down)
			{
				dino.g=dino.g+0.01;
			}
			break;
		case 1://站立 
			if(keyMsg.key==key_up)
			{
				dino.state=0;//跳起  
				dino.vy=-8;
			}
			if(keyMsg.key==key_down)
			{
				dino.state=2;//下蹲 
				dino.act=3;
			}
			break;
		case 2://下蹲 
			
			if(keyMsg.msg==key_msg_up)
			{
				dino.state=1;//站立 
				dino.act=1;
			}
			break;
		
	}
}


void update()//数据更新 
{
	//恐龙动作 
	if(dino.state==1)//站立 
	{
		if(cnt%16<8)	dino.act=1;
		else	dino.act=2;
	}
	else if(dino.state==2)//下蹲 
	{
		if(cnt%16<8)	dino.act=3;
		else	dino.act=4;
	}
	else if(dino.state==0)//空中 
	{
		dino.act=0;
		dino.y+=dino.vy;//位置变化 
		dino.vy+=dino.g;//速度变化  
		if(dino.y>=222)//落到地面 
		{
			dino.state=1;//进入站立状态 
			dino.vy=0;
			dino.y=222;
		}
	}
	
	for(int i=0;i<4;i++)//路面 
	{
		gro[i].x+=vx;//路面运动 
		if(gro[i].x<=-300)//1个路面显示
		{
			gro[i].type=rand()%6; 
			gro[i].x=900;
			gro[i].y=250;	
		}
	} 
	
	for(int i=0;i<3;i++)//云 
	{
		clo[i].x+=clo[i].vx;
		if(clo[i].x<=-50)//云消失后重新产生 
		{
			clo[i].vx= -3;
			clo[i].x=900;
			clo[i].y=rand()%100+50;
		}	
	}
	 
	for(int i=0;i<3;i++)//障碍物 
	{
		obs[i].x+= vx;
		if(obs[i].x<-450)
		{
			obs[i].type=rand()%9;
			obs[i].x=rand()%50+900;
			if(obs[i].type<3)	obs[i].y=235;//小仙人掌
			else if(obs[i].type<6)	obs[i].y=220;//大仙人掌
			else if(obs[i].type==6)	obs[i].y=170;//高  鸟 
			else if(obs[i].type==7)	obs[i].y=200;//中  鸟  
			else if(obs[i].type==8)	obs[i].y=230;//低  鸟  
		}
	}
	
	//翼龙动作 
	if(cnt%40<25)	bird_act=0;
	else bird_act=1;
	
	if(cnt>topScore) topScore=cnt;
	vx=-5-cnt/1000;
}

void loadRecord()
{
	FILE* fp = fopen(recordFile, "r");

	fscanf(fp, "topScore:%d",&topScore);
	fclose(fp);
}

void gameSave()
{
	//数据写入
	FILE* fp = fopen(recordFile, "w");
	if (fp == NULL)
		return;
	fprintf(fp, "topScore:%d",topScore);
	fclose(fp);
}


bool gameover()
{
	if(dino.state==2)//恐龙下蹲
	{
		dino.lx=dino.x+2;
		dino.ly=dino.y+22;
		dino.rx=dino.x+56;
		dino.ry=dino.y+47;
	} 
	else
	{
		dino.lx=dino.x+14;
		dino.ly=dino.y+5;
		dino.rx=dino.x+49;
		dino.ry=dino.y+43;
	}
	
	for(int i=0;i<3;i++)//障碍物 
	{
		if(obs[i].type==0)// 小仙人掌*1
		{
			obs[i].lx=obs[i].x+18;
			obs[i].ly=obs[i].y+1;
			obs[i].rx=obs[i].x+32;
			obs[i].ry=obs[i].y+34;
		}
		else if(obs[i].type==1)// 小仙人掌*2
		{
			obs[i].lx=obs[i].x+10;
			obs[i].ly=obs[i].y+1;
			obs[i].rx=obs[i].x+41;
			obs[i].ry=obs[i].y+33;
		}
		else if(obs[i].type==2)// 小仙人掌*3
		{
			obs[i].lx=obs[i].x+1;
			obs[i].ly=obs[i].y+1;
			obs[i].rx=obs[i].x+49;
			obs[i].ry=obs[i].y+33;
		}
		else if(obs[i].type==3)// 大仙人掌*1
		{
			obs[i].lx=obs[i].x+30;
			obs[i].ly=obs[i].y+3;
			obs[i].rx=obs[i].x+54;
			obs[i].ry=obs[i].y+49;
		}
		else if(obs[i].type==4)// 大仙人掌*2
		{
			obs[i].lx=obs[i].x+18;
			obs[i].ly=obs[i].y+3;
			obs[i].rx=obs[i].x+67;
			obs[i].ry=obs[i].y+49;
		}
		else if(obs[i].type==5)// 大仙人掌*3
		{
			obs[i].lx=obs[i].x+5;
			obs[i].ly=obs[i].y+3;
			obs[i].rx=obs[i].x+79;
			obs[i].ry=obs[i].y+50;
		}
		else// 鸟 
		{
			obs[i].lx=obs[i].x+4;
			obs[i].ly=obs[i].y+4;
			obs[i].rx=obs[i].x+45;
			obs[i].ry=obs[i].y+39;
		}
	}
	
	for(int i=0;i<3;i++)
	{
		if(((dino.lx>=obs[i].rx)||(dino.rx<=obs[i].lx))||((dino.ly>=obs[i].ry)||(dino.ry<=obs[i].ly)))
		{
			return false;
		}
		else
		{
			return true;
		}
//		if((dino.rx>=obs.lx)&&(dino.lx<=obs.rx))
//		{
//			if((dino.ly>obs.ry)&&(dino.ry<obs.ly))
//			{
//				return true;
//			}
//		}
	//	return false;
	}
}

int main()
{	
	loadRecord();
a:	start();//初始化 
	draw();//图形绘制 
	while(1)
	{
		while(kbmsg()) 
		{
			keyMsg = getkey();
			if (((keyMsg.msg == key_msg_down)||(keyMsg.msg == key_msg_up))&&((keyMsg.key==key_up)||
			(keyMsg.key==key_down)||(keyMsg.key=='Q')))
			{
				dino_act();	
				if(keyMsg.key=='Q')	
				{
					cnt=0;
					goto a; 
				}	
			}
			gameSave();
		}
		
		update();
		cleardevice();
		draw();
		cnt++; 
		delay_fps(60);
		
		if(gameover())
		{
			break;
		}
	}
	
	putimage_withalpha(NULL, pimg_gameover,350,125 );//首个路面号循环递增
	getch();
	cnt=0;
	goto a;
	return 0;
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-2-13 05:17 , Processed in 0.051726 second(s), 27 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表