基于控制臺(tái)的C語(yǔ)言貪吃蛇項(xiàng)目

相信對(duì)很多人來(lái)說(shuō),學(xué)完C語(yǔ)言以后,都會(huì)找一些小程序來(lái)練練手。例如貪吃蛇、五子棋、俄羅斯方塊等等。

今天給大家分享一個(gè)基于控制臺(tái)的C語(yǔ)言貪吃蛇小程序。


基礎(chǔ)知識(shí)要求:C語(yǔ)言基礎(chǔ)。

知識(shí)點(diǎn)補(bǔ)充

這里寫一些你可能沒學(xué)到的知識(shí)點(diǎn)
  • system("cls"):清屏函數(shù)
  • Sleep(300):等待函數(shù),其中括號(hào)內(nèi)的為毫秒。
  • _getch():獲取一個(gè)鍵盤字符,不顯示該字符就返回
  • kbhit():判斷是否有鍵按下,是返回1,否返回0.

代碼部分

  • 廢話不多說(shuō),直接貼代碼
#include <conio.h>
#include <stdio.h>
#include <malloc.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
struct node
{
    int x;
    int y;
    struct node *next;
};

node *init(int length);                 //初始化蛇
void print(int a[15][30],node *head);   //輸出函數(shù)
void food(node *head);                  //食物產(chǎn)生函數(shù)
void move(node *head);                  //移動(dòng)函數(shù)
bool dead(node *head);                  //判斷死亡函數(shù)

int food_x=0,food_y=0;
int food_flag = 0;
int target = 2;


void main(){
    int a[15][30];
    node *head;
    head = init(2);
    while(1)
    {   
        system("cls");
        move(head);
        
        if(dead(head))
            break;
        print(a,head);
        Sleep(300);
    }
    system("cls");
    printf("Game Over!!!\n");
    
}
node *init(int length)
{
    int i = 2;
    node *head;
    head = (node*)malloc(sizeof(node));//分配地址
    node *p = head;
    head->x = 1;
    head->y = 1;
    while(i <= length)
    {
        node *s;
        s = (node*)malloc(sizeof(node));
        s->x = 1;
        s->y = i;
        s->next = NULL;
        p->next = s;
        p = p->next;
        i++;
    }
    food(head);
    return head;
}

void print(int a[15][30],node *head)
{
//0為空格,-1為墻壁#,1為蛇身*,2為蛇頭@,3為食物$
    int i,j;
    node *p = head;
    for(i = 0; i < 15; i++)
    {
        for(j = 0; j < 30; j++)
        {
            if(i == 0 || j == 0 || i == 14 || j == 29)
                a[i][j] = -1;
            else
                a[i][j] = 0;
        }
    }
    while(p != NULL)
    {
        if(p->next == NULL)
            a[p->x][p->y] = 2;
        else
            a[p->x][p->y] = 1;
        p = p->next;
    }
    a[food_x][food_y] = 3;
    for(i = 0; i < 15; i++)
    {
        for(j = 0; j < 30; j++)
        {
            if(a[i][j] == -1)
                printf("#");
            if(a[i][j] == 0)
                printf(" ");
            if(a[i][j] == 1)
                printf("*");
            if(a[i][j] == 2)
                printf("@");
            if(a[i][j] == 3)
                printf("$");
        }
        printf("\n");
    }   
}

void food(node *head)
{
    node *p = head;
    srand((unsigned)time(NULL));
    food_x = rand()%12 + 1;
    food_y = rand()%27 + 1;
    while(p != NULL)
    {
        if(p->x == food_x && p->y == food_y)
        {
            food_x = rand()%12 + 1;
            food_y = rand()%27 + 1;
            p = head;
        }
        p = p->next;
    }
}

void move(node *head)
{

    char ch;
    if(kbhit())
    {
        ch = _getch();      //VS中g(shù)etch()用_getch()代替。
        switch (ch)
        {
            case -32:        //上下左右占兩個(gè)字節(jié),低八位存ASCII碼,高八位存按鍵掃描碼
            ch = _getch(); //其中-32為我的電腦的按鍵掃描碼
            switch (ch)
            {
                case 72:if(target != 1)target = 0; break;       //72為上的ASCII碼
                case 80:if(target != 0)target = 1; break;       //80為下的ASCII碼
                case 77:if(target != 3)target = 2; break;       //77為右的ASCII碼
                case 75:if(target != 2)target = 3; break;       //75位左的ASCII碼
                default:break;
            }
        }
    }
    node *p;
    node *q = head->next;
    while(q->next != NULL)
        q = q->next;
    switch(target)
    {
        case 0:if(q->x - 1 == food_x && q->y == food_y)food_flag = 1;break;
        case 1:if(q->x + 1 == food_x && q->y == food_y)food_flag = 1;break;
        case 2:if(q->x == food_x && q->y + 1 == food_y)food_flag = 1;break;
        case 3:if(q->x == food_x && q->y - 1 == food_y)food_flag = 1;break;
    }
    if(food_flag == 1)
    {
        node *s;
        s = (node*)malloc(sizeof(node));
        s->x = food_x;
        s->y = food_y;
        s->next = NULL;
        q->next = s;
        food(head);
        food_flag = 0;
    }
    else
    {
        p = head;
        q = head->next;
        while(q != NULL)
        {
            p->x = q->x;
            p->y = q->y;
            if(q->next == NULL)
            {
                switch(target)
                {
                    case 0:q->x = q->x - 1;break;
                    case 1:q->x = q->x + 1;break;
                    case 2:q->y = q->y + 1;break;
                    case 3:q->y = q->y - 1;break;
                }
            }
            p = p->next;
            q = q->next;
            
        }
    }   
}
bool dead(node *head)
{
    node *p = head;
    while(p->next != NULL)
        p = p->next;
    if(p->x >= 14 || p->x <= 0 || p->y >= 29 || p->y <= 0)
        return true;
    node *q = head;
    while(q->next != NULL)
    {
        if(q->x == p->x && q->y == p->y)
            return true;
        q = q->next;
    }
    return false;
}

以上就是全部?jī)?nèi)容。

下次再見!?。?/h3>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容