gpt4 book ai didi

java - 用 Java Applet 制作一条蛇

转载 作者:行者123 更新时间:2023-12-01 14:40:50 25 4
gpt4 key购买 nike

所以我试图重现这个图形程序 here对于我的 java 类。

这是我到目前为止所想到的:

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. 我是一个无能的程序员:/

请大家给我一些建议,告诉我我可能做错了什么或根本没有做错。谢谢!

最佳答案

问题是您在此处同时设置所有值:

//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 个元素,并将其他元素移动 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>

关于java - 用 Java Applet 制作一条蛇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15981236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com