在ARM版上开发的贪吃蛇

本文主要为贪吃蛇部分代码的实现,开发板的按键以及触屏中断等,本文不做重点讲解

前一阵子学习了mini2440板的开发,并开发了一个简单贪吃蛇的游戏

游戏功能清单:1.游戏开始

2.游戏难度设置(简单,普通,困难)

3.游戏结束

4.蛇的正常运行路线(触碰障碍物或者蛇本身随即游戏结束,不能反方向行动)

5.通过内设时钟,产生随机种子,从而产生随机分布的“食物”

以下为代码解析部分:

#include "snake_game.h"
#include "adcts.h"
#include "lcd.h"
#include "graphicslib.h"
#include "timer.h"


#define N 300




extern int click_x; //触屏触碰的点的x轴
extern int click_y; //y轴




int iHard = 0;  // 0 初级 1 普通 2 困难
int goDir = 4;//1--上 2 下 3 左 4 右
int choice_do = 1;//选择使能 0 游戏开始,除“结束游戏”以外功能不可使用 1 功能选择开启 2 正在进行难度设置 


struct Food
{
int x;                    /*食物的横坐标*/
int y;                    /*食物的纵坐标*/
int yes;                  /*判断是否要出现食物的变量*/
};                        /*食物的结构体*/


struct Snake
{
int x[N];
int y[N];
int node;                 /*蛇的节数*/
int life;                 /* 蛇的生命,0活着,1死亡*/   
};


struct Snake sn_t = {0};
struct Food fd_t = {0};


/**
游戏界面初始化
**/
void game_init()
{  
xrect(5, 5, 240 - 5 * 2, 320 - 5 * 2, RGB(0,0,0),RGB(0,0,0),2);
xrect(0, 240, 240, 5, RGB(255,255,255),RGB(255,255,255),2);
//xline(0, 240, 240, 240, RGB(255,255,255));
lcd_printWord(" 难度 ",10,250,RGB(0,0,0),RGB(255,255,255));
lcd_printWord(" 结束 ",10,290,RGB(0,0,0),RGB(255,255,255));
lcd_printWord(" 开始 ",40,270,RGB(0,0,0),RGB(255,255,255));
lcd_printWord(" 初级 ",10,210,RGB(0,0,0),RGB(255,255,255));
//xcircle(180,260,15,RGB(255,255,255),RGB(0,255,0),1);//上
xrect(170,250,22,22,RGB(255,255,255),RGB(0,255,0),1);
lcd_printWord("↑",173,253,RGB(255,255,255),RGB(0,0,0));
//printWord("↓←→",180,255,RGB(0,0,0),RGB(255,255,255));  
//xcircle(180,295,15,RGB(255,255,255),RGB(0,255,0),1); //下
xrect(170,285,22,22,RGB(255,255,255),RGB(0,255,0),1);
lcd_printWord("↓",173,288,RGB(255,255,255),RGB(0,0,0));
//xcircle(150,280,15,RGB(255,255,255),RGB(0,255,0),1);//左
xrect(140,270,22,22,RGB(255,255,255),RGB(0,255,0),1);
lcd_printWord("←",143,273,RGB(255,255,255),RGB(0,0,0));
//xcircle(210,280,15,RGB(255,255,255),RGB(0,255,0),1);//右
xrect(200,270,22,22,RGB(255,255,255),RGB(0,255,0),1);
lcd_printWord("→",203,273,RGB(255,255,255),RGB(0,0,0));
//choice_do = 1;
//game_choice_handler();
}


 /**
游戏界面清除
**/
void game_clear()
{
xrect(5,5,(240-5-5),(240-5),RGB(0,0,0),RGB(0,0,0),2);
}
/************************************************************************/
/* 设置游戏的点,isRight表示填充是否有效
*/
/************************************************************************/
void game_setPoint(int x,int y,int isRight)
{
if (isRight != 0)
{
xrect(x,y,5,5,RGB(255,255,255),RGB(255,255,255),2);
}
else if (isRight == 0)
{
xrect(x,y,5,5,RGB(0,0,0),RGB(0,0,0),2);
}
}


