51 single chip microcomputer + 8*8 dot matrix to achieve the greedy snake source code

Publisher:玉米哥哥Latest update time:2015-06-25 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere



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 k;
}
/*********************
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 {P2=mux(x[i]);P1=255-mux(y[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  [0]=4;y[0]=4; //fruitn                            
 =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            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
  }      
 }   
}
Reference address:51 single chip microcomputer + 8*8 dot matrix to achieve the greedy snake source code

Previous article:ILI9235 51 MCU driver - Push Box Game
Next article:51 single-chip multi-channel DS18B20 temperature measurement program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号