C/C++制作简单小球游戏(EasyX)

最近学了下EasyX图形库,做了个小游戏温习一下...

EasyX下载(很简单):https://easyx.cn/download/EasyX_20240601.exe

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>

void graphload(int X1,int Y1,int Xbot,int Ybot,int Xr,int Yr,int speed,int score){
	cleardevice();
	setfillcolor(WHITE);
	fillcircle(X1,Y1,10);//玩家小球
	setfillcolor(GREEN);
	solidcircle(Xbot,Ybot,5);//敌人小球
	setfillcolor(RED);
	solidcircle(Xr,Yr,4);//奖励小球
	settextstyle(30,15,"微软雅黑");
	char sp[] = "Player Speed:";
	char spi[50];
	itoa(speed,spi,10);//将int转为char数组
	outtextxy(10,10,sp);
	outtextxy(220,11,spi);//速度提示栏
	char sc[] = "Score:";
	char sci[50];
	itoa(score,sci,10);
	outtextxy(280,10,sc);
	outtextxy(380,11,sci);//得分栏
}
void check(int &X1,int &Y1,int &Xbot,int &Ybot,int &Xr,int &Yr,int &speed,int &count,int &score){
	double distance = sqrt((X1-Xbot)*(X1-Xbot)+(Y1-Ybot)*(Y1-Ybot));//检测玩家小球与敌人距离
	double distance2 = sqrt((X1-Xr)*(X1-Xr)+(Y1-Yr)*(Y1-Yr));//检测玩家小球与奖励的距离
	if(distance<=16){
		setbkmode(TRANSPARENT);
		settextcolor(RED);
		settextstyle(55,42,"微软雅黑");
		char fail[] = "You dead!";//失败文本
		char tip[] = "press R to quit";//提示退出文本
		outtextxy(30,100,fail);
		settextstyle(18,11,"微软雅黑");
		outtextxy(260,210,tip);
		for(;;){
			char key = getch();//死循环使死亡后时游戏暂停
			if(key=='r' || key=='R'){
				closegraph();
			}
		}
	}//玩家小球死亡
	else{
		if(X1-Xbot<=0 && Y1-Ybot<=0){
			Xbot-=3;
			Ybot-=3;
		}
		if(X1-Xbot<=0 && Y1-Ybot>=0){
			Xbot-=3;
			Ybot+=3;
		}
		if(X1-Xbot>=0 && Y1-Ybot<=0){
			Xbot+=3;
			Ybot-=3;
		}
		if(X1-Xbot>=0 && Y1-Ybot>=0){
			Xbot+=3;
			Ybot+=3;
		}
	}//设置敌人自动跟踪ai

	if(count > 0){
		count-=1;
		speed-=1;
	}//检测加速次数
	
	if(distance2<=20){
		score+=1;//加一分
		speed = 19;//增加速度
		count = 14;//加速次数
		srand(time(NULL));//设置标签
		Xr = rand()%420+20;
		Yr = rand()%320+60;
		//设置奖励小球随机坐标
		graphload(X1,Y1,Xbot,Ybot,Xr,Yr,speed,score);
	}//触发奖励
	
}

int X1 = 250;
int Y1 = 200;
int Xbot = 250;
int Ybot = 50;
int Xr,Yr;
int speed = 5;
int count = 0;
int score = 0;
//初始数值
int main(){
	srand(time(NULL));//设置标签
	Xr = rand()%370+30;
	Yr = rand()%330+50;
	//设置奖励小球随机坐标

	initgraph(500,400,NULL);
	graphload(X1,Y1,Xbot,Ybot,Xr,Yr,speed,score);

	while(1){
		if(_kbhit()){
			char key = _getch();
			switch(key){
				case 's':
				case 'S':
					Y1+=speed;
					graphload(X1,Y1,Xbot,Ybot,Xr,Yr,speed,score);
					check(X1,Y1,Xbot,Ybot,Xr,Yr,speed,count,score);
					break;
				case 'w':
				case 'W':
					Y1-=speed;
					graphload(X1,Y1,Xbot,Ybot,Xr,Yr,speed,score);
					check(X1,Y1,Xbot,Ybot,Xr,Yr,speed,count,score);
					break;
				case 'a':
				case 'A':
					X1-=speed;
					graphload(X1,Y1,Xbot,Ybot,Xr,Yr,speed,score);
					check(X1,Y1,Xbot,Ybot,Xr,Yr,speed,count,score);
					break;
				case 'd':
				case 'D':
					X1+=speed;
					graphload(X1,Y1,Xbot,Ybot,Xr,Yr,speed,score);
					check(X1,Y1,Xbot,Ybot,Xr,Yr,speed,count,score);
					break;
				case 'r':
				case 'R':
					closegraph();
					break;
			}//检测键盘移动
		}
	}
	
	_getch();
	closegraph();
	return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值