貪食蛇

用C寫DOS版貪食蛇遊戲

簡介:

因為我準備要在NIOS上面跑貪食蛇程式,所以就要先用C寫一隻程式,不過這是DOS版。


 

 

原始碼:

  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <conio.h>   
  4. #include <windows.h>   
  5. #include <time.h>   
  6. #include "utilwin32.h"   
  7. /*  
  8.     up          00      48   shift-up  
  9.     down        00      50   shift-down  
  10.     left        00      4b   shift-left  
  11.     right       00      4d   shift-right  
  12. */  
  13. #define UP 0x48   
  14. #define DOWN 0x50   
  15. #define LEFT 0x4b   
  16. #define RIGHT 0x4d   
  17. #define MAX_X 80   
  18. #define MAX_Y 25   
  19. #define bool int   
  20. #define true 1   
  21. #define false 0   
  22.   
  23. struct node {    
  24.     int x;    
  25.     int y;    
  26.     struct node *next;    
  27. };    
  28.   
  29. int foodx,foody;   
  30.   
  31. typedef struct node queue;   
  32.   
  33. queue *front, *rear;   
  34.   
  35. bool outFrame(int x,int y){   
  36.     bool b = false;   
  37.     if(x<1 || x>=MAX_X || y<1 || y>=MAX_Y )   
  38.         b=true;   
  39.     return b;   
  40. }   
  41.   
  42. void print(int x,int y,char c){   
  43.     gotoxy(x,y);   
  44.     printf("%c",c);   
  45. }   
  46.   
  47. void createq() {    
  48.     front = (queue*) malloc(sizeof(queue));    
  49.     rear = (queue*) malloc(sizeof(queue));    
  50.     front->next = rear;    
  51.     rear->next = NULL;   
  52.     rear->x = 1;    
  53.     rear->y = 1;    
  54. }    
  55.   
  56. void removeq(){   
  57.     if(!front->next->next==NULL){   
  58.         queue *temp;   
  59.         temp=front->next;   
  60.         front->next=temp->next;   
  61.         free(temp);   
  62.     }   
  63. }   
  64.   
  65. void addq(int x,int y){   
  66.     queue *newnode = (queue*) malloc(sizeof(queue));   
  67.     if(front->next->next==NULL){   
  68.         front->next->next = newnode;   
  69.     }   
  70.     newnode->x = x;   
  71.     newnode->y = y;   
  72.     newnode->next = NULL;   
  73.     rear->next = newnode;   
  74.     rear = newnode;   
  75. }   
  76.   
  77. void showq() {    
  78.     queue *temp;    
  79.   
  80.     temp = front->next;    
  81.   
  82.     while(temp != NULL) {    
  83.         print(temp->x,temp->y,'#');   
  84.         temp = temp->next;    
  85.     }   
  86. }   
  87.   
  88. bool isSame(){   
  89.     bool b = false;   
  90.     queue *temp;    
  91.   
  92.     temp = front->next;    
  93.   
  94.     while(temp != NULL) {    
  95.         if(temp->x==foodx && temp->y==foody){   
  96.             return true;   
  97.         }   
  98.         temp = temp->next;    
  99.     }   
  100.     return false;   
  101. }   
  102.   
  103. void food() {    
  104.     while(true){   
  105.         foodx=rand()%78+1;   
  106.         foody=rand()%23+1;   
  107.         if(!isSame())   
  108.            break;   
  109.     }      
  110.     //printf("foodx=%d,\tfoody=%d\n",foodx,foody);   
  111. }   
  112.   
  113. int main()   
  114. {   
  115.     srand(time(NULL));   
  116.     createq();   
  117.     food();   
  118.     int c1, c2;   
  119.     int x=1,y=1;   
  120.     char control=RIGHT;   
  121.     //RUN game   
  122.     while (true){   
  123.            
  124.         if(kbhit()){   
  125.             //Enter KEY   
  126.             c1 = getch();   
  127.             if ((c1 == 0xe0)||(c1==00))   
  128.             {   
  129.                 c2 = getch();    
  130.                 switch(c2){   
  131.                     case UP:   
  132.                         if(control!=DOWN)   
  133.                            control=UP;   
  134.                         break;   
  135.                     case LEFT:   
  136.                         if(control!=RIGHT)   
  137.                            control=LEFT;   
  138.                         break;   
  139.                     case DOWN:   
  140.                         if(control!=UP)   
  141.                            control=DOWN;   
  142.                         break;   
  143.                     case RIGHT:   
  144.                         if(control!=LEFT)   
  145.                            control=RIGHT;   
  146.                         break;   
  147.                 }   
  148.             }    
  149.         }   
  150.         switch(control){   
  151.             case UP:   
  152.                 if(!outFrame(x,y-1)){   
  153.                     clrscr();   
  154.                     y=y-1;   
  155.                     if(! (x==foodx && foody==y )){   
  156.                         removeq();   
  157.                     }else{   
  158.                         food();   
  159.                     }   
  160.                     addq(x,y);   
  161.                     showq();   
  162.                     //print(x,y,'#');   
  163.                 }   
  164.                 break;   
  165.             case LEFT:   
  166.                 if(!outFrame(x-1,y)){   
  167.                     clrscr();   
  168.                     x=x-1;   
  169.                     if(! (x==foodx && foody==y )){   
  170.                         removeq();   
  171.                     }else{   
  172.                         food();   
  173.                     }   
  174.                     addq(x,y);   
  175.                     showq();   
  176.                     //print(x,y,'#');   
  177.                 }   
  178.                 break;   
  179.             case DOWN:   
  180.                 if(!outFrame(x,y+1)){   
  181.                     clrscr();   
  182.                     y=y+1;   
  183.                     if(! (x==foodx && foody==y )){   
  184.                         removeq();   
  185.                     }else{   
  186.                         food();   
  187.                     }   
  188.                     addq(x,y);   
  189.                     showq();   
  190.                     //print(x,y,'#');   
  191.                 }   
  192.                 break;   
  193.             case RIGHT:   
  194.                 if(!outFrame(x+1,y)){   
  195.                     clrscr();   
  196.                     x=x+1;   
  197.                     if(! (x==foodx && foody==y )){   
  198.                         removeq();   
  199.                     }else{   
  200.                         food();   
  201.                     }   
  202.                     addq(x,y);   
  203.                     showq();   
  204.                     //print(x,y,'#');   
  205.                 }   
  206.                 break;   
  207.         }   
  208.         print(foodx,foody,'*');   
  209.         Sleep(50);   
  210.     }   
  211.   
  212.     system("pause");   
  213.     return 0;   
  214. }  
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include "utilwin32.h"
/*
    up          00      48   shift-up
    down        00      50   shift-down
    left        00      4b   shift-left
    right       00      4d   shift-right
*/
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4b
#define RIGHT 0x4d
#define MAX_X 80
#define MAX_Y 25
#define bool int
#define true 1
#define false 0

