C语言编程小游戏分享之贪吃蛇,源码,亲手做才算收获

C语言编程小游戏分享之贪吃蛇,源码,亲手做才算收获_第1张图片

今天给大家分享一个简易的贪吃蛇小游戏,源码来小辰,在此表示感谢。贪吃蛇游戏的核心是二维数组的变换。

#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);

}

C语言编程小游戏分享之贪吃蛇,源码,亲手做才算收获_第2张图片
C语言编程小游戏分享之贪吃蛇,源码,亲手做才算收获_第3张图片

你可能感兴趣的:(C语言编程小游戏分享之贪吃蛇,源码,亲手做才算收获)