int i;
int x;
int y;
/**
游戏结束
**/
void game_over()
{
lcd_printWord(" Game over!!! ",60,120,RGB(0,0,0),RGB(255,255,255));
choice_do = 1;
sn_t.life = 0;
sn_t.x[sn_t.node] = 30;
sn_t.y[sn_t.node] = 100;

}




/**
游戏产生食物
**/
void game_create_food()
{
int iRight = 0;
if (fd_t.yes == 1)
{
fd_t.x = rand()%22 * 10 + 5;
fd_t.y = rand()%23 * 10 + 5;

loop: while (!iRight)
{
for (i = sn_t.node;i > 0;i--)
{
if (fd_t.x == sn_t.x[i] && fd_t.y == sn_t.y[i])
{
fd_t.x = rand()%22 * 10 + 5;
fd_t.y = rand()%23 * 10 + 5;
goto loop;
}
}
iRight = 1;
}
game_setPoint(fd_t.x,fd_t.y,1);
fd_t.yes = 0;
}
}


/**
蛇捕获食物
**/
void game_eat_food()
{
if (sn_t.x[sn_t.node - 1] == fd_t.x && sn_t.y[sn_t.node - 1] == fd_t.y)
{
sn_t.node++;

fd_t.yes = 1;
for (i = sn_t.node;i > 0;i-- )
{
sn_t.x[i] = sn_t.x[i - 1];
sn_t.y[i] = sn_t.y[i - 1];
}

}
}




/**
蛇移动的主要程序
**/
void game_move_handler()
{
if (sn_t.life == 1)
{
//xrect(worm_x,worm_y,5,5,RGB(255,255,255),RGB(255,255,255),2);
game_create_food();
//lcd_SetColor(RGB(255,0,0));
//xrect(x,y,ilong,5,RGB(255,255,255),RGB(255,255,255),2);
if (goDir == 1)//上
{
sn_t.x[sn_t.node] = sn_t.x[sn_t.node - 1];
sn_t.y[sn_t.node] = sn_t.y[sn_t.node - 1] - 5;
}
if (goDir == 2)//下
{
sn_t.x[sn_t.node] = sn_t.x[sn_t.node - 1];
sn_t.y[sn_t.node] = sn_t.y[sn_t.node - 1] + 5;
}
if (goDir == 3)//左
{
sn_t.x[sn_t.node] = sn_t.x[sn_t.node - 1] - 5;
sn_t.y[sn_t.node] = sn_t.y[sn_t.node - 1];
}
if (goDir == 4)//右
{
sn_t.x[(sn_t.node)] = sn_t.x[(sn_t.node - 1)] + 5;
sn_t.y[(sn_t.node)] = sn_t.y[(sn_t.node - 1)];
}

game_setPoint(sn_t.x[0],sn_t.y[0],0);
for (i = 0;i < sn_t.node;i++)
{
sn_t.x[i] = sn_t.x[i + 1];
sn_t.y[i] = sn_t.y[i + 1];
game_setPoint(sn_t.x[i],sn_t.y[i],1);
}
}
game_eat_food();
//while()
//触碰自身,游戏结束
for (i = 0;i < (sn_t.node-1);i++)
{
if (sn_t.x[i] == sn_t.x[sn_t.node] && sn_t.y[sn_t.node] == sn_t.y[i])
{
game_over();
}
}
//触碰障碍物,游戏结束
if(sn_t.x[sn_t.node] == 0 || sn_t.x[sn_t.node] == 240 || sn_t.y[sn_t.node] == 0 || sn_t.y[sn_t.node] == 240)
{
game_over();
}

}


/**
游戏开始,一些系数的初始化
**/
void game_start()
{


sn_t.life = 1;
sn_t.node = 5;
sn_t.x[4] = 30;
sn_t.y[4] = 100;
fd_t.yes = 1;
goDir = 4;
//srand((int)time(0));
game_clear();
for (i = 4;i >= 0;i--)
{
game_setPoint(sn_t.x[i],sn_t.y[i],1);
sn_t.x[i - 1] = sn_t.x[i] - 5;
sn_t.y[i -1 ] = sn_t.y[i];
}

timer_create(timer0,249,Mux4,1,(10000 - iHard * 4000),game_move_handler);
timer_start(timer0);

}






