象棋
畫棋盤
利用for迴圈畫格線
```
size(500,550);
for( int x=50; x<=450; x+=50 ){
line(x,50,x,250);
line(x,300,x,500);
}
for( int y=50; y<=500; y+=50 ){
line(50,y, 450,y);
}
```
```
size(500,550);
for( int x=50; x<=450; x+=50 ){
line(x,50,x,250);
line(x,300,x,500);
}
for( int y=50; y<=500; y+=50 ){
line(50,y, 450,y);
}
```
宣告陣列,暫時將數字當作棋子的種類
```
```
int [][]board={
{4,5,3,2,1,2,3,5,4},
{0,0,0,0,0,0,0,0,0},
{0,6,0,0,0,0,0,6,0},
{7,0,7,0,7,0,7,0,7}
};///1:將2:士3:象4:車5:馬6:包7:卒
void setup(){
size(500,550);
}
void draw(){
for(int x=50; x<=450;x+=50){
line(x, 50, x, 250);
line(x, 300, x, 450);
}
for(int y=50; y<=450; y+=50){
line(50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<9; j++){
text(board[i][j], 50+j*50, 50+i*50);
}
}
}
```
宣告字串陣列替換數字
```
int [][]board={
{4,5,3,2,1,2,3,5,4},
{0,0,0,0,0,0,0,0,0},
{0,6,0,0,0,0,0,6,0},
{7,0,7,0,7,0,7,0,7}
};///1:將2:士3:象4:車5:馬6:包7:卒
String[]name = {"將","士","象","車","馬","包","卒"};
void setup(){
size(500,550);
PFont font = createFont("標楷體", 30);
textFont(font);
textAlign(CENTER, CENTER);
}
void draw(){
for(int x=50; x<=450;x+=50){
line(x, 50, x, 250);
line(x, 300, x, 450);
}
for(int y=50; y<=450; y+=50){
line(50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<9; j++){
int id = board[i][j];
if(id==0)continue;
text(name[id-1], 50+j*50, 50+i*50);
}
}
}
```
把紅方也畫出來
```
```
int [][]board={
{4,5,3,2,1,2,3,5,4},
{0,0,0,0,0,0,0,0,0},
{0,6,0,0,0,0,0,6,0},
{7,0,7,0,7,0,7,0,7},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{-7,0,-7,0,-7,0,-7,0,-7},
{0,-6,0,0,0,0,0,-6,0},
{0,0,0,0,0,0,0,0,0},
{-4,-5,-3,-2,-1,-2,-3,-5,-4},
};///1:將2:士3:象4:車5:馬6:包7:卒
String[]name = {"將","士","象","車","馬","包","卒"};
String[]name2= {"帥","仕","相","俥","傌","炮","兵"};
void setup(){
size(500,550);
PFont font = createFont("標楷體", 30);
textFont(font);
textAlign(CENTER, CENTER);
}
void draw(){
background(#F0B82C);
for(int x=50; x<=450;x+=50){
line(x, 50, x, 250);
line(x, 300, x, 450);
}
for(int y=50; y<=450; y+=50){
line(50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<9; j++){
int id = board[i][j];
if(id==0)continue;
fill(0);
text(name[id-1], 50+j*50, 50+i*50);
fill(#FF0000);
text(name2[id-1],50+j*50, 450-i*50);
}
}
}
```
沒有留言:
張貼留言