问题描述
我正在完成一个学校的期末项目,我对制作的内容充满了创造力。我决定制作一个简单的 Conway 模拟器,它的底部有几个按钮。一个播放/暂停,一个步进,一个清除,一个改变速度。它是在处理过程中制作的,我试过实现速度按钮,但每当我点击它时,它总是恢复到 1 倍速度,即使这不是预期的行为。我的代码如下(处理 3):
void setup() {
size(550,615);
pixelDensity(2);
frameRate(60);
}
//initial array,set as 2 glider guns ----------------------------
boolean[][] colour = {{false,false,false},{false,true,false}
};
//----------------------------------------------------------------
//game variables -------------------------------------------------
int xposGame = 25;
int yposGame = 25;
int[] arraySize = {50,50};
int slowDown = 4;
int speedMultiplier = slowDown;
//----------------------------------------------------------------
//play button variables ------------------------------------------
int xposPlay = xposGame;
int yposPlay = 550;
int playWidth = 40;
boolean playActive = false;
//----------------------------------------------------------------
//clear button variables -----------------------------------------
int clearWidth = 40;
int xposClear = 485;
int yposClear = 550;
//----------------------------------------------------------------
//step button variables ------------------------------------------
int stepWidth = 40;
int xposstep = xposGame + xposPlay + 25;
int yposstep = 550;
//----------------------------------------------------------------
//speed multiplier button variables ------------------------------
int speedWidth = 40;
int xposspeed = xposGame + xposPlay + xposstep;
int yposspeed = 550;
//----------------------------------------------------------------
//clear the board ------------------------------------------------
void cleararr(){
for(int i = 0; i < arraySize[0]; i++){
for(int j = 0; j < arraySize[1]; j++){
colour[i][j] = false;
}
}
}
//----------------------------------------------------------------
//iteration function ---------------------------------------------
void iteration(){
//counter for # of neighboring cells
int neighborNum = 0;
//next generations array
boolean[][] newColour = new boolean[arraySize[0]][arraySize[1]];
//read the current generation's array
for(int i = 0; i < arraySize[0]; i++){
for(int j = 0; j < arraySize[1]; j++){
//search all values around the cell (excluding those outside the array) and count how many neighbors are alive
if(i != -0){
if(j != 0){
if(colour[i-1][j-1]==true){neighborNum++;}
}
if(colour[i-1][j]==true){neighborNum++;}
if(j != arraySize[1]-1){
if(colour[i-1][j+1]==true){neighborNum++;}
}
}
if(j != 0){
if(colour[i][j-1]==true){neighborNum++;}
}
if(j != arraySize[1]-1){
if(colour[i][j+1]==true){neighborNum++;}
}
if(i != arraySize[0]-1){
if(j != 0){
if(colour[i+1][j-1]==true){neighborNum++;}
}
if(colour[i+1][j]==true){neighborNum++;}
if(j != arraySize[1]-1){
if(colour[i+1][j+1]==true){neighborNum++;}
}
}
//check if the current cell is alive. if it has 2-3 neighbors,it survives,else it doesnt
if(colour[i][j]==true){
if(neighborNum==3||neighborNum==2){
newColour[i][j] = true;
}
else{
newColour[i][j] = false;
}
}
//check if the current cell is dead. if it has 3 neighbors,birth it next round
if(colour[i][j]==false){
if(neighborNum==3){
newColour[i][j] = true;
}
else{
newColour[i][j] = false;
}
}
//set the number of neighbors on the current cell to 0 for the next cell
neighborNum = 0;
}
}
//set the new array to be the current array
colour = newColour;
}
//----------------------------------------------------------------
//iteration drawing function -------------------------------------
void drawIteration(){
for(int i = 0; i < arraySize[0]; i++){
for(int j = 0; j < arraySize[1]; j++){
if(colour[i][j] == true){
fill(255);
}
else{fill(0);}
strokeWeight(2);
stroke(50);
rect(10*j+xposGame,10*i+yposGame,10,10);
}
}
}
//----------------------------------------------------------------
//play/pause button function -------------------------------------
void drawPlay(){
nostroke();
fill(0);
rect(xposPlay,yposPlay,playWidth,5);
fill(255);
if(!playActive){
triangle(xposPlay + (playWidth/3),yposPlay + (playWidth/3.3),xposPlay + (playWidth/3),yposPlay + playWidth - (playWidth/3.3),xposPlay + (playWidth*3.5/5),yposPlay + (playWidth/2));
}
if(playActive){
rect(xposPlay + (playWidth/3),playWidth/9,playWidth-(2*(playWidth/3.3)),2);
rect(xposPlay + playWidth - (playWidth/3) - (playWidth/9),2);
}
}
//----------------------------------------------------------------
//clear button function ------------------------------------------
void drawClear(){
nostroke();
fill(0);
rect(xposClear,yposClear,clearWidth,5);
strokeWeight(clearWidth/9);
stroke(255);
line(xposClear+clearWidth/3,yposClear + clearWidth/3,xposClear + 2 * clearWidth/3,yposClear + 2 * clearWidth/3);
line(xposClear+clearWidth/3,yposClear + 2 * clearWidth/3,yposClear + clearWidth/3);
}
//----------------------------------------------------------------
//step button function -------------------------------------------
void drawStep(){
nostroke();
fill(0);
rect(xposstep,yposstep,stepWidth,5);
strokeWeight(stepWidth/9);
stroke(255);
line(xposstep + stepWidth/3,yposstep + stepWidth/3,xposstep + (stepWidth*3.5/5),yposstep + (stepWidth/2));
line(xposstep + stepWidth/3,yposstep + 2*stepWidth/3,yposstep + (stepWidth/2));
}
//----------------------------------------------------------------
//speed multiplier button ----------------------------------------
void drawSpeed(){
nostroke();
fill(0);
rect(xposspeed,yposspeed,speedWidth,5);
fill(255);
textAlign(CENTER);
textSize(25);
if(slowDown == 12){
text("1x",xposspeed + (speedWidth/2),yposspeed + (speedWidth/1.4));
}
if(slowDown == 6){
text("2x",yposspeed + (speedWidth/1.4));
}
if(slowDown == 4){
text("3x",yposspeed + (speedWidth/1.4));
}
if(slowDown == 3){
text("4x",yposspeed + (speedWidth/1.4));
}
}
//----------------------------------------------------------------
//all mousepressed events ----------------------------------------
void mousepressed(){
//play button
if(mouseX > xposPlay && mouseX < xposPlay + playWidth && mouseY > yposPlay && mouseY < yposPlay + playWidth){
playActive = !playActive;
}
//clear button
if(mouseX > xposClear && mouseX < xposClear + clearWidth && mouseY > yposClear && mouseY < yposClear + clearWidth){
cleararr();
playActive = false;
}
//step button
if(mouseX > xposstep && mouseX < xposstep + stepWidth && mouseY > yposstep && mouseY < yposstep + stepWidth){
iteration();
playActive = false;
}
//speed multiplier button
if(mouseX > xposspeed && mouseX < xposspeed + speedWidth && mouseY > yposspeed && mouseY < yposspeed + speedWidth){
if(slowDown == 12){
slowDown = 6;
}
if(slowDown == 6){
slowDown = 4;
}
if(slowDown == 4){
slowDown = 3;
}
if(slowDown == 3){
slowDown = 2;
}
if(slowDown == 2){
slowDown = 12;
}
}
//adding cells
if(!playActive && mouseX > xposGame && mouseX < xposGame + 10*arraySize[0] && mouseY > yposGame && mouseY < yposGame + 10*arraySize[1]){
colour[(mouseY-yposGame)/10][(mouseX-xposGame)/10] = !colour[(mouseY-yposGame)/10][(mouseX-xposGame)/10];
}
}
//----------------------------------------------------------------
void draw() {
background(50);
nostroke();
fill(50);
drawPlay();
drawClear();
drawStep();
drawSpeed();
if(speedMultiplier == 0){
if(playActive){
iteration();
}
speedMultiplier = slowDown;
}
speedMultiplier --;
drawIteration();
}
解决方法
这部分逻辑看起来过于复杂且容易出错:
if(slowDown == 12){
slowDown = 6;
}
if(slowDown == 6){
slowDown = 4;
}
if(slowDown == 4){
slowDown = 3;
}
if(slowDown == 3){
slowDown = 2;
}
if(slowDown == 2){
slowDown = 12;
}
一个基本的例子是,如果 slowDown
是 6
,您将其设置为 4
,但是在下一个条件中,您检查它是否为 4
并将其更改为 {{1}因为它是 6,所以它不仅会变为 4,而且会立即变为 3
。您的所有条件都会逐渐下降,最终在 3
处循环。
试试看:
12
我能想到的最简单的方法就是将 if(mouseX > xposSpeed && mouseX < xposSpeed + speedWidth && mouseY > yposSpeed && mouseY < yposSpeed + speedWidth){
println("before",slowDown);
if(slowDown == 12){
slowDown = 6;
}
if(slowDown == 6){
slowDown = 4;
}
if(slowDown == 4){
slowDown = 3;
}
if(slowDown == 3){
slowDown = 2;
}
if(slowDown == 2){
slowDown = 12;
}
println("after",slowDown);
}
设置为一个值:
slowDown
如果您想要多个速度倍增器,您可能打算使用 switch
: ?
if(mouseX > xposSpeed && mouseX < xposSpeed + speedWidth && mouseY > yposSpeed && mouseY < yposSpeed + speedWidth){
slowDown = 1;
}
就实际行为而言,根据按钮标签,如果看起来您想要在速度预设中循环:1x、2x、3x、4x,然后返回到 1x。
或者,您可以管理一组预设选项并简单地增加(并循环)一个索引。您也可以选择使用预设文本标签数组。
例如:
if(mouseX > xposSpeed && mouseX < xposSpeed + speedWidth && mouseY > yposSpeed && mouseY < yposSpeed + speedWidth){
println("before",slowDown);
switch(slowDown){
case 12:
slowDown = 6;
break;
case 6:
slowDown = 4;
break;
case 4:
slowDown = 3;
break;
case 3:
slowDown = 2;
break;
case 2:
slowDown = 12;
break;
}
println("after",slowDown);
}
以下是包含上述更改的代码的修改版本:
int speedPresetIndex = 0;
int[] speedPresetValues = {12,6,4,3,2};
String[] speedPresetLabels = {"1x","2x","3x","4x",""};
...
if(mouseX > xposSpeed && mouseX < xposSpeed + speedWidth && mouseY > yposSpeed && mouseY < yposSpeed + speedWidth){
// increment index (looping back to start using %)
speedPresetIndex = (speedPresetIndex + 1) % speedPresetValues.length;
// update slowDown based on preset
slowDown = speedPresetValues[speedPresetIndex];
}
...
text(speedPresetLabels[speedPresetIndex],xposSpeed + (speedWidth/2),yposSpeed + (speedWidth/1.4));
希望这能解决您的主要问题。
此外,我还有一些可能有用的建议:
- avoid repeating yourself 在代码中。所有鼠标/按钮矩形边界检查都可以将逻辑封装在可重用的函数中。 (如果在学校上课,你可以实现和重用一个 Button 类)。您可以在 this answer 中查看示例。即使它在 p5.js 中,语法也非常相似。
- 同样,如果教授课程,2D CA 可以封装成一个可重用的课程
- 这是一个很好的建议:您可以使用 .png 图像,而不是像这样手动定义大型 2D 布尔数组,其中
void setup() { size(550,615); pixelDensity(2); frameRate(60); println(colour.length,colour[0].length); } int speedPresetIndex = 0; int[] speedPresetValues = {12,""}; //initial array,set as 2 glider guns ---------------------------- boolean[][] colour = {{false,false,false},{false,true,false} }; //boolean[][] colour = new boolean[50][50]; //---------------------------------------------------------------- //game variables ------------------------------------------------- int xposGame = 25; int yposGame = 25; int[] arraySize = {50,50}; int slowDown = 4; int speedMultiplier = slowDown; //---------------------------------------------------------------- //play button variables ------------------------------------------ int xposPlay = xposGame; int yposPlay = 550; int playWidth = 40; boolean playActive = false; //---------------------------------------------------------------- //clear button variables ----------------------------------------- int clearWidth = 40; int xposClear = 485; int yposClear = 550; //---------------------------------------------------------------- //step button variables ------------------------------------------ int stepWidth = 40; int xposStep = xposGame + xposPlay + 25; int yposStep = 550; //---------------------------------------------------------------- //speed multiplier button variables ------------------------------ int speedWidth = 40; int xposSpeed = xposGame + xposPlay + xposStep; int yposSpeed = 550; //---------------------------------------------------------------- //clear the board ------------------------------------------------ void clearArr(){ for(int i = 0; i < arraySize[0]; i++){ for(int j = 0; j < arraySize[1]; j++){ colour[i][j] = false; } } } //---------------------------------------------------------------- //iteration function --------------------------------------------- void iteration(){ //counter for # of neighboring cells int neighborNum = 0; //next generations array boolean[][] newColour = new boolean[arraySize[0]][arraySize[1]]; //read the current generation's array for(int i = 0; i < arraySize[0]; i++){ for(int j = 0; j < arraySize[1]; j++){ //search all values around the cell (excluding those outside the array) and count how many neighbors are alive if(i != -0){ if(j != 0){ if(colour[i-1][j-1]==true){neighborNum++;} } if(colour[i-1][j]==true){neighborNum++;} if(j != arraySize[1]-1){ if(colour[i-1][j+1]==true){neighborNum++;} } } if(j != 0){ if(colour[i][j-1]==true){neighborNum++;} } if(j != arraySize[1]-1){ if(colour[i][j+1]==true){neighborNum++;} } if(i != arraySize[0]-1){ if(j != 0){ if(colour[i+1][j-1]==true){neighborNum++;} } if(colour[i+1][j]==true){neighborNum++;} if(j != arraySize[1]-1){ if(colour[i+1][j+1]==true){neighborNum++;} } } //check if the current cell is alive. if it has 2-3 neighbors,it survives,else it doesnt if(colour[i][j]==true){ if(neighborNum==3||neighborNum==2){ newColour[i][j] = true; } else{ newColour[i][j] = false; } } //check if the current cell is dead. if it has 3 neighbors,birth it next round if(colour[i][j]==false){ if(neighborNum==3){ newColour[i][j] = true; } else{ newColour[i][j] = false; } } //set the number of neighbors on the current cell to 0 for the next cell neighborNum = 0; } } //set the new array to be the current array colour = newColour; } //---------------------------------------------------------------- //iteration drawing function ------------------------------------- void drawIteration(){ for(int i = 0; i < arraySize[0]; i++){ for(int j = 0; j < arraySize[1]; j++){ if(colour[i][j] == true){ fill(255); } else{fill(0);} strokeWeight(2); stroke(50); rect(10*j+xposGame,10*i+yposGame,10,10); } } } //---------------------------------------------------------------- //play/pause button function ------------------------------------- void drawPlay(){ noStroke(); fill(0); rect(xposPlay,yposPlay,playWidth,5); fill(255); if(!playActive){ triangle(xposPlay + (playWidth/3),yposPlay + (playWidth/3.3),xposPlay + (playWidth/3),yposPlay + playWidth - (playWidth/3.3),xposPlay + (playWidth*3.5/5),yposPlay + (playWidth/2)); } if(playActive){ rect(xposPlay + (playWidth/3),playWidth/9,playWidth-(2*(playWidth/3.3)),2); rect(xposPlay + playWidth - (playWidth/3) - (playWidth/9),2); } } //---------------------------------------------------------------- //clear button function ------------------------------------------ void drawClear(){ noStroke(); fill(0); rect(xposClear,yposClear,clearWidth,5); strokeWeight(clearWidth/9); stroke(255); line(xposClear+clearWidth/3,yposClear + clearWidth/3,xposClear + 2 * clearWidth/3,yposClear + 2 * clearWidth/3); line(xposClear+clearWidth/3,yposClear + 2 * clearWidth/3,yposClear + clearWidth/3); } //---------------------------------------------------------------- //step button function ------------------------------------------- void drawStep(){ noStroke(); fill(0); rect(xposStep,yposStep,stepWidth,5); strokeWeight(stepWidth/9); stroke(255); line(xposStep + stepWidth/3,yposStep + stepWidth/3,xposStep + (stepWidth*3.5/5),yposStep + (stepWidth/2)); line(xposStep + stepWidth/3,yposStep + 2*stepWidth/3,yposStep + (stepWidth/2)); } //---------------------------------------------------------------- //speed multiplier button ---------------------------------------- void drawSpeed(){ noStroke(); fill(0); rect(xposSpeed,yposSpeed,speedWidth,5); fill(255); textAlign(CENTER); textSize(25); text(speedPresetLabels[speedPresetIndex],yposSpeed + (speedWidth/1.4)); } //---------------------------------------------------------------- //all mousePressed events ---------------------------------------- void mousePressed(){ //play button if(mouseX > xposPlay && mouseX < xposPlay + playWidth && mouseY > yposPlay && mouseY < yposPlay + playWidth){ playActive = !playActive; } //clear button if(mouseX > xposClear && mouseX < xposClear + clearWidth && mouseY > yposClear && mouseY < yposClear + clearWidth){ clearArr(); playActive = false; } //step button if(mouseX > xposStep && mouseX < xposStep + stepWidth && mouseY > yposStep && mouseY < yposStep + stepWidth){ iteration(); playActive = false; } //speed multiplier button if(mouseX > xposSpeed && mouseX < xposSpeed + speedWidth && mouseY > yposSpeed && mouseY < yposSpeed + speedWidth){ // increment index (looping back to start using %) speedPresetIndex = (speedPresetIndex + 1) % speedPresetValues.length; // update slowDown based on preset slowDown = speedPresetValues[speedPresetIndex]; } //adding cells if(!playActive && mouseX > xposGame && mouseX < xposGame + 10*arraySize[0] && mouseY > yposGame && mouseY < yposGame + 10*arraySize[1]){ colour[(mouseY-yposGame)/10][(mouseX-xposGame)/10] = !colour[(mouseY-yposGame)/10][(mouseX-xposGame)/10]; } } //---------------------------------------------------------------- void draw() { background(50); noStroke(); fill(50); drawPlay(); drawClear(); drawStep(); drawSpeed(); if(speedMultiplier == 0){ if(playActive){ iteration(); } speedMultiplier = slowDown; } speedMultiplier --; drawIteration(); }
被标记为黑色背景上的白色像素。通过这种方法,未来版本不仅可以绘制,还可以保存/加载 2D CA 配置。
剩下的一点都不差(相当干净的代码,一致的命名等)!干得好!