easyx设计的界面跳转黑屏问题

-2

自己根据网上的教程模仿制作的第一个游戏,可是在贪吃蛇游戏结束后,返回的菜单界面是黑屏的,点击相应的地方还是能进入游戏里面,求大佬解决,希望能正常跳转,接麦你可以是插图或者自己设计的,

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#define _CRT_SECURE_NO_WARNINGS 1 
#define WIDTH 40
#define HEIGHT 30
#define BLOCK_SIZE 20
int Blocks[HEIGHT][WIDTH] = { 0 }; //  二维数组,用于记录所有的游戏数据
int score = 0;
int moveDirection;  //  小蛇移动方向
int food_i, food_j; //  食物的位置
int isFailure = 0;//  是否游戏失败
int meng_i, meng_j;
void startup();
void show();
void updateWithoutInput();
void updateWithInput();
void moveSnake();
void menu();
void text();
void game();

void moveSnake() //  移动小蛇及相关处理函数
{
	int i, j;
	for (i = 0;i < HEIGHT;i++) // 对行遍历 
		for (j = 0;j < WIDTH;j++) // 对列遍历
			if (Blocks[i][j] > 0) // 大于0的为小蛇元素 
				Blocks[i][j]++; //  让其+1
	int oldTail_i, oldTail_j, oldHead_i, oldHead_j; // 定义变量,存储旧蛇尾、旧蛇头坐标  
	int max = 0; // 用于记录最大值 
	for (i = 0;i < HEIGHT;i++) //  对行列遍历
	{
		for (j = 0;j < WIDTH;j++)
		{
			if (max < Blocks[i][j]) //  如果当前元素值比max大
			{
				max = Blocks[i][j]; // 更新max的值
				oldTail_i = i; //  记录最大值的坐标,就是旧蛇尾的位置
				oldTail_j = j; //  
			}
			if (Blocks[i][j] == 2) // 找到数值为2 
			{
				oldHead_i = i; //  数值为2恰好是旧蛇头的位置
				oldHead_j = j; //  
			}
		}
	}
	int newHead_i = oldHead_i; //  设定变量存储新蛇头的位置
	int newHead_j = oldHead_j;

	//  根据用户按键,设定新蛇头的位置
	if (moveDirection == 'w' || moveDirection == 72) // 向上移动
		newHead_i = oldHead_i - 1;
	else if (moveDirection == 's'||moveDirection==80) // 向下移动
		newHead_i = oldHead_i + 1;
	else if (moveDirection == 'a' || moveDirection == 75) // 向左移动
		newHead_j = oldHead_j - 1;
	else if (moveDirection == 'd' || moveDirection == 77) // 向右移动
		newHead_j = oldHead_j + 1;
	//  如果蛇头超出边界,或者蛇头碰到蛇身,游戏失败
	if (newHead_i >= HEIGHT || newHead_i < 0 || newHead_j >= WIDTH || newHead_j < 0
		||Blocks[newHead_i][newHead_j] > 0 )
	{
		isFailure = 1; //  游戏失败
		return; // 函数返回
	}

	Blocks[newHead_i][newHead_j] = 1;  // 新蛇头位置数值为1	
	if (newHead_i == food_i && newHead_j == food_j) //  如果新蛇头正好碰到食物
	{
		food_i = rand() % (HEIGHT - 5) + 2; //  食物重新随机位置
		food_j = rand() % (WIDTH - 5) + 2; //
		score++;
		// 不对旧蛇尾处理,相当于蛇的长度+1
	}
	else // 新蛇头没有碰到食物
		Blocks[oldTail_i][oldTail_j] = 0; // 旧蛇尾变成空白,不吃食物时保持蛇的长度不变
}

