# 20215蔡昀達_Greenfoot遊戲(個人版打磚塊)
###### tags: `Greenfoot遊戲設計`
## 遊戲情境(擷圖)

## 程式碼整理
* 場景的程式
```java
public class Space extends World
{
public static int brickHit=0;
/**
* Space 類別的物件建構子。
*
*/
public Space()
{
// 建立600x400方格的新 場景,方格大小為1x1像素。
super(800, 500, 1);
addObject(new Ball(), 400, 350);
addObject(new Paddle(), 400, 470);
//產生8*5(40顆磚塊)
for (int i=1;i<=8;i++)
for (int j=1;j<=5;j++)
{
addObject(new Brick(), 90*i, 40+45*j);
}
}
GreenfootSound backgroundMusic = new
GreenfootSound("cat BGM17.mp3");
public void stopped() //判斷程式停止時
{
backgroundMusic.pause();
}
public void started() //判斷程式執行時
{
backgroundMusic.playLoop();
}
}
```
* 球的程式
```java=
public class Ball extends Actor
{
private int motionX=2;
private int motionY=2;
private int brickHit=0;
private GreenfootImage image1,image2;
public Ball()
{
{
image1=new GreenfootImage("mine cat.png");
image2=new GreenfootImage("minecat2.png");
setImage(image1);
}
}
public void act()
{
int newX;
int newY;
newX = getX() + motionX;
newY = getY() + motionY;
if (getImage()==image1)
{
setImage(image2);
}
else
{
setImage(image1);
}
if (newX > 800)
{
motionX = -2;
} else if (newX < 0) {
motionX = 2;
}
if (newY > 500)
{
motionY = -2;
} else if (newY < 0)
{
motionY = 2;
}
setLocation(newX, newY);
Actor brick = getOneIntersectingObject(Brick.class);
if (brick != null)
{
motionY = -motionY;
brickHit++;
getWorld().removeObject(brick);
}
Actor paddle = getOneIntersectingObject(Paddle.class);
if (paddle != null)
{
motionY = -motionY;
}
if (brickHit==40) {
getWorld().addObject(new 過關(),400,300);
getWorld().removeObjects(getWorld().getObjects(Brick.class));
getWorld().removeObjects(getWorld().getObjects(Ball.class));
getWorld().removeObjects(getWorld().getObjects(Paddle.class));
getWorld().removeObjects(getWorld().getObjects(Space.class));
Greenfoot.stop();
}
if (newY>500)
{
getWorld().addObject(new 慘敗(),400,300);
getWorld().removeObjects(getWorld().getObjects(Brick.class));
getWorld().removeObjects(getWorld().getObjects(Ball.class));
getWorld().removeObjects(getWorld().getObjects(Paddle.class));
getWorld().removeObjects(getWorld().getObjects(Space.class));
Greenfoot.stop();
}
}
}
```
* 板子的程式
```java=
public class Paddle extends Actor
{
/**
* Act - 隨便 Paddle 想做什麼。
* 每次按下「單步執行」或「執行」按鈕,都會呼叫這個方法。
*/
public void act( )
{
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse!= null)
{
setLocation(mouse.getX(), getY());
}
}
}
```
### 特色/功能介紹
* 背景音樂
* 過關,結束畫面
## 遊戲玩法
* 移動滑鼠來操控掃地機貓,並用礦貓把每個未來貓打掉。
## 遊戲連結
https://www.greenfoot.org/scenarios/30520
## 開始玩遊戲
<iframe src="https://www.greenfoot.org/scenarios/30520?embed=true"width="800" height="600" frameborder="0"></iframe>