140行,搞定贪吃蛇(Java Applet)

/**************************************************************************
 *             projectG all right reserved       作者:韩景维                              *
 *             交互精彩,尽在G计划                                                                    *
 *             http:// www.projectG.cn                                                                   *
 **************************************************************************/
package mysnake;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class snake extends Applet implements Runnable, KeyListener {
  private boolean[][] m_link;
  private boolean m_over, otherWayCondition;
  private int mAI_X, mAI_Y;
  private char mCtrlWay;
  private int mCount;
  private perXY m_snakeXY;
  private Stack stk;
  private String msg;
  public void init() {
    for (int y = 0; y < 30; y++)
      for (int x = 0; x < 30; x++)
        m_link[y][x] = false;
    mAI_X = (int) (5 + Math.random() * 15);
    mAI_Y = (int) (5 + Math.random() * 15);
    m_link[mAI_Y][mAI_X] = true;
    stk=new Stack();
    stk.add(0,m_snakeXY);
    m_snakeXY.y = (int) (10 + Math.random() * 10);
    m_snakeXY.x = (int) (10 + Math.random() * 10);
    m_link[m_snakeXY.y][m_snakeXY.x] = true;
    m_over = false;
    mCtrlWay = 's';
    otherWayCondition = false;
    mCount = 0;
    addKeyListener(this);
    msg="";
    Thread t = new Thread(this);
    t.start();
    t = null;
  }
  public void paint(Graphics g) {
    for (int y = 0; y < 30; y++)
      for (int x = 0; x < 30; x++)
        if (m_link[y][x] == true)
          g.drawRect(x * 10, y * 10, 10, 10);
    g.drawString("ÄãÄ¿Ç°µÄ»ý·Ö£º" + mCount+"  "+msg, 10, 10);
  }
  public snake() {
    m_link = new boolean[30][30];
    m_snakeXY = new perXY();
  }
  public void checkCount(){
    if(mCount<10){
      msg = "Game Over! you play so bad!";
    }else if(mCount>=10 && mCount<20){
      msg = "Game Over! just so so!";
    }else{
      msg="Game Over! you play great";
    }
  }
  public void keyPressed(KeyEvent e) {
  }
  public void keyReleased(KeyEvent e) {
  }
  public void keyTyped(KeyEvent e) {
    if ((e.getKeyChar() == 'a' || e.getKeyChar() == 'A' || e.getKeyChar() == 'd' || e.getKeyChar() == 'D') && !otherWayCondition) {
      otherWayCondition = true;
      mCtrlWay = e.getKeyChar();
    }
    if ((e.getKeyChar() == 'w' || e.getKeyChar() == 'W' || e.getKeyChar() == 's' || e.getKeyChar() == 'S') && otherWayCondition) {
      otherWayCondition = false;
      mCtrlWay = e.getKeyChar();
    }
    repaint();
  }
  public void run() {
    while (!m_over) {
      try {
        Thread.sleep(200);
      }
      catch (Exception ee) {}
      perXY temp=(perXY)stk.get(0);
      int tempx=temp.x;
      int tempy=temp.y;
      if (tempy == mAI_Y && tempx == mAI_X) {
        mCount++;
        perXY te=new perXY();
        te.x=mAI_X;
        te.y=mAI_Y;
        stk.add(0,te);
        m_link[mAI_Y][mAI_X] = false;
        mAI_X = (int) (3 + Math.random() * 25);
        mAI_Y = (int) (3 + Math.random() * 25);
        m_link[mAI_Y][mAI_X] = true;
      }
      for(int i=0;i<stk.size();i++){
        perXY te2=(perXY)stk.get(i);
        m_link[te2.y][te2.x] = false;
      }
      if (mCtrlWay == 's' || mCtrlWay == 'S') {
        tempy++;
      }else if (mCtrlWay == 'w' || mCtrlWay == 'W') {
        tempy--;
      }else if (mCtrlWay == 'a' || mCtrlWay == 'A') {
        tempx--;
      }else if (mCtrlWay == 'd' || mCtrlWay == 'D') {
        tempx++;
      }
      perXY perxy=new perXY();
      perxy.x=tempx;
      perxy.y=tempy;
      stk.add(0,perxy);
      stk.pop();
      perXY ck=(perXY)stk.get(0);
      for(int i=0;i<stk.size();i++){
        perXY te2=(perXY)stk.get(i);
        m_link[te2.y][te2.x] =true;
        if(i>3)
          if (ck.x == te2.x && ck.y == te2.y) {
            m_over = true;
            checkCount();
          }
      }
      if (tempx<= 0 || tempx >= 29 || tempy <= 0 || tempy >= 29) {
        m_over = true;
        checkCount();
      }
      requestFocus();
      this.repaint();
    }
  }
}
class perXY {
  public int x;
  public int y;
}
  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

flashmagic

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值