寒假成果

大鱼吃小鱼游戏

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#define Width 889//画面尺寸
#define Hight 500
int fish1_x = Width / 2;
int fish1_y = Hight / 2;
int fish2_x = 0;
int fish2_y = Hight / 2;
int fish3_x = Width - 50;
int fish3_y = 40;
int fish4_x = Width - 30;
int fish4_y = 300;
int fish5_x = Width - 30;
int fish5_y = 230;
int fish6_x = -50;
int fish6_y = Hight / 4 + 20;
int score = 0;//判断吃鱼
int appear = 0;//生成鱼的种类
int fish1_size = 2;//鱼的大小
int fish2_size = 1;
int fish3_size = 3;
int fish4_size = 4;
int fish5_size = 5;
int fish6_size = 6;
int count = 0;//尺寸增加
IMAGE img_bk; //背景图片
IMAGE img_end;
IMAGE img_gameover;
IMAGE img_starup;
IMAGE img_star;
IMAGE img_star1;
IMAGE img_star2;
IMAGE img_fish1, img_fish12; //加入小鱼100x50
IMAGE img_fish2, img_fish22;
IMAGE img_fish3, img_fish32;
IMAGE img_fish4, img_fish42;
IMAGE img_fish5, img_fish52;
IMAGE img_fish6, img_fish62;
void startup() {
	int a;
	int i = 1;
	initgraph(Width, Hight);
	loadimage(&img_bk, "D:\\background.jpg");//读取图片
	putimage(0, 0, &img_bk);
	if (i > 0) {
		loadimage(&img_starup, "D:\\starup.jpg");
		putimage(0, 0, &img_starup);
		loadimage(&img_star, "D:\\star.jpg");
		putimage(fish1_x, fish1_y, &img_star);
	}
	MOUSEMSG m;//定义鼠标消息
	while (1) {
		m = GetMouseMsg();
		if (m.uMsg == WM_LBUTTONDOWN) {
			if (m.x > 445 && m.x < 823 && m.y>250 && m.y < 332) {
				i = 0;
				break;//点击开始游戏进行游戏
			}
		}
	}
	if (i == 0) {
		loadimage(&img_star2, "D:\\star2.jpg");//读取图片
		putimage(0, 0, &img_star2);
		Sleep(2000);
		loadimage(&img_star1, "D:\\star1.jpg");//读取图片
		putimage(0, 0, &img_star1);
		Sleep(2000);
	}
}
void gameover() {
	int x = 0;
	loadimage(&img_gameover, "D:\\gameover.jpg");
	putimage(0, 0, &img_gameover);
	EndBatchDraw();
	Sleep(3000);
	loadimage(&img_end, "D:\\end.jpg");
	BeginBatchDraw();
	putimage(0, 0, &img_end);
	FlushBatchDraw();
	Sleep(3000);
	exit(0);
	_getch();
	closegraph();
}

void show() {
	loadimage(&img_bk, "D:\\background.jpg");//读取图片
	loadimage(&img_fish1, "D:\\fish1.jpg");
	loadimage(&img_fish12, "D:\\fish12.jpg");
	loadimage(&img_fish2, "D:\\fish2.jpg");
	loadimage(&img_fish22, "D:\\fish22.jpg");
	loadimage(&img_fish3, "D:\\fish3.jpg");
	loadimage(&img_fish32, "D:\\fish32.jpg");
	loadimage(&img_fish4, "D:\\fish4.jpg");
	loadimage(&img_fish42, "D:\\fish42.jpg");
	loadimage(&img_fish5, "D:\\fish5.jpg");
	loadimage(&img_fish52, "D:\\fish52.jpg");
	loadimage(&img_fish6, "D:\\fish6.jpg");
	loadimage(&img_fish62, "D:\\fish62.jpg");
	BeginBatchDraw();
	putimage(0, 0, &img_bk);
	putimage(fish1_x, fish1_y, &img_fish12, NOTSRCERASE);
	putimage(fish1_x, fish1_y, &img_fish1, SRCINVERT);
	putimage(fish2_x, fish2_y, &img_fish22, NOTSRCERASE);
	putimage(fish2_x, fish2_y, &img_fish2, SRCINVERT);
	putimage(fish3_x, fish3_y, &img_fish32, NOTSRCERASE);
	putimage(fish3_x, fish3_y, &img_fish3, SRCINVERT);
	putimage(fish4_x, fish4_y, &img_fish42, NOTSRCERASE);
	putimage(fish4_x, fish4_y, &img_fish4, SRCINVERT);
	putimage(fish5_x, fish5_y, &img_fish52, NOTSRCERASE);
	putimage(fish5_x, fish5_y, &img_fish5, SRCINVERT);
	putimage(fish6_x, fish6_y, &img_fish62, NOTSRCERASE);
	putimage(fish6_x, fish6_y, &img_fish6, SRCINVERT);
	FlushBatchDraw();
	Sleep(50);
}

