#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define High 20
#define Width 30
//全局变量
int moveDirection,moveDirection2; //小蛇移动方向,上下左右分别用1234表示
int food_x,food_y; //食物的位置
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;
pos.Y = y; //定义光标位置
SetConsoleCursorPosition(handle, pos); //移动光标
}
//移动小蛇
//第一步扫描数组canvas的所有元素,找到正数元素都加1
//找到最大元素(蛇尾巴),把其变为0
//找到等于2的元素(蛇头),根据输出的上下左右方向把对应的另一像素值设为1(新蛇头)
void moveSnakeByDirection()
{
int i, j;
for (i = 1; i < High - 1; i++)
for (j = 1; j < Width - 1; j++)
if (canvas[i][j] > 0&&canvas[i][j]<101)
canvas[i][j]++;
int oldTail_i, oldTail_j, oldHead_i, oldHead_j;
int max = 0;
for(i=1;i<High-1;i++)
for(j=1;j<Width-1;j++)
if (canvas[i][j] > 0)
{
if (max < canvas[i][j]&&canvas[i][j]<101)
{
max = canvas[i][j];
oldTail_i = i;
oldTail_j = j;
}
if (canvas[i][j] == 2)
{
oldHead_i = i;
oldHead_j = j;
}
}
int newHead_i,newHead_j;
if (moveDirection == 1) //向上移动
{
newHead_i = oldHead_i-1;
newHead_j = oldHead_j;
}
if (moveDirection == 2) //向下移动
{
newHead_i = oldHead_i+1;
newHead_j = oldHead_j;
}
if (moveDirection == 3) //向左移动
{
newHead_i = oldHead_i;
newHead_j = oldHead_j-1;
}
if (moveDirection == 4) //向右移动
{
newHead_i = oldHead_i;
newHead_j = oldHead_j+1;
}
//如果新蛇头吃到食物
if(canvas[newHead_i][newHead_j]==-2)
{
canvas[food_x][food_y] = 0;
//产生一个新的食物
food_x = rand()%(High-5)+2;
food_y = rand()%(Width-5)+2;
canvas[food_x][food_y] = -2;
//原来的旧蛇尾留着,长度自动加一
}
else //否则 原来旧蛇尾去掉长度不变
canvas[oldTail_i][oldTail_j] = 0;
//小蛇是否和自身撞或和边框撞,游戏失败
if(canvas[newHead_i][newHead_j]>0||canvas[newHead_i][newHead_j]==-1)
{
printf("游戏失败!\n");
Sleep(2000);
system("pause");
exit(0);
}
else
canvas[newHead_i][newHead_j] = 1;
}
void moveSnakeByDirection2()
{
int i, j;
for (i = 1; i < High - 1; i++)
for (j = 1; j < Width - 1; j++)
if (canvas[i][j] >100)
canvas[i][j]++;
int oldTail_i, oldTail_j, oldHead_i, oldHead_j;
int max = 0;
for(i=1;i<High-1;i++)
for(j=1;j<Width-1;j++)
if (canvas[i][j] > 0)
{
if (max < canvas[i][j]&&canvas[i][j]>100)
{
max = canvas[i][j];
oldTail_i = i;
oldTail_j = j;
}
if (canvas[i][j] == 102)
{
oldHead_i = i;
oldHead_j = j;
}
}
int newHead_i,newHead_j;
if (moveDirection2 == 1) //向上移动
{
newHead_i = oldHead_i-1;
newHead_j = oldHead_j;
}
if (moveDirection2 == 2) //向下移动
{
newHead_i = oldHead_i+1;
newHead_j = oldHead_j;
}
if (moveDirection2 == 3) //向左移动
{
newHead_i = oldHead_i;
newHead_j = oldHead_j-1;
}
if (moveDirection2 == 4) //向右移动
{
newHead_i = oldHead_i;
newHead_j = oldHead_j+1;
}
//如果新蛇头吃到食物
if(canvas[newHead_i][newHead_j]==-2)
{
canvas[food_x][food_y] = 0;
//产生一个新的食物
food_x = rand()%(High-5)+2;
food_y = rand()%(Width-5)+2;
canvas[food_x][food_y] = -2;
//原来的旧蛇尾留着,长度自动加一
}
else //否则 原来旧蛇尾去掉长度不变
canvas[oldTail_i][oldTail_j] = 0;
//小蛇是否和自身撞或和边框撞,游戏失败
if(canvas[newHead_i][newHead_j]>0||canvas[newHead_i][newHead_j]==-1)
{
printf("游戏失败!\n");
Sleep(2000);
system("pause");
exit(0);
}
else
canvas[newHead_i][newHead_j] = 101;
}
void startup() //数据初始化
{
int i, j;
//初始化边框
for (i = 0; i < High; i++)
{
canvas[i][0] = -1;
canvas[i][Width - 1] = -1;
}
for (j = 0; j < Width; j++)
{
canvas[0][j] = -1;
canvas[High - 1][j] = -1;
}
//初始化蛇头位置
canvas[High / 4][Width /2] = 1;
canvas[High*3/4][Width/2] = 101;
//初始化蛇身,画布(canvas)中的元素值分别为2、3、4、5、102、103、104、105等
for (i = 1; i <= 4; i++)
canvas[High / 4][Width / 2 - i] = i + 1;
for(i=1;i<=4;i++)
canvas[High*3/4][Width/2-i] = i+101;
//初始小蛇向右移动
moveDirection = 4;
moveDirection2 = 4;
food_x = rand()%(High-5)+2;
food_y = rand()%(Width-5)+2;
canvas[food_x][food_y] = -2;
}
void show() //显示画面
{
gotoxy(0, 0); //光标移动到原点位置,以下重画清屏
int i, j;
for (i = 0; i < High; i++)
{
for (j = 0; j < Width; j++)
{
if (canvas[i][j] == 0)
printf(" "); //输出空格
else if (canvas[i][j] == -1)
printf("#"); //输出边框#
else if (canvas[i][j] == 1||canvas[i][j]==101)
printf("@"); //输出蛇身@
else if (canvas[i][j] > 1&&canvas[i][j]!=101)
printf("*"); //输出蛇身*
else if(canvas[i][j]==-2)
printf("F");
}
printf("\n");
}
Sleep(100);
}
void updateWithoutInput() //与用户输入无关的更新
{
moveSnakeByDirection();
moveSnakeByDirection2();
}
void updateWithInput() //与用户输入有关的更新
{
char input;
if (_kbhit()) //判断是否有输入
{
input = _getch(); //根据用户的不同输入来移动,不必输入回车(无回显非缓冲)
if (input == 'a')
{
moveDirection = 3; //位置左移
moveSnakeByDirection();
}
else if (input == 'd')
{
moveDirection = 4; //位置右移
moveSnakeByDirection();
}
else if (input == 'w')
{
moveDirection = 1; //位置上移
moveSnakeByDirection();
}
else if (input == 's')
{
moveDirection = 2; //位置下移
moveSnakeByDirection();
}
else if (input == 'j')
{
moveDirection2 = 3; //位置左移
moveSnakeByDirection2();
}
else if (input == 'l')
{
moveDirection2 = 4; //位置右移
moveSnakeByDirection2();
}
else if (input == 'i')
{
moveDirection2 = 1; //位置上移
moveSnakeByDirection2();
}
else if (input == 'k')
{
moveDirection2 = 2; //位置下移
moveSnakeByDirection2();
}
}
}
int main()
{ srand(time(0));
startup(); //数据的初始化
while (1) //游戏循环执行
{
show(); //显示画面
updateWithoutInput(); //与用户输入无关的更新
updateWithInput(); //与用户输入有关的更新
}
return 0;
}
贪吃蛇双人
最新推荐文章于 2024-08-14 20:16:41 发布