贪吃蛇的算法分析(4) (转)

贪吃蛇的算法分析(4) (转)[@more@]

  XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />
James @ www.chenshen.com

WormPit

WormPit类中包括了Worm和WormFood。贪吃蛇将会在画面中移动寻找食物。如果它吃到食物它将会长一格。如果它碰到边界或者吃到自己将Game Over。

下面介绍几个重要的函数

private void paintPitContents(Graphics g)

重绘屏幕上的所有元素

// 更新贪吃蛇的状态

    myWorm.update(g); 

    // 头部的位置和食物的位置重合就吃到食物

    if (myFood.isAt(myWorm.getX(), myWorm.getY())) {

  myWorm.eat();

  SCOre += level;

  foodEaten++;

  if (foodEaten > (level << 1)) {

    /* 增加游戏难度 */

    forceRedraw = true;

    foodEaten = 0;

    level++;

    if (toneplayer != null) {

  try {

    tonePlayer.setMediaTime(0);

    tonePlayer.start();

  } catch (MediaException me) { }    }

  } else {

    if (audioPlayer != null) {

  try {

    Manager.playTone(69, 50, 100);  // Play audio

  } catch (MediaException me) { }    }  }

  g.setColor(WormPit.ERASE_COLOUR);

  // 填充长方形(三个字的宽度)

  g.fillRect((width - (SCORE_CHAR_WIDTH * 3))-START_POS,

    height-START_POS,

    (SCORE_CHAR_WIDTH * 3),

    SCORE_CHAR_HEIGHT);

  g.setColor(WormPit.DRAW_COLOUR);

  // 显示新的分数

  g.drawString("" + score,

    width - (SCORE_CHAR_WIDTH * 3) - START_POS,

    height - START_POS, Graphics.TOP|Graphics.LEFT);

  // 重新生成食物

  myFood.regenerate();

  int x = myFood.getX();

  int y = myFood.getY();

  while (myWorm.contains(x, y)) {

  // 如果食物和贪吃蛇的身体重复就重新生成

    myFood.regenerate();

    x = myFood.getX();  y = myFood.getY();  }    }

    // 画出食物

    myFood.paint(g);

  } catch (WormException se) {  gameOver = true; }

public void run()

主循环体:

while (!gameDestroyed) { // 游戏不终止就一直循环执行

    try {

  synchronized (myWorm) { // 多线程中要进行同步

  // 如果游戏结束

    if (gameOver) {

  if (WormScore.getHighScore(level) < score) {

    // 把最高分保存

    WormScore.setHighScore(level, score, "me");  }

  if ((audioPlayer != null) &&

    (audioPlayer.getState() == Player.STARTED)) {

    try {

  audioPlayer.stop();

  Manager.playTone(60, 400, 100);

    } catch (Exception ex) { }  }

  // 重绘

  repaint();

  // 游戏结束时等待用户重新开始

  myWorm.wait(); 

    } else if (gamePaused) {

    //重绘

  repaint();

  // 游戏暂停时等待用户重新开始

  myWorm.wait();

    } else {

    // 游戏继续

    myWorm.moveOnUpdate();

  repaint();

  // 这里的等待时间决定了游戏难度!!!

  myWorm.wait(DEFAULT_WAIT-(level*40));

    }

  }

    } catch (Java.lang.InterruptedException ie) {

    }

  }

关于作者:

沈晨,高级程序员,SCJP

www.chenshen.com

Mailto:JinaShen@BenQ.com" rel="nofollow">JinaShen@BenQ.com

2003" Day="10" Month="8">August 10, 2003



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-960869/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-960869/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值