void updateWithoutInput()//与用户输入无关的更新 
{
	if (score == 10 && count == 0) {
		fish1_size = fish1_size + 1;
		count++;
	}
	if (score == 15 && count == 1) {
		fish1_size = fish1_size + 1;
		count++;
	}
	if (score == 30 && count == 2) {
		fish1_size = fish1_size + 1;
		count++;
	}
	if (score == 40 && count == 3) {
		fish1_size = fish1_size + 1;
		count++;
	}
	if (fish2_x < Width) {
		fish2_x = fish2_x + 5;
	}
	else {
		fish2_x = 0;
		fish2_y = rand() % Hight - 10;//鱼随机生成
	}
	if (abs(fish1_x - fish2_x) < 200 && abs(fish1_y - fish2_y) < 50) {
		fish2_x = 0;
		fish2_y = rand() % Hight - 10;
		score = score + 1;
	}
	if (fish3_x > 0) {
		fish3_x = fish3_x - 5;
	}
	else {
		fish3_x = Width - 100;
		fish3_y = rand() % Hight - 10;//鱼随机生成
	}
	if (abs(fish1_x - fish3_x) < 100 && abs(fish1_y - fish3_y + 10) < 95) {
		if (fish1_size >= fish3_size) {
			fish3_x = Width - 100;
			fish3_y = rand() % Hight - 10;
			score = score + 2;
		}
		else {
			gameover();
		}
	}
	if (fish4_x > 0) {
		fish4_x = fish4_x - 5;
	}
	else {
		fish4_x = Width - 100;
		fish4_y = rand() % Hight - 10;//鱼随机生成
	}
	if (abs(fish1_x - fish4_x) < 100 && abs(fish1_y - fish4_y - 50) < 100) {
		if (fish1_size >= fish4_size) {
			fish4_x = Width - 100;
			fish4_y = rand() % Hight - 10;
			score = score + 5;
		}
		else {
			gameover();
		}
	}
	if (fish5_x > 0) {
		fish5_x = fish5_x - 5;
	}
	else {
		fish5_x = Width - 100;
		fish5_y = rand() % Hight - 10;//鱼随机生成
	}
	if (abs(fish1_x - fish5_x) < 100 && abs(fish1_y - fish5_y - 20) < 85) {
		if (fish1_size >= fish5_size) {
			fish5_x = Width - 100;
			fish5_y = rand() % Hight - 10;
			score = score + 5;
		}
		else {
			gameover();
		}
	}
	if (fish6_x < Width) {
		fish6_x = fish6_x + 5;
	}
	else {
		fish6_x = 0;
		fish6_y = rand() % Hight - 10;//鱼随机生成
	}
	if (abs(fish1_x - fish6_x) < 190 && abs(fish1_y - fish6_y) < 70) {
		if (fish1_size >= fish6_size) {
			fish6_x = 0;
			fish6_y = rand() % Hight - 10;
			score = score + 10;
		}
		else {
			gameover();
		}
	}
}
void updateWithInput()//与用户输入有关的更新 
{
	char input;
	if (_kbhit()) //当按下键时
	{
		input = _getch();
		if (input == 'w') {
			fish1_y = fish1_y - 10;
		}
		if (input == 's') {
			fish1_y = fish1_y + 10;
		}
		if (input == 'a') {
			fish1_x = fish1_x - 10;
		}
		if (input == 'd') {
			fish1_x = fish1_x + 10;
		}
	}
}



int main() {
	startup();//数据初始化 
	while (1) {
		show();//显示画面
		updateWithoutInput();//与用户输入无关的更新 
		updateWithInput();//与用户输入有关的更新 
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_62837200/article/details/123001443