struct node { 
    int x; 
    int y; 
    struct node *next; 
}; 

int foodx,foody;

typedef struct node queue;

queue *front, *rear;

bool outFrame(int x,int y){
    bool b = false;
    if(x<1 || x>=MAX_X || y<1 || y>=MAX_Y )
        b=true;
    return b;
}

void print(int x,int y,char c){
    gotoxy(x,y);
    printf("%c",c);
}

void createq() { 
    front = (queue*) malloc(sizeof(queue)); 
    rear = (queue*) malloc(sizeof(queue)); 
    front->next = rear; 
    rear->next = NULL;
    rear->x = 1; 
    rear->y = 1; 
} 

void removeq(){
    if(!front->next->next==NULL){
        queue *temp;
        temp=front->next;
        front->next=temp->next;
        free(temp);
    }
}

void addq(int x,int y){
    queue *newnode = (queue*) malloc(sizeof(queue));
    if(front->next->next==NULL){
        front->next->next = newnode;
    }
    newnode->x = x;
    newnode->y = y;
    newnode->next = NULL;
    rear->next = newnode;
    rear = newnode;
}

void showq() { 
    queue *temp; 

    temp = front->next; 

    while(temp != NULL) { 
        print(temp->x,temp->y,'#');
        temp = temp->next; 
    }
}

