# 20112陳文昱_Greenfoot遊戲(個人版打磚塊) 1. 遊戲情境(擷圖) ![image](https://hackmd.io/_uploads/B1JRcC3u6.png) 2. 程式碼整理 brickWorld: ```java= import greenfoot.*; // (World、Actor、GreenfootImage、GreenfootSound、Greenfoot及MouseInfo) /** * 請在此撰寫 BrickWorld 類別的說明。 * * @author (20112我超帥) * @version (2023.12.14 v1.0第一版) */ public class BrickWorld extends World { /** * BrickWorld 類別的物件建構子。 * */ public BrickWorld() { // 建立600x400方格的新場景,方格大小為1x1像素。 super(800,600, 1); addObject(new Ball(),400,480); addObject(new Paddle(),400,560); //產生8*5(40顆磚塊) for( int i=0;i<9;i++) for(int x=0;x<5;x++) { addObject(new Brick(),80+i*70,90+x*40); } } } ``` ball: ```java= import greenfoot.*; // (World、Actor、GreenfootImage、GreenfootSound、Greenfoot及MouseInfo) /** * 請在此撰寫 Ball 類別的說明。 * * @author (你的名字) * @version (日期或版號) */ public class Ball extends Actor { private int motionX=2; private int motionY=2; private int brickHit=0; /** * Act - 隨便 Ball 想做什麼。 * 每次按下「單步執行」或「執行」按鈕,都會呼叫這個方法。 */ private boolean touchPaddle; public void act() { // 在這裏寫程式。 int newX; int newY; newX=getX()+motionX; newY=getY()+motionY; setLocation(getX()+motionX,getY()+motionY); if(newX>800) { motionX=-2; } else if(newX<0) { motionX=-2; } if(newY>600) { getWorld().addObject(new Loss(),400,300); Greenfoot.stop(); } else if(newY<0) { motionY=2; } Actor brick=getOneIntersectingObject(Brick.class); if(brick !=null) { motionY=-motionY; brickHit++;//紀錄打掉的磚塊數量加1 Greenfoot.playSound("ocz8t-wjzgk.wav"); getWorld().removeObject(brick); } //球碰到板子會反彈 Actor paddle=getOneIntersectingObject(Paddle.class); if(paddle!=null && !touchPaddle) { motionY=-motionY; Greenfoot.playSound("60hjp-ihofp.wav"); touchPaddle=true; } else touchPaddle=false; //若打完磚塊,則遊戲結束 if(brickHit==45) { getWorld().addObject(new Win(),400,300); Greenfoot.stop(); } } } ``` Paddle: ```java= import greenfoot.*; // (World、Actor、GreenfootImage、GreenfootSound、Greenfoot及MouseInfo) /** * 請在此撰寫 Paddle 類別的說明。 * * @author (你的名字) * @version (日期或版號) */ public class Paddle extends Actor { /** * Act - 隨便 Paddle 想做什麼。 * 每次按下「單步執行」或「執行」按鈕,都會呼叫這個方法。 */ public void act() { // 在這裏寫程式。 MouseInfo mouse=Greenfoot.getMouseInfo(); if(mouse!=null) { setLocation(mouse.getX(),getY()); } } } ``` * 2-1特色/功能介紹 * 打到板子、磚塊都會有音效、會出現結束圖片 3. 遊戲連結 https://www.greenfoot.org/scenarios/32620 4. 遊戲玩法 * 控制板子去擊打磚塊、打完45塊就贏了;如果球掉到板子下就輸了 5. embed嵌入 <iframe src="https://www.greenfoot.org/scenarios/32620?embed=true"width="800" height="600" frameborder="0"></iframe>