首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java小程序制作蛇

用Java小程序制作蛇
EN

Stack Overflow用户
提问于 2013-04-12 21:35:53
回答 1查看 483关注 0票数 0

因此,我试图为我的java类复制这个图形程序这里

这就是我到目前为止想出的:

代码语言:javascript
复制
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. 我是一个无能的程序员:/

拜托,伙计们,你们能不能给我一些建议,说明我可能做错了什么,或者根本没有做错。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-12 23:24:43

问题是,您在这里同时设置所有的值:

代码语言:javascript
复制
//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

代码语言:javascript
复制
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
}

这使得完整的清单:

代码语言:javascript
复制
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循环和数组。这是一件你最终会大量使用的东西,值得你练习/掌握它的诀窍。祝你好运!

代码语言:javascript
复制
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
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.4/p5.min.js"></script>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15981236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档