以下是尋夢版(Dream Maker)的常用語法介紹:
尋夢版的語法類似於C語言,但更簡化,適合遊戲開發。以下是基本結構:
// 註釋
function main() {
// 主程式邏輯
}
int
float
string
bool
array
範例:
int health = 100;
float speed = 2.5;
string name = "Player";
bool isAlive = true;
array inventory = ["sword", "shield", "potion"];
if-else:
if (health > 50) {
print("健康狀態良好");
} else {
print("需要治療");
}
switch-case:
switch (item) {
case "sword":
print("裝備劍");
break;
case "shield":
print("裝備盾牌");
break;
default:
print("未知物品");
}
for迴圈:
for (int i = 0; i < 10; i++) {
print("次數: " + i);
}
while迴圈:
while (health > 0) {
print("戰鬥中...");
health -= 10;
}
定義函數:
function attack(int damage) {
print("造成傷害: " + damage);
}
呼叫函數:
attack(30);
定義類別:
class Player {
int health;
string name;
function takeDamage(int damage) {
health -= damage;
print(name + "受到傷害,剩餘血量: " + health);
}
}
創建物件:
Player hero = new Player();
hero.health = 100;
hero.name = "勇者";
hero.takeDamage(20);
onEvent("keyPress", function(key) {
if (key == "space") {
print("跳躍!");
}
});
讀取檔案:
string content = readFile("data.txt");
print(content);
寫入檔案:
writeFile("log.txt", "遊戲開始");
try {
int result = 10 / 0;
} catch (error) {
print("發生錯誤: " + error);
}
print()
random(min, max)
substring(str, start, end)
sqrt(num)
, pow(base, exponent)
引入模組:
import "math.dm";
定義模組:
module math {
function add(int a, int b) {
return a + b;
}
}
計時器:
setTimeout(function() {
print("3秒後執行");
}, 3000);
動畫與圖形:
drawSprite("hero.png", x, y);
startThread(function() {
print("新執行緒啟動");
});
以上是尋夢版的基本語法與功能介紹,適合用於遊戲開發與腳本撰寫。根據需求,可以進一步深入學習進階功能與API。