Circuit diagram and MCU simulation file download: http://www.51hei.com/bbs/dpj-20604-1.html
Below is the source code:
#include
#define uchar unsigned char
#define SNAKE 20 //maximum length
#define TIME 50 //display delay time
#define SPEED 71 //speed control
//#define keyenable 1
sbit keyenable=P3^6; //direction enable
/*sbit keyx=P0^1; //left and right
sbit keyy=P0^2; //up and down */
//sbit up=P0^0;
sbit up=P3^3; //down
sbit down=P3^1;
sbit right=P3^2;
sbit left=P3^4;
uchar x[SNAKE+1];
uchar y[SNAKE+1];
uchar time,n,i,e; //Delay time, current snake length, general loop variable, current speed
char addx, addy; //Displacement offset
/********************
Delay program
************************/
void delay(char MS)
{
char us, usn;
while(MS!=0)
{
usn = 0;
while(usn!=0)
{
us=0xff;
while (us!=0){us--;}; usn--;
}
MS--
;
}
}
/*******************************************
Judge collision
*******************************************/
bit knock()
{bit k;
k=0;
if(x[1]>7||y[1]>7)k=1; //Hit the wallfor
(i=2;i
}
/*********************
Up, down, left, and right key position processing
********************/
void turnkey()// interrupt 0 using 2
{//up=1;
if(keyenable)
{
if(left){addy=0;if(addx!=1)addx=-1; else addx=1;}
if(right){addy=0;if(addx!=-1)addx=1; else addx=-1;}
if(up){addx=0;if(addy!=-1)addy=1; else addy=-1;} if(down){ addx=0;
if(addy!=1)addy=-1; else addy=1;}
/*if(keyy)
{addy=0; //left and right
if(keyx)if(addx==-1)addx=-1; else addx=1; //left
else
if(addx==1)addx=1; else addx=-1; //right
}
else
{addx=0; //upper and lower
if(keyx)if(addy==1)addy=1; else addy=-1; //lower
else
if(addy==-1)addy=-1; else addy=1; //upper
} */
}
}[page]
/*******************
exponentiation program
****************/
uchar mux(uchar temp)
{
if(temp==7)return 128;
if(temp==6)return 64;
if(temp==5)return 32; ==
4)return 16;
if(temp==3)return 8;
if(temp==2)return 4;
if(temp==1)return 2;
if(temp==0)return 1;
return 0;
}
/*****************
Show clock display program
********************/
void timer0(uchar k)
{while(k--)
{for(i=0;i
turnkey(); //Up, down, left and right key processing
delay(TIME); //Display delay
P2=0x00;P1=0xff;
}}
}
/********************* Main
program
********************/
void main(void)
{e=SPEED;
P0=0x00;
P1=0xff;
P2=0x00;
P3=0x00;
while(1)
{//if(keyenable==1){P1=0x00;P2=0xff;}else{P1=0xff;P2=0x00;}
for(i=3;i
=3; //snake lengthn=-1
x[1]=1;y[1]=0; //snake headx
[2]=0;y[2]=0; //snake tail1
addx=0;addy=0; //displacement offset
//k=1;
while(1){if(keyenable)break;timer0(1);}
while(1)
{timer0(e);
if(knock()){e=SPEED;break;} //Judge collisionif
((x[0]==x[1]+addx)&(y[0]==y[1]+addy)) //Eat or not
{n++;
if(n==SNAKE+1)
{n=3;e=e-10;
for(i=3;i
x[0]=x[n-2];
y[0]=y[n-2];
}
for(i=n-1;i>1;i--){x[i]=x[i-1];y[i ]=y[i-1];}
//x[n-1]=x[2];y[n-1]=y[2];
x[1]=x[2]+addx;y[1]=y[2]+addy; //Move
}
}
}
Previous article:ILI9235 51 MCU driver - Push Box Game
Next article:51 single-chip multi-channel DS18B20 temperature measurement program
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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
Guess you like
- The sine wave signal is distorted after passing through the op amp
- Update summary: justd0 analyzes the official routine of LSM6DSOX finite state machine
- A 25-year-old DJI employee died suddenly of cardiac arrest, having just graduated with a master's degree
- Test method for power system conversion efficiency
- Talk about picking up leaks on Xianyu - Sangfor Internet Behavior Audit AC1300 (Part 1)
- C2000 chip calculation skills and precautions
- The beauty of algorithms
- Worried about switching from Class AB to Class D? No need to worry
- "Practice together in 2021" + continue to persevere
- [TI recommended course] #[High Precision Laboratory] Magnetic Sensor Technology#