貪喫蛇 C/C++代碼

寫了一上午,哈哈


#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <time.h>
using namespace std;

struct node {
    int x,y;
    node *next;
};
struct snake {
    node *head;   // 蛇頭指針
    int  toward;  // 0向左 1向上 2向右 3向下
    int length;   //  長度
    int state;    //狀態 0表示正常 1完蛋
};
snake *play1;
char map[12][22];
int v;

void init();
void Insert(int x,int y);
void Deal(int x,int y,node *p);
int ifstatus(int x,int y,node *p);
void Running(int To);
int GameRunning();
void UpdataSnakeMap();
void Print();
void Romdo();
int welcome();

void init() {
    play1=new snake;
    play1->toward=2;
    play1->length=0;
    play1->state=0;
    play1->head=new node;
    node *temp,*tail=play1->head;
    for(int i=0; i<3; i++) {
        temp=new node;
        temp->x=6;
        temp->y=11-i;
        temp->next=NULL;
        tail->next=temp;
        tail=temp;
    }
    int k=0;
    strcpy(map[k++],"  貪 喫 蛇 大 作 戰  ");   //0-10
    strcpy(map[k++],"*********************");   //0-21
    strcpy(map[k++],"**                 **");
    strcpy(map[k++],"**                 **");   //2-18
    strcpy(map[k++],"**                 **");   //2-9
    strcpy(map[k++],"**                 **");
    strcpy(map[k++],"**                 **");
    strcpy(map[k++],"**                 **");
    strcpy(map[k++],"**                 **");
    strcpy(map[k++],"**                 **");
    strcpy(map[k++],"*********************");
    Romdo();
    printf("請輸入你要選擇的難度:(100-1000)\n");
    while(cin>>v) {
        if(v<=1000&v>=100) {
            v=1100-v;
            break;
        }
        printf("超出範圍,重新輸入\n");
    }
}
void Insert(int x,int y) {
    node *temp=new node;
    temp->x=x;
    temp->y=y;
    temp->next=play1->head->next;
    play1->head->next=temp;
}
void Deal(int x,int y,node *p,int t) {  // 0,2表示正常前進 1表示喫果加長
    if(!t||t==2) {
        node *p1=p->next;
        p->next=NULL;
        map[p1->x][p1->y]=' ';
        delete p1;
    } else {
        Romdo();
        play1->length++;
    }
    Insert(x,y);
}
int ifstatus(int x,int y,node *p) { //0表示正常 2表示咬到自己或撞牆 1表示喫果
    if(map[x][y]==' '||(map[x][y]=='o'&&x==p->x&&y==p->y))
        return 0;
    if(map[x][y]=='+')
        return 1;
    play1->state=1;
    return 0;
}
void Running() {
    int x,y;
    switch(play1->toward) {
    case 0:
        x=play1->head->next->x;
        y=play1->head->next->y-1;
        break;
    case 1:
        x=play1->head->next->x-1;
        y=play1->head->next->y;
        break;
    case 2:
        x=play1->head->next->x;
        y=play1->head->next->y+1;
        break;
    case 3:
        x=play1->head->next->x+1;
        y=play1->head->next->y;
        break;
    }
    node *p=play1->head->next;
    while(p->next->next)
        p=p->next;
    int status=ifstatus(x,y,p->next);
    Deal(x,y,p,status);
    UpdataSnakeMap();

}
void end() {
    strcpy(map[4],"**    遊戲結束!   **");
    strcpy(map[5],"**         分      **");
    strcpy(map[6],"**   按ESC回到首頁 **");
    strcpy(map[7],"** 按1重新開始遊戲 **");
    map[5][10]=play1->length%10+'0';
    if(play1->length>10){
       map[5][9]=(play1->length/10)%10+'0';
        if(play1->length>10)
         map[5][8]=(play1->length/10)%10+'0';
    }
    Print();
    Sleep(5000);
}
int GameRunning() {
    char ch;
    while(1) {
        Running();
        Print();
        Sleep(v);
        if(play1->state)
            break;
        if(kbhit()) {
            if((ch=getch())==0x1B)
                break;
            if(ch!=-32)
                continue ;
            ch=getch();
            if(ch==75&&play1->toward!=2)play1->toward=0;
            else if(ch==72&&play1->toward!=3) play1->toward=1;
            else if(ch==77&&play1->toward!=0) play1->toward=2;
            else if(ch==80&&play1->toward!=1) play1->toward=3;
            else continue ;
        }
    }
    end();
    while(1) {
        if((ch=getch())==0x1B)
            return 0;
        if(ch=='1');
        return 1;
    }
}
void UpdataSnakeMap() {   //0表示正常 1表示完蛋
    node *p=play1->head->next;
    while(p) {
        map[p->x][p->y]='o';
        p=p->next;
    }
    if(play1->state)
        map[play1->head->next->x][play1->head->next->y]='x';
}
void Print() {
    system("cls");
    for(int i=0; i<11; i++)
        printf("%s\n",map[i]);
}
void Romdo() {
    srand(time(NULL));
    int x=rand()%8+2,y=rand()%17+2;
    while(map[x][y]=='o')
        x=rand()%8+2;
    y=rand()%17+2;
    map[x][y]='+';
}
int welcome() {
    printf("---貪喫蛇大作戰---\n");
    printf("------------------\n");
    printf("按1開始遊戲\n按2退出遊戲\n");
    char c;
    while(1) {
        c=getch();
        if(c=='1')
            return 1;
        if(c=='2')
            return 0;
    }
}
int main() {
    if(welcome())
        while(1) {
            init();
            if(!GameRunning())
                if(!welcome())
                    break;
        }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章