ComputerCraft > サンプルプログラム





サンプルプログラムについて

赤文字は実際に利用する際には変更しなければならない部分です、このままでは動作しません。



サンプルプログラム


パスワードドア

正しいパスワードを入力するとN秒間レッドストーン動力を出力するプログラムです。
passwordに正しいパスワード、NにRS動力を出力する時間、OutputにRS動力を出力する面を入れて下さい。
write()内の\nは改行を意味します。

pass = ("password")
write("Enter password.\n")
a = read()
if a == pass then
write("Password correct!\n")
rs.setOutput("Output", true)
sleep(N)
rs.setOutput("Output", false)
else
write("Password incorrect!\n")
end


NOT回路

Inputから入力を受けている時は何も出力せず、入力を受けてない時はOutputから出力するプログラムです。
プログラムを終了させる時はctrl+Tを利用して下さい。

if rs.getInput("Input") == false then
rs.setOutput("Output", true)
end
while true do
os.pullEvent("redstone")
if rs.getInput("Input") == true then
rs.setOutput("Output", false)
else
rs.setOutput("Output", true)
end
end


ランダムパルス回路

ランダムな間隔で0.2秒のパルスをOutputから出力するプログラムです。

while true do
a = math.random(1, 10)
if a = 5 then
rs.setOutput("Output", true)
sleep(0.2)
rs.setOutput("Output", false)
sleep(0.2)
else
sleep(0.4)
end
end


ワイヤレスレッドストーンスイッチ

送信側でプログラムを起動する度に受信側でレッドストーンのON/OFFが切り替わるプログラムです。
modemにはモデムを設置している面、idには受信側のコンピューターID又はラベル、OutputにはRS動力を出力する面を入れて下さい。

送信側プログラム
rednet.open("modem")
rednet.send(id, "")

受信側プログラム
rednet.open("modem")
while true do
os.pullEvent("rednet_message")
rs.setOutput("Output", true)
os.pullEvent("rednet_message")
rs.setOutput("Output", false)
end


鉄道建設プログラム

マイニングタートル専用の自動レール敷設プログラムです。
プログラムを起動して、数字を入力するとそのブロック数レールを敷設します。
障害となるブロックは全て採掘し、足場が無ければ設置します。
タートルのインベントリの1番目に足場となるブロック、2番目にレールを入れて下さい。
なおスタックの都合上このプログラムでは65ブロック以上の敷設はできません。
またMOBにぶつかったりした場合正常に動作しない恐れがあります。

function reversedirection()
turtle.turnRight()
turtle.turnRight()
end

function dig(n)
turtle.up()
for i=1,n do
while turtle.detect() == true do
turtle.dig()
sleep(0.5)
end
turtle.forward()
turtle.digDown()
end
turtle.down()
end

function construction(n)
for i=1,n do
if turtle.detectDown() == false then
turtle.select(1)
turtle.placeDown()
turtle.up()
else
turtle.up()
end
turtle.select(2)
turtle.placeDown()
turtle.forward()
turtle.down()
end
end

a = read()
dig(a)
reversedirection()
construction(a)
reversedirection()


Turtle遠隔操作プログラム

WirelessTurtleを遠隔操作するプログラムです。
送信側のインターフェースを開いておく必要がある為実用性は疑問ですが、
rednetAPIを用いたプログラムの参考にして頂ければと思います。
modemにはモデムの設置面、idには受信側Turtleのid又はラベルを入れて下さい。

送信側プログラム
function keyconvert(k)
if k == 17 then
return "Forward"
elseif k == 31 then
return "Back"
elseif k == 30 then
return "TurnLeft"
elseif k == 32 then
return "TurnRight"
elseif k == 42 then
return "Down"
elseif k == 57 then
return "Up"
elseif k == 18 then
return "Exit"
else
return "This key is not assigned."
end
end

print("w = Forward\ns = Back\na = TurnLeft\nd = TurnRight\nshift = Down\nspace = Up\ne = Exit")
rednet.open("modem")
while true do
a, b = os.pullEvent("key")
c = keyconvert(b)
if c == "This key is not assigned." then
print(c)
else
rednet.send(id, c)
print(c)
if c == "Exit" then break end
end
end

受信側プログラム
rednet.open("right")
while true do
a, b = rednet.receive()
if b == "Exit" then break end
if b == "Forward" then
turtle.forward()
elseif b == "Back" then
turtle.back()
elseif b == "TurnLeft" then
turtle.turnLeft()
elseif b == "TurnRight" then
turtle.turnRight()
elseif b == "Down" then
turtle.down()
elseif b == "Up" then
turtle.up()
end
end



タグ:

+ タグ編集
  • タグ:
最終更新:2012年04月04日 20:21
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。