用Java Applet制作一条蛇

时间:2013-04-12 21:35:53

标签: java graphics processing

所以我正在尝试为我的java类重现这个图形程序here

这是我到目前为止所提出的:

import processing.core.PApplet;

public class Assignment09b extends PApplet {

// Create arrays to stort the x & y values of the mouse
int [] xArray = new int [100];
int [] yArray = new int [100];

public void setup(){
    //Runs at 60Fps

    size(500, 500);
}

public void draw(){
    //Changes the background each frame
    background(0);

    //Stores the x&y values of the mouse in the arrays
    for (int i = 0; i < xArray.length; i++){
        xArray[i] = this.mouseX;
        yArray[i] = this.mouseY;
    }

    //SHOULD print out the snake using series of ellipses
    for (int i = 0; i < xArray.length; i++){

        //Generates a random color each time
        fill(random(255), random(255), random(255));
        ellipse(xArray[i], yArray[i], 25, 25);
    }

}
   }

我对这个问题可能有一些想法:

  1. 因为我在一个循环中,它只是很快产生一个椭圆,但我不知道如何让它同时产生它们
  2. 这可能是帧率速度的问题?
  3. 我是一个不称职的程序员:/
  4. 请伙计们,请你给我一些建议,告诉我可能做错了什么或根本没做错。谢谢!

1 个答案:

答案 0 :(得分:1)

问题是你在这里同时设置所有值:

//Stores the x&y values of the mouse in the arrays
    for (int i = 0; i < xArray.length; i++){
        xArray[i] = this.mouseX;
        yArray[i] = this.mouseY;
    }

你只想更新数组中的1个元素并让其他元素移动一个:“最旧的”值熄灭,新的值进入。你可以手动使用反向for循环(从数组的末尾开始)除了第一个元素之外的前面)或使用arrayCopy

private void updateArrays(int x,int y){
  arrayCopy(xArray, 0, xArray, 1, xArray.length-1);//shift all elements backwards by 1
  arrayCopy(yArray, 0, yArray, 1, yArray.length-1);//so x at index 99 goes 98, 98 to 97, etc. excepting index 0
  xArray[0] = x;//finally add the newest value 
  yArray[0] = y;//at the start of the array (so in the next loop it gets shifted over by 1) like the above values
}

制作完整列表:

import processing.core.PApplet;

public class Assignment09b extends PApplet {
  // Create arrays to stort the x & y values of the mouse
  int [] xArray = new int [100];
  int [] yArray = new int [100];

  public void setup(){
      //Runs at 60Fps

      size(500, 500);
  }

  public void draw(){
      //Changes the background each frame
      background(0);

      updateArrays(mouseX,mouseY);

      //SHOULD print out the snake using series of ellipses
      for (int i = 0; i < xArray.length; i++){

          //Generates a random color each time
          fill(random(255), random(255), random(255));
          ellipse(xArray[i], yArray[i], 25, 25);
      }

  }

  private void updateArrays(int x,int y){
    arrayCopy(xArray, 0, xArray, 1, xArray.length-1);//shift all elements backwards by 1
    arrayCopy(yArray, 0, yArray, 1, yArray.length-1);//so x at index 99 goes 98, 98 to 97, etc. excepting index 0
    xArray[0] = x;//finally add the newest value 
    yArray[0] = y;//at the start of the array (so in the next loop it gets shifted over by 1) like the above values
  }
}

由于这是一个练习,我建议更多地使用for循环和数组。 这将是你最终会使用相当多的东西,值得练习/掌握。 古德勒克!

var xArray = new Array(100);
var yArray = new Array(100);

function setup(){
  createCanvas(500, 500);
}

function draw(){
    //Changes the background each frame
    background(0);

    updateArrays(mouseX,mouseY);

    //SHOULD print out the snake using series of ellipses
    for (var i = 0; i < xArray.length; i++){

        //Generates a random color each time
        fill(random(255), random(255), random(255));
        ellipse(xArray[i], yArray[i], 25, 25);
    }

}

function updateArrays(x,y){
  arrayCopy(xArray, 0, xArray, 1, xArray.length-1);//shift all elements backwards by 1
  arrayCopy(yArray, 0, yArray, 1, yArray.length-1);//so x at index 99 goes 98, 98 to 97, etc. excepting index 0
  xArray[0] = x;//finally add the newest value 
  yArray[0] = y;//at the start of the array (so in the next loop it gets shifted over by 1) like the above values
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.4/p5.min.js"></script>