The microcontroller source program is as follows:
#include #include #include #define uchar unsigned char #define uint unsigned int #define Random(x) (rand()%x) #define dgrade 11 #define igrade 15 #define up 9 #define down 13 #define left 12 #define right 14 #define passscore 50 #define N 25 uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}; sbit dula=P2^6; sbit wela=P2^7; uchar code score0[]={"score"}; uchar code speed0[]={"speed"}; uchar score = 0,speed = 5; uchar temp,key=16; struct Snake { uchar x[N]; uchar y[N]; uchar node; uchar direction; uchar life; }snake; struct Food { uchar x; uchar y; uint exist; }food; void delay(uint t) { uint i,j; for(i=0;i } //Matrix keyboard void ReadKey() { P3 = 0xfe; temp = P3; temp = temp & 0xf0; if(temp != 0xf0) { delay(100); if(temp != 0xf0) { temp = P3; switch(temp) { case 0xee: key = 0; break; case 0xde: key = 1; break; case 0xbe: key = 2; break; case 0x7e: key = 3; break; } while(temp != 0xf0) { temp = P3; temp = temp & 0xf0; } } } P3 = 0xfd; temp = P3; temp = temp & 0xf0; if(temp != 0xf0) { delay(100); if(temp != 0xf0) { temp = P3; switch(temp) { case 0xed: key = 4; break; case 0xdd: key = 5; break; case 0xbd: key = 6; break; case 0x7d: key = 7; break; } while(temp != 0xf0) { temp = P3; temp = temp & 0xf0; } } } P3 = 0xfb; temp = P3; temp = temp & 0xf0; if(temp != 0xf0) { delay(100); if(temp != 0xf0) { temp = P3; switch(temp) { case 0xeb: key = 8; break; case 0xdb: key = 9; break; case 0xbb: key = 10; break; case 0x7b: key = 11; break; } while(temp != 0xf0) { temp = P3; temp = temp & 0xf0; } } } P3 = 0xf7; temp = P3; temp = temp & 0xf0; if(temp != 0xf0) { delay(100); if(temp != 0xf0) { temp = P3; switch(temp) { case 0xe7: key = 12; break; case 0xd7: key = 13; break; case 0xb7: key = 14; break; case 0x77: key = 15; break; } while(temp != 0xf0) { temp = P3; temp = temp & 0xf0; } } } } void Lcd_DisNode(uchar x,uchar y,uchar color) { Lcd_DisRectangle(2*x,2*y,2*x+1,2*y+1,color); } void Timer0_Init() { TMOD = 0x01; TH0 = (65536-1000)/256; TL0 = (65536-1000)%256; TR0 = 1; ET0 = 1; EA = 1; } //Create the basic framework void CreateBasicFrame() { uchar n; for(n=0;n<128;n++) { Lcd_DisDot(n,0,1); Lcd_DisDot(n,1,1); Lcd_DisDot(n,62,1); Lcd_DisDot(n,63,1); } for(n=0;n<63;n++) { Lcd_DisDot(0,n,1); Lcd_DisDot(1,n,1); Lcd_DisDot(78,n,1); Lcd_DisDot(79,n,1); Lcd_DisDot(126,n,1); Lcd_DisDot(127,n,1); } } // Display the results void DisScore() { uchar str[3]; str[0] = (score/10)|0x30; str[1] = (score%10)|0x30; str[2] = 0; Lcd_WriteStr(5,0,score0); Lcd_WriteStr(6,1,str); } //Display speed void DisSpeed() { uchar str[3]; str[0] = ' '; str[1] = speed|0x30; str[2] = 0; Lcd_WriteStr(5,2,speed0); Lcd_WriteStr(6,3,str); } //game over void GameOver() { uchar n; Lcd_DisNode(food.x,food.y,0); for(n=1;n Lcd_DisNode(snake.x[n],snake.y[n],0); } if(snake.life==1) Lcd_WriteStr(1,1,"PASS"); else Lcd_WriteStr(1,1,"LOSE"); Lcd_WriteStr(0,2,"GameOver"); } void GamePlay() { uchar n; food.exist=0; snake.life=1; snake.direction=down; snake.x[0]=6;snake.y[0]=6; snake.x[1]=5;snake.y[1]=6; snake.x[2]=4;snake.y[2]=6; snake.node=3; DisScore(); DisSpeed(); while(1) { if(food.exist==0) { // Generate food while(1) { srand(TL0); food.x = Random(30); food.y=Random(38); for(n=0;n if ((food.x == snake.x[n]) && (food.y == snake.y[n])) break; } if(n==snake.node) { if(food.x>0&&food.x<39&&food.y>0&&food.y<31) food.exist=1; break; } } } if(food.exist==1) { Lcd_DisNode(food.x,food.y,1); } for(n=snake.node-1;n>0;n--) { snake.x[n]=snake.x[n-1]; snake.y[n]=snake.y[n-1]; } switch(snake.direction) { case down:snake.y[0]++;break; case up:snake.y[0]--;break; case right:snake.x[0]++;break; case left:snake.x[0]--;break; default:break; } //Determine if it is dead for(n=3;n if (snake.x[n]==snake.x[0]&&snake.y[n]==snake.y[0]) { GameOver(); snake.life=0; break; } } if(snake.x[0]<1||snake.x[0]>38||snake.y[0]<1||snake.y[0]>30) { GameOver(); snake.life=0; } if(snake.life==0) break; //Judge whether food has been eaten if (snake.x[0]==food.x&&snake.y[0]==food.y) { Lcd_DisNode(food.x,food.y,0); snake.x[snake.node]=200; snake.y[snake.node]=200; snake.node++; food.exist=0; score=score+speed; if(score>=passscore) { DisScore(); GameOver(); break; } DisScore(); }
Previous article:The single chip microcomputer adds a PCF8574 module to realize the IIC mode to drive LCD1602
Next article:Two-way learning fan remote control switch with natural wind MCU source program + circuit
Recommended ReadingLatest update time:2024-11-16 15:41
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Things to note when measuring compressed air with vortex flowmeter
- Signal anti-reflection and impedance matching
- Temperature coefficient of aluminum electrolysis
- Android & BlueNRG2
- Problems with two types of channel MOS tube circuits
- MSP430 Program Library--Use of DAC12
- [Job seeking] Xia Xian; Bachelor degree, 35 years old, graduated in Mechanical and Electrical Engineering; Looking for a job as a Mechanical Engineer
- CRC16 algorithm code for UDMA
- MSP430 ADC conversion + mean filter
- The functions of handheld devices realize information management in various industries