零基礎學編程相關知識:簡單的c語言程序代碼大全

序言

很多學習了C語言的小夥伴,雖然說學完了C語言入門,但是實際能力還是停留在一個很低的水平,基本上就是套幾個for循環,暴力解一下排列組合問題的水平,很多人基本上不能夠獨立寫一個小程序,今天就給大家我很久以前做的一個簡單的貪吃蛇案例。

這次的編寫控制台貪吃蛇程序對學完C語言沒做過項目的小夥伴來說可能是一個不小的挑戰。

C/C++編程入門基礎項目:C語言控制台版本,貪吃蛇源代碼示例

本文的貪吃蛇案例用的東西也並不是很多,遊戲的實現主要是對一個二維數組按一定邏輯進行修改、變換(實際操作時,為了減少閃爍,我用的是字符串)。這裡不對編寫過程進行贅述,主要說一下最基本功能的邏輯、和一些之前較少用的函數等。

一、 基本功能邏輯

1、遊戲的背景、打印

定義一個二維字符串,用“”和空格表示邊界、蛇身、空白等。打印是用for循環遍歷整個字符串,並以一定頻率刷新,就可以達到遊戲效果。

2、建立蛇數組

考慮到沒用鏈表做過東西,不太熟練,我採用了數組來做蛇。數組主要有容量有限,最長長度需要先定義(只要我定的足夠長hhhh),以及很多地方需要取地址(N次打掉了”&“)等缺點。數組存儲蛇的節數、XY坐標、移動方向等參數。主要需要注意“”佔兩個字節,在寫坐標時很多地方要乘二。

3、生成蛇的隨機坐標

首先種隨機種子,採用系統時間做種子。定義x、y兩個變量作為坐標值,用rand()函數搭配取余來獲得想要的坐標值範圍。然後初始生成兩三節就可以了。

4、把蛇畫到地圖上

建立for循環遍歷整條蛇,利用strncpy()函數將空白部分複製為“”就行了。

5、蛇的運動

這裡卡了比較久,期間去玩了玩貪吃蛇,發現蛇的運動方式不是很複雜,可以說就是蛇尾去一個,蛇頭加一個。我採用了整個蛇身向前移,蛇頭單獨處理的方法,這樣也便於以後控制方向。

6、擦除運動軌跡

寫到上一步運行會發現蛇越來越長。。。。就像死機了以後的鼠標光標一樣。。。。是因為雖然前一節點的屬性賦給了後一個節點,但是這個節點並沒有變。所以在每次運動前把之前的蛇擦掉,方法同第四步,只是把“”換成兩個空格。

7、蛇改變方向

由於蛇運動方式的特殊性,只需要對蛇頭處理。用GetAsyncKeyState()函數讀取鍵盤輸入,並需要注意通過附加條件防止蛇掉頭。

8、生成食物

隨機坐標、複製、打印。

9、蛇吃食物長長

蛇運動到食物的地方會把食物覆蓋掉,所以吃掉食物的效果不用寫。只用判斷蛇頭坐標和食物坐標重合,然後判斷運動方向來確定在哪裡加一節就行了。然後用一個布爾值判斷場上是否還有食物,來生成新的食物。計分也可以在此處寫。C/C++編程入門基礎項目:C語言控制台版本,貪吃蛇源代碼示例

網絡效果圖

代碼如下:

#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include <stdlib.h>#include <math.h>#include <conio.h>#include <time.h>#include <windows.h>#define MAXWIDTH 30#define MAXHEIGHT 30#define INITLEN 3 //貪吃蛇的初始長度 struct{

char *ch; int color; char type;

}

charBorder = { “”, 4, 1 }, //邊框charBg = { “”, 2, 2 }, //背景charSnake = { “”, 0xe, 3 }, //貪吃蛇節點charFood = { “”, 0xc, 4 }; //食物//用一個結構體數組保存地圖中的各個點struct{

char type; int index;

}globalMap[MAXWIDTH][MAXHEIGHT];struct{

int x; int y;

} snakeMap[(MAXWIDTH – 2)*(MAXHEIGHT – 2)], scoresPostion;int scores = 0; //得分int snakeMapLen = (MAXWIDTH – 2)*(MAXHEIGHT – 2);int headerIndex, tailIndex;

HANDLE hStdin;

// 設置光標位置,x為行,y為列void setPosition(int x, int y){

COORD coord;

coord.X = 2 * y;

coord.Y = x;

SetConsoleCursorPosition(hStdin, coord);

}// 設置顏色void setColor(int color){

SetConsoleTextAttribute(hStdin, color);

}//創建食物void createFood(){ int index, rang, x, y;

srand((unsigned)time(NULL)); if (tailIndex<headerIndex){

rang = headerIndex – tailIndex – 1;

index = rand() % rang + tailIndex + 1;

} else{

rang = snakeMapLen – (tailIndex – headerIndex + 1);

index = rand() % rang; if (index >= headerIndex){

index += (tailIndex – headerIndex + 1);

}

}

x = snakeMap[index].x;

y = snakeMap[index].y;

setPosition(x, y);

setColor(charFood.color); printf(“%s”, charFood.ch);

globalMap[x][y].type = charFood.type;

}//死了void die(){ int xCenter = MAXHEIGHT % 2 == 0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter = MAXWIDTH % 2 == 0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;

setPosition(xCenter, yCenter – 5);

setColor(0xC); exit(1);

_getch(); exit(0);

}// 蛇移動void move(char direction){ int newHeaderX, newHeaderY; //新蛇頭的坐標

int newHeaderPreIndex; //新蛇頭坐標以前對應的索引

int newHeaderPreX, newHeaderPreY; //新蛇頭的索引以前對應的坐標

