Ciao 访客, welcome back to old school! :p
color White_C = color(255, 255, 255),Black_C = color(0, 0, 0);final int Side = 60, // Side of cellRank = 5; //Number of cells across = number of cells downfinal int W = Side * Rank, // Width of canvasH = W; // Height of canvasint[][] Vals = new int[Rank][Rank];//To hold cell valuesPFont Font; // Font for rendering cell valuesPFont DFont; // Font for rendering direction valuesvoid setup() { size(W, H); Font = loadFont("Arial-BoldMT-36.vlw"); textFont(Font); DFont = loadFont("Century-18.vlw"); textFont(CFont); Random_Populate_Array(); Draw_Vals(); Draw_Controls();}final int Hi = 4, // Max cell valueLo = -2;// Min cell valuevoid Random_Populate_Array() { int i, j, k; for (i = 0; i < Rank; i++) { for (j = 0; j < Rank; j++) { for (k = 0; k < Rank*Rank; k++) { int r = int(random(Lo,Hi)); Vals[i][j] = r; } } }} void Draw_Vals() { int i, j; int R; for(i = 0; i < Rank; i++) { for(j = 0; j < Rank; j++) { fill(White_C); rect(i * Side,j * Side, Side, Side); fill(Black_C); textAlign(CENTER, CENTER); text(Vals[i][j], (i + 0.5) * Side, (j + 0.5) * Side); } }}String[] Legends = {"Up", "Right", "Down", "Left"};void Draw_Controls() { }
编程不是拼凑代码,不是复制黏贴learning processing你看完了没?
color White_C = color(255, 255, 255), Black_C = color(0, 0, 0), Green_C = color(0, 255, 0), Yellow_C = color(255, 255, 0);final int Side = 60, // Side of cell Rank = 5; //Number of cells across = number of cells downfinal int W = Side * Rank, // Width of canvas H = W + Side; // Height of canvasint[][] Vals = new int[Rank][Rank];//To hold cell valuesPFont Font; // Font for rendering cell valuesPFont DFont; // Font for rendering direction valuesString[] Legends = {"Up", "Right", "Down", "Left"};// circle original position.int Xposition = 30;int Yposition = 270;void setup() { size(W, H); Font = loadFont("Arial-BoldMT-36.vlw"); DFont = loadFont("Century-18.vlw"); textFont(Font); Random_Populate_Array(); Draw_Squares(); Draw_Vals(); Draw_Controls();}void draw(){}final int Hi = 4, // Max cell value Lo = -2;// Min cell value// This draws the squares the 'i' is the Columns and 'j' is the Rows.void Draw_Squares() { for(int i = 0; i < Rank; i++) { for(int j = 0; j < Rank; j++) { fill(Yellow_C); stroke(Green_C); rect(i * Side,j * Side, Side, Side); } }} void Random_Populate_Array() { for (int i = 0; i < Rank; i++) { for (int j = 0; j < Rank; j++) { Vals[i][j] = int(random(Lo,Hi)); } }}//This Draws the random values between -2 and 4 void Draw_Vals() { for(int i = 0; i < Rank; i++) { for(int j = 0; j < Rank; j++) { fill(Black_C); textFont(Font); textAlign(CENTER, CENTER); text(Vals[i][j], (i + 0.5) * Side, (j + 0.5) * Side); } }}void Draw_Controls() { textFont(DFont); for(int i = 0; i < Rank; i++){ for(int j = 5; j < Rank + 1; j++){ fill (Green_C); stroke (Black_C); rect(i * Side, j * Side, Side, Side); if(i > 0 && i < 5){ fill(Black_C); textAlign(CENTER, CENTER); text(Legends[i-1], (i + 0.5) * Side, (j + 0.5) * Side); } else{ fill(Yellow_C); rect(i * Side, j * Side, Side, Side); // draw circle. if (Yposition == 270 && Xposition == 30){stroke(Green_C);noFill();ellipse(Side/2,4.5*Side,Side,Side); } else{ } } } }}void Update_Position(String ChangePosition) { Draw_Squares(); Draw_Vals(); Draw_Controls(); if (ChangePosition == "UP") { if (Yposition - Side > 0){ Yposition = Yposition - 60 ; stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } else{ stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } } if (ChangePosition == "Right") { if (Xposition + Side < 300){ Xposition = Xposition + 60 ; stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } else{ stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } } if (ChangePosition == "Down") { if (Yposition + Side < 300){ Yposition = Yposition + 60 ; stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } else{ stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } } if (ChangePosition == "Left") { if (Xposition - Side > 0){ Xposition = Xposition - 60 ; stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } else{ stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } }}void mousePressed() { if( mouseY > 300 && mouseY < 360 ){ if ( mouseX > 60 && mouseX < 120 ){Update_Position("UP");} if ( mouseX > 120 && mouseX < 180 ){Update_Position("Right");} if ( mouseX > 180 && mouseX < 240 ){Update_Position("Down");} if ( mouseX > 240 && mouseX < 300 ){Update_Position("Left");} }}
color White_C = color(255, 255, 255), Black_C = color(0, 0, 0), Green_C = color(0, 255, 0), Yellow_C = color(255, 255, 0);final int Side = 60, // Side of cell Rank = 5; //Number of cells across = number of cells downfinal int W = Side * Rank, // Width of canvas H = W + Side; // Height of canvasint[][] Vals = new int[Rank][Rank];//To hold cell valuesPFont Font; // Font for rendering cell valuesPFont DFont; // Font for rendering direction valuesString[] Legends = {"Up", "Right", "Down", "Left"};// circle original position.int Xposition = 30;int Yposition = 270;int p,q;int sum; //hold value of Running_Total in bottom left cellvoid setup() { size(W, H); Font = loadFont("Arial-BoldMT-36.vlw"); DFont = loadFont("Century-18.vlw"); textFont(Font); text(sum,Side/2,(Side/2)+5*Side); Random_Populate_Array(); Draw_Squares(); Draw_Vals(); Draw_Controls();}void draw(){}final int Hi = 4, // Max cell value Lo = -2;// Min cell value// This draws the squares the 'i' is the Columns and 'j' is the Rows.void Draw_Squares() { for(int i = 0; i < Rank; i++) { for(int j = 0; j < Rank; j++) { fill(Yellow_C); stroke(Green_C); rect(i * Side,j * Side, Side, Side); } }} void Random_Populate_Array() { for (int i = 0; i < Rank; i++) { for (int j = 0; j < Rank; j++) { Vals[i][j] = int(random(Lo,Hi)); } }}//This Draws the random values between -2 and 4 void Draw_Vals() { for(int i = 0; i < Rank; i++) { for(int j = 0; j < Rank; j++) { fill(Black_C); textFont(Font); textAlign(CENTER, CENTER); text(Vals[i][j], (i + 0.5) * Side, (j + 0.5) * Side); } }}void Draw_Controls() { textFont(DFont); for(int i = 0; i < Rank; i++){ for(int j = 5; j < Rank + 1; j++){ fill (Green_C); stroke (Black_C); rect(i * Side, j * Side, Side, Side); if(i > 0 && i < 5){ fill(Black_C); textAlign(CENTER, CENTER); text(Legends[i-1], (i + 0.5) * Side, (j + 0.5) * Side); } else{ fill(Yellow_C); rect(i * Side, j * Side, Side, Side); // draw circle. if (Yposition == 270 && Xposition == 30){stroke(Green_C);noFill();ellipse(Side/2,4.5*Side,Side,Side); } else{ } } } }}void Update_Position(String ChangePosition) { Draw_Squares(); Draw_Vals(); Draw_Controls(); Running_Total(); if (ChangePosition == "UP") { if (Yposition - Side > 0){ Yposition = Yposition - 60 ; stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } else{ stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } } if (ChangePosition == "Right") { if (Xposition + Side < 300){ Xposition = Xposition + 60 ; stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } else{ stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } } if (ChangePosition == "Down") { if (Yposition + Side < 300){ Yposition = Yposition + 60 ; stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } else{ stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } } if (ChangePosition == "Left") { if (Xposition - Side > 0){ Xposition = Xposition - 60 ; stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } else{ stroke(Green_C); noFill(); ellipse(Xposition,Yposition,Side,Side); } }}void mousePressed() { if( mouseY > 300 && mouseY < 360 ){ if ( mouseX > 60 && mouseX < 120 ){Update_Position("UP");} if ( mouseX > 120 && mouseX < 180 ){Update_Position("Right");} if ( mouseX > 180 && mouseX < 240 ){Update_Position("Down");} if ( mouseX > 240 && mouseX < 300 ){Update_Position("Left");} }} void Running_Total(){ p=(((2*Yposition)-Side)/(2*Side));q=(((2*Xposition)-Side)/(2*Side));sum = sum +(Vals[q][p]);fill(255,255,0); rect(0,5*Side,Side,Side); fill(0,0,0); textFont(DFont); text(sum,Side/2,(Side/2)+5*Side); pushStyle(); fill(255,255,0); stroke(255,255,0); rectMode(CENTER); rect(Xposition,Yposition,Side-5,Side-5); popStyle(); Vals[q][p]=10; noFill(); print(sum); println(); }