贪吃蛇的单片机源程序如下:
/***********************************************************
功能说明:贪吃蛇
编译环境:keil4
MCU :STC89C52RC
时钟频率: 22.1184MHz
注意:该例程中“过关”不能在keil4编译环境中正常显示
************************************************************/
/*****************************************************************
/^/^
_|__| O|
/ /~ _/
____|__________/
_______
`
| |
/ /
/ / \
/ /
/ /
/ / _----_
/ / _-~ ~-_ | |
( ( _-~ _--_ ~-_ _/ |
~-____-~ _-~ ~-_ ~-_-~ /
~-_ _-~ ~-_ _-~
~--______-~ ~-___-~
*****************************************************************/
#include "stc.h"
#include "Lcd12864.h"
#include "Key.h"
#define uchar unsigned char
#define uint unsigned int
static unsigned long Seed = 1;
#define A 48271L
#define M 2147483647L
#define Q (M / A)
#define R (M % A)
/************************************
伪随机数发生器
*************************************/
double Random(void)
{
long TmpSeed;
TmpSeed=A*(Seed%Q)-R*(Seed/Q);
if(TmpSeed>=0)
Seed=TmpSeed;
else
Seed=TmpSeed+M;
return (double)Seed/M;
}
/**************************************
为伪随机数发生器播种
***************************************/
void InitRandom(unsigned long InitVal)
{
Seed=InitVal;
}
//延时子程序
void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i
for(j=0;j<10;j++);
}
/*********************************
初始化MPU
**********************************/
void InitCpu(void)
{
TMOD=0x01;
TH0=0;
TL0=0;
TR0=1;
ET0=1;
EA=1;
}
#define N 25
struct Food
{
unsigned char x;
unsigned char y;
unsigned char yes;
}food;//食物结构体
struct Snake
{
unsigned char x[N];
unsigned char y[N];
unsigned char node;
unsigned char direction;
unsigned char life;
}snake;//蛇结构体
unsigned char Flag=0;
unsigned char Score=0;
unsigned char Speed=5; //speed越大,速度越慢
unsigned char KeyBuffer=0;
#define FUNC 1 //(P3^1)表示级别
#define UP 2 //(P3^3)表示左
#define DOWN 3 //(P3^5)表示右
#define LEFT 4 //(P3^4)表示下
#define RIGHT 5 //(P3^2)表示上
#define PASSSCORE 20 //预定义过关成绩
void Timer0Int(void) interrupt 1
{
switch(OSReadKey())
{
case 5:
KeyBuffer=FUNC; //表示级别
/*if(++Speed>=10)
Speed=1;
Flag|=1<<1;//速度变化标志置1*/
break;
case 21:
KeyBuffer=DOWN; //表示右
/*if(snake.direction!=2)
snake.direction=1;*/
break;
case 13:
KeyBuffer=UP; //表示左
/*if(snake.direction!=1)
snake.direction=2;*/
break;
case 9:
KeyBuffer=RIGHT; //表示上
/*if(snake.direction!=4)
snake.direction=3;*/
break;
case 17:
KeyBuffer=LEFT; //表示下
/*if(snake.directi
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』