Version:1.0StartHTML:000000203EndHTML:000015376StartFragment:000000615EndFragment:000015320StartSelection:000000615EndSelection:000015316SourceURL:http://kuaibao.qq.com/s/20180709A0QCVE00
导语
今天给大家分享一个简易的贪吃蛇小游戏源码,源码来自百度贴吧,在此表示感谢。贪吃蛇游戏的核心是二维数组的变换。
1需要的条件:一台电脑 、编译器(如VC++6.0)、C语言基础
编译器:常用的有VC++6.0,Visual Studio 2012,Code#Block等等
先给电脑安装好编译器,并基本熟练怎么如何使用(当然有C语言基础的可以省略此步骤),小编采用的是VS2012(以下操作皆在上面运行)
请输入图片描述2新建项目:
打开编译器新建控制台项目,并添加源文件
3源代码:
#include
#include
#include
#include
int map[48][35], xz[1090], yz[1090], zxz[1090], zyz[1090];
int x, y, m, n;
int FXA = 1, FXB = 1, FEN = 1, BS = 0;
int SD = 250, SY = 1, LL = 0;
int iii = 0;
void gotoxy(int x, int y);
void paint(int xx, int yy);
void wall();
void nengliang();
void food();
void chishi();
void sheshe();
void qinqin();
void zixing(int FX);
void move();
void over();
int main()
{
system("title 贪吃蛇");
system("mode con cols=80 lines=40");
srand((unsigned)time(NULL));
wall();
gotoxy(55, 20);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("z 加速");
gotoxy(55, 22);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("x 减速");
gotoxy(55, 24);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("L 声音");
x = rand() % 40 + 4; if (x % 2 != 0) x++;
y = rand() % 20 + 4;
food();
map[x][y] = 1;
paint(x, y);
move();
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void paint(int xx, int yy)
{
gotoxy(xx, yy);
switch (map[xx][yy])
{
case 1:printf(""); break;
case 2:printf(""); break;
case 3:printf(" "); break;
case 4:printf(""); break;
}
}
void nengliang()
{
int i;
if (iii>0)
{
for (i = 0; i
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
gotoxy(57 + i * 2, 5); printf("");
}
if (iii == 7)
{
LL += 3; iii = 0;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
Sleep(200); for (i = 1; i
Sleep(400); for (i = 1; i
}
}
}
void wall()
{
int i;
for (i = 0; i
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
map[i][0] = 4; paint(i, 0);
map[i][35] = 4; paint(i, 35);
}
for (i = 0; i
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
map[0][i] = 4; paint(0, i);
map[48][i] = 4; paint(48, i);
}
}
void food()
{
int i;
m = rand() % 40 + 4; if (m % 2 != 0) m++;
n = rand() % 20 + 4;
if (map[m][n] == 4)
{
for (i = 0; i
{
n++;
if (map[m][n] != 4)
break;
}
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(55, 16); printf("食物坐标 %d,%d", m, n);
if (m == x&&n == y) m += 2;
map[m][n] = 2;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
paint(m, n);
}
void chishi()
{
if (x == m&&y == n)
{
if (SY == 1)
putchar(7);
FEN++;
iii++;
nengliang();
food();
}
}
void sheshe()
{
int i;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(55, 18); printf("蛇头坐标 %d,%d", x, y);
xz[BS - 1] = x; yz[BS - 1] = y;
for (i = 1; i
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
map[xz[BS - 1 - i]][yz[BS - 1 - i]] = 4;
paint(xz[BS - 1 - i], yz[BS - 1 - i]);
}
}
void qinqin()
{
int i;
zxz[BS - 1] = x; zyz[BS - 1] = y;
for (i = 0; i
{
map[zxz[BS - 1 - i]][zyz[BS - 1 - i]] = 3;
paint(zxz[BS - 1 - i], zyz[BS - 1 - i]);
}
}
void zixing(int FX)
{
while (!kbhit())
{
nengliang();
switch (FX)
{
case 1:
Sleep(SD - LL * 30);
map[x][y] = 3; paint(x, y);
qinqin();
x += 2; BS++;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
map[x][y] = 1; paint(x, y);
sheshe();
chishi();
break;
case 2:
Sleep(SD - LL * 30);
map[x][y] = 3; paint(x, y);
qinqin();
x -= 2; BS++;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
map[x][y] = 1; paint(x, y);
sheshe();
chishi();
break;
case 3:
Sleep(SD - LL * 30);
map[x][y] = 3; paint(x, y);
qinqin();
y--; BS++;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
map[x][y] = 1; paint(x, y);
sheshe();
chishi();
break;
case 4:
Sleep(SD - LL * 30);
map[x][y] = 3; paint(x, y);
qinqin();
y++; BS++;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
map[x][y] = 1; paint(x, y);
sheshe();
chishi();
break;
}
if (map[x][y] == 4 || x == 48 || x == 0 || y == 35 || y == 0)
{
over(); break;
}
}
}
void move()
{
int i;
char ch;
while (1)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
gotoxy(62, 3); printf("能量槽");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
gotoxy(55, 4);
printf("╔═══════╗");
putchar('\n');
gotoxy(55, 5);
printf("║ ║");
putchar('\n');
gotoxy(55, 6);
printf("╚═══════╝");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
gotoxy(55, 8); printf("速度");
gotoxy(63, 8);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("%d", SD + LL * 30);
gotoxy(55, 10);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("声音");
if (SY == 1)
{
gotoxy(63, 10); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY); printf("开");
}
else
{
gotoxy(63, 10); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY); printf("关");
}
gotoxy(55, 12);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("分数");
gotoxy(63, 12);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("%d", FEN * 10 - 10);
zixing(FXB);
ch = getch();
switch (ch)
{
case 'd':FXA = 1; if (FXB == 2) FXA = 0; break;
case 'D':FXA = 1; if (FXB == 2) FXA = 0; break;
case 77:FXA = 1; if (FXB == 2) FXA = 0; break;
case 'a':FXA = 2; if (FXB == 1) FXA = 0; break;
case 'A':FXA = 2; if (FXB == 1) FXA = 0; break;
case 75:FXA = 2; if (FXB == 1) FXA = 0; break;
case 'w':FXA = 3; if (FXB == 4) FXA = 0; break;
case 'W':FXA = 3; if (FXB == 4) FXA = 0; break;
case 72:FXA = 3; if (FXB == 4) FXA = 0; break;
case 's':FXA = 4; if (FXB == 3) FXA = 0; break;
case 'S':FXA = 4; if (FXB == 3) FXA = 0; break;
case 80:FXA = 4; if (FXB == 3) FXA = 0; break;
case 'z':LL++; break;
case 'Z':LL++; break;
case 'x':LL--; break;
case 'X':LL--; break;
case 'l':SY *= -1; break;
case 'L':SY *= -1; break;
}
if (FXA) FXB = FXA;
}
}
void over()
{
system("cls");
Sleep(350);
gotoxy(32, 16);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
gotoxy(26, 12);
printf("╔═══════════╗");
putchar('\n');
gotoxy(26, 13);
printf("║ ║");
putchar('\n');
gotoxy(26, 14);
printf("║ ║");
putchar('\n');
gotoxy(26, 15);
printf("║ ║");
putchar('\n');
gotoxy(26, 16);
printf("╚═══════════╝");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(36, 14); printf("失败");
Sleep(400);
gotoxy(26, 28);
exit(0);
}
温馨提示:编写程序时建议模块化编程,即将不同部分的内容分开编写,且需注意代码风格,这样可提高可读性,同时在程序编译出错的时候也容易检查错误。特别是像比较大的程序,动不动就几万行代码甚至更多,良好的编程习惯应该从平时就要养成。
4编译运行:
点击生成,运行即可实现控制台游戏,如图所示:
接下来就可以欢快地玩着自己编写的贪吃蛇啦~是不是有一股莫名的冲动呢
想学习C语言/C++快来加入咱们的大家庭吧:六六六二九五四九八 邀请编码【凌云】 群里有很多学习资料哦 ,还有老司机解答问题