踩地雷遊戲 高慧君南港高中 開啟專案 MineSweep 任務一 : 產生 30X20 個磚塊編輯 Table 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class MyWorld here. @author Jean, Hue Ching Kao @version 2013.4.3 public class MyWorld extends World private ArrayList<Block> blocks = new ArrayList<Block>(); Constructor for objects of class MyWorld. public MyWorld() // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(32, 22, 25); for (int i=1; i<31; i++) for (int j=1; j<21; j++) Block block = new Block(); blocks.add(block); addobject(block, i, j);
任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Block here. @author Jean, Hue Ching Kao @version 2013.4.3 public class Block extends Actor public boolean isbomb=false; Act do whatever the Block wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. 編輯 MyWorld 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; import java.util.collections; Write a description of class MyWorld here. @author Jean, Hue Ching Kao @version 2013.4.3 public class MyWorld extends World private ArrayList<Block> blocks = new ArrayList<Block>();
Constructor for objects of class MyWorld. public MyWorld() // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(32, 22, 25); for (int i=1; i<31; i++) for (int j=1; j<21; j++) Block block = new Block(); blocks.add(block); addobject(block, i, j); Collections.shuffle(blocks); for (int i=0; i<20; i++) blocks.get(i).isbomb = true; 任務三 : 按下滑鼠右鍵, 可以插旗子或取消插旗子 編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Block here. @author Jean, Hue Ching Kao @version 2013.4.3 public class Block extends Actor public boolean isbomb=false; public boolean isflagged=false;
Act do whatever the Block wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) if (mouse.getbutton()==3) if (isflagged) setimage("block.png"); isflagged = false; else setimage("blockflagged.png"); isflagged = true; return; 任務三 : 按下滑鼠左鍵, 可以打開磚塊, 如果該磚塊是炸彈, 就將所有有炸彈的磚塊顯示出來, 遊戲結束 如果不是炸彈, 則顯示該磚塊周圍有幾個炸彈 若是該磚塊周圍的磚塊的炸彈數是零, 則再展開該磚塊 編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.list; Write a description of class Block here. @author Jean, Hue Ching Kao @version 2013.4.3 public class Block extends Actor public boolean isbomb=false;
public boolean isflagged=false; public boolean isopen=false; Act do whatever the Block wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) if (mouse.getbutton()==3) if (isflagged) setimage("block.png"); isflagged = false; else setimage("blockflagged.png"); isflagged = true; return; if (isbomb) MyWorld world = (MyWorld) getworld(); world.showallbomb(); Greenfoot.playSound("bomb.wav"); Greenfoot.stop(); else if (!isflagged) openblock(this); public int getbombnumber(block block)
int bombnumber=0; List<Block> blocks = block.getneighbours(1, true, Block.class); for (Block nblock: blocks) if (nblock.isbomb) bombnumber++; return bombnumber; public void openblock(block block) block.isopen = true; block.setimage("blockclicked["+block.getbombnumber(block)+"].png"); if (block.getbombnumber(block)==0) List<Block> blocks = block.getneighbours(1, true, Block.class); for (Block nblock : blocks) if (!nblock.isopen) if (nblock.getbombnumber(nblock)==0) openblock(nblock); else nblock.setimage("blockclicked["+nblock.getbombnumber(nblock)+"].png"); nblock.isopen=true; 編輯 MyWorld 類別的程式碼 public void showallbomb() for (int i=0; i<20; i++) blocks.get(i).setimage("bomb.png"); 任務四 : 檢查 20 個炸彈磚塊是否都被標上旗子, 若是, 則播放音樂, 遊戲停止 public void act() for (int i=0; i<20; i++)
if (blocks.get(i).isbomb &&!blocks.get(i).isflagged) return; Greenfoot.playSound("win.wav"); Greenfoot.stop();