void startup()  //  初始化函数
{
	int i;
	Blocks[HEIGHT / 2][WIDTH / 2] = 1; // 画面中间画蛇头,数字为1
	for (i = 1;i <= 4;i++) //  向左依次4个蛇身,数值依次为2,3,4,5
		Blocks[HEIGHT / 2][WIDTH / 2 - i] = i + 1;
	moveDirection = 'd';	 //  初始向右移动
	food_i = rand() % (HEIGHT - 5) + 2; //  初始化随机食物位置
	food_j = rand() % (WIDTH - 5) + 2; // 
	meng_i = rand() % (HEIGHT - 20) + 2;
	meng_j = rand() % (WIDTH - 20) + 2;
	initgraph(WIDTH* BLOCK_SIZE, HEIGHT* BLOCK_SIZE); //  新开画面
	setlinecolor(RGB(200, 200, 200)); // 设置线条颜色
	BeginBatchDraw(); // 开始批量绘制
	if (isFailure)
	{
		closegraph();
	}
	
}

void show()  // 绘制函数
{   
	cleardevice(); // 清屏
	int i, j;
	for (i = 0;i < HEIGHT;i++) //  对二维数组所有元素遍历
	{
		for (j = 0;j < WIDTH;j++)
		{
			if (Blocks[i][j] > 0) // 元素大于0表示是蛇,这里让蛇的身体颜色色调渐变
				setfillcolor(HSVtoRGB(Blocks[i][j] * 10, 0.9, 1));
			else
				setfillcolor(RGB(150, 150, 150)); // 元素为0表示为空,颜色为灰色
			// 在对应位置处,以对应颜色绘制小方格
			fillrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE,
				(j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE);
		}
	}
	//if (score < 15)
	//{
	setfillcolor(RGB(0, 255, 0)); //  食物为绿色
	//  绘制食物小方块
	fillrectangle(food_j * BLOCK_SIZE, food_i * BLOCK_SIZE,
		(food_j + 1) * BLOCK_SIZE, (food_i + 1) * BLOCK_SIZE);
	//}
	 /*if (score % 5 == 0 && score != 0)
	{
		setfillcolor(BLUE);
		fillrectangle(meng_j * BLOCK_SIZE, meng_i * BLOCK_SIZE, (meng_j + 1) * BLOCK_SIZE, (meng_i + 1) * BLOCK_SIZE);
	}*/
	if (isFailure) //  如果游戏失败
	{
		setbkmode(TRANSPARENT); // 文字字体透明    
		settextcolor(RGB(255, 0, 0));// 设定文字颜色
		settextstyle(80, 0, _T("宋体")); //  设定文字大小、样式
		outtextxy(240, 220, _T("游戏失败")); //  输出文字内rong
	}
	TCHAR s[20];
	_stprintf(s, _T("得分:%d"), score);//将score转换为字符串
	settextstyle(15, 0, _T("宋体"));//设置文字大小,字体
	outtextxy(50, 30, s);//输出得分文字
	FlushBatchDraw(); // 批量绘制
}

void updateWithoutInput() // 与输入无关的更新函数
{
	if (isFailure) //  如果游戏失败,函数返回
		return;
	static int waitIndex = 1; // 静态局部变量,初始化时为1
	waitIndex++; // 每一帧+1
	if (waitIndex == 15) // 如果等于10才执行,这样小蛇每隔10帧移动一次
	{
		moveSnake(); //  调用小蛇移动函数
		waitIndex = 1; // 再变成1
	}
}

