C语言 VC6.0控制台编写一个贪吃蛇游戏

最近看到有人发布贪吃蛇的编码实现,想到了自己多年之前也实现过一个,发布在这里,做一下回忆。

C语言面向过程编码,基本功能实现以函数为单位,重点就是各个函数的设计和实现。

本篇使用VC6.0来实现,因为我大二大概就是2012年的时候,上手就使用了这个工具。直接上代码吧:

自定义的函数实现:

//文件名:map.cpp
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include "map.h"
#include <time.h>

int a[H][W];	//游戏区域
int s[H*W][2];	//蛇的动态长度
int sLength;	//蛇的初始长度
int direction;   //蛇的方向
int score=0;	  //分数记录
bool eated = false;


void init()    //界面初始化
{	
	srand((unsigned)time(NULL));
	//设置光标隐藏
	CONSOLE_CURSOR_INFO cursor_info = {1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);	
	int i,j;
	for(i=0;i<H;i++)
	{
		a[i][0]=1;
		a[i][W-1]=1;
	}
	for(j=0;j<W;j++)
	{
		a[0][j]=1;
		a[H-1][j]=1;
	}
	sLength=4;
	s[0][0]=H/2;
	s[0][1]=W/2;
	for(i=1;i<4;i++)
	{
		s[i][0]=s[0][0]+i;
		s[i][1]=s[0][1];
	}
	direction=UP;
}

void gotoxy(int i,int j)	//移动光标到指定点
{
	COORD position={j,i};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
}

void drawmap()	//画地图
{
	int i,j;
	for(i=0;i<H;i++)
	{
		for(j=0;j<W;j++)
		{
			if(a[i][j]==0)
				printf(" ");
			else
				printf("*");
		}
		printf("\n");
	}
}

void drawSnake()	//画蛇
{
	int i;
	for(i=0;i<sLength;i++)
	{
		gotoxy(s[i][0],s[i][1]);
		printf("@");
	}
}

void move()
{
	int i;
	gotoxy(s[sLength-1][0],s[sLength-1][1]);
	printf(" ");
	if(eated)
	{
		sLength++;
		eated=false;
	}
	for(i=sLength-1;i>0;i--)
	{
		s[i][0]=s[i-1][0];
		s[i][1]=s[i-1][1];
	}
	switch(direction)
	{
	case UP:
		s[0][0]--;
		break;
	case DOWN:
		s[0][0]++;
		break;
	case LEFT:
		s[0][1]--;
		break;
	case RIGHT:
		s[0][1]++;
		break;
	}
}

void key()
{
	if(_kbhit()!=0)
	{
		char in;
		while(!_kbhit()==0)
			in = _getch();
		switch(in)
		{
		case 'W':
		case 'w':
			if(direction!=DOWN)
				direction=UP;
			break;
		case 'S':
		case 's':
			if(direction!=UP)
				direction=DOWN;
			break;
		case 'A':
		case 'a':
			if(direction!=RIGHT)
				direction=LEFT;
			break;
		case 'D':
		case 'd':
			if(direction!=LEFT)
				direction=RIGHT;
			break;
		case 'P':
		case 'p':
			gotoxy(H,0);
			system("pause");
			gotoxy(H,0);
			printf(" ");
				break;
		}
	}
}

int check(int ii,int jj)	//判断这个点是否可以投放食物
{
	if(a[ii][jj]==1)
		return 0;
	int i;
	for(i=0;i<sLength;i++)
	{
		if(ii==s[i][0]&&jj==s[i][1])
			return 0;
	}
	if(ii==0||ii==H-1||jj==0||jj==W-1)
		return 0;
	return 1;
}

void food()			//投放食物函数
{
	int i,j;
	do
	{
		i=rand()%H;
		j=rand()&W;
	}while(check(i,j)==0);
	a[i][j]=-1;
	gotoxy(i,j);
	printf("$");
}

bool gameOver()
{
	bool isGameOver = false;
	int sX = s[0][0],sY=s[0][1];  // 蛇头的X坐标和Y坐标
	if(sX==0||sX==H-1||sY==0||sY==W-1)
		isGameOver = true;
	for(int i=1;i<sLength;i++)
	{
		if(s[i][0]==sX&&s[i][1]==sY)
			isGameOver = true;
	}
	return isGameOver;
}

void printfScore()
{
	gotoxy(0,W+2);
	printf("  EatSnake game!");
	gotoxy(1,W+2);
	printf("  JYH制作");
	gotoxy(7,W+2);
	printf("  得分:%d",score);
	gotoxy(3,W+2);
	printf("操作:W:上 S:下");
	gotoxy(4,W+2);
	printf("      A:左 D:右");
	gotoxy(5,W+2);
	printf("      P:暂停");
}

头文件实现:

//文件名 map.h
#ifndef _MAP_H_
#define _MAP_H_

#define H 23
#define W 55
#define WAIT_TIME 500
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3

extern int a[H][W];	//游戏区域
extern int s[H*W][2];	//蛇的动态长度
extern int sLength;	//蛇的初始长度
extern int direction;   //蛇的方向
extern int score;
extern bool eated;

void init();
void drawmap();
void drawSnake();
void key();
void move();
int check(int ii,int jj);
void food();
bool gameOver();
void printfScore();

#endif

主函数实现:

//文件名:main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#include "map.h"

int main()
{
	init();
	drawmap();
	food();
	while(1)
	{
		drawSnake();
		printfScore();
		Sleep(WAIT_TIME);
		key();
		move();
		if(gameOver())
		{
			system("cls");
			printf("Game Over! \n");
			system("pause");
		}
		if(a[s[0][0]][s[0][1]]==-1)
		{
			eated = true;
			score+=10;
			food();
			a[s[0][0]][s[0][1]]=0;
		}
	}
	getchar();

	return 0;
}

运行效果:

C语言 VC6.0控制台编写一个贪吃蛇游戏

不足之处,是只能运行一次,game over之后,需要重新ctrl+F5 。

上一篇:C语言实现一个走迷宫小游戏(深度优先算法)


下一篇:[CSS]关于Flexbox