CotEditorでオープン中の最前面の書類の内容を書き換えて、ブロック崩し(Breakout)ゲームを行うAppleScriptです。
最前面の書類ウィンドウ内の表示フォントに等幅フォントを指定し、行間をなるべく少なく(0.6行ぐらい)指定した状態で実行してください。
実行はスクリプトエディタなどの実行環境のほか、CotEditorの内蔵スクリプトメニューを利用することも可能です。
CotEditor依存部分はほんの2箇所なので、JeditΩ、mi、BBEdit、Pagesなど幅広いアプリに容易に対応できます。
操作は、パドルの左移動を[option]キー、右移動を[shift]キーで行います。スコアの管理は行なっていません。ミスるとその場でゲームオーバーです。
動作速度は相当に速いので、速すぎてウェイト(delayコマンド)を入れているぐらいです。テキストエディタ上で実行できるブロック崩しとしては、文句のない出来になっていますが、ブロック崩しとしては不満の残る仕上がりです。
開発とテストはMacBook Air M2で行なっているため、delayコマンドの値は同機のパフォーマンスに合わせて調整していますが、Intel Macでは半分以下ぐらいの速度しか出ないので、delayコマンドの値を減らす必要があることでしょう。
Apple Silicon Macの上位機種でコアを貼り合わせてSoCを構成しているものは、シンプルなSoCの構成のものよりもパフォーマンスが下がる可能性もあります。
ボールを打ち返す角度が一定であるため、ブロック崩しといいつつ永遠に消せないブロックが大量に存在しています。実際には、パドルの移動速度を勘案して反射角度を変えるといった処理が必要になることでしょう。
そのままXcode上にもっていって実行していますが、やはりこの反射角度が固定されていることは相当にストレスフルなので、いい感じに書き換えられるとよさそうです。
AppleScript名:Block崩し v3.1.1.scpt |
— – Created by: Takaaki Naganoya – Created on: 2025/06/14 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" — for NSEvent property screenWidth : 30 property screenHeight : 20 property paddleSize : 6 property ballX : 0 property ballY : 0 property paddleX : 0 script spd property blockList : {} property screenLines : {} property lineText : "" end script set paddleX to (screenWidth div 2) – (paddleSize div 2) set ballX to screenWidth div 2 set ballY to screenHeight – 3 set ballDX to 1 set ballDY to -1 set (blockList of spd) to {} — ブロックは「x,y」の文字列形式で格納し、検索高速化 set blockSet to {} repeat with y from 1 to 3 repeat with x from 0 to screenWidth – 1 if (x mod 2 = 0) then set end of blockSet to (x as text) & "," & (y as text) end if end repeat end repeat set text item delimiters to return –CotEditorに依存している部分(1) 差し替え可能 — CotEditorのドキュメント取得 tell application "CotEditor" set dCount to count every document if dCount = 0 then make new document activate set doc to front document display dialog "◀︎[Option] [Shift]▶︎" with title "How to control" end tell delay 2 — ゲームループ repeat — キー入力処理 if my modKeyScan:"Option" then if paddleX > 0 then set paddleX to paddleX – 1 else if my modKeyScan:"Shift" then if paddleX < screenWidth – paddleSize then set paddleX to paddleX + 1 end if — ボール移動 set ballX to ballX + ballDX set ballY to ballY + ballDY — 壁に当たったら反射 if ballX ≤ 0 or ballX ≥ screenWidth – 1 then set ballDX to ballDX * -1 if ballY ≤ 0 then set ballDY to ballDY * -1 — キー入力処理 if my modKeyScan:"Option" then if paddleX > 0 then set paddleX to paddleX – 1 else if my modKeyScan:"Shift" then if paddleX < screenWidth – paddleSize then set paddleX to paddleX + 1 end if — パドル反射 if ballY = screenHeight – 2 and ballX ≥ paddleX and ballX < (paddleX + paddleSize) then set ballDY to ballDY * -1 end if — ブロック衝突 set coordKey to (ballX as text) & "," & (ballY as text) if coordKey is in blockSet then set blockSet to my removeFromList(coordKey, blockSet) set ballDY to ballDY * -1 end if — ゲームオーバー if ballY ≥ screenHeight – 1 then my renderScreen(doc, blockSet, paddleX, ballX, ballY, "GAME OVER") exit repeat end if — 描画 my renderScreen(doc, blockSet, paddleX, ballX, ballY, "") delay 0.06 end repeat — 描画ルーチン(配列構築をまとめて実行) on renderScreen(theDoc, blockSet, paddleX, ballX, ballY, messageText) set screenBuffer to {} repeat with y from 0 to screenHeight – 1 set rowText to "" repeat with x from 0 to screenWidth – 1 if y = ballY and x = ballX then set rowText to rowText & "●" else if y = (screenHeight – 1) and x ≥ paddleX and x < (paddleX + paddleSize) then set rowText to rowText & "〓" else if ((x as text) & "," & (y as text)) is in blockSet then set rowText to rowText & "■" else set rowText to rowText & " " end if end repeat set end of screenBuffer to rowText end repeat if messageText is not "" then set end of screenBuffer to "" set end of screenBuffer to messageText end if –CotEditorに依存している部分(2) 差し替え可能 ignoring application responses tell application "CotEditor" set contents of theDoc to screenBuffer as rich text end tell end ignoring end renderScreen — 指定要素をリストから削除 on removeFromList(target, sourceList) set newList to {} repeat with i in sourceList set j to contents of i if j is not target then set end of newList to j end repeat return newList end removeFromList on modKeyScan:(targKeyName as string) set chkList to {"fn", "", "", "Command", "Option", "Control", "Shift", "Caps"} set theKey to current application’s NSEvent’s modifierFlags() as integer set aBin to binaryEncode(theKey) of me set detectStr to text 9 thru 16 of aBin set detectList to characters of detectStr set aList to {} set aCount to 1 repeat with i in detectList set j to contents of i if j = "1" then set jj to contents of item aCount of chkList set the end of aList to jj end if set aCount to aCount + 1 end repeat return (targKeyName is in aList) as boolean end modKeyScan: –0~2^32範囲の10進数を2進数の文字列に変換して返す on binaryEncode(aNum) if aNum < 0 or 67108864 < aNum then return false set bitStr to "" repeat with i from 31 to 0 by -1 try set aRes to aNum div (2 ^ i) set aNum to aNum mod (aRes * (2 ^ i)) on error set aRes to 0 end try set aRes to aRes as integer set bitStr to bitStr & (aRes as string) end repeat return bitStr end binaryEncode |