程式設計工作室 (program)   
一般區 精華區 休閒聊天 個人郵件 個人設定 重新登入

←回列表   ↑上一篇   ↓下一篇           

發信人: kenlkk@hotmail.com (garyboy)
日期: Wed, 29 Dec 1999 13:47:03 +0800
標題: 貪食蛇
信群: tw.bbs.comp.language    看板: 
來源: <84c6t1$f631@imsp212.netvigator.com>:21678, bbig020174.netvigator.com
組織: IMS Netvigator

#include <bios.h>
#include <conio.h>
#include <alloc.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#define LEFT  19200
#define RIGHT 19712
#define UP    18432
#define DOWN  20480
#define ESC   283
void draw_window(int,int,int,int);
void initial(void);
void draw_snake(void);
void run(void);
void gen(void);
void iseat(void);
int isdie(void);
void demo(void);
typedef struct snake *snake_pointer;
typedef struct snake {
   int x,y;
   snake_pointer llink,rlink;
  }snake;
snake_pointer start=NULL,ptr=NULL,old=NULL;
int direct=RIGHT;
int food_x=0,food_y=0;
int have_food=1;
int count,speed;
/*=================================================*/
main()
{
   int choose;
   draw_window(9,1,71,25);
   printf("please choose 1.Demo 2.Play");
   scanf("%d",&choose);
S: initial();
   clrscr();
   if(choose==1) demo();
   else run();
   window(1,1,80,25);
   gotoxy(1,4);
   printf("Again?\n(yn)");
   do
   {
    choose=getch();
    if(choose==121){window(10,2,70,24) ;goto S;}
   }while(choose!=110);
   return(0);
}
/*================================================*/
void draw_window(int left,int up,int right,int down)
{
 int i;
 clrscr();
 textcolor(LIGHTGREEN);
 gotoxy(left,up);
 putch('?');
 gotoxy(right,up);
 putch('?');
 gotoxy(left,down);
 putch('?');
 gotoxy(right,down);
 putch('?');
 for(i=left+1;i<right;i++)
 {
  gotoxy(i,up);
  putch('?');
  gotoxy(i,down);
  putch('?');
 }
 for(i=up+1;i<down;i++)
 {
  gotoxy(right,i);
  putch('?');
  gotoxy(left,i);
  putch('?');
 }
 window(left+1,up+1,right-1,down-1);
 gotoxy(19,6);
 printf("The snake game version 1.00");
 gotoxy(19,7);
 printf("  Write by Tseng Yi-Hseng  ");
 gotoxy(19,20);
 printf(" Press any key to start... ");
 getch();
 clrscr();
}
void initial(void)
{
  int i;
  snake_pointer temp;
  count=0;
  direct=RIGHT;
  gotoxy(15,8);
  printf("Please choose Speed?(1.fast--10.slow)");
  scanf("%d",&speed);
  start=(snake_pointer) malloc(sizeof(snake));
  ptr=start;
  ptr->llink=NULL;
  for(i=0;i<5;i++)
  {
   temp=(snake_pointer) malloc(sizeof(snake));
   ptr->x=20-i;
   ptr->y=23;
   ptr->rlink=temp;
   temp->llink=ptr;
   ptr=temp;
   ptr->rlink=NULL;
  }
}
void draw_snake(void)
{
  snake_pointer temp;
  temp=start;
  gotoxy(temp->x,temp->y);
  putch('@');
  temp=temp->rlink;
  gotoxy(temp->x,temp->y);
  putch('@');
  gotoxy(old->x,old->y);
  putch(' ');
  textcolor(YELLOW);
  gotoxy(food_x,food_y);
  putch('?');
}
void run(void)
{
 int tag=0;
 snake_pointer temp;
 while(direct != ESC)
 {
  old=ptr;
  temp=ptr;
  if(have_food) gen();
/*======================================*/
  textcolor(LIGHTRED);
  switch(direct)
  {
    case RIGHT:
        if((start->x)+1 < 61)
        {
   while(temp!=start)
   {
    temp->x=(*(temp->llink)).x;
    temp->y=(*(temp->llink)).y;
    temp=temp->llink;
   }
   (start->x)++;
   draw_snake();
   delay(1200*speed);
        }
        else goto bye;
        break;
    case LEFT :
        if((start->x)-1 > 0)
        {
                  while(temp!=start)
   {
    temp->x=(*(temp->llink)).x;
    temp->y=(*(temp->llink)).y;
    temp=temp->llink;
   }
   (start->x)--;
   draw_snake();
   delay(1200*speed);
 }
        else goto bye;
        break;
    case UP   :
        if((start->y)-1 > 0)
        {
                   while(temp!=start)
   {
    temp->x=(*(temp->llink)).x;
    temp->y=(*(temp->llink)).y;
    temp=temp->llink;
   }
   (start->y)--;
   draw_snake();
   delay(1800*speed);
        }
        else goto bye;
        break;
    case DOWN :
        if((start->y)+1 < 24)
        {
                   while(temp!=start)
   {
    temp->x=(*(temp->llink)).x;
    temp->y=(*(temp->llink)).y;
    temp=temp->llink;
   }
   (start->y)++;
   draw_snake();
   delay(1800*speed);
        }
        else goto bye;
        break;
  }
/*=====================================*/
  iseat();
  if(isdie()) goto bye;
  tag=bioskey(1);
  if(tag)
   {
   switch(bioskey(0))
    {
     case RIGHT: if(direct!=LEFT) direct=RIGHT;break;
     case LEFT : if(direct!=RIGHT)direct=LEFT ;break;
     case UP   : if(direct!=DOWN) direct=UP   ;break;
     case DOWN : if(direct!=UP)   direct=DOWN ;break;
     case ESC  : direct=ESC;break;
    }
   }
/*=====================================*/

 }
bye:;
}
void gen(void)
{
 snake_pointer temp;
 int tag;
 do
 {
   food_x=random(60)+1;
   food_y=random(23)+1;
   temp=start;
   tag=0;
   do
   {
    if(temp->x==food_x && temp->y==food_y) tag=1;
    temp=temp->rlink;
   }while(temp!=NULL);
 }while(tag);
 temp=NULL;
 free(temp);
 have_food=0;
}
int isdie(void)
{
  snake_pointer temp;
  temp=start;
  do
  {
    temp=temp->rlink;
    if(start->x==temp->x && start->y==temp->y) return(1);
  }while(temp->rlink!=NULL);
  return(0);
}
void iseat(void)
{
  snake_pointer temp;
  if(start->x==food_x && start->y==food_y)
  {
    temp=(snake_pointer) malloc(sizeof(snake));
    temp->x=(ptr->x)+((ptr->x)-((*(ptr->llink)).x));
    temp->y=(ptr->y)+((ptr->y)-((*(ptr->llink)).y));
    ptr->rlink=temp;
    temp->llink=ptr;
    temp->rlink=NULL;
    ptr=temp;
    sound(100);
    count++;
    window(1,1,80,25);
    gotoxy(1,1);
    printf("Total=%d",count);
    window(10,2,70,24);
    have_food=1;
  }
  temp=NULL;
  free(temp);
}
void demo(void)
{
  snake_pointer temp;
  int last_direct;
  while(1){
  old=ptr;
  temp=ptr;
  if(have_food) gen();
/*======================================*/
  textcolor(LIGHTRED);
  switch(direct)
  {
    case RIGHT:
        if((start->x)+1 < 61)
        {
   while(temp!=start)
   {
    temp->x=(*(temp->llink)).x;
    temp->y=(*(temp->llink)).y;
    temp=temp->llink;
   }
   (start->x)++;
   draw_snake();
   delay(2*speed);
        }
        else {last_direct=direct;direct=UP;}
        break;
    case LEFT :
        if((start->x)-1 > 0)
        {
                  while(temp!=start)
   {
    temp->x=(*(temp->llink)).x;
    temp->y=(*(temp->llink)).y;
    temp=temp->llink;
   }
   (start->x)--;
   draw_snake();
   delay(2*speed);
   if(last_direct==UP) direct=DOWN;
   if(last_direct==DOWN) direct=UP;
        }
        break;
    case UP   :
        if((start->y)-1 > 0)
        {
                   while(temp!=start)
   {
    temp->x=(*(temp->llink)).x;
    temp->y=(*(temp->llink)).y;
    temp=temp->llink;
   }
   (start->y)--;
   draw_snake();
   delay(8*speed);
        }
        else {last_direct=direct;direct=LEFT;}
        break;
    case DOWN :
        if((start->y)+1 < 24)
        {
                   while(temp!=start)
   {
    temp->x=(*(temp->llink)).x;
    temp->y=(*(temp->llink)).y;
    temp=temp->llink;
   }
   (start->y)++;
   draw_snake();
   if(start->y==22 && (start->x)!=1){direct=LEFT;last_direct=DOWN;}
   delay(8*speed);
        }
        else if(start->x==1) direct=RIGHT;
        break;
  }
  iseat();
  }
}



keigoo <keigooo.bbs@Viator.twbbs.org> wrote in message
news:3YDgUb$w9i@Viator.twbbs.org...
> ※ 引述《"K.K." <u8190868@tknet.tku.edu.tw>, 看板: programming》之銘言:
> : 有沒有人曾經用C++寫過貪食蛇 and 踩地雷的,可以把原始檔寄給我(寄至我信箱)
參考
> : 嗎...?
> 對ㄚ~~~我也要~~~謝謝
>
> --
> ※ Origin: 清華衛道會 <140.113.93.37> ◆ From: angler.cs.tku.edu.tw




←回列表   ↑上一篇   ↓下一篇