1. 程式人生 > >貪吃蛇(windows C語言)

貪吃蛇(windows C語言)

#include <stdio.h>
#include <malloc.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <unistd.h>
#include <pthread.h>
#include<conio.h>
#include<process.h>
//蛇
typedef struct
{
    int x;
    int y;
    struct SNAKE *pNext;
} SNAKE;
//蘋果
typedef struct
{
    int x;
    int y;
} Point;
//邊框
typedef struct
{
    int w;
    int h;
} Border;
char key;
Border b;
Point p;
SNAKE *h;//頭
SNAKE *end;
Point e;//尾
char way;
//定位
void gotoxy(int x, int y)
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 獲取標準輸出裝置控制代碼
    SetConsoleCursorPosition(hOut, pos);//兩個引數分別是指定哪個窗體,具體位置
}
//隱藏游標
void HideCursor()
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut,&cci);//獲取游標資訊
    cci.bVisible = FALSE;//隱藏游標
    SetConsoleCursorInfo(hOut,&cci);//設定游標資訊
}
//顯示邊框
void showborder()
{
    //邊框
    for (int i = 0; i < b.h; i++)
    {
        for (int n = 0; n < b.w; n++)
        {
            if (i == 0 || i == b.h - 1 || n == 0 || n == b.w - 1)
                printf(".");
            else
                printf(" ");
        }
        printf("\n");
    }
}
//顯示蘋果
void showpoint()
{
    srand((int)time(0));
    p.x=rand()%(b.w-2)+2;
    p.y=rand()%(b.h-2)+2;
    gotoxy(p.x,p.y);
    printf(".");
}
void printmove()
{
    SNAKE *temp=h;
    //吃蘋果
    if(temp->x==p.x&&temp->y==p.y)
    {
        SNAKE *temp2=(SNAKE *) malloc(sizeof(SNAKE));
        temp2->x=e.x;
        temp2->y=e.y;
        temp2->pNext=NULL;
        end->pNext=temp2;
        end=temp2;
        showpoint();
    }
    else
    {
        gotoxy(e.x,e.y);
        printf(" ");
    }
    do
    {
        //撞牆
        if(temp->x==0||temp->x==b.w||temp->y==0||temp->y==b.h)
            _endthreadex( 0 );
        gotoxy(temp->x,temp->y);
        printf(".");
    }
    while(temp=temp->pNext);
}
// 蛇移動
unsigned __stdcall SnakThreadFunc( void* pArguments )
{
    while(1)
    {
        SNAKE *temp=h;
        SNAKE *temp2=temp->pNext;
        Point tp1,tp2;
        e.x=end->x;
        e.y=end->y;
        tp1.x=temp->x;
        tp1.y=temp->y;
        switch(way)
        {
        case 'U':
        {
            if(temp->y-1<temp2->y)
                temp->y-=1;
            else
                temp->y+=1;
            break;
        }

        case 'D':
        {
            if(temp->y+1>temp2->y)
                temp->y+=1;
            else
                temp->y-=1;
            break;
        }

        case 'L':
        {
            if(temp->x-1<temp2->x)
                temp->x-=1;
            else
                temp->x+=1;
            break;
        }
        case 'R':
        {
            if(temp->x+1>temp2->x)
                temp->x+=1;
            else
                temp->x-=1;
            break;
        }
        }
        while(temp=temp->pNext)
        {
            tp2.x=temp->x;
            tp2.y=temp->y;
            temp->x=tp1.x;
            temp->y=tp1.y;
            tp1.x=tp2.x;
            tp1.y=tp2.y;
        }
        Sleep(1000);
        printmove();
    }
    return 0;
}
//初始化
void init()
{
    HideCursor();
    way='R';
    //遊戲寬高
    b.h = 20, b.w = 60;
    h=(SNAKE *) malloc(sizeof(SNAKE));
    h->x=b.w/2;
    h->y=b.h/2;
    SNAKE *temp=(SNAKE *) malloc(sizeof(SNAKE));
    temp->x=h->x-1;
    temp->y=h->y;
    temp->pNext=NULL;
    h->pNext=temp;
    end=temp;
}
int main(int argc, char const *argv[])
{
    /* code */
    init();
    showborder();
    showpoint();

    HANDLE hThread;
    unsigned threadID;
    // Create the second thread.
    hThread = (HANDLE)_beginthreadex( NULL, 0, &SnakThreadFunc, NULL, 0, &threadID );
    // Wait until second thread terminates. If you comment out the line
    // below, Counter will not be correct because the thread has not
    // terminated, and Counter most likely has not been incremented to
    // 1000000 yet.
    WaitForSingleObject( hThread, 0 );
    // Destroy the thread object.
    CloseHandle( hThread );
    while(key=getch())
    {
        switch(key)
        {
        case (int)'w':
            way='U';
            break;
        case (int)'s':
            way='D';
            break;
        case (int)'a':
            way='L';
            break;
        case (int)'d':
            way='R';
            break;
        }
    }
    return 0;
}