week07
(一)複習暗棋
1.把老師給的程式碼複製過來
```c
int [][]show={
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}
};//翻牌前, 0 都不會秀哦!
int [][]board={
{ 1, 2, 2, 3, 3, 4, 4, 5},
{ 5, 6, 6, 7, 7, 7, 7, 7},
{-1,-2,-2,-3,-3,-4,-4,-5},
{-5,-6,-6,-7,-7,-7,-7,-7}
}; //暗棋的格子,比較少 4x8=32個棋子
void setup(){
size(500,300);
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);
}
for (int y=50; y<=250; y+=50) {
line( 50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(show[i][j]==0){
fill(255);
ellipse( 50+25+j*50, 50+25+i*50, 40, 40);
}else{
int id = board[i][j];
drawChess(50+25+j*50, 50+25+i*50, id);
}
}
}
}
void mousePressed(){
for(int i=0; i<10; i++){
for(int j=0; j<9; j++){
if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){
if( show[i][j]==0 ) show[i][j] = 1; //沒秀? 秀它
//之後再加棋子的移動
}
}
}
}
String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};
String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};
void drawChess(int x, int y, int id){
fill(255);
ellipse( x, y, 40, 40);
if(id>0){//黑
fill(0);
text( name2[id-1], x, y-3);
}else{//紅
fill(255,0,0);
text( name2[-id-1], x, y-3);
}
}
```
int [][]show={
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}
};//翻牌前, 0 都不會秀哦!
int [][]board={
{ 1, 2, 2, 3, 3, 4, 4, 5},
{ 5, 6, 6, 7, 7, 7, 7, 7},
{-1,-2,-2,-3,-3,-4,-4,-5},
{-5,-6,-6,-7,-7,-7,-7,-7}
}; //暗棋的格子,比較少 4x8=32個棋子
void setup(){
size(500,300);
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);
}
for (int y=50; y<=250; y+=50) {
line( 50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(show[i][j]==0){
fill(255);
ellipse( 50+25+j*50, 50+25+i*50, 40, 40);
}else{
int id = board[i][j];
drawChess(50+25+j*50, 50+25+i*50, id);
}
}
}
}
void mousePressed(){
for(int i=0; i<10; i++){
for(int j=0; j<9; j++){
if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){
if( show[i][j]==0 ) show[i][j] = 1; //沒秀? 秀它
//之後再加棋子的移動
}
}
}
}
String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};
String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};
void drawChess(int x, int y, int id){
fill(255);
ellipse( x, y, 40, 40);
if(id>0){//黑
fill(0);
text( name2[id-1], x, y-3);
}else{//紅
fill(255,0,0);
text( name2[-id-1], x, y-3);
}
}
```
2.新增亂數,使紅、黑色棋子不會在固定位置,並且新增洗牌函式,至多洗1000次
```c
int [][]show={
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}
};//翻牌前, 0 都不會秀哦!
int [][]board={
{ 1, 2, 2, 3, 3, 4, 4, 5},
{ 5, 6, 6, 7, 7, 7, 7, 7},
{-1,-2,-2,-3,-3,-4,-4,-5},
{-5,-6,-6,-7,-7,-7,-7,-7}
}; //暗棋的格子,比較少 4x8=32個棋子
void setup(){
size(500,300);
PFont font = createFont("標楷體", 30);
textFont(font);
textAlign(CENTER, CENTER);
for(int k=0;k<1000;k++){ ///洗牌1000次
int i1=int(random(4)),j1=int(random(8));
int i2=int(random(4)),j2=int(random(8));
int temp=board[i1][j1];
board[i1][j1]=board[i2][j2];
board[i2][j2]=temp;
}
}
void draw(){
background(#F0B82C);
for (int x=50; x<=450; x+=50) {
line( x, 50, x, 250);
}
for (int y=50; y<=250; y+=50) {
line( 50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(show[i][j]==0){
fill(255);
ellipse( 50+25+j*50, 50+25+i*50, 40, 40);
}else{
int id = board[i][j];
drawChess(50+25+j*50, 50+25+i*50, id);
}
}
}
}
void mousePressed(){
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){
if( show[i][j]==0 ) show[i][j] = 1; //沒秀? 秀它
//之後再加棋子的移動
}
}
}
}
String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};
String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};
void drawChess(int x, int y, int id){
fill(255);
ellipse( x, y, 40, 40);
if(id>0){//黑
fill(0);
text( name[id-1], x, y-3);
}else{//紅
fill(255,0,0);
text( name2[-id-1], x, y-3);
}
}
```
int [][]show={
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}
};//翻牌前, 0 都不會秀哦!
int [][]board={
{ 1, 2, 2, 3, 3, 4, 4, 5},
{ 5, 6, 6, 7, 7, 7, 7, 7},
{-1,-2,-2,-3,-3,-4,-4,-5},
{-5,-6,-6,-7,-7,-7,-7,-7}
}; //暗棋的格子,比較少 4x8=32個棋子
void setup(){
size(500,300);
PFont font = createFont("標楷體", 30);
textFont(font);
textAlign(CENTER, CENTER);
for(int k=0;k<1000;k++){ ///洗牌1000次
int i1=int(random(4)),j1=int(random(8));
int i2=int(random(4)),j2=int(random(8));
int temp=board[i1][j1];
board[i1][j1]=board[i2][j2];
board[i2][j2]=temp;
}
}
void draw(){
background(#F0B82C);
for (int x=50; x<=450; x+=50) {
line( x, 50, x, 250);
}
for (int y=50; y<=250; y+=50) {
line( 50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(show[i][j]==0){
fill(255);
ellipse( 50+25+j*50, 50+25+i*50, 40, 40);
}else{
int id = board[i][j];
drawChess(50+25+j*50, 50+25+i*50, id);
}
}
}
}
void mousePressed(){
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){
if( show[i][j]==0 ) show[i][j] = 1; //沒秀? 秀它
//之後再加棋子的移動
}
}
}
}
String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};
String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};
void drawChess(int x, int y, int id){
fill(255);
ellipse( x, y, 40, 40);
if(id>0){//黑
fill(0);
text( name[id-1], x, y-3);
}else{//紅
fill(255,0,0);
text( name2[-id-1], x, y-3);
}
}
```
3.移動棋子,按住棋子可將任意一個棋子轉換為綠色,並可隨意變換
```c
int [][]show={
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}
};//翻牌前, 0 都不會秀哦!
int [][]board={
{ 1, 2, 2, 3, 3, 4, 4, 5},
{ 5, 6, 6, 7, 7, 7, 7, 7},
{-1,-2,-2,-3,-3,-4,-4,-5},
{-5,-6,-6,-7,-7,-7,-7,-7}
}; //暗棋的格子,比較少 4x8=32個棋子
void setup(){
size(500,300);
PFont font = createFont("標楷體", 30);
textFont(font);
textAlign(CENTER, CENTER);
for(int k=0;k<1000;k++){ ///洗牌1000次
int i1=int(random(4)),j1=int(random(8));
int i2=int(random(4)),j2=int(random(8));
int temp=board[i1][j1];
board[i1][j1]=board[i2][j2];
board[i2][j2]=temp;
}
}
void draw(){
background(#F0B82C);
for (int x=50; x<=450; x+=50) {
line( x, 50, x, 250);
}
for (int y=50; y<=250; y+=50) {
line( 50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(show[i][j]==0){
fill(255);
ellipse( 50+25+j*50, 50+25+i*50, 40, 40);
}else{
int id = board[i][j];
drawChess(50+25+j*50, 50+25+i*50, id);
}
}
}
if(moving){
drawChess(50+25+moveJ*50, 50+25+moveI*50, 9);
}
}
int moveI=-1,moveJ=-1;
boolean moving=false; //不是移動中
void mousePressed(){
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){
if( show[i][j]==0 ) show[i][j] = 1; //沒秀? 秀它
else{
moveI=i; //我們想移動的棋子 i座標
moveJ=j; //我們想移動的棋子 j座標
moving=true; //現在移動中
} //現在要加棋子的移動
}
}
}
}
String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};
String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};
void drawChess(int x, int y, int id){
fill(255);
ellipse( x, y, 40, 40);
if(id==9){ //待移動的棋子
fill(0,255,0); //綠色
ellipse(x,y,40,40);
}else if(id>0){//黑
fill(0);
text( name[id-1], x, y-3);
}else{//紅
fill(255,0,0);
text( name2[-id-1], x, y-3);
}
}
``````c
int [][]show={
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}
};//翻牌前, 0 都不會秀哦!
int [][]board={
{ 1, 2, 2, 3, 3, 4, 4, 5},
{ 5, 6, 6, 7, 7, 7, 7, 7},
{-1,-2,-2,-3,-3,-4,-4,-5},
{-5,-6,-6,-7,-7,-7,-7,-7}
}; //暗棋的格子,比較少 4x8=32個棋子
void setup(){
size(500,300);
PFont font = createFont("標楷體", 30);
textFont(font);
textAlign(CENTER, CENTER);
for(int k=0;k<1000;k++){ ///洗牌1000次
int i1=int(random(4)),j1=int(random(8));
int i2=int(random(4)),j2=int(random(8));
int temp=board[i1][j1];
board[i1][j1]=board[i2][j2];
board[i2][j2]=temp;
}
}
void draw(){
background(#F0B82C);
for (int x=50; x<=450; x+=50) {
line( x, 50, x, 250);
}
for (int y=50; y<=250; y+=50) {
line( 50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(show[i][j]==0){
fill(255);
ellipse( 50+25+j*50, 50+25+i*50, 40, 40);
}else{
int id = board[i][j];
drawChess(50+25+j*50, 50+25+i*50, id);
}
}
}
if(moving){
fill(0,255,0,128); //綠色,半透明
ellipse(50+25+moveJ*50,50+25+moveI*50,40,40); //原來的位置
drawChess(mouseX,mouseY,moveID); //正在移動的棋子
}
}
int moveI=-1,moveJ=-1,moveID=-1;
boolean moving=false; //不是移動中
void mousePressed(){
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){
if( show[i][j]==0 ) show[i][j] = 1; //沒秀? 秀它
else{
moveI=i; //我們想移動的棋子 i座標
moveJ=j; //我們想移動的棋子 j座標
moveID=board[i][j];
moving=true; //現在移動中
} //現在要加棋子的移動
}
}
}
}
String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};
String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};
void drawChess(int x, int y, int id){
fill(255);
ellipse( x, y, 40, 40);
//if(id==9){ //待移動的棋子
//fill(0,255,0); //綠色
// ellipse(x,y,40,40);
//}else
if(id>0){//黑
fill(0);
text( name[id-1], x, y-3);
}else{//紅
fill(255,0,0);
text( name2[-id-1], x, y-3);
}
}
5.前面的程式有個缺陷,無法吃掉棋子
新增mouseReleased()函式,可以按住棋子移動放到要吃掉的棋子上後放開即可
```c
int [][]show={
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}
};//翻牌前, 0 都不會秀哦!
int [][]board={
{ 1, 2, 2, 3, 3, 4, 4, 5},
{ 5, 6, 6, 7, 7, 7, 7, 7},
{-1,-2,-2,-3,-3,-4,-4,-5},
{-5,-6,-6,-7,-7,-7,-7,-7}
}; //暗棋的格子,比較少 4x8=32個棋子
void setup(){
size(500,300);
PFont font = createFont("標楷體", 30);
textFont(font);
textAlign(CENTER, CENTER);
for(int k=0;k<1000;k++){ ///洗牌1000次
int i1=int(random(4)),j1=int(random(8));
int i2=int(random(4)),j2=int(random(8));
int temp=board[i1][j1];
board[i1][j1]=board[i2][j2];
board[i2][j2]=temp;
}
}
void draw(){
background(#F0B82C);
for (int x=50; x<=450; x+=50) {
line( x, 50, x, 250);
}
for (int y=50; y<=250; y+=50) {
line( 50, y, 450, y);
}
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(show[i][j]==0){
fill(255);
ellipse( 50+25+j*50, 50+25+i*50, 40, 40);
}else{
int id = board[i][j];
drawChess(50+25+j*50, 50+25+i*50, id);
}
}
}
if(moving){
fill(0,255,0,128); //綠色,半透明
ellipse(50+25+moveJ*50,50+25+moveI*50,40,40); //原來的位置
drawChess(mouseX,mouseY,moveID); //正在移動的棋子
}
}
int moveI=-1,moveJ=-1,moveID=-1;
boolean moving=false; //不是移動中
void mousePressed(){
for(int i=0; i<4; i++){
for(int j=0; j<8; j++){
if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){
if( show[i][j]==0 ) show[i][j] = 1; //沒秀? 秀它
else{
moveI=i; //我們想移動的棋子 i座標
moveJ=j; //我們想移動的棋子 j座標
moveID=board[i][j];
moving=true; //現在移動中
} //現在要加棋子的移動
}
}
}
}
void mouseReleased(){ //放開時就把它吃掉
for(int i=0;i<4;i++){
for(int j=0;j<8;j++){
if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){
if(moving){
//println(moveI,moveJ,moveing,moveID);
board[moveI][moveJ]=0;
//println(i,j);
board[i][j]=moveID;
moving=false;
}
}
}
}
}
String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};
String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};
void drawChess(int x, int y, int id){
if(id==0)return; //沒有棋子,就別進來
fill(255);
ellipse( x, y, 40, 40);
//if(id==9){ //待移動的棋子
//fill(0,255,0); //綠色
// ellipse(x,y,40,40);
//}else
if(id>0){//黑
fill(0);
text( name[id-1], x, y-3);
}else{//紅
fill(255,0,0);
text( name2[-id-1], x, y-3);
}
}
```
1.先安裝sound外掛
2.安裝好外掛後,把音樂拉進來,Ctrl-K可以看檔案
//存檔,mp3拉進來,Ctrl-K看檔案
```c
import processing.sound.*; //音樂功能
void setup(){
Sound s=new Sound(this);
}
void draw(){
}
void mousePressed(){
}
```
3.撥放音訊
```c
//存檔,mp3拉進來,Ctrl-K看檔案
import processing.sound.*; //音樂功能
void setup(){
SoundFile file = new SoundFile(this,"Intro Song_Final.mp3");
file.play();
}
void draw(){
}
void mousePressed(){
SoundFile file2 = new SoundFile(this,"In Game Music.mp3");
file2.play();
}
```
//存檔,mp3拉進來,Ctrl-K看檔案
import processing.sound.*; //音樂功能
void setup(){
SoundFile file = new SoundFile(this,"Intro Song_Final.mp3");
file.play();
}
void draw(){
}
void mousePressed(){
SoundFile file2 = new SoundFile(this,"In Game Music.mp3");
file2.play();
}
```
4.統整,一起撥放音訊
```c
//存檔,mp3拉進來,Ctrl-K看檔案
import processing.sound.*; //音樂功能
SoundFile file1,file2,file3,file4;
void setup(){
file1= new SoundFile(this,"Intro Song_Final.mp3");
file2= new SoundFile(this,"In Game Music.mp3");
file3= new SoundFile(this,"Monkey 1.mp3");
file4= new SoundFile(this,"Fruit Missed.mp3");
file1.play();
}
void draw(){
}
void mousePressed(){
file2.play();
}
void keyPressed(){
file3.play();
}
```
//存檔,mp3拉進來,Ctrl-K看檔案
import processing.sound.*; //音樂功能
SoundFile file1,file2,file3,file4;
void setup(){
file1= new SoundFile(this,"Intro Song_Final.mp3");
file2= new SoundFile(this,"In Game Music.mp3");
file3= new SoundFile(this,"Monkey 1.mp3");
file4= new SoundFile(this,"Fruit Missed.mp3");
file1.play();
}
void draw(){
}
void mousePressed(){
file2.play();
}
void keyPressed(){
file3.play();
}
```
沒有留言:
張貼留言