/**
游戏设置界面
**/
void game_setHard()
{
choice_do = 2;
game_clear();
lcd_printWord(" 初级 ",100,40,RGB(0,0,0),RGB(255,255,255));
lcd_printWord(" 普通 ",100,80,RGB(0,0,0),RGB(255,255,255));
lcd_printWord(" 困难 ",100,120,RGB(0,0,0),RGB(255,255,255));
}


/**
点击触屏后进入的时钟函数
**/
void game_choice_handler()
{

if (choice_do == 2)
{
if (click_x >= 100 && click_x <= (100 + 48) && click_y >= 40 && click_y <= (40 + 16))
{
iHard = 0;
sn_t.iSpeed = 10000;
lcd_printWord(" 初级 ",10,210,RGB(0,0,0),RGB(255,255,255));
}
if (click_x >= 100 && click_x <= (100 + 48) && click_y >= 80 && click_y <= (80 + 16))
{
iHard = 1;
sn_t.iSpeed = 6000;
lcd_printWord(" 普通 ",10,210,RGB(0,0,0),RGB(255,255,255));
}
if (click_x >= 100 && click_x <= (100 + 48) && click_y >= 120 && click_y <= (120 + 16))
{
iHard = 2;
sn_t.iSpeed = 3000;
lcd_printWord(" 困难 ",10,210,RGB(0,0,0),RGB(255,255,255));
}
//game_clear();
//choice_do = 1;
}
if ((choice_do == 1 || choice_do == 2) && click_x >= 40 && click_x <= (40 + 48) && click_y >= 270 && click_y <= (270 + 16))
{
choice_do = 3;
game_start();

}
//lcd_printWord(" 结束 ",10,290,RGB(0,0,0),RGB(255,255,255));
if (choice_do == 0 && click_x >= 10 && click_x <= (10 + 48) && click_y >= 290 && click_y <= (290 + 16))
{
choice_do = 0;
game_over();
}
//lcd_printWord(" 难度 ",10,250,RGB(0,0,0),RGB(255,255,255));
if (choice_do == 1 && click_x >= 10 && click_x <= (10 + 48) && click_y >= 250 && click_y <= (250 + 16))
{
choice_do = 0;
game_setHard();
}

else if (choice_do == 0)
{
/************************************************************************/
/* //xcircle(180,260,15,RGB(255,255,255),RGB(0,255,0),1);//上
xrect(170,250,22,22,RGB(255,255,255),RGB(0,255,0),1);
lcd_printWord("↑",173,253,RGB(255,255,255),RGB(0,0,0));
//printWord("↓←→",180,255,RGB(0,0,0),RGB(255,255,255));  
//xcircle(180,295,15,RGB(255,255,255),RGB(0,255,0),1); //下
xrect(170,285,22,22,RGB(255,255,255),RGB(0,255,0),1);
lcd_printWord("↓",173,288,RGB(255,255,255),RGB(0,0,0));                                                                     */
/************************************************************************/
if (click_x >= 170 && click_x <= (170 + 22) && goDir != 1 && goDir != 2)
{
if (click_y >= 250 && click_y <= (250 + 22)) 
{
goDir = 1;
}
else if (click_y >= 280 && click_y <= (280 + 22))
{
goDir = 2;
}
}
/************************************************************************/
/* //xcircle(150,280,15,RGB(255,255,255),RGB(0,255,0),1);//左
xrect(140,270,22,22,RGB(255,255,255),RGB(0,255,0),1);
lcd_printWord("←",143,273,RGB(255,255,255),RGB(0,0,0));
//xcircle(210,280,15,RGB(255,255,255),RGB(0,255,0),1);//右
xrect(200,270,22,22,RGB(255,255,255),RGB(0,255,0),1);
lcd_printWord("→",203,273,RGB(255,255,255),RGB(0,0,0));                                                                     */
/************************************************************************/
if (click_y >= 270 && click_y <= (270 + 22) && goDir != 3 && goDir != 4)
{
if (click_x >= 140 && click_x <= (140 + 22))//140
{
goDir = 3;
}
else if (click_x >= 200 && click_x <= (200 + 22))//200
{
goDir = 4;
}
}
}
}

  • 0
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值