Single chip LCD12864 linked list to achieve the greedy snake source code

Publisher:QianfengLatest update time:2019-10-21 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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                for(j=0;j<10;j++);    

}

//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();

                }

                

[1] [2]
Keywords:MCU Reference address:Single chip LCD12864 linked list to achieve the greedy snake source code

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

Learn the AVR microcontroller tutorial header file in ten days
I recently started to learn AVR microcontrollers. Since the 51 development board I bought before is compatible with AVR microcontrollers, there is no need for a development board. So I found a tutorial on the Internet called "Learn AVR Microcontrollers in Ten Days", which was also taught by someone from Guo Tianxiang's
[Microcontroller]
51 MCU driver for proximity switch
Today I made a little thing: the digital tube displays the change value of the proximity switch. The proximity switch is connected to the microcontroller through external interrupt 0; the digital tube reading increases to 50 and then clears to zero; #include reg51.h // Reference the header file of the standard libra
[Microcontroller]
Different Applications of MCS-51 Single Chip Microcomputer in Timing Interruption
    The interrupt response delay time of the MCS-51 microcontroller depends on whether other interrupt service routines are in progress, or on what instruction is being executed. The interrupt response time in a single interrupt system is 3 to 8 machine cycles . Regardless of the cause of the error, their impact must
[Microcontroller]
Design of sine wave inverter power supply based on PICFxx microcontroller control
    introduction   An inverter is a device that uses power electronics technology to convert electrical energy. With the development of power electronics technology, inverters are increasingly used, but the application system also places higher and higher requirements on the output voltage waveform characteristics of
[Microcontroller]
Design of sine wave inverter power supply based on PICFxx microcontroller control
Single chip design of ultrasonic distance measurement simulation
1. Circuit Design Ultrasonic Module Introduction Ultrasonic waves are often used to measure distances because they have strong directivity, slow energy consumption, and can travel farther in the medium. Ultrasonic waves are often used to measure distances. Ultrasonic detection is often quick, convenient, simple to cal
[Microcontroller]
Single chip design of ultrasonic distance measurement simulation
Main features of AVR microcontroller
Main features of AVR High reliability, powerful functions, high speed, low power consumption and low price have always been the important indicators to measure the performance of single-chip microcomputers, and are also the necessary conditions for single-chip microcomputers to occupy the market and survive. The e
[Microcontroller]
Tips for re-programming the OTP of Songhan MCU!
1. Problem: In my early work, I often encountered that when doing tests, if I just want to modify some short instructions or data, I have to replace a new chip and re-burn it before testing. This wastes time, consumes chips, and also consumes money.     2. Solution: Actually, OTP is not as "stubborn" as you think. As
[Microcontroller]
Single pin on microcontroller to detect ambient light and control lighting
A previous design idea (Reference 1) used an LED as a transducer to measure ambient light intensity and provide illumination. This design idea uses the same principles, but only has an LED, two resistors, an IC, and a 0.1mF bypass capacitor. When the circuit is used to provide ambient light feedback, it does not requir
[Microcontroller]
Single pin on microcontroller to detect ambient light and control lighting
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号