一、构造小蛇
首先 构造出静止小蛇的模样,大致效果如下:
详细C代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define high 20
#define width 30
int canvas[high][width] = {0};
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);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
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 / 2][width / 2] = 1;
for(i = 1; i <= 4; i++)
canvas[high / 2][width / 2 - i] = i + 1;
}
void show()
{
gotoxy(0, 0);
HideCursor();
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)
printf("&");
else if(canvas[i][j] > 1)
printf("*");
}
printf("\n");
}
}
void updateWithoutInput()
{
}
void updateWithInput()
{
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
二、让小蛇自己动起来
实现的逻辑:
先将小蛇身上所有元素加1,然后用变量head_i,head_j记录原来蛇头的位置,然后依照移动方向,在原蛇头的位置上找到新蛇头,并把数字为6的蛇尾去掉
我这里默认往右移动,大家也可以依照自己的想法让它向别的方向动
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define high 20
#define width 30
int direct;
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);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void move()
{
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]++;
}
}
int tail_i, tail_j, head_i, head_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])
{
max = canvas[i][j];
tail_i = i;
tail_j = j;
}
if(canvas[i][j] == 2)
{
head_i = i;
head_j = j;
}
}
}
}
canvas[tail_i][tail_j] = 0;
if(direct == 1) //向上移动
canvas[head_i - 1][head_j] = 1;
if(direct == 2) //向右移动
canvas[head_i][head_j + 1] = 1;
if(direct == 3) //向下移动
canvas[head_i - 1][head_j] = 1;
if(direct == 4) //向左移动
canvas[head_i][head_j - 1] = 1;
}
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 / 2][width / 2] = 1;
for(i = 1; i <= 4; i++)
canvas[high / 2][width / 2 - i] = i + 1;
direct = 2; //先默认向右移
}
void show()
{
gotoxy(0, 0);
HideCursor();
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)
printf("&");
else if(canvas[i][j] > 1)
printf("*");
}
printf("\n");
}
}
void updateWithoutInput()
{
move();
}
void updateWithInput()
{
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
三、控制小蛇游走
贪吃蛇小游戏移动效果
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define high 20
#define width 30
int direct;
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);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void move()
{
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]++;
}
}
int tail_i, tail_j, head_i, head_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])
{
max = canvas[i][j];
tail_i = i;
tail_j = j;
}
if(canvas[i][j] == 2)
{
head_i = i;
head_j = j;
}
}
}
}
canvas[tail_i][tail_j] = 0;
if(direct == 1) //向上移动
canvas[head_i - 1][head_j] = 1;
if(direct == 2) //向右移动
canvas[head_i][head_j + 1] = 1;
if(direct == 3) //向下移动
canvas[head_i + 1][head_j] = 1;
if(direct == 4) //向左移动
canvas[head_i][head_j - 1] = 1;
}
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 / 2][width / 2] = 1;
for(i = 1; i <= 4; i++)
canvas[high / 2][width / 2 - i] = i + 1;
direct = 2; //先默认向右移
}
void show()
{
gotoxy(0, 0);
HideCursor();
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)
printf("&");
else if(canvas[i][j] > 1)
printf("*");
}
printf("\n");
}
}
void updateWithoutInput()
{
// move();
}
void updateWithInput()
{
char input;
if(kbhit())
{
input = getch();
if(input == 'w')
{
direct = 1;
move();
}
else if(input == 'd')
{
direct = 2;
move();
}
else if(input == 's')
{
direct = 3;
move();
}
else if(input == 'a')
{
direct = 4;
move();
}
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
四、增加判断失败机制
主要是看小蛇是否咬到自己或者撞到墙
详细C代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define high 20
#define width 30
int direct;
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);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void move()
{
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]++;
}
}
int tail_i, tail_j, head_i, head_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])
{
max = canvas[i][j];
tail_i = i;
tail_j = j;
}
if(canvas[i][j] == 2)
{
head_i = i;
head_j = j;
}
}
}
}
canvas[tail_i][tail_j] = 0;
int new_i, new_j; //新头
if(direct == 1) //向上移动
{
new_i = head_i - 1;
new_j = head_j;
}
if(direct == 2) //向右移动
{
new_i = head_i;
new_j = head_j + 1;
}
if(direct == 3) //向下移动
{
new_i = head_i + 1;
new_j = head_j;
}
if(direct == 4) //向左移动
{
new_i = head_i;
new_j = head_j - 1;
}
if(canvas[new_i][new_j] > 0 || canvas[new_i][new_j] == -1)
{ //判断小蛇是否咬到自己或者撞墙
printf("\nGame Over!\n");
exit(0);
}
else
canvas[new_i][new_j] = 1;
}
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 / 2][width / 2] = 1;
for(i = 1; i <= 4; i++)
canvas[high / 2][width / 2 - i] = i + 1;
direct = 2; //先默认向右移
}
void show()
{
gotoxy(0, 0);
HideCursor();
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)
printf("&");
else if(canvas[i][j] > 1)
printf("*");
}
printf("\n");
}
}
void updateWithoutInput()
{
// move();
}
void updateWithInput()
{
char input;
if(kbhit())
{
input = getch();
if(input == 'w')
{
direct = 1;
move();
}
else if(input == 'd')
{
direct = 2;
move();
}
else if(input == 's')
{
direct = 3;
move();
}
else if(input == 'a')
{
direct = 4;
move();
}
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
五、终极版
要让蛇每吃一个食物长度就增加一个单位
我是电脑玩家——贪吃蛇C语言实现
详细代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define high 30
#define width 36
int direct;
int canvas[high][width] = {0}; //0为空格,-1为边框,1为蛇头&,大于1的为蛇身*
int food_x, food_y; //食物位置
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);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void move()
{
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]++;
}
}
int tail_i, tail_j, head_i, head_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])
{
max = canvas[i][j];
tail_i = i;
tail_j = j;
}
if(canvas[i][j] == 2)
{
head_i = i;
head_j = j;
}
}
}
}
int new_i, new_j; //新头
if(direct == 1) //向上移动
{
new_i = head_i - 1;
new_j = head_j;
}
if(direct == 2) //向右移动
{
new_i = head_i;
new_j = head_j + 1;
}
if(direct == 3) //向下移动
{
new_i = head_i + 1;
new_j = head_j;
}
if(direct == 4) //向左移动
{
new_i = head_i;
new_j = head_j - 1;
}
if(canvas[new_i][new_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[tail_i][tail_j] = 0;
}
if(canvas[new_i][new_j] > 0 || canvas[new_i][new_j] == -1)
{ //判断小蛇是否咬到自己或者撞墙
printf("\nGame Over!\n");
exit(0);
}
else
canvas[new_i][new_j] = 1;
}
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 / 2][width / 2] = 1;
for(i = 1; i <= 4; i++)
canvas[high / 2][width / 2 - i] = i + 1;
direct = 2; //先默认向右移
food_x = rand() % (high - 5) + 2;
food_y = rand() % (width - 5) + 2;
canvas[food_x][food_y] = -2;
}
void show()
{
gotoxy(0, 0);
HideCursor();
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)
printf("&");
else if(canvas[i][j] > 1)
printf("*");
else if(canvas[i][j] == -2)
printf("$");
}
printf("\n");
}
}
void updateWithoutInput()
{
// move();
}
void updateWithInput()
{
char input;
if(kbhit())
{
input = getch();
if(input == 'w')
{
direct = 1;
move();
}
else if(input == 'd')
{
direct = 2;
move();
}
else if(input == 's')
{
direct = 3;
move();
}
else if(input == 'a')
{
direct = 4;
move();
}
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}