Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

CotEditor用ブロック崩しゲーム

Posted on 6月 14 by Takaaki Naganoya

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

★Click Here to Open This Script 

More from my site

  • Twitter投稿Twitter投稿
  • Safari v16がリリースされるSafari v16がリリースされる
  • 人類史上初、魔導書の観点から書かれたAppleScript入門書「7つの宝珠」シリーズ開始?!人類史上初、魔導書の観点から書かれたAppleScript入門書「7つの宝珠」シリーズ開始?!
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI ScriptingmacOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • フルパスからファイル名を取得するフルパスからファイル名を取得する
  • 文字列から丸つき数字のみ抽出する文字列から丸つき数字のみ抽出する
(Visited 6 times, 6 visits today)
Posted in GAME list | Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 15, Sequoia
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AppleScriptによる並列処理
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • macOS 15でも変化したText to Speech環境
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • デフォルトインストールされたフォント名を取得するAppleScript
  • macOS 15 リモートApple Eventsにバグ?
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • AppleScript入門① AppleScriptってなんだろう?
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • Script Debuggerの開発と販売が2025年に終了
  • NSObjectのクラス名を取得 v2.1
  • 有害ではなくなっていたSpaces
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • AVSpeechSynthesizerで読み上げテスト

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (194) 14.0savvy (147) 15.0savvy (135) CotEditor (66) Finder (51) iTunes (19) Keynote (119) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (55) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC