初学c语言写出的贪吃蛇

本人是一在校大学生,初学c语言,最近疫情在家也是无聊于是写了个贪吃蛇给自己玩儿 大佬们别笑我啊,也希望大家多多给我提提意见最近在学java了,学校也开了数据结构,想多学几门语言但又想学精大佬们给点意见吧 我把贪吃蛇源码贴在下面

#include <graphics.h>
#include<stdio.h>
#include<conio.h>				//_getch() _kbhit()(检查是否有见键盘输入)
#include<easyx.h>
#include<time.h>				//time()
#define SPE  100					//蛇的速度 越小速度越快
#define ROW 46
#define COL 64
enum game
{
	SPACE, WALL, SNAKE, FOOD//空地  墙  蛇  食物
};
int score = 0;
int SnakeSize = 3;
DWORD t1, t2;			//定义两个时间控制移动速度
COORD snake[1024];				//蛇			typedef struct _COORD {SHORT X;SHORT Y;} COORD, *PCOORD;
int map[ROW][COL];		//地图大小
char SnakeDir = 'D';			//蛇的初始方向
void ChangeMove();				//蛇的方向(改变)
void move();					//蛇的移动
void DrawMap();					//绘制地图
void init();					//初始化
void AddFood();					//添加食物
int main()
{
	initgraph(640, 480);
	init();
			
	while (1)
	{		
		t2 = GetTickCount();
		DrawMap();
		if (_kbhit())
		{	
			ChangeMove();
			move();	
			t2 = GetTickCount();
			t1 = t2;
		}
		if (t2 - t1 > SPE)
		{
			move();
			t1 = t2;
		}	
	}
	getchar();
	closegraph();
	return 0;
}
void init()
{
	srand((unsigned)time(NULL));	//随机种子
	memset(map, SPACE, sizeof(map));//初始化map数组为0即SPACE
	//每一行的 第一个 和 最后一个 是墙
	for (int i = 0; i < ROW; i++)
	{
		map[i][0] = map[i][COL - 1] = WALL;
	}
	//每一列的 第二个 和 倒数第二 个是墙
	for (int j = 1; j < COL - 1; j++)
	{
		map[0][j] = map[ROW - 1][j] = WALL;
	}
	map[6][5]=map[6][4] = map[6][3] = SNAKE;
	snake[0].X = 6;
	snake[0].Y = 5;
	snake[1].X = 6;
	snake[1].Y = 4;
	snake[2].X = 6;
	snake[2].Y = 3;
	AddFood();
}
void ChangeMove()
{
	switch (_getch())
	{
	case'A':
	case'a':
	case 75:
		if (SnakeDir != 'D') SnakeDir = 'A';	//蛇不能后退
		break;
	case'D':
	case'd':
	case 77:
		if (SnakeDir != 'A') SnakeDir = 'D';
		break;
	case'W':
	case'w':
	case 72:
		if (SnakeDir != 'S') SnakeDir = 'W';
		break;
	case'S':
	case's':
	case 80:
		if (SnakeDir != 'W') SnakeDir = 'S';
		break;
	case 32:
		_getch();
		break;
	default:
		break;
	}
}
void move()
{	
	COORD next;		//蛇头的下一个位置
	next.X = 0; next.Y = 0;
	switch (SnakeDir)
	{
	case'A':
		next.X = snake[0].X;
		next.Y = snake[0].Y - 1;
		break;
	case'W':
		next.X = snake[0].X - 1;
		next.Y = snake[0].Y;
		break;
	case'D':
		next.X = snake[0].X;
		next.Y = snake[0].Y + 1;
		break;
	case'S':
		next.X = snake[0].X + 1;
		next.Y = snake[0].Y;
		break;
	default:
		break;
	}
	switch (map[next.X][next.Y])
	{	
	case SPACE: 
		map[snake[SnakeSize - 1].X][snake[SnakeSize - 1].Y] = SPACE;	// 删除蛇尾最后一个
		for (int i = SnakeSize - 1; i > 0; i--)
		{
			snake[i] = snake[i - 1];
			map[snake[i].X][snake[i].Y] = SNAKE;
		}
		snake[0] = next;
		map[snake[0].X][snake[0].Y] = SNAKE;
		break;
	case FOOD:
		SnakeSize++;	score++;					//蛇的长度加一,得分+1
		for (int i = SnakeSize - 1; i > 0; i--)
		{
			snake[i] = snake[i - 1];
			map[snake[i].X][snake[i].Y] = SNAKE;
		}
		snake[0] = next;
		map[snake[0].X][snake[0].Y] = SNAKE;
		AddFood();
		break;
	
	case SNAKE:
		MessageBox(GetHWnd(), "你太菜啦!!贪吃蛇都能输了来", "小辣鸡", MB_OK);
		exit(0);
		break;
	case WALL:
		MessageBox(GetHWnd(), "你太菜啦!!贪吃蛇都能输了来", "小辣鸡", MB_OK);
		exit(0);
		break;	
	}
}
void DrawMap()
{
	BeginBatchDraw();
	//setbkcolor(WHITE);				//设置背景颜色
	settextcolor(RGB(238, 0, 0));
	cleardevice();		//清屏
	char arr[10],str[]="操作方式:按w a s d 或上下左右键控制蛇";		//保存成绩
	sprintf_s(arr, "总分:%d", score);	//将成绩格式化输出到字符串arr中 
	outtextxy(0, 0, arr);				//显示成绩
	outtextxy(100, 0, str);
	for (int y = 0; y < ROW; y++)		//y轴方向向下
	{
		for (int x = 0; x < COL; x++)	//x轴方向向右
		{
			switch (map[y][x])
			{
			case SPACE:
				break;
			case WALL:
				setlinecolor(BLACK);
				setfillcolor(BROWN);	//棕色
				fillrectangle(x * 10, y * 10 + 20, x * 10 + 10, y * 10 + 30);
				break;
			case SNAKE:
				setlinecolor(RGB(0, 245, 255));		//绿色 
				setfillcolor(LIGHTMAGENTA);			//亮紫色
				//setfillcolor(BROWN);
				fillrectangle(x * 10, y * 10 + 20, x * 10 + 10, y * 10 + 30);
				break;
			case FOOD:
				setlinecolor(RGB(0, 245, 255));		//绿色 
				setfillcolor(RED);			//亮紫色
				//setfillcolor(YELLOW);
				fillrectangle(x * 10, y * 10 + 20, x * 10 + 10, y * 10 + 30);
				break;
			default:
				break;
			}
		}
	}
	EndBatchDraw();
}
void AddFood()
{	
	int x, y;
	do {
		x = rand() % (ROW - 1) ;
		y = rand() % (COL - 1) ;
		
	} while (map[x][y] != SPACE);
	map[x][y] = FOOD;
}

要用到easyx图形库,大家可以去搞个玩一下

以下是一个简单的使用 C 语言编写的贪吃蛇游戏示例: ```c #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <windows.h> #define WIDTH 20 #define HEIGHT 20 #define UP 'w' #define DOWN 's' #define LEFT 'a' #define RIGHT 'd' int score; int gameover; int x, y; // 蛇头的坐标 int fruitX, fruitY; // 水果的坐标 int tailX[100], tailY[100]; // 蛇身的坐标 int tailLength; enum eDirection { STOP = 0, UP, DOWN, LEFT, RIGHT }; enum eDirection dir; void Setup() { gameover = 0; dir = STOP; x = WIDTH / 2; y = HEIGHT / 2; fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; score = 0; } void Draw() { system("cls"); for (int i = 0; i < WIDTH + 2; i++) printf("#"); printf("\n"); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (j == 0) printf("#"); if (i == y && j == x) printf("O"); else if (i == fruitY && j == fruitX) printf("F"); else { int printTail = 0; for (int k = 0; k < tailLength; k++) { if (tailX[k] == j && tailY[k] == i) { printf("o"); printTail = 1; } } if (!printTail) printf(" "); } if (j == WIDTH - 1) printf("#"); } printf("\n"); } for (int i = 0; i < WIDTH + 2; i++) printf("#"); printf("\n"); printf("Score: %d\n", score); } void Input() { if (_kbhit()) { switch (_getch()) { case UP: dir = UP; break; case DOWN: dir = DOWN; break; case LEFT: dir = LEFT; break; case RIGHT: dir = RIGHT; break; case 'x': gameover = 1; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < tailLength; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case UP: y--; break; case DOWN: y++; break; case LEFT: x--; break; case RIGHT: x++; break; } if (x >= WIDTH) x = 0; else if (x < 0) x = WIDTH - 1; if (y >= HEIGHT) y = 0; else if (y < 0) y = HEIGHT - 1; for (int i = 0; i < tailLength; i++) { if (tailX[i] == x && tailY[i] == y) { gameover = 1; break; } } if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; tailLength++; } } int main() { Setup(); while (!gameover) { Draw(); Input(); Logic(); Sleep(10); // 控制游戏速度 } return 0; } ``` 这是一个简单的控制台贪吃蛇游戏,使用了 Windows.h 头文件来实现延迟。你可以根据需要进行修改和扩展,添加额外的功能和界面。请确保在编译和运行之前,你的编译环境中已经包含了 `conio.h` 和 `windows.h` 头文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值