原程序如下:
#include <reg51.h>
#include <stdlib.h>
#define uchar unsigned char
#define TRUE 1
#define FALSE 0
//#define MAX_LENGTH 33 /* max length of snake nodess */
//#define GAME_SPEED 100 /* game speed */
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define qian 5
#define hou 6
sbit KEY_UP=P2^3; /*定义按键*/
sbit KEY_DOWN=P2^1;
sbit KEY_LEFT=P2^6;
sbit KEY_RIGHT=P2^2;
sbit KEY_qian=P2^4;
sbit KEY_hou=P2^5;
sbit START=P2^7;
unsigned char zong[8]={0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80}; //点阵 P0=zong[k]就能让k位为高
unsigned char green[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
unsigned char red[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
struct Point { //点阵**1-1
uchar x,y,z;
};
struct Snake {
struct Point nodes[50];
uchar len;
uchar dir;
uchar live;
} snake;
struct Food {
struct Point pos; //**1-1
uchar exist;
} food;
int lastx,lasty,lastz,keyCode,keyCode2,sp=10,l;
void init(void); //初始化
void gamePlay(void);
void drawWall(void);
void createFood(void);
void drawFood(void);
void drawSnake(void);
uchar touchWall(void); //碰到墙
uchar touchSelf(void); //碰到自己
void gameOver(void); //游戏结束
void moveSnake(void); //移动蛇
int oppositeDirec
tion(int keyCode); //键盘输入方向
int foodEat(void); //吃食物
void expandSnake(void); //
void select(void);
uchar getKey(void);
//主函数
void delay_50us(unsigned char t) //延迟函数
{
unsigned char j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--)
;
}
void main() {
init();
l=1;
while(l)
{
gamePlay();
}
gameOver();
}
//随机产生食物
void createFood() {
int i;
label:
food.pos.x=((int)rand()%8);
food.pos.y=((int)rand()%8);
food.pos.z=((int)rand()%8);
for(i=0;i<=snake.len-1;i++){
if(snake.nodes
.x==food.pos.x&&snake.nodes.y==food.pos.y&&snake.nodes.z==food.pos.z)
goto label;
}
}
uchar getKey() //键盘输入 消抖
{
if(!KEY_UP)
{
delay_50us(2);
if(!KEY_UP)
{
return UP;
}
}
if(!KEY_DOWN)
{
delay_50us(2);
if(!KEY_DOWN)
{
return DOWN;
}
}
if(!KEY_LEFT)
{
delay_50us(2);
if(!KEY_LEFT)
{
return LEFT;
}
}
if(!KEY_RIGHT)
{
delay_50us(2);
if(!KEY_RIGHT)
{
return RIGHT;
}
}
if(!KEY_qian)
{
delay_50us(2);
if(!KEY_qian)
{
return qian;
}
}
if(!KEY_hou)
{
delay_50us(2);
if(!KEY_qian)
{
return qian;
}
}
return snake.dir;//什么都没按
}
//检测方向与按键输入是否冲突
int oppositeDirection(int keyCode) {
if(keyCode==UP&&snake.dir==DOWN){
return 1;
}
else if(keyCode==DOWN&&snake.dir==UP) {
return 1;
}
else if(keyCode==LEFT&&snake.dir==RIGHT){
return 1;
}
else if(keyCode==RIGHT&&snake.dir==LEFT){
return 1;
}
else if(keyCode==qian&&snake.dir==hou){
return 1;
}
else if(keyCode==hou&&snake.dir==qian) {
return 1;
}
else
return 0;
}
//食物是否可以吃到
int foodEat() {
if(snake.nodes[0].x==food.pos.x&&snake.nodes[0].y==food.pos.y&&snake.nodes[0].z==food.pos.z)
return 1;
else
return 0;
}
void moveSnake() { //移动蛇
int k;
lastx=snake.nodes[snake.len-1].x;
lasty=snake.nodes[snake.len-1].y;
lasty=snake.nodes[snake.len-1].z;
for(k=snake.len-2;k>=0;k--)
{
snake.nodes[k+1].x=snake.nodes[k].x;
snake.nodes[k+1].y=snake.nodes[k].y;
snake.nodes[k+1].y=snake.nodes[k].z;
}
if(snake.dir==UP)
snake.nodes[0].y-=1;
else if(snake.dir==DOWN)
snake.nodes[0].y+=1;
else if(snake.dir==LEFT)
snake.nodes[0].x-=1;
else if(snake.dir==RIGHT)
snake.nodes[0].x+=1;
else if(snake.dir==hou)
snake.nodes[0].z-=1;
else if(snake.dir==qian)
snake.nodes[0].z+=1;
else
;
}
uchar touchSelf() { //判定是否碰到自身
uchar i;
for (i=3;i<snake.len-1;i++)
{
if(snake.nodes[0].x==snake.nodes.x&&snake.nodes[0].y==snake.nodes.y&&snake.nodes[0].y==snake.nodes.z)
return TRUE;
}
return FALSE;
}
uchar touchWall() { //判断是否碰到墙
uchar x1=snake.nodes[0].x;
uchar y1=snake.nodes[0].y;
uchar z1=snake.nodes[0].z;
if(x1<0||x1>7||y1<0||y1>7||z1<0||z1>7)
return TRUE;
else
return FALSE;
}
void gamePlay() {
int d;
uchar keyCode;
while(START); //等待按开始键
while(TRUE) {
if (touchWall() || touchSelf()) {
gameOver(); //跳到游戏结束子函数
return;
}
if (!food.exist) createFood();
food.exist=1;
for(d=0;d<(sp*36);d++)
{
drawFood();
drawSnake();
}
keyCode = getKey();
lastx=snake.nodes[snake.len-1].x;
lasty=snake.nodes[snake.len-1].y;
lastz=snake.nodes[snake.len-1].z;
if (!oppositeDirection(keyCode))
{
snake.dir = keyCode;
}
moveSnake();
if (foodEat()) {
food.exist = FALSE;
expandSnake();
}
}
}
void init() { //初始化
snake.nodes[0].x=1;
snake.nodes[0].y=1;
snake.nodes[0].z=1;
snake.nodes[1].x=1;
snake.nodes[1].y=2;
snake.nodes[1].z=1;
snake.len=2;
snake.live=1;
snake.dir=UP;
food.exist=0;
srand(5); /*sui ji shu fa sheng qi随机数发生器*/
}
void drawFood() { //???
P0=zong[food.pos.x];
P1=green[food.pos.y];
P3=green[food.pos.z];
delay(2);
P0=0;
P1=0xff;
}
void gameOver() { //???
while(1)
{
P3=0xff;
P0=0xff;
P1=0x0;
}
}
void expandSnake() { //???增长蛇
if(keyCode==UP){
lastx-=1;
}
else if(keyCode==DOWN) {
lastx+=1;
}
else if(keyCode==LEFT){
lasty-=1;
}
else if(keyCode==RIGHT){
lasty+=1;
}
else if(keyCode==qian){
lastz-=1;
}
else if(keyCode==hou){
lastz+=1;
}
else
{}
snake.nodes[snake.len].x=lastx;
snake.nodes[snake.len].y=lasty;
snake.len++;
}
编译错误如下:
求大神指导:还有这个程序是否能够运行;
跪求;
你知不知道51可用的RAM是很有限的,你倒好,定义了这么个变量
struct Snake {
struct Point nodes[50];
uchar len;
uchar dir;
uchar live;
} snake;
单单是snake变量,就占用了应该是至少50 *4 + 1 + 1 + 1 = 203字,想知道确切结果,就sizeof(变量)看看。然后你还有其他变量的定义,你想想占用了多少字节,回去翻下51书本,看看51可用的RAM有多大,你都超出了,编译当然报错了……
非常感谢 我知道了
一周热门 更多>