bool isSame(){
    bool b = false;
    queue *temp; 

    temp = front->next; 

    while(temp != NULL) { 
        if(temp->x==foodx && temp->y==foody){
            return true;
        }
        temp = temp->next; 
    }
    return false;
}

void food() { 
    while(true){
        foodx=rand()%78+1;
        foody=rand()%23+1;
        if(!isSame())
           break;
    }   
    //printf("foodx=%d,\tfoody=%d\n",foodx,foody);
}

int main()
{
    srand(time(NULL));
    createq();
    food();
    int c1, c2;
    int x=1,y=1;
    char control=RIGHT;
    //RUN game
    while (true){
        
        if(kbhit()){
            //Enter KEY
            c1 = getch();
            if ((c1 == 0xe0)||(c1==00))
            {
                c2 = getch(); 
                switch(c2){
                    case UP:
                        if(control!=DOWN)
                           control=UP;
                        break;
                    case LEFT:
                        if(control!=RIGHT)
                           control=LEFT;
                        break;
                    case DOWN:
                        if(control!=UP)
                           control=DOWN;
                        break;
                    case RIGHT:
                        if(control!=LEFT)
                           control=RIGHT;
                        break;
                }
            } 
        }
        switch(control){
            case UP:
                if(!outFrame(x,y-1)){
                    clrscr();
                    y=y-1;
                    if(! (x==foodx && foody==y )){
                        removeq();
                    }else{
                        food();
                    }
                    addq(x,y);
                    showq();
                    //print(x,y,'#');
                }
                break;
            case LEFT:
                if(!outFrame(x-1,y)){
                    clrscr();
                    x=x-1;
                    if(! (x==foodx && foody==y )){
                        removeq();
                    }else{
                        food();
                    }
                    addq(x,y);
                    showq();
                    //print(x,y,'#');
                }
                break;
            case DOWN:
                if(!outFrame(x,y+1)){
                    clrscr();
                    y=y+1;
                    if(! (x==foodx && foody==y )){
                        removeq();
                    }else{
                        food();
                    }
                    addq(x,y);
                    showq();
                    //print(x,y,'#');
                }
                break;
            case RIGHT:
                if(!outFrame(x+1,y)){
                    clrscr();
                    x=x+1;
                    if(! (x==foodx && foody==y )){
                        removeq();
                    }else{
                        food();
                    }
                    addq(x,y);
                    showq();
                    //print(x,y,'#');
                }
                break;
        }
        print(foodx,foody,'*');
        Sleep(50);
    }

    system("pause");
    return 0;
}

解釋:

  1. #include "utilwin32.h"  
#include "utilwin32.h"

因為DEV-C++沒有gotoxy() and clrscr() 所以要加入這2個檔

下載處utilwin32.c, utilwin32.h
gotoxy() 將游標移到那裡
clrscr() 清除螢幕

  1. /*  
  2.     up          00      48   shift-up  
  3.     down        00      50   shift-down  
  4.     left        00      4b   shift-left  
  5.     right       00      4d   shift-right  
  6. */  
  7. #define UP 0x48   
  8. #define DOWN 0x50   
  9. #define LEFT 0x4b   
  10. #define RIGHT 0x4d   
  11. #define MAX_X 80   
  12. #define MAX_Y 25  
/*
    up          00      48   shift-up
    down        00      50   shift-down
    left        00      4b   shift-left
    right       00      4d   shift-right
*/
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4b
#define RIGHT 0x4d
#define MAX_X 80
#define MAX_Y 25
定義一些按鍵控制碼(左、上、右、下)
  1. struct node {    
  2.     int x;    
  3.     int y;    
  4.     struct node *next;    
  5. };    
  6.   
  7. typedef struct node queue;   
  8.   
  9. queue *front, *rear;  
struct node { 
    int x; 
    int y; 
    struct node *next; 
}; 

typedef struct node queue;

queue *front, *rear;

用到Queue資料結構  這裡就不多講了

待續...

程式下載:

snake.rar

參考:

[1].簡單遊戲程式--貪食蛇