网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 交易代码 > Java精品代码 > 正文

用java编写的小游戏源代码分析

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务
t-1);
   }
   
   public void updateScore(){
    String s = "Score: " + snakeModel.score;
    labelScore.setText(s);
   }
  
   void begin(){
    if (snakeModel == null || !snakeModel.running){
     snakeModel = new SnakeModel(this,
                   canvasWidth/nodeWidth,
                   canvasHeight/nodeHeight);
     (new Thread(snakeModel)).start();
    }
   }
  
   public static void main(String[] args){
    GreedSnake gs = new GreedSnake();
   }
  }
  
  ///////////////////////////////////////////////////
  // 文件2
  ///////////////////////////////////////////////////
  
  import java.util.*;
  import javax.swing.*;
  
  class SnakeModel implements Runnable{
   GreedSnake gs;
   boolean[][] matrix;
   LinkedList nodeArray = new LinkedList();
   Node food;
   int maxX;
   int maxY;
   int direction = 2;
   boolean running = false;
  
   int timeInterval = 200;
   double speedChangeRate = 0.75;
   boolean paused = false;
   
   int score = 0;
   int countMove = 0;
  
   // UP and DOWN should be even
   // RIGHT and LEFT should be odd
   public static final int UP = 2;
   public static final int DOWN = 4;
   public static final int LEFT = 1;
   public static final int RIGHT = 3;
  
   public SnakeModel(GreedSnake gs, int maxX, int maxY){
    this.gs = gs;
    this.maxX = maxX;
    this.maxY = maxY;
  
    // initial matirx
    matrix = new boolean[maxX][];
    for(int i=0; i<maxX; ++i){
     matrix[i] = new boolean[maxY];
     Arrays.fill(matrix[i],false);
    }
  
    // initial the snake
    int initArrayLength = maxX > 20 ? 10 : maxX/2;
    for(int i = 0; i < initArrayLength; ++i){
     int x = maxX/2+i;
     int y = maxY/2;
     nodeArray.addLast(new Node(x, y));
     matrix[x][y] = true;
    }
  
    food = createFood();
    matrix[food.x][food.y] = true;
   }
  
   public void changeDirection(int newDirection){
    if (direction % 2 != newDirection % 2){
     direction = newDirection;
    }
   }
  
   public boolean moveOn(){
    Node n = (Node)nodeArray.getFirst();
    int x = n.x;
    int y = n.y;
  
    switch(direction){
     case UP:
      y--;
      break;
     case DOWN:
      y++;
      break;
     case LEFT:
      x--;
      break;
     case RIGHT:
      x++;
      break;
    }
  
    if ((0 <= x && x < maxX) && (0 <= y && y < maxY)){
     if (matrix[x][y]){
      if(x == food.x && y == food.y){
       nodeArray.addFirst(food);
       
       int scoreGet = (10000 - 200 * countMove) / timeInterval;
       score += scoreGet > 0? scoreGet : 10;
       countMove = 0;
       
       food = createFood();
       matrix[food.x][food.y] = true;
       return true;
      }
      else
       return false;
     }
     else{
      nodeArray.addFirst(new Node(x,y));
      matrix[x][y] = true;
      n = (Node)nodeArray.removeLast();
      matrix[n.x][n.y] = false;
      countMove++;
      return true;


文章转载自网管网:http://www.bitscn.com/plus/view.php?aid=21018

  • 下一篇资讯: 使用非JAVA代码
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师