欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言制作貪吃蛇小游戲

 更新時間:2022年07月25日 16:45:30   作者:無咎.lsy  
這篇文章主要為大家詳細介紹了C語言制作貪吃蛇小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C語言制作貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下

直接上代碼

?#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
?
//MAXWIDTH、MAXHEIGHT、INITLEN 以字符記
#define MAXWIDTH (30)
#define MAXHEIGHT MAXWIDTH
#define INITLEN (3) ?//貪吃蛇的初始長度?
?
//程序中用到的各種字符,以及它們的顏色和類型(以數(shù)字表示)
struct{
? ? char *ch;
? ? int color;
? ? char type;
}
charBorder = {"□", 4, 1}, ?//邊框
charBg = {"■", 2, 2}, ?//背景
charSnake = {"★", 0xe, 3}, ?//貪吃蛇節(jié)點
charFood = {"●", 0xc, 4}; ?//食物
?
//用一個結(jié)構(gòu)體數(shù)組保存地圖中的各個點
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; ?//蛇頭蛇尾對應(yīng)的snakeMap中的索引(下標)
HANDLE hStdin; ?//控制臺句柄
?
// 設(shè)置光標位置,x為行,y為列
void setPosition(int x, int y){
? ? COORD coord;
? ? coord.X = 2*y;
? ? coord.Y = x;
? ? SetConsoleCursorPosition(hStdin, coord);
}
?
// 設(shè)置顏色
void setColor(int color){
? ? SetConsoleTextAttribute(hStdin, color);
}
?
//創(chuàng)建食物
void createFood(){
? ? int index, rang, x, y;
? ? //產(chǎn)生隨機數(shù),確定 snakeMap 數(shù)組的索引?
? ? 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);
?
? ? printf("You die! Game Over!");
? ? getch();
? ? exit(0);
}
?
// 蛇移動
void move(char direction){
? ? int newHeaderX, newHeaderY; ?//新蛇頭的坐標
? ? int newHeaderPreIndex; ?//新蛇頭坐標以前對應(yīng)的索引
? ? int newHeaderPreX, newHeaderPreY; ?//新蛇頭的索引以前對應(yīng)的坐標
? ? 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;
? ? // -----------------------------------------------
? ? //新蛇頭坐標以前對應(yīng)的索引
? ? newHeaderPreIndex = globalMap[newHeaderX][newHeaderY].index;
? ? //新蛇頭的索引以前對應(yīng)的坐標
? ? newHeaderPreX = snakeMap[headerIndex].x;
? ? newHeaderPreY = snakeMap[headerIndex].y;
?
? ? //雙向綁定新蛇頭索引與坐標的對應(yīng)關(guān)系
? ? snakeMap[headerIndex].x = newHeaderX;
? ? snakeMap[headerIndex].y = newHeaderY;
? ? globalMap[newHeaderX][newHeaderY].index = headerIndex;
?
? ? //雙向綁定以前的索引與坐標的對應(yīng)關(guān)系
? ? snakeMap[newHeaderPreIndex].x = newHeaderPreX;
? ? snakeMap[newHeaderPreIndex].y = newHeaderPreY;
? ? globalMap[newHeaderPreX][newHeaderPreY].index = newHeaderPreIndex;
?
? ? //新蛇頭以前的類型
? ? newHeaderPreType = globalMap[newHeaderX][newHeaderY].type;
? ? //設(shè)置新蛇頭類型
? ? 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();
? ? ? ? //更改分數(shù)
? ? ? ? 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(){
? ? // 設(shè)置相關(guān)變量?
? ? int x, y, i, 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; ?//控制臺光標信息
?
? ? //判斷相關(guān)設(shè)置是否合理
? ? if(MAXWIDTH<16){
? ? ? ? printf("'MAXWIDTH' is too small!");
? ? ? ? getch();
? ? ? ? exit(0);
? ? }
?
? ? //設(shè)置窗口大小?
? ? 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();
?
? ? //設(shè)置程序信息
? ? setPosition(xCenter-1, MAXWIDTH+2);
? ? printf(" ? Apples : 0");
? ? setPosition(xCenter, MAXWIDTH+2);
? ? printf(" ? Author : xiao p");
? ? setPosition(xCenter+1, MAXWIDTH+2);
? ? printf("Copyright : c.biancheng.net");
? ? 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;
}

【操作過程】

編譯運行后,先按enter鍵使游戲開始,然后w,s a d 分別控制上下左右移動

【運行展示】

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言函數(shù)指針詳解

    C語言函數(shù)指針詳解

    本文主要介紹 C語言函數(shù)指針的知識,這里整理了詳細的資料及示例代碼以便大家學習參考,有需要學習此部分知識的朋友可以參考下
    2021-09-09
  • C語言 詳細講解邏輯運算符的使用

    C語言 詳細講解邏輯運算符的使用

    在C語言中,邏輯運算符有&&、||、!;&&表示“與”的意思,需要兩端的表達式的值都為true,該式的值才為true。||表示“或”的意思,兩端的表達式的值只要有一端為true,該式的值就為true。!表示“非”的意思,將該式的真值換成相反的真值,即false和true互換
    2022-04-04
  • VSCode斷點調(diào)試CMake工程項目的實現(xiàn)步驟

    VSCode斷點調(diào)試CMake工程項目的實現(xiàn)步驟

    這篇文章主要介紹了VSCode斷點調(diào)試CMake工程項目的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • C/C++指針介紹與使用詳解

    C/C++指針介紹與使用詳解

    不知從何時起對你一眼萬年,從此,每一天被賦予了特別的意義。時隔多年,依然揮之不去是你------指針?。。”酒袔缀鯏?shù)據(jù)類型只用了int,但是float、double等也是可以的
    2022-08-08
  • C語言結(jié)構(gòu)體版學生成績管理系統(tǒng)

    C語言結(jié)構(gòu)體版學生成績管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言結(jié)構(gòu)體版的學生成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C++中set/multiset容器詳解(附測試用例與結(jié)果圖)

    C++中set/multiset容器詳解(附測試用例與結(jié)果圖)

    set/multiset屬于關(guān)聯(lián)式容器,底層結(jié)構(gòu)是用二叉樹實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于C++中set/multiset容器的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • C++ OpenCV單峰三角閾值法Thresh_Unimodal詳解

    C++ OpenCV單峰三角閾值法Thresh_Unimodal詳解

    本文主要介紹了適合當圖像的直方圖具有明顯單峰特征時使用,結(jié)合了三角法的原理而設(shè)計的圖像分割方法,感興趣的小伙伴可以了解一下
    2021-12-12
  • C/C++的全緩沖、行緩沖和無緩沖

    C/C++的全緩沖、行緩沖和無緩沖

    這篇文章主要介紹了C/C++的全緩沖、行緩沖和無緩沖的相關(guān)知識,幫助大家更好的理解和學習c/c++,感興趣的朋友可以了解下
    2020-08-08
  • C語言實現(xiàn)數(shù)獨輔助器(附源碼)

    C語言實現(xiàn)數(shù)獨輔助器(附源碼)

    數(shù)獨是源自瑞士的一種數(shù)學游戲。是一種運用紙、筆進行演算的邏輯游戲。本文將利用C語言制作一個數(shù)獨輔助器,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-01-01
  • 有關(guān)C++頭文件的包含順序研究

    有關(guān)C++頭文件的包含順序研究

    下面小編就為大家?guī)硪黄嘘P(guān)C++頭文件的包含順序研究。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論