SNAKE.C
上传用户:shuquanyi
上传日期:2022-02-08
资源大小:2k
文件大小:9k
源码类别:

其他游戏

开发平台:

Asm

  1. #include "w77e58.h"
  2. #include "vt100.h"
  3. #include "init.h"
  4. #include "stdio.h"
  5. #include "stdlib.h"
  6. typedef struct kk{
  7. unsigned char x;
  8. unsigned char y;
  9. }SnakeType;
  10. xdata struct kk snake[400];
  11. #define UP 0xf0
  12. #define DOWN 0x0f
  13. //-----------------------------------------------------------------------------------
  14. #define LEFT 0x00
  15. #define RIGHT 0xff
  16. #define L_SHEET (2*2 +1)
  17. #define R_SHEET (13*2+1)
  18. #define T_SHEET 5
  19. #define B_SHEET 16
  20. //-----------------------------------------------------------------------------------
  21. void init(void);
  22. void delay(unsigned int);
  23. void put_block(char,char);
  24. void hide_block(char,char);
  25. bit flash_snake(unsigned char);
  26. void putch(char);
  27. bit kbhit(void);
  28. char getch(void);
  29. void clr_wchdog(void){}
  30. void put_bug(void);
  31. void init_snake(void);
  32. void putstr(char xdata *);
  33. void flash_data(void);
  34. void type_kbd(char code*);
  35. //-----------------------------------------------------------------------------------
  36. data char GameScore;
  37. data char HighScore;
  38. data char direct;
  39. data char SnakeLen;
  40. xdata char print_buf[20];
  41. xdata int Level;
  42. xdata int bug_x,bug_y;
  43. bit TimerEnd;
  44. bit GameOver;
  45. bit GameStart;
  46. bit blink_flag0;
  47. bit blink_flag1;
  48. bit flashed;
  49. bit type_flag;
  50. //-----------------------------------------------------------------------------------
  51. void main()
  52. {
  53. data int ch,ch1;
  54. bit scoreadd;
  55. init();
  56. put_bug();
  57. while(1)
  58. {
  59. REN_1=1;
  60. if(kbhit())
  61.   {
  62.    ch1=ch;
  63.    ch=getch();
  64.      ch=ch&(0x20^0xff);
  65.      switch(ch)
  66.        {
  67.          case 'W':
  68.                   ch=UP;
  69.                   if(~ch == ch1) ch=ch1;
  70.                   else {TimerEnd=1;scoreadd=1;}
  71. break;
  72.          case 'S':
  73.                   ch=DOWN;
  74.                   if(~ch == ch1) ch=ch1;
  75.                   else {TimerEnd=1;scoreadd=1;}
  76. break;
  77.          case 'A':
  78.                           ch=LEFT;
  79.                   if(~ch == ch1) ch=ch1;
  80.                   else {TimerEnd=1;scoreadd=1;}
  81. break;
  82.          case 'D':
  83.                           ch=RIGHT;
  84.                   if(~ch == ch1) ch=ch1;
  85.                   else {TimerEnd=1;scoreadd=1;}
  86. break;
  87.          case 'r':
  88.                           if(GameOver)
  89.                                {
  90.                   init_snake();
  91.             GameOver=0;
  92.                                     GameScore=0;
  93.                                        //GameStart=0;
  94. }
  95.     if(!GameStart)
  96.        {
  97.                   del_line(4);
  98.                                  gotoxy(L_SHEET,4);
  99.                                  fputstr("Playing game....");
  100.                                  del_line(24);
  101.                                  gotoxy(L_SHEET,24);
  102. fputstr("Option| UP(&W) DOWN(&S) LEFT(&A) RIGHT(D) &PAUSE");
  103. }
  104. GameStart=1;ch=1;
  105.                                break;
  106. case 'P':
  107.    if(!GameStart) break;
  108.    while(!kbhit())
  109.       {
  110.                          gotoxy(L_SHEET,4);
  111.                          type_kbd("Game pause, any key to continue...");
  112. del_line(4);
  113.      }
  114.        gotoxy(L_SHEET,4);
  115. fputstr("Playing game....");
  116.     break;
  117. case 'N':
  118.    if(!GameStart && Level>0) Level--;
  119.       flash_data();
  120. ch=1;
  121.     break;
  122. case 'U':
  123.    if(!GameStart && Level<16) Level++;
  124.       flash_data();
  125. default:
  126.    ch=1;
  127. }
  128.    }
  129. if(!GameStart)
  130.   {
  131.    del_line(4);
  132.    gotoxy(L_SHEET,4);
  133.    if(GameOver) type_kbd("Game Over,");
  134.      type_kbd("Press Enter to start    ");
  135.    }
  136.   if(!GameOver && GameStart)
  137.    {
  138.   if(blink_flag0)
  139.    {
  140. hide_block(bug_x,bug_y);
  141.    blink_flag0=0;
  142.    }
  143.    if(blink_flag1)
  144.    {
  145. put_block(bug_x,bug_y);
  146.    blink_flag1=0;
  147.    }
  148.     }
  149. if( TimerEnd && GameStart && !GameOver)
  150.   {
  151.    TimerEnd=0;
  152.      if(scoreadd)
  153.      {
  154.       scoreadd=0;
  155.       GameScore += (20+Level*2);
  156.       }
  157.      if(!flash_snake(ch))                            
  158.        {
  159. if(GameScore>HighScore) HighScore=GameScore;
  160.     GameScore=0;
  161.                  GameOver=1;
  162.                          GameStart=0;
  163.                           hide_block(bug_x,bug_y);
  164.                           del_line(24);
  165. gotoxy(L_SHEET,24);
  166. fputstr("Option| Level &UP/DOW&N");
  167. }
  168. flash_data();
  169.    }
  170. }
  171. }
  172. //-----------------------------------------------------------------------------------
  173. void time_int(void) interrupt 1
  174. {
  175. static int tcc;
  176. TH0=0x80;
  177. if((tcc&0x03)==0) blink_flag0=1;
  178. if((tcc&0x03)==2) blink_flag1=1;
  179. if( ++tcc >= (16-Level+4) )
  180. {
  181. tcc=0;
  182.   TimerEnd=!flashed;
  183. flashed=0;
  184.    }
  185. if(type_flag ) TimerEnd=1;
  186. }
  187. //-----------------------------------------------------------------------------------
  188. void init(void)
  189. {
  190. char i;
  191. char xdata*xptr;
  192.        EA=0;
  193.        use_inter_SRAM;
  194. xptr=(char xdata*)0x400;
  195. while(xptr) *xptr--=0;
  196. timer0_16bit;
  197. timer0_start;
  198. ET0=1;
  199. serial_uart8;
  200. REN_1=1;
  201. serial_baud_9600;
  202. TI_1=1;
  203. Level=8;
  204. fputstr("#");
  205. clrscr();
  206. for(i=L_SHEET;i<=R_SHEET;i+=2)
  207. {
  208.   put_block(i,T_SHEET);
  209.   put_block(i,B_SHEET);
  210. }
  211. for(i=T_SHEET+1;i<B_SHEET;i++)
  212. {
  213.   put_block(L_SHEET,i);
  214.   put_block(R_SHEET,i);
  215. }
  216. fputstr("$");
  217. gotoxy(L_SHEET,1);fputstr("#Cupidity snake version 1.00                               $");
  218. gotoxy(L_SHEET,2);fputstr("High Score:");
  219. gotoxy(L_SHEET,3);fputstr("Score     :");
  220. gotoxy(L_SHEET+30,2);fputstr("Tuched:");
  221. gotoxy(L_SHEET+30,3);fputstr("Level :");
  222. gotoxy(L_SHEET,24);
  223. fputstr("Option| Level &UP/DOW&N");
  224.     snake[0].x=0;
  225. init_snake();
  226. flash_data();
  227. GameStart=0;
  228. GameOver=0;
  229. EA=1;
  230. }
  231. //-----------------------------------------------------------------------------------
  232. void init_snake(void)
  233. {
  234. unsigned char i;
  235.        i=0;
  236. while(snake[i].x!=0)
  237. {
  238.      hide_block(snake[i].x,snake[i].y);
  239.                i++;
  240. }
  241. for(i=0;i<7;i++)
  242. {
  243. snake[i].x=L_SHEET+20;
  244.   snake[i].y=T_SHEET+4+i;
  245.   }
  246. snake[7].x=0;
  247. direct=LEFT;
  248. for(i=0;snake[i].x!=0;i++)
  249. put_block(snake[i].x,snake[i].y);
  250.   SnakeLen=7;
  251.  
  252. }
  253. //-----------------------------------------------------------------------------------
  254. void put_bug(void)
  255. {
  256. data int i,ch;
  257. re_find:
  258. do{
  259. ch =rand()%76;
  260.        }while((ch<=L_SHEET || ch>=R_SHEET)|| 0==(ch&0x01));
  261.        bug_x=ch;
  262.        do{
  263. ch =rand()%24;
  264.        }while(ch<=T_SHEET || ch>=B_SHEET);
  265. for(i=0;snake[i].x!=0;i++)
  266. {
  267. if(ch==snake[i].y && bug_x==snake[i].x)
  268.   goto re_find;
  269. }
  270.        bug_y=ch;
  271.        put_block(bug_x,bug_y);
  272. }
  273. //-----------------------------------------------------------------------------------
  274. bit flash_snake(unsigned char dir1)
  275. {
  276. data unsigned char x0,y0,i,j;
  277. if(dir1==1) dir1=direct;
  278. else
  279. {
  280. if(direct == ~dir1) dir1=direct;
  281.   else direct=dir1;
  282.  
  283. }
  284. switch(dir1)
  285. {
  286. case UP:
  287. y0=snake[0].y-1;
  288.   x0=snake[0].x;
  289. break;
  290. case DOWN:
  291. y0=snake[0].y+1;
  292.   x0=snake[0].x;
  293. break;
  294. case LEFT:
  295. x0=snake[0].x-2;
  296.   y0=snake[0].y;
  297. break;
  298. case RIGHT:
  299. x0=snake[0].x+2;
  300.   y0=snake[0].y;
  301. break;
  302. }
  303.        if( (y0<=T_SHEET)|| (y0>=B_SHEET)|| (x0<=L_SHEET)||(x0>=R_SHEET))return(0);
  304.       
  305. for(i=0;snake[i].x!=0;i++)
  306. {
  307. if(x0==snake[i].x && y0==snake[i].y) return(0);
  308.   }
  309.   hide_block(x0,y0);
  310.   put_block(x0,y0);
  311. hide_block(snake[i-1].x,snake[i-1].y);
  312.   if(bug_x==x0 && bug_y==y0)
  313.   {
  314.   SnakeLen++;
  315.    GameScore +=(SnakeLen*5 + Level*3);
  316. if((SnakeLen%15)==0 && Level<16) Level++;
  317.   snake[i].x=snake[i-1].x;
  318.   snake[i].y=snake[i-1].y;
  319.    snake[i+1].x=0;
  320.      i++;
  321.     
  322. for(j=0;snake[j].x!=0;j++)
  323.   {
  324.    hide_block(snake[j].x,snake[j].y);
  325.        put_block(snake[j].x,snake[j].y);
  326.          delay(0x200);
  327.    }
  328.     put_bug();
  329.    }
  330. while(--i) snake[i]=snake[i-1];
  331. snake[0].x=x0;
  332. snake[0].y=y0;
  333. flashed=1;
  334. return(1);
  335. }
  336. //-----------------------------------------------------------------------------------
  337. void put_block(char x,char y)
  338. {
  339. gotoxy(x,y);
  340. fputstr("▓");
  341. delay(100);
  342. }
  343. //-----------------------------------------------------------------------------------
  344. void hide_block(char x,char y)
  345. {
  346. gotoxy(x,y);
  347. fputstr("  ");
  348. }
  349. //-----------------------------------------------------------------------------------
  350. bit kbhit(void)
  351. {
  352. return(RI_1);
  353. }
  354. //-----------------------------------------------------------------------------------
  355. void putstr(char xdata*str)
  356. {
  357. while(*str)
  358. {
  359. putch(*str++);
  360. }
  361. }
  362. //-----------------------------------------------------------------------------------
  363. void flash_data(void)
  364. {
  365. gotoxy(L_SHEET+sizeof("High Score: "),2);
  366. sprintf(print_buf,"%-10ld",HighScore);putstr(print_buf);
  367. gotoxy(L_SHEET+sizeof("High Score: "),3);
  368. sprintf(print_buf,"%-10ld",GameScore);putstr(print_buf);
  369. gotoxy(L_SHEET+30+sizeof("Tuched: "),2);
  370. sprintf(print_buf,"%3d",SnakeLen);putstr(print_buf);
  371. gotoxy(L_SHEET+30+sizeof("Tuched: "),3);
  372. sprintf(print_buf,"%3d",(int)(char)Level);putstr(print_buf);
  373. }
  374. //-----------------------------------------------------------------------------------
  375. void type_kbd(char code *str)
  376. {
  377. data int ch,i=0;
  378. type_flag=1;
  379. flashed=1;
  380. while( (ch=str[i]) !=0)
  381. {
  382. if(kbhit()) break;
  383. if(TimerEnd)
  384. {
  385.   TimerEnd=0;
  386.    putch(ch);
  387.    if(ch=='n') fputstr("r    ");
  388.        i++;
  389.    }
  390.     clr_wchdog();
  391. }
  392. type_flag=0;
  393. }
  394. //-----------------------------------------------------------------------------------