Version:1.0StartHTML:000000203EndHTML:000015376StartFragment:000000615EndFragment:000015320StartSelection:000000615EndSelection:000015316SourceURL:http://kuaibao.qq.com/s/20180709A0QCVE00
導(dǎo)語(yǔ)
今天給大家分享一個(gè)簡(jiǎn)易的貪吃蛇小游戲源碼坑夯,源碼來(lái)自百度貼吧,在此表示感謝。貪吃蛇游戲的核心是二維數(shù)組的變換。
1需要的條件:一臺(tái)電腦 行楞、編譯器(如VC++6.0)、C語(yǔ)言基礎(chǔ)
編譯器:常用的有VC++6.0土匀,Visual Studio 2012子房,Code#Block等等
先給電腦安裝好編譯器,并基本熟練怎么如何使用(當(dāng)然有C語(yǔ)言基礎(chǔ)的可以省略此步驟)恒削,小編采用的是VS2012(以下操作皆在上面運(yùn)行)
請(qǐng)輸入圖片描述2新建項(xiàng)目:
打開編譯器新建控制臺(tái)項(xiàng)目,并添加源文件
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("食物坐標(biāo) %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("蛇頭坐標(biāo) %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("關(guān)");
}
gotoxy(55, 12);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("分?jǐn)?shù)");
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);
}
溫馨提示:編寫程序時(shí)建議模塊化編程尾序,即將不同部分的內(nèi)容分開編寫钓丰,且需注意代碼風(fēng)格,這樣可提高可讀性每币,同時(shí)在程序編譯出錯(cuò)的時(shí)候也容易檢查錯(cuò)誤携丁。特別是像比較大的程序,動(dòng)不動(dòng)就幾萬(wàn)行代碼甚至更多,良好的編程習(xí)慣應(yīng)該從平時(shí)就要養(yǎng)成梦鉴。
4編譯運(yùn)行:
點(diǎn)擊生成李茫,運(yùn)行即可實(shí)現(xiàn)控制臺(tái)游戲,如圖所示:
接下來(lái)就可以歡快地玩著自己編寫的貪吃蛇啦~是不是有一股莫名的沖動(dòng)呢
想學(xué)習(xí)C語(yǔ)言/C++快來(lái)加入咱們的大家庭吧:六六六二九五四九八 邀請(qǐng)編碼【凌云】? ? 群里有很多學(xué)習(xí)資料哦 肥橙,還有老司機(jī)解答問(wèn)題