用javaapplet制作蛇
所以我试图为我的java类重现这个图形程序 这就是我到目前为止的想法:用javaapplet制作蛇,java,graphics,processing,Java,Graphics,Processing,所以我试图为我的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
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);
}
}
}
import processing.core.PApplet;
公共类赋值09b扩展了PApplet{
//创建数组以存储鼠标的x和y值
int[]xArray=新int[100];
int[]雅雷=新int[100];
公共作废设置(){
//以每秒60帧的速度运行
大小(500500);
}
公众抽签(){
//每帧更改背景
背景(0);
//将鼠标的x和y值存储在数组中
for(int i=0;i
关于问题可能是什么,我有一些想法:
伙计们,请你们给我一些建议,看看我到底做错了什么,或者根本没做错什么。谢谢大家! 问题是您在此处同时设置了所有值:
//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;
}
这使得完整的列表:
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
}
}
import processing.core.PApplet;
公共类赋值09b扩展了PApplet{
//创建数组以存储鼠标的x和y值
int[]xArray=新int[100];
int[]雅雷=新int[100];
公共作废设置(){
//以每秒60帧的速度运行
大小(500500);
}
公众抽签(){
//每帧更改背景
背景(0);
更新耳环(mouseX、mouseY);
//应该使用一系列椭圆打印出蛇
for(int i=0;i
因为这是一个练习,我建议更多地使用for循环和数组。
这是你最终会大量使用的东西,值得练习/掌握诀窍。
祝你好运
var xArray=新数组(100);
var yArray=新阵列(100);
函数设置(){
createCanvas(500500);
}
函数绘图(){
//每帧更改背景
背景(0);
更新耳环(mouseX、mouseY);
//应该使用一系列椭圆打印出蛇
对于(变量i=0;i
非常感谢您!是的,我肯定需要更深入地探索数组和循环。
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
}
}