本 科 课 程 设 计
贪吃蛇游戏设计
摘要 .................................................................................................................................................. 3 Abstract ............................................................................................................................................. 3 一.详细设计 ................................................................................................................................... 4
1.属性解释 ................................................................................................................................ 4 2.初始化坐标: ........................................................................................................................ 4 3.调用线程 ................................................................................................................................ 4 4.具体功能实现: .................................................................................................................... 5
4.1.移动功能的实现: ..................................................................................................... 5 4.2.吃食物功能实现: ..................................................................................................... 6 4.3.判断游戏是否结束的功能实现: ............................................................................. 8 4.4.画图方法的实现 ......................................................................................................... 9 4.5.监听键盘动作: ....................................................................................................... 10 5.界面的设计: ...................................................................................................................... 11
5.1进入程序界面设计 ................................................................................................... 11 5.2第2个界面的设计: ............................................................................................... 12 5.3第3个界面设计: ................................................................................................... 12 6.把程序植入手机中 .............................................................................................................. 13 二.字符设备驱动程序实现 ......................................................................................................... 13 三.总结:..................................................................................................................................... 14 四.参考文献: ............................................................................................................................. 14 五. 致谢....................................................................................................................................... 15
摘要
程序核心思想:运行程序首界面(进入游戏,退出游戏)进入游戏后选择难度(简单,普通,高难)根据不同的难度设置相应的属性。进入游戏界面。首先初始化蛇的坐标,食物的坐标,和障碍物的坐标。然后获取当前屏幕的大小为将来的边界判断做准备。然后创建并执行线程。 线程基本流程:判断是否是暂停阶段,如果没有暂停就执行:eatFood();move(direction); repaint();如果游戏结束了就重新游戏或者退出。
Abstract
Core of the process: first running the program interface (into the game, withdraw from the game) into the game after the difficult choice of (simple, ordinary, highly difficult) depending on the difficulty of setting the corresponding attribute. Into the game interface. First initialize the coordinates of the snake, coordinates food, and the coordinates of obstructions. And access to the size of the current screen for the future of the border to prepare for judgement. And then create and execution threads. Thread the basic process: determine whether the stage is suspended, if not the moratorium on the implementation of: eatFood (); move (direction); repaint (); game to an end if the game or out on the re.
一.详细设计
1.属性解释
用1个2维数组snake[200][2]存放蛇身坐标,第2维是0的时候代表横坐标,为1的时候为纵坐标。第1维数字代表蛇身方块的数字,例如snake[0][0] 就代表蛇头的横坐标snake[1][1]就代表第2个方块的总坐标。snakeNum为蛇的长度(以方格为单位)。SNAKEWIDTH为方格大小。direction;为移动方向,zhangai0x,zhangai1x,zhangai2x,zhangai3x,zhangai0y,zhangai1y, zhangai2y,zhangai3y,为障碍物的坐标。foodx,foody为食物的坐标。isPaused为判定是否是暂停状态,true为暂停false为非暂停。A为难度系数,a可以为1,2,3。1的时候最难,3的时候最简单。SLEEP_TIME为系统沉睡时间,其值越小速度越快。
2.初始化坐标:
isPaused = true; // 设置游戏开始为暂停状态 snakeNum = 7; //设置蛇身长度7个方格
// 循环依次初始化蛇身没个方格的坐标,蛇头的坐标为(100,40) for(int i = 0;i < snakeNum;i++)
{
snake[i][0] = 100 - SNAKEWIDTH * i; snake[i][1] = 40; }
//初始化移动方向为向右
direction = DIRECTION_RIGHT; //初始化食物坐标 foodX = 100; foodY = 100;
//初始化障碍物的坐标,其位置环绕挡住食物, zhangai0x=foodX+4*a; zhangai0y=foodY+4*a; zhangai1x=foodX+4*a; zhangai1y=foodY-4*a; zhangai2x=foodX-4*a; zhangai2y=foodY+4*a; zhangai3x=foodX-4*a; zhangai3y=foodY-4*a;
3.调用线程
thread = new Thread(this); thread.start();
while (isRun) //开始时间 {
if(!isPaused)
{
eatFood(); //吃食物
move(direction); //移动 if(gameover) //如果游戏结束
{
isRun = false; restart(); break;
} b = !b; //闪烁控制 }
repaint(); //重新画图 Thread.sleep(SLEEP_TIME); }
开始创建并执行线程,如果没有暂停就执行eatFood();move(direction);然后判断游戏是否结束,然后重新画图repaint();然后等待SLEEP_TIME时间继续循环。
4.具体功能实现: 4.1.移动功能的实现:
private void move(int direction) {
//除蛇头外的蛇身坐标移动
for(int i = snakeNum - 1;i > 0;i--) {
snake[i][0] = snake[i - 1][0]; snake[i][1] = snake[i - 1][1]; }
//蛇头坐标的移动 switch(direction)
{
case DIRECTION_UP:
snake[0][1] = snake[0][1] - SNAKEWIDTH; break;
case DIRECTION_DOWN:
snake[0][1] = snake[0][1] + SNAKEWIDTH; break;
case DIRECTION_LEFT:
snake[0][0] = snake[0][0] - SNAKEWIDTH;