---
# System prepended metadata

title: 20112陳文昱_Greenfoot遊戲(鋼琴模擬)
tags: [Greenfoot遊戲設計]

---

# 20112陳文昱_Greenfoot遊戲(鋼琴模擬)
加入標籤: Greenfoot遊戲設計

於筆記開頭註記完成的功能
:::info
遊戲重點功能
- [x] 12個白鍵+8個黑鍵
- [x] 鋼琴面版文字
- [x] 琴鍵與鍵盤對應文字
- [ ] 自動彈奏曲目
- [ ] 七彩炫光效果
:::

## 遊戲情境(擷圖)
![image](https://hackmd.io/_uploads/BkRuRVhEa.png)

## 程式碼整理
* 鋼琴(場景)
```java=
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
/**
 * 
 * A piano that can be played with the computer keyboard.
 * 
 * @author: M. Kolling
 * @version: 0.1
 */


public class Piano extends World
{
    
    /**
     * Make the piano.
     */
    private String[ ] whiteKeys{"a","s","d","f","g","h","j","k","l",";","'","/"};
    private String[ ] whiteNotes={"3c","3d","3e","3f","3g","3a","3b","4c","4d","4e","4f","4g"};
    private String [ ] blackKeys={"W","E","","T","Y","U","","O","P","","}"};
    private String [ ] blackNotes={"3c#","3d#","","3f#","3g#","3a#","","4c#","4d#","","4f#"};
    public void showMessage()
    {
        GreenfootImage bg = getBackground();
        bg.setColor(Color.WHITE);
        bg.drawString("北中葛泡.鋼琴",0,330);
    }
    public void showkey()
    {
        GreenfootImage bg = getBackground();bg.setColor(Color.GREEN);bg.drawString("                  A                  S                  D                  F                  G                  H                  J                  K                  L                  ;                  '                  /",25,330);
    }
    public Piano() 
    {
        super(800, 340, 1);
        for(int i=0;i<12;i++)
        {
            addObject(new Key(whiteKeys[i],whiteNotes[i]+".wav"),50+63*i,170);
    }
    for(int i=0;i<11;i++)
    {
        if( blackKeys[i]!="")
        {
            addObject(new Blackkey(blackKeys[i],blackNotes[i]+".wav"),85+63*i,115);
        }
        }
        showMessage();
        showkey();
    }
}
```

* 白鍵
```java=
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class Key extends Actor
{
    /**
     * Create a new key.
     */
    private String key;
    private String sound;
    public Key(String keyname,String soundFile)
    {
        key=keyname;
        sound=soundFile;
    }
    /**
     * Do the action for this key.
     */
    
    private boolean isDown;
    public void act()
    {
        if(!isDown && Greenfoot.isKeyDown(key))
        {
            setImage("white-key-down.png");
            Greenfoot.playSound(sound);
            isDown=true;
    }
    if(isDown && !Greenfoot.isKeyDown(key))
    {
        setImage("white-key.png");
        isDown=false;
    }
}
}
```
* 黑鍵
``` java=
import greenfoot.*;  // （World、Actor、GreenfootImage、GreenfootSound、Greenfoot及MouseInfo）

/**
 * 請在此撰寫 Blackkey 類別的說明。
 * 
 * @author （你的名字）
 * @version （日期或版號）
 */
public class Blackkey extends Actor
{ 
    private String key;
    private String sound;
    /**
     * Act - 隨便 Blackkey 想做什麼。
     * 每次按下「單步執行」或「執行」按鈕，都會呼叫這個方法。
     */
    public Blackkey(String keyname,String soundFile)
    {
        key=keyname;
        sound=soundFile;
    }
    private boolean isDown;
     public void act()
    {
     if (isDown==false && Greenfoot.isKeyDown(key))
     {
            setImage("black-key-down.png");
            Greenfoot.playSound(sound);
            isDown=true;
    }
    if(isDown==true && !Greenfoot.isKeyDown(key))
    {
        setImage("black-key.png");
        isDown=false;
    } 
}
}
```

*
## 遊戲連結
https://www.greenfoot.org/scenarios/32388
## 開始玩遊戲(embed嵌入)
* 遊戲說明:
    * 用A、S、D……控制白鍵
    * 用W、E、R……控制黑鍵
<iframe src="https://www.greenfoot.org/scenarios/32388?embed=true""width="810"height="400"frameborder="0"></iframe>