void updateWithInput()  // 和输入有关的更新函数
{
	if (kbhit() && isFailure == 0)  //  如果有按键输入,并且不失败
	{
		int input = getch(),ch; //  获得按键输入
		if (input == 'w' || input == 's' || input == 'd' || input == 'a') // 如果是asdw 
		{
			moveDirection = input;  // 设定移动方向
			moveSnake(); // 调用小蛇移动函数
		}
		else if (input == ' ')
			system("pause");
		else if (input)
		{
			ch = getch();
			moveDirection = ch;
		}
	}
}
void menu() {	
a:	initgraph(800, 600);
	MOUSEMSG m;
	IMAGE bk;
	loadimage(&bk, _T("back.jpg"), 800, 600);
	putimage(0, 0, &bk);
	settextstyle(25, 0, _T("草书"));
	settextcolor(RGB(250, 0, 0));
	outtextxy(300, 100, _T("贪吃蛇大作战"));
	setfillcolor(GREEN);
	fillcircle(200, 120, 20);
	putpixel(190, 115, BLACK);
	putpixel(210, 115, BLACK);
	line(190, 125, 210, 125);
	fillcircle(550, 120, 20);
	putpixel(540, 115, BLACK);
	putpixel(560, 115, BLACK);
	line(540, 125, 560, 125);
	setfillcolor(RGB(135, 120, 110));
	fillrectangle(300, 250, 450, 280);
	fillrectangle(300, 290, 450, 320);
	fillrectangle(300, 330, 450, 360);
	fillrectangle(300, 370, 450, 400);
	fillrectangle(300, 410, 450, 440);
	settextstyle(15, 8, _T("楷书"));
	settextcolor(RGB(196, 198, 197));
	outtextxy(333, 260, _T("普 通 模 式"));
	settextstyle(15, 8, _T("楷书"));
	settextcolor(RGB(196, 198, 197));
	outtextxy(333, 300, _T("关 卡 模 式"));
	settextstyle(15, 8, _T("楷书"));
	settextcolor(RGB(196, 198, 197));
	outtextxy(333, 340, _T("冒 险 模 式"));
	settextstyle(15, 8, _T("楷书"));
	settextcolor(RGB(196, 198, 197));
	outtextxy(333, 380, _T("选 择 皮 肤"));
	settextstyle(15, 8, _T("楷书"));
	settextcolor(RGB(196, 198, 197));
	outtextxy(333, 420, _T("离 开 游 戏"));
	while (1) {
		m = GetMouseMsg();
		if (m.x >= 300 && m.x <= 450 && m.y >= 250 && m.y <= 280)
		{
			settextcolor(RED);
			outtextxy(333, 260, _T("普 通 模 式"));
			if (m.uMsg == WM_LBUTTONDOWN)
			{   
				game();
				goto a;
			}
		}
		
		else {
			settextstyle(15, 8, _T("楷书"));
			settextcolor(RGB(196, 198, 197));
			outtextxy(333, 260, _T("普 通 模 式"));
			settextstyle(15, 8, _T("楷书"));
			settextcolor(RGB(196, 198, 197));
			outtextxy(333, 300, _T("关 卡 模 式"));
			settextstyle(15, 8, _T("楷书"));
			settextcolor(RGB(196, 198, 197));
			outtextxy(333, 340, _T("冒 险 模 式"));
			settextstyle(15, 8, _T("楷书"));
			settextcolor(RGB(196, 198, 197));
			outtextxy(333, 380, _T("选 择 皮 肤"));
			settextstyle(15, 8, _T("楷书"));
			settextcolor(RGB(196, 198, 197));
			outtextxy(333, 420, _T("离 开 游 戏"));

		}
	}
}
void game() {
	startup();
	while (1)
	{
			show();
			updateWithoutInput();
			updateWithInput();

		if (isFailure)
		{  
			show();
			system("pause");
			closegraph();
			break;
		}
	}
	 
}
int main() {
	a:menu();
    goto a;
	return 0;
}
ava
llllliiii

2022-6-30

额,这个我改了之后游戏进不去了,我知道后面需要让游戏地图初始化,主要是游戏结束之后,返回到的那个菜单是黑屏状态需要解决 -  llllliiii  2022-6-30
0

看起来,贪吃蛇不是你写的,你只想加一个"结束后重新开始"的功能。而你能力不够,导致无法完成。

于是,这个程序就被改的乱七八糟了,乱到我都不知道如何帮你改。比如,两个明显的 goto 都是你加的吧。

写程序要循序渐进。对初学者来说,读别人的程序比自己写更难,所以你进行不下去了。换一个学习方式吧。比如,可以试着做 C 语言天罡三十六题。

ava
慢羊羊

2022-6-30

主要是改了好多天了,加上期末复习,又过了很久这是修改很多次之后的,我都不知道之前改了些什么,原来没修改的版本我也有,还是比较精简的,但是也是不能解决这个问题,然后我就借鉴了网上的一些人的菜单跳转,添加goto,,没有出现菜单,然后我就想问怎么解决这个问题,其中一些代码被注释掉,是朋友对这个游戏有一些新的理解,我想菜单实现之后能将功能实现  -  llllliiii  2022-6-30
技术讨论社区