|
解壓縮,可以查看這個貪吃蛇程序。支持液晶屏,矩陣鍵盤操作
0.png (61.14 KB, 下載次數: 146)
下載附件
2017-5-26 16:09 上傳
單片機源程序如下:
- /*
- * 貪吃蛇
- *
- * author:cole3
- * date:2010.11.28
- */
- #include <reg52.h>
- #include <stdlib.h>
- #include "lcd.h"
- #define LEN_MAX 40
- sbit KeyIn1 = P2^4;
- sbit KeyIn2 = P2^5;
- sbit KeyIn3 = P2^6;
- sbit KeyIn4 = P2^7;
- sbit KeyOut1 = P2^3;
- sbit KeyOut2 = P2^2;
- sbit KeyOut3 = P2^1;
- sbit KeyOut4 = P2^0;
- void key(void);
- void rand_dot(void);
- void turncount(int length,int position,int row,int page);
- int i=4,j=0,length=5,direction=2,k=0,dotx,doty,temx[LEN_MAX],temy[LEN_MAX];
- char speed=5, t_flag=0;
- main()
- {
- unsigned int t,flag=1;
- LCD12864_init();
- TMOD = 0x01;
- TH0 = 0x3C;
- TL0 = 0xB0;
- ET0 = 0;
- TR0 = 0;
- EA = 1;
- printf2lcd(0, 0, "----------------");
- printf2lcd(0, 2, "Gluttonous Snake");
- printf2lcd(0, 4, "----------------");
- printf2lcd(0, 6, " Made By Kingst");
- for (t=0; t<60000; t++);
- ClearSree(0);
- for (t=0; t<length; t++)
- {
- change(t,0);
- temx[t] = t;
- temy[t] = 0;
- }
- rand_dot();
- ET0 = 1;
- TR0 = 1;
- while(flag)
- {
- key(); // 按鍵掃描
- // 是否前行
- if (t_flag == 1)
- {
- t_flag = 0;
- switch (direction)
- {
- case 0: change(i,--j); break;
- case 1: change(--i,j); break;
- case 2: change(++i,j); break;
- case 3: change(i,++j); break;
- }
-
- clear(temx[0], temy[0]);
- for(t=0; t<length-1; t++)
- {
- temx[t] = temx[t+1];
- }
- temx[length-1] = i;
-
- for(t=0; t<length-1; t++)
- {
- temy[t] = temy[t+1];
- }
- temy[length-1] = j;
-
- if((i == dotx) && (j == doty))
- {
- temx[length] = dotx;
- temy[length++] = doty;
- rand_dot();
- }
- }
- // 是否撞墻
- if ((i < 0) || (i > 31))
- {
- flag = 0;
- }
- if ((j < 0) || (j > 15))
- {
- flag = 0;
- }
- // 是否撞自己
- for (t=0; t<length-4; t++)
- {
- if ((i == temx[t]) && (j == temy[t]))
- {
- flag = 0;
- }
- }
- // 是否晉級
- if (length == LEN_MAX)
- {
- TR0 = 0;
- ET0 = 0;
- ClearSree(0);
- speed--;
- if (speed < 0)
- {
- printf2lcd(4, 1, "You Win!");
- while (1);
- }
- else
- {
- printf2lcd(3, 1, "Next level!");
- for (t=0; t<60000; t++);
- ClearSree(0);
- }
- length = 5;
- direction = 2;
- i = 4;
- j = 0;
- k = 0;
- for (t=0; t<length; t++)
- {
- change(t,0);
- temx[t] = t;
- temy[t] = 0;
- }
- rand_dot();
- TR0 = 1;
- ET0 = 1;
- }
- }
- // game over
- TR0 = 0;
- ET0 = 0;
- ClearSree(0);
- printf2lcd(3, 1, "Game over!");
- printf2lcd(3, 3, "score:");
- ShowNum(9, 3, (length-5)+(LEN_MAX-5)*(5-speed));
- while(1);
- }
- void key(void)
- {
- KeyOut1 = 0;
- KeyOut2 = 1;
- KeyOut3 = 1;
- KeyOut4 = 1;
- if ((KeyIn4 == 0) && (direction != 3))
- direction = 0;
-
- KeyOut1 = 1;
- KeyOut2 = 1;
- KeyOut3 = 0;
- KeyOut4 = 1;
- if ((KeyIn4 == 0) && (direction != 0))
- direction = 3;
- KeyOut1 = 1;
- KeyOut2 = 0;
- KeyOut3 = 1;
- KeyOut4 = 1;
- if ((KeyIn4 == 0) && (direction != 2))
- direction = 1;
- KeyOut1 = 1;
- KeyOut2 = 1;
- KeyOut3 = 1;
- KeyOut4 = 0;
- if ((KeyIn4 == 0) && (direction != 1))
- direction = 2;
- }
- void rand_dot(void)
- {
- int ii;
-
- while (1)
- {
- dotx = rand() % 32;
- doty = rand() % 16;
- for (ii=0; ii<length; ii++)
- {
- if ((dotx == temx[ii]) && (doty == temy[ii]))
- break;
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
貪吃蛇程序.rar
(41.5 KB, 下載次數: 16)
2017-5-26 13:15 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
|