int newHeaderPreType; //新蛇頭以前的類型

int oldTailX, oldTailY; //老蛇尾坐標

switch (direction){ case ‘w’:

newHeaderX = snakeMap[headerIndex].x – 1;

newHeaderY = snakeMap[headerIndex].y; break; case ‘s’:

newHeaderX = snakeMap[headerIndex].x + 1;

newHeaderY = snakeMap[headerIndex].y; break; case ‘a’:

newHeaderX = snakeMap[headerIndex].x;

newHeaderY = snakeMap[headerIndex].y – 1; break; case ‘d’:

newHeaderX = snakeMap[headerIndex].x;

newHeaderY = snakeMap[headerIndex].y + 1; break;

}

headerIndex = headerIndex == 0 ? snakeMapLen – 1 : headerIndex – 1;

newHeaderPreIndex = globalMap[newHeaderX][newHeaderY].index;

newHeaderPreX = snakeMap[headerIndex].x;

newHeaderPreY = snakeMap[headerIndex].y;

snakeMap[headerIndex].x = newHeaderX;

snakeMap[headerIndex].y = newHeaderY;

globalMap[newHeaderX][newHeaderY].index = headerIndex;

snakeMap[newHeaderPreIndex].x = newHeaderPreX;

snakeMap[newHeaderPreIndex].y = newHeaderPreY;

globalMap[newHeaderPreX][newHeaderPreY].index = newHeaderPreIndex; //新蛇頭以前的類型

newHeaderPreType = globalMap[newHeaderX][newHeaderY].type; //設置新蛇頭類型

globalMap[newHeaderX][newHeaderY].type = charSnake.type; // 判斷是否出界或撞到自己

if (newHeaderPreType == charBorder.type || newHeaderPreType == charSnake.type){

die();

} //輸出新蛇頭

setPosition(newHeaderX, newHeaderY);

setColor(charSnake.color); printf(“%s”, charSnake.ch); //判斷是否吃到食物

if (newHeaderPreType == charFood.type){ //吃到食物

createFood(); //更改分數

setPosition(scoresPostion.x, scoresPostion.y); printf(“%d”, ++scores);

} else{ //老蛇尾坐標

oldTailX = snakeMap[tailIndex].x;

oldTailY = snakeMap[tailIndex].y; //刪除蛇尾

setPosition(oldTailX, oldTailY);

setColor(charBg.color); printf(“%s”, charBg.ch);

globalMap[oldTailX][oldTailY].type = charBg.type;

tailIndex = (tailIndex == 0) ? snakeMapLen – 1 : tailIndex – 1;

}

}//下次移動的方向char nextDirection(char ch, char directionOld){ int sum = ch + directionOld;

ch = tolower(ch); if ((ch == ‘w’ || ch == ‘a’ || ch == ‘s’ || ch == ‘d’) && sum != 197 && sum != 234){ return ch;

} else{ return directionOld;

}

}//暫停char pause(){ return _getch();

}// 初始化void init(){ // 設置相關變量

int x, y, index; int xCenter = MAXHEIGHT % 2 == 0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter = MAXWIDTH % 2 == 0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;

CONSOLE_CURSOR_INFO cci; //控制台光標信息

//判斷相關設置是否合理

if (MAXWIDTH<16){ printf(“‘MAXWIDTH’ is too small!”);

_getch(); exit(0);

} //設置窗口大小

system(“mode con: cols=96 lines=32”); //隱藏光標

hStdin = GetStdHandle(STD_OUTPUT_HANDLE);

GetConsoleCursorInfo(hStdin, &cci);

cci.bVisible = 0;

SetConsoleCursorInfo(hStdin, &cci); //打印背景

for (x = 0; x<MAXHEIGHT; x++){ for (y = 0; y<MAXWIDTH; y++){ if (y == 0 || y == MAXWIDTH – 1 || x == 0 || x == MAXHEIGHT – 1){

globalMap[x][y].type = charBorder.type;

setColor(charBorder.color); printf(“%s”, charBorder.ch);

} else{

index = (x – 1)*(MAXWIDTH – 2) + (y – 1);

snakeMap[index].x = x;

snakeMap[index].y = y;

globalMap[x][y].type = charBg.type;

globalMap[x][y].index = index;

setColor(charBg.color); printf(“%s”, charBg.ch);

}

} printf(“n”);

} //初始化貪吃蛇

globalMap[xCenter][yCenter – 1].type = globalMap[xCenter][yCenter].type = globalMap[xCenter][yCenter + 1].type = charSnake.type;

headerIndex = (xCenter – 1)*(MAXWIDTH – 2) + (yCenter – 1) – 1;

tailIndex = headerIndex + 2;

setPosition(xCenter, yCenter – 1);

setColor(charSnake.color); for (y = yCenter – 1; y <= yCenter + 1; y++){ printf(“%s”, charSnake.ch);

} //生成食物

createFood(); //設置程序信息

setPosition(xCenter – 1, MAXWIDTH + 2); printf(” 得分 : 0″);

setPosition(xCenter, MAXWIDTH + 2); printf(” 姓名班級 :33班楊超”);

scoresPostion.x = xCenter – 1;

scoresPostion.y = MAXWIDTH + 8;

}int main(){ char charInput, direction = ‘a’;

init();

charInput = tolower(_getch());

direction = nextDirection(charInput, direction); while (1){ if (_kbhit()){

charInput = tolower(_getch()); if (charInput == ‘ ‘){

charInput = pause();

}

direction = nextDirection(charInput, direction);

}

move(direction);

Sleep(500);

}

_getch(); return 0;

}

希望對大家有幫助~

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/226565.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 14:51
下一篇 2024-12-09 14:51

相關推薦

發表回復

登錄後才能評論