SNAKE.C
上传用户:shuquanyi
上传日期:2022-02-08
资源大小:2k
文件大小:9k
- #include "w77e58.h"
- #include "vt100.h"
- #include "init.h"
- #include "stdio.h"
- #include "stdlib.h"
- typedef struct kk{
- unsigned char x;
- unsigned char y;
- }SnakeType;
- xdata struct kk snake[400];
- #define UP 0xf0
- #define DOWN 0x0f
- //-----------------------------------------------------------------------------------
- #define LEFT 0x00
- #define RIGHT 0xff
- #define L_SHEET (2*2 +1)
- #define R_SHEET (13*2+1)
- #define T_SHEET 5
- #define B_SHEET 16
- //-----------------------------------------------------------------------------------
- void init(void);
- void delay(unsigned int);
- void put_block(char,char);
- void hide_block(char,char);
- bit flash_snake(unsigned char);
- void putch(char);
- bit kbhit(void);
- char getch(void);
- void clr_wchdog(void){}
- void put_bug(void);
- void init_snake(void);
- void putstr(char xdata *);
- void flash_data(void);
- void type_kbd(char code*);
- //-----------------------------------------------------------------------------------
- data char GameScore;
- data char HighScore;
- data char direct;
- data char SnakeLen;
- xdata char print_buf[20];
- xdata int Level;
- xdata int bug_x,bug_y;
- bit TimerEnd;
- bit GameOver;
- bit GameStart;
- bit blink_flag0;
- bit blink_flag1;
- bit flashed;
- bit type_flag;
- //-----------------------------------------------------------------------------------
- void main()
- {
- data int ch,ch1;
- bit scoreadd;
- init();
- put_bug();
- while(1)
- {
- REN_1=1;
- if(kbhit())
- {
- ch1=ch;
- ch=getch();
- ch=ch&(0x20^0xff);
- switch(ch)
- {
- case 'W':
- ch=UP;
- if(~ch == ch1) ch=ch1;
- else {TimerEnd=1;scoreadd=1;}
- break;
- case 'S':
- ch=DOWN;
- if(~ch == ch1) ch=ch1;
- else {TimerEnd=1;scoreadd=1;}
- break;
- case 'A':
- ch=LEFT;
- if(~ch == ch1) ch=ch1;
- else {TimerEnd=1;scoreadd=1;}
- break;
- case 'D':
- ch=RIGHT;
- if(~ch == ch1) ch=ch1;
- else {TimerEnd=1;scoreadd=1;}
- break;
- case 'r':
- if(GameOver)
- {
- init_snake();
- GameOver=0;
- GameScore=0;
- //GameStart=0;
- }
- if(!GameStart)
- {
- del_line(4);
- gotoxy(L_SHEET,4);
- fputstr("Playing game....");
- del_line(24);
- gotoxy(L_SHEET,24);
- fputstr("Option| UP(&W) DOWN(&S) LEFT(&A) RIGHT(D) &PAUSE");
- }
- GameStart=1;ch=1;
- break;
- case 'P':
- if(!GameStart) break;
- while(!kbhit())
- {
- gotoxy(L_SHEET,4);
- type_kbd("Game pause, any key to continue...");
- del_line(4);
- }
- gotoxy(L_SHEET,4);
- fputstr("Playing game....");
- break;
- case 'N':
- if(!GameStart && Level>0) Level--;
- flash_data();
- ch=1;
- break;
- case 'U':
- if(!GameStart && Level<16) Level++;
- flash_data();
- default:
- ch=1;
- }
- }
- if(!GameStart)
- {
- del_line(4);
- gotoxy(L_SHEET,4);
- if(GameOver) type_kbd("Game Over,");
- type_kbd("Press Enter to start ");
- }
- if(!GameOver && GameStart)
- {
- if(blink_flag0)
- {
- hide_block(bug_x,bug_y);
- blink_flag0=0;
- }
- if(blink_flag1)
- {
- put_block(bug_x,bug_y);
- blink_flag1=0;
- }
- }
-
- if( TimerEnd && GameStart && !GameOver)
- {
- TimerEnd=0;
- if(scoreadd)
- {
- scoreadd=0;
- GameScore += (20+Level*2);
- }
- if(!flash_snake(ch))
- {
- if(GameScore>HighScore) HighScore=GameScore;
- GameScore=0;
- GameOver=1;
- GameStart=0;
- hide_block(bug_x,bug_y);
- del_line(24);
- gotoxy(L_SHEET,24);
- fputstr("Option| Level &UP/DOW&N");
- }
- flash_data();
- }
- }
- }
- //-----------------------------------------------------------------------------------
- void time_int(void) interrupt 1
- {
- static int tcc;
-
- TH0=0x80;
- if((tcc&0x03)==0) blink_flag0=1;
- if((tcc&0x03)==2) blink_flag1=1;
-
- if( ++tcc >= (16-Level+4) )
- {
- tcc=0;
- TimerEnd=!flashed;
- flashed=0;
- }
- if(type_flag ) TimerEnd=1;
- }
- //-----------------------------------------------------------------------------------
- void init(void)
- {
- char i;
- char xdata*xptr;
-
- EA=0;
- use_inter_SRAM;
- xptr=(char xdata*)0x400;
- while(xptr) *xptr--=0;
- timer0_16bit;
- timer0_start;
- ET0=1;
- serial_uart8;
- REN_1=1;
- serial_baud_9600;
- TI_1=1;
- Level=8;
- fputstr("#");
- clrscr();
- for(i=L_SHEET;i<=R_SHEET;i+=2)
- {
- put_block(i,T_SHEET);
- put_block(i,B_SHEET);
- }
- for(i=T_SHEET+1;i<B_SHEET;i++)
- {
- put_block(L_SHEET,i);
- put_block(R_SHEET,i);
- }
- fputstr("$");
- gotoxy(L_SHEET,1);fputstr("#Cupidity snake version 1.00 $");
- gotoxy(L_SHEET,2);fputstr("High Score:");
- gotoxy(L_SHEET,3);fputstr("Score :");
- gotoxy(L_SHEET+30,2);fputstr("Tuched:");
- gotoxy(L_SHEET+30,3);fputstr("Level :");
- gotoxy(L_SHEET,24);
- fputstr("Option| Level &UP/DOW&N");
- snake[0].x=0;
- init_snake();
- flash_data();
- GameStart=0;
- GameOver=0;
- EA=1;
- }
- //-----------------------------------------------------------------------------------
- void init_snake(void)
- {
- unsigned char i;
-
- i=0;
- while(snake[i].x!=0)
- {
- hide_block(snake[i].x,snake[i].y);
- i++;
- }
- for(i=0;i<7;i++)
- {
- snake[i].x=L_SHEET+20;
- snake[i].y=T_SHEET+4+i;
- }
- snake[7].x=0;
- direct=LEFT;
- for(i=0;snake[i].x!=0;i++)
- put_block(snake[i].x,snake[i].y);
- SnakeLen=7;
-
- }
- //-----------------------------------------------------------------------------------
- void put_bug(void)
- {
- data int i,ch;
- re_find:
- do{
- ch =rand()%76;
- }while((ch<=L_SHEET || ch>=R_SHEET)|| 0==(ch&0x01));
- bug_x=ch;
- do{
- ch =rand()%24;
- }while(ch<=T_SHEET || ch>=B_SHEET);
- for(i=0;snake[i].x!=0;i++)
- {
- if(ch==snake[i].y && bug_x==snake[i].x)
- goto re_find;
- }
- bug_y=ch;
- put_block(bug_x,bug_y);
- }
- //-----------------------------------------------------------------------------------
- bit flash_snake(unsigned char dir1)
- {
- data unsigned char x0,y0,i,j;
- if(dir1==1) dir1=direct;
- else
- {
- if(direct == ~dir1) dir1=direct;
- else direct=dir1;
-
- }
- switch(dir1)
- {
- case UP:
- y0=snake[0].y-1;
- x0=snake[0].x;
- break;
- case DOWN:
- y0=snake[0].y+1;
- x0=snake[0].x;
- break;
- case LEFT:
- x0=snake[0].x-2;
- y0=snake[0].y;
- break;
- case RIGHT:
- x0=snake[0].x+2;
- y0=snake[0].y;
- break;
- }
- if( (y0<=T_SHEET)|| (y0>=B_SHEET)|| (x0<=L_SHEET)||(x0>=R_SHEET))return(0);
-
- for(i=0;snake[i].x!=0;i++)
- {
- if(x0==snake[i].x && y0==snake[i].y) return(0);
- }
- hide_block(x0,y0);
- put_block(x0,y0);
- hide_block(snake[i-1].x,snake[i-1].y);
- if(bug_x==x0 && bug_y==y0)
- {
- SnakeLen++;
- GameScore +=(SnakeLen*5 + Level*3);
- if((SnakeLen%15)==0 && Level<16) Level++;
- snake[i].x=snake[i-1].x;
- snake[i].y=snake[i-1].y;
- snake[i+1].x=0;
- i++;
-
- for(j=0;snake[j].x!=0;j++)
- {
- hide_block(snake[j].x,snake[j].y);
- put_block(snake[j].x,snake[j].y);
- delay(0x200);
- }
- put_bug();
- }
- while(--i) snake[i]=snake[i-1];
- snake[0].x=x0;
- snake[0].y=y0;
- flashed=1;
- return(1);
- }
- //-----------------------------------------------------------------------------------
- void put_block(char x,char y)
- {
- gotoxy(x,y);
- fputstr("▓");
- delay(100);
- }
- //-----------------------------------------------------------------------------------
- void hide_block(char x,char y)
- {
- gotoxy(x,y);
- fputstr(" ");
- }
- //-----------------------------------------------------------------------------------
- bit kbhit(void)
- {
- return(RI_1);
- }
- //-----------------------------------------------------------------------------------
- void putstr(char xdata*str)
- {
- while(*str)
- {
- putch(*str++);
- }
- }
- //-----------------------------------------------------------------------------------
- void flash_data(void)
- {
- gotoxy(L_SHEET+sizeof("High Score: "),2);
- sprintf(print_buf,"%-10ld",HighScore);putstr(print_buf);
- gotoxy(L_SHEET+sizeof("High Score: "),3);
- sprintf(print_buf,"%-10ld",GameScore);putstr(print_buf);
- gotoxy(L_SHEET+30+sizeof("Tuched: "),2);
- sprintf(print_buf,"%3d",SnakeLen);putstr(print_buf);
- gotoxy(L_SHEET+30+sizeof("Tuched: "),3);
- sprintf(print_buf,"%3d",(int)(char)Level);putstr(print_buf);
- }
- //-----------------------------------------------------------------------------------
- void type_kbd(char code *str)
- {
- data int ch,i=0;
-
- type_flag=1;
- flashed=1;
- while( (ch=str[i]) !=0)
- {
- if(kbhit()) break;
- if(TimerEnd)
- {
- TimerEnd=0;
- putch(ch);
- if(ch=='n') fputstr("r ");
- i++;
- }
- clr_wchdog();
- }
- type_flag=0;
- }
- //-----------------------------------------------------------------------------------