如何用C語(yǔ)言編寫控制臺(tái)小游戲星压?

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)題

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末魄宏,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子存筏,更是在濱河造成了極大的恐慌宠互,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,214評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件椭坚,死亡現(xiàn)場(chǎng)離奇詭異予跌,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)善茎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門券册,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人垂涯,你說(shuō)我怎么就攤上這事烁焙。” “怎么了集币?”我有些...
    開封第一講書人閱讀 152,543評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵考阱,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我鞠苟,道長(zhǎng)乞榨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,221評(píng)論 1 279
  • 正文 為了忘掉前任当娱,我火速辦了婚禮吃既,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘跨细。我一直安慰自己鹦倚,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,224評(píng)論 5 371
  • 文/花漫 我一把揭開白布冀惭。 她就那樣靜靜地躺著震叙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪散休。 梳的紋絲不亂的頭發(fā)上媒楼,一...
    開封第一講書人閱讀 49,007評(píng)論 1 284
  • 那天,我揣著相機(jī)與錄音戚丸,去河邊找鬼划址。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的夺颤。 我是一名探鬼主播痢缎,決...
    沈念sama閱讀 38,313評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼世澜!你這毒婦竟也來(lái)了独旷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,956評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤宜狐,失蹤者是張志新(化名)和其女友劉穎势告,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體抚恒,經(jīng)...
    沈念sama閱讀 43,441評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡咱台,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,925評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了俭驮。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片回溺。...
    茶點(diǎn)故事閱讀 38,018評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖混萝,靈堂內(nèi)的尸體忽然破棺而出遗遵,到底是詐尸還是另有隱情,我是刑警寧澤逸嘀,帶...
    沈念sama閱讀 33,685評(píng)論 4 322
  • 正文 年R本政府宣布车要,位于F島的核電站,受9級(jí)特大地震影響崭倘,放射性物質(zhì)發(fā)生泄漏翼岁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,234評(píng)論 3 307
  • 文/蒙蒙 一司光、第九天 我趴在偏房一處隱蔽的房頂上張望琅坡。 院中可真熱鬧,春花似錦残家、人聲如沸榆俺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,240評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)茴晋。三九已至,卻和暖如春回窘,著一層夾襖步出監(jiān)牢的瞬間诺擅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,464評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工毫玖, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留掀虎,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,467評(píng)論 2 352
  • 正文 我出身青樓付枫,卻偏偏與公主長(zhǎng)得像烹玉,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子阐滩,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,762評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容