C语言小游戏贪吃蛇实现不了,求解!

小白刚入坑,想尝试小游戏,但奈何运行不了,又看不太懂问题,求各路大神给予解~~~万分感谢!

 

https://blog.csdn.net/zhao5502169/article/details/61620774

小游戏代码借鉴自上方的大神

 

程序用的是:vc++6

 

下图是我打的代码:

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#define MAX 100

long long int speed = 0;	//控制敌机的速度
int position_x, position_y;	//飞机的所在位置
int high, width;	//地图的大小
int bullet_x, bullet_y;	//子弹的位置
int enemy_x, enemy_y;	//敌人的位置
int map[MAX][MAX];

/*	0表示空白,1表示战机*的区域,2表示敌人敌机的位置。
	3表示上下围墙,4表示左右围墙,5表示子弹的位置*/

int score;
void starup()	//初始化所有的信息
{
	high = 20;
	width = 30;
	position_x = high / 2;
	position_y = width / 2;
	bullet_x = 0;
	bullet_y = position_y;
	enemy_x = 2;
	enemy_y = position_y - 1;
	score = 0;
}
void startMap()
{
	int i, j;
	for(i = 1; i <= high - 1; i++)
	{
		map[i][1] = 4;
		for(j = 2; j <= width - 1; j++)
			map[i][j] = 0;
		map[i][width] = 4;
	}
		//下方围墙的初始化
	i = high;
	for(j = 1; j <= width; j++)
		map[i][j] = 3;

	map[bullet_x][bullet_y] = 5;

		//这里是战机大小的初始化开始
	map[position_x - 1][position_y] = 1;
	i = position_x;
	for(j = position_y - 2; j <= position_y + 2; j++)
		map[i][j] = 1;
	map[position_x + 1][position_y - 1] = 1;
	map[position_x + 1][position_y + 1] = 1;
		//初始化结束

		//敌人战机的初始化
	map[enemy_z][enemy_y] = 2;
	map[enemy_x - 1][enemy_y - 1] = 2;
	map[enemy_x - 1][enemy_y + 1] = 2;
		//敌人战机初始化结束
}

void HideCursor()	//隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x, int y)	//清理一部分屏幕
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void updatewithoutInput()	//于输入无关的跟新
{
	if(bullet_x > 0)
		bullet_x--;
	if((bullet_x == enemy_x) && (bullet_y == enemy_y))	//当敌人的飞机被击中时
	{
		score++;
		enemy_x = 0;
		enemy_y = rand() % width;
		bullet_x = 0;
	}
	if(enemy_x > high)	//当飞机超出区域
	{
		enemy_x = 0;
		enemy_y = rand() % width;
	}
	if(speed == 1)
		for(int i = 1; i <= 10000; i++)		//用来控制敌机的速度
		{
			for(int j = 1; j <= 1000; j++)
			{
				speed = 1;
			}
		}
	speed = 0;
	if(speed == 0)
	{
		enemy_x++;
		speed = 1;
	}
}
void updateWithInput()	//与输入有关的更新
{
	char input;
	if(kdhit())	//在vc6.0++下为_kdhit();
	{
		input = getch();	//在vc6.0++下为_getch();
		if(input == 'a')
			position_y--;
		if(input == 's')
			position_x++;
		if(input == 'd')
			position_y++;
		if(input == 'w')
			position_x--;
		if(input == ' ')
		{
			bullet_x = position_x - 1;
			bullet_y = position_y;
		}
	}
}
void show()	//展示的内容
{
	gotoxy(0, 0);
	int i, j;
	for(i = 1; i <= high; i++)
	{
		for(j = 1; j <= width; j++)
		{
			if(map[i][j] == 0)
				printf(" ");
			if(map[i][j] == 1)
				printf("*");
			if(map[i][j] == 2)
				printf("#");
			if(map[i][j] == 3)
				printf("~");
			if(map[i][j] == 4)
				printf("|");
			if(map[i][j] == 5)
				printf("|");
		}
		printf("\n");
	}
	printf("\n你的得分:%d\n\n", score);
	printf("操作说明:ASDW分别操作 左下右上四个的移动\n");
	printf("**空格是发出的子弹**\n");
}
int main()
{
	starup();
	while(1)
	{
		HideCursor();
		startMap();
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

 

下图是问题原因:

--------------------Configuration: C_grame - Win32 Debug--------------------
Compiling...
C_grame.cpp
D:\C Programme\C_grame.cpp(8) : error C2632: 'long' followed by 'long' is illegal
D:\C Programme\C_grame.cpp(58) : error C2065: 'enemy_z' : undeclared identifier
D:\C Programme\C_grame.cpp(113) : error C2065: 'kdhit' : undeclared identifier
D:\C Programme\C_grame.cpp(166) : error C2065: 'updateWithoutInput' : undeclared identifier
执行 cl.exe 时出错.

C_grame.obj - 1 error(s), 0 warning(s)

 

代号8错误我看不太懂,但代号58,113,166的错误应该是没有定义,可前面代码不是有定义吗?为什么会出现问题?

 

求指点~

谢谢各路大神

愿各位大神升官发财,头发越来越茂盛,也越来越强。

error C2065: 'enemy_z' : undeclared identifier

未定义的错误。

第6行不支持 long long,你可以改成int

第58行的enemy_z没有定义,你一开始只定义了int enemy_x, enemy_y;

另外在VC6.0下,113行的kbhit()要用_kbhit()

166行改为updatewithoutInput()

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632