之前由于任务用51单片机做了一个贪吃蛇系统,因为没有驱动芯片,最后是直接用IO口驱动点阵,唯一的缺点是不大亮。做好一个贪吃蛇,需要考虑很多方面的东西,怎么控制它的方向,怎么控制食物的随机生成,怎么控制蛇的死亡等等,下面附上部分源代码:
main.c
- #include<reg52.h>
- #include"main.h"
- #include"delay.h"
- #include"key.h"
- #include"display.h"
- #include"gamelist.h"
- void main()
- {
- u8 gameSelect = 0; //ÓÎÏ·Ñ¡Ôñ
- while(1)
- {
- (*Game[gameSelect])();
-
- }
- }
- key.c
- #include"key.h"
- #include"main.h"
- #include"delay.h"
- /////////////// °´¼ü¼ì²âº¯Êý ///////////////////
- u8 keySelect(void){
- if(!KSelect){
- delayMs(KEY_DELAY_MS);
- if(!KSelect)
- while(!KSelect);
- return 1;
- }
- return 0;
- }
- u8 keyStart(void){
- if(!KStart){
- delayMs(KEY_DELAY_MS);
- if(!KStart){
- while(!KStart);
- return 1;
- }
- }
- return 0;
- }
- u8 keyUP(void){
- if(!KU){
- delayMs(KEY_DELAY_MS);
- if(!KU)
- return 1;
- }
- return 0;
- }
- u8 keyDOWN(void){
- if(!KD){
- delayMs(KEY_DELAY_MS);
- if(!KD)
- return 1;
- }
- return 0;
- }
- u8 keyLEFT(void){
- if(!KL){
- delayMs(KEY_DELAY_MS);
- if(!KL)
- return 1;
- }
- return 0;
- }
- u8 keyRIGHT(void){
- if(!KR){
- delayMs(KEY_DELAY_MS);
- if(!KR)return 1;
- }
- return 0;
- }
- u8 keyY(void){
- if(!KY){
- delayMs(KEY_DELAY_MS);
- if(!KY)
- return 1;
- }
- return 0;
- }
- u8 keyX(void){
- if(!KX){
- delayMs(KEY_DELAY_MS);
- if(!KX)
- return 1;
- }
- return 0;
- }
- u8 keyA(void){
- if(!KA){
- delayMs(KEY_DELAY_MS);
- if(!KA)
- return 1;
- }
- return 0;
- }
- u8 keyB(void){
- if(!KB){
- delayMs(KEY_DELAY_MS);
- if(!KB)
- return 1;
- }
- return 0;
- }
- snack.c
- #include"snake.h"
- #include<stdlib.h>
- #include"key.h"
- #include"display.h"
- #include"main.h"
- #include"delay.h"
- void setDir(u8 *dir_1,u8 *dir_2,u8 value){
- *dir_1 = 0;
- *dir_2 = value;
- }
- void setHead(u8 *z,u8 dir){
- *z+=dir;
- if(*z == -1)*z = 7;
- else if(*z == 8)*z = 0;
- }
- void moveSnakeXY(u8 *snakeXY,const u8 num,const u8 XY){
- u8 i = 0;
- while(i<num-1){
- snakeXY[i] = snakeXY[i+1];
- i++;
- }
- snakeXY[i] = XY;
- }
- void changeLED(u8 (*led)[LED_WIDTH],const u8 *x,const u8 *y,u8 num,u8 apx,u8 apy){
- int i = 0;
- cleanLED(led);
- while(i < num){
- led[y[i]][x[i]] = 1;
- i++;
- }
- led[apy][apx] = 1;
- }
- void randXY(u8 *x,u8 *y,u8 (*led)[LED_WIDTH],u8 t){
- srand(t);
- do{
- *x = rand()%8;
- *y = rand()%8;
- }while(led[*y][*x]);
- led[*y][*x] = 1;
- }
- void showDegree(u8 degree){
- u8 i,j;
- for(i = 0;i<LED_HEIGHT;i++)
- for(j = 0;j<=degree;j++)
- setPoint(j,i);
- }
- u8 Snake(void){
- #define BLOCKNUM 64
- u8 degree = 0,snakeNum = 1,dirX = 1,dirY = 0,dirOX,dirOY;
- u8 LED[LED_HEIGHT][LED_WIDTH];
- u8 headX = 0,headY = 0,appleX,appleY;
- u8 t,time;
- u8 r = 0;
- u8 snakeX[BLOCKNUM];
- u8 snakeY[BLOCKNUM];
- while(1){
- showDegree(degree);
- if(keySelect())
- if(degree<7)degree++;
- else degree = 0;
- else if(keyStart())break;
- }
- time = 100-degree*10;
- cleanLED(LED);
- LED[headY][headX] = 1;
- snakeX[0] = headX;
- snakeY[0] = headY;
- dirOX = dirX;
- dirOY = dirY;
- randXY(&appleX,&appleY,LED,t);
- t = 0;
- while(1){
- display(LED);
- if(dirY!=-1 && (keyUP()||keyY()))setDir(&dirX,&dirY,-1);
- else if(dirY!=1 &&(keyDOWN()||keyA()))setDir(&dirX,&dirY,1);
- else if(dirX!=-1 &&(keyLEFT()||keyX()))setDir(&dirY,&dirX,-1);
- else if(dirX!=1 &&(keyRIGHT()||keyB()))setDir(&dirY,&dirX,1);
-
- if(keyStart())
- while(!keyStart())
- display(LED);
-
- r++;
- if(t++ >time){
- t = 0;
- if((dirX!=0 && dirOX ==0)||(dirY!=0 && dirOY==0)){
- dirOX = dirX;
- dirOY = dirY;
- }
- setHead(&headX,dirOX);
- setHead(&headY,dirOY);
- if(headY == appleY && headX == appleX){
- snakeNum++;
- snakeX[snakeNum-1] = appleX;
- snakeY[snakeNum-1] = appleY;
- randXY(&appleX,&appleY,LED,r);
- }else if(!LED[headY][headX]){
- //Ç°·½ÎÞÕÏ°
- moveSnakeXY(snakeX,snakeNum,headX);
- moveSnakeXY(snakeY,snakeNum,headY);
- }else if(headX == snakeX[0]&& headY == snakeY[0]){//
- moveSnakeXY(snakeX,snakeNum,headX);
- moveSnakeXY(snakeY,snakeNum,headY);
- }else {
- return GAMEOVER;
- }
- changeLED(LED,snakeX,snakeY,snakeNum,appleX,appleY);
- }
- }
- return GAMEOVER;
复制代码
|