一、构造
二维数组canvas[Higth][width]的对应元素,值为0输出空格;值为-1输出蛇头@,值为大于1的正数输出蛇身*。在startup()函数中初始化蛇头(for(i=1;i<=4;i++) canvas[High/2][Width/2-1]=i+1;),元素值分别为2、3、4、5。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
//游戏尺寸
#define High 20
#define Width 30
//全局变量
int canvas[High][width]=0; //二为数组存储游戏画布中对应的元素
//0为空格,-1为边框,1为蛇头@,大于1的正数为蛇身*
void gotoxy(int x,int y) //将光标移动到(x,y)位置
{
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
posY=y;
SetConsoleCursorPosition(handle,pos);
}
void startup()
{
int i, j;
//初始化边框
for(i=0;i<High;i++)
{
canvas[i][0]=-1;
canvas[High-1][j]=-1;
}
//初始化蛇头位置
canvas[High/2][width/2-i];
今天的学习就到这