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

カテゴリー: GAME

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 

Posted in GAME list | Leave a comment

2005年に作ったゲーム「length」

Posted on 7月 23, 2022 by Takaaki Naganoya

はるかかなた昔、2005年に作ったゲームのソースが出てきました。日本語でコメントを書いてあったのですが、文字化けしていて作った本人にも読めません。作ったまま忘れていたので、掲載しておきます。

# ユーザー名を求める処理だけMac OS X→OS X→macOSと推移してそのまま動作しなかったので、書き換えをおこなっています

ゲーム名は「length」です。指定の文字数のアルファベットを入力すると、システム辞書を検索して、そのアルファベットで始まる英単語を検索します。

ゲームルールは「Count Up」と「Count Down」の2種類があり、入力文字ではじまる英単語がだんだん増えていくように解答するのが「Count Up」、だんだん少なくなるように答えていくのが「Count Down」モードです。


▲ゲームタイトル表示


▲入力アルファベット数の選択。長くなるとヒットする単語が少なくなるので難しい。2か3ぐらいが適当?


▲ゲームルール選択。ヒットする単語数が少なくなっていくように答える「countDown」と、増えていくように答える「countUp」。countUpのほうが簡単


▲ゲームスタート


▲ここで文字入力


▲1単語だけヒットした


▲続いて文字入力


▲5単語ヒットした


▲424単語ヒット!


▲389単語ヒット! 前の答えよりも減ってしまった! Count up失敗!


▲Count Upに失敗したので、ゲームオーバー


▲ハイスコア入力

AppleScript名:length v0.5.scpt
【コメント】 ?uLeng???Q?[??v0.4?v
2005?N 9?? 11?? ???j?? 11:37:00 PM?Åc?o?[?W?????A?b?v

?uLeng???Q?[??v0.3?v
2005?N 9?? 11?? ???j?? 10:05:51 PM?Åc?o?[?W?????A?b?v


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property prefFileName : "jp.piyomarusoft.length"
property dataVer : 1.0
property emptyHscore : {{{0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}}, {{0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}}}

display dialog ("Length GAME" & return & return & "version 1.0 By Piyomaru Software") buttons {"OK"} default button 1 giving up after 3 with title "Welcome!"

set lenList to {2, 3, 4, 5, 6, 7, 8}
set aRes to (choose from list lenList with prompt "please select the length of battle keyword" default items 3)
if aRes = false then return
set bRes to aRes as number
set histNum to {}
set histKeywords to {}
set scoreNum to 0
set ansHist to {}

set ruleList to {"Count Down", "Count Up"}
set ruleRes to (choose from list ruleList with prompt "Select Game Rule" default items "Count Up")
if ruleRes = false then return
set ruleRes to ruleRes as string

if ruleRes = "Count Down" then
  –Count Down
  
set prevNum to 9999999
else
  –Count Up
  
set prevNum to 0
end if

—ÉnÉCÉXÉRÉAÇÃämîFÇ®ÇÊÇ—ÉnÉCÉXÉRÉAèÓïÒÇÃê›íË
set hsList to writeHighScore(0, bRes, "", "", {}, ruleRes) of me
set highScore to item 1 of hsList
set greatPerson to item 2 of hsList

display dialog "GAME START" buttons {"OK"} default button 1 giving up after 2

—ÉÅÉCÉìÉãÅ[Év
repeat
  
  
—ÉLÅ[ÉèÅ[Éhì¸óÕÉãÅ[Évïîï™
  
repeat
    if length of ansHist is not equal to 0 then
      —í èÌèàóù
      
set aText to "History: [" & retArrowText(histNum) of me & "]" & return & return
    else
      —èââÒÇÃÇ›
      
set aText to ""
    end if
    
set titleText to "LENGTH GAME (" & ruleRes & " MODE) "
    
set mesText to ("SCORE:" & scoreNum as string) & return & return & aText & ("Input some keyword in English" & " ( Just " & bRes as string) & " characters.)"
    
set a to text returned of (display dialog mesText default answer "" with title titleText)
    
    
—ï∂éöéÌófiÇÉ`ÉFÉbÉN
    
set kRes to checkAN(a) of me
    
if kRes = false then
      display dialog "Wrong Character…." buttons {"OK"} default button 1
    else
      —í∑Ç≥ÇÉ`ÉFÉbÉN
      
if (length of a) is not equal to bRes then
        set tmpRes to (display dialog "Ooops!" & return & "The length of your keyword is not equal to " & aRes & "!" & return & "Try another keyword." buttons {"QUIT", "Again!"} default button 2 with title "Wrong Length")
        
        
if tmpRes = "QUIT" then
          display dialog "—Goodbye—" buttons {"OK"} default button 1 giving up after 3
          
return
        end if
      else
        —âflãéÇÃâÒìöÇ∆èdï°ÇµÇƒÇ¢Ç»Ç¢Ç©É`ÉFÉbÉN
        
if a is not in ansHist then
          exit repeat
        else
          set tmpRes to (display dialog "Ooops! " & a & " is already answerd! Try another word." buttons {"QUIT", "Again!"} default button 2 with title "Wrong Keyword")
          
          
if tmpRes = "QUIT" then
            display dialog "—Goodbye—" buttons {"OK"} default button 1 giving up after 3
            
return
          end if
        end if
      end if
    end if
  end repeat
  
  
–lookÉRÉ}ÉìÉhÇ≈ÉqÉbÉgÉLÅ[ÉèÅ[ÉhêîÇìæÇÈ
  
set ansRes to getHitWords(a) of me
  
  
—DZDZǩÇÁìñÇΩÇËîªíËÇ∆ǩǢÇÎÇ¢ÇÎ
  
if ruleRes = "Count Down" then
    –Count DownÇÃèÍçá
    
if ansRes < prevNum and ansRes is not equal to 0 then
      set scoreNum to scoreNum + 1
      
set prevNum to ansRes
    else
      —ÉQÅ[ÉÄÉIÅ[ÉoÅ[ÇÃèàóù
      
set tmpRes to (display dialog ("You lose!" & return & " SCORE:" & scoreNum as string) buttons {"OK"} default button 1)
      
      
—ÉnÉCÉXÉRÉAèàóù
      
if highScore < scoreNum then
        —ÉnÉCÉXÉRÉAÉâÅ[ÇÃñºëOÇÃéÊìæÇæÇØ
        
set greatPerson to recHighScore(scoreNum, highScore) of me
        
—é¿ç€Ç…ÉtÉ@ÉCÉãÇ…îΩâf
        
set aDate to do shell script "date +%Y/%m/%d"
        
set dummyList to writeHighScore(scoreNum, bRes, greatPerson, aDate, {ansHist, histNum}, ruleRes) of me
      end if
      
—èIóπ
      
return
    end if
  else
    –Count UpÇÃèÍçá
    
if ansRes > prevNum then
      set scoreNum to scoreNum + 1
      
set prevNum to ansRes
    else
      —ÉQÅ[ÉÄÉIÅ[ÉoÅ[ÇÃèàóù
      
set tmpRes to (display dialog ("You lose!" & return & " SCORE:" & scoreNum as string) buttons {"OK"} default button 1)
      
      
—ÉnÉCÉXÉRÉAèàóù
      
if highScore < scoreNum then
        —ÉnÉCÉXÉRÉAÉâÅ[ÇÃñºëOÇÃéÊìæÇæÇØ
        
set greatPerson to recHighScore(scoreNum, highScore) of me
        
—é¿ç€Ç…ÉtÉ@ÉCÉãÇ…îΩâf
        
set aDate to do shell script "date +%Y/%m/%d"
        
set dummyList to writeHighScore(scoreNum, bRes, greatPerson, aDate, {ansHist, histNum}, ruleRes) of me
      end if
      
—èIóπ
      
return
    end if
  end if
  
  
set the end of ansHist to a
  
set the end of histNum to ansRes
  
end repeat

—ï∂éöéÌófiÅiÉAÉãÉtÉ@ÉxÉbÉgÅjÇÃÉ`ÉFÉbÉN
on checkAN(aKeyword)
  set anList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  
set aKeyword to aKeyword as Unicode text
  
set aKeyword to aKeyword as string
  
set kList to characters of aKeyword
  
repeat with i in kList
    ignoring case
      if i is not in anList then
        return false
      end if
    end ignoring
  end repeat
  
return true
end checkAN

on recHighScore(scoreNum, highScore)
  if scoreNum > highScore then
    set highScore to scoreNum
    
set myShortName to word 1 of (do shell script "who am i")
    
set myHomeDirectory to do shell script "echo ~"
    
set theResponse to current application’s NSFullUserName() as string
    
–set getFullNameScript to "nicl . -read /users/" & myShortName & " realname"
    
–set theResponse to do shell script getFullNameScript
    
set myFullName to (characters 11 through (length of theResponse) of theResponse) as string
    
set pRes to text returned of (display dialog "Input Your Name:" default answer myFullName with title "You made high-score!!")
    
if pRes = false or pRes = "" then
      set greatPerson to "Anonymous"
    else
      set greatPerson to pRes as string
    end if
  end if
  
  
return greatPerson
  
end recHighScore

on retArrowText(aList)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to " -> "
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retArrowText

on getHitWords(a)
  set c to ""
  
set b to ""
  
try
    set b to do shell script "look " & a & " | wc -l"
    
set c to do shell script "look " & a
    
if (b as number) > 30 then
      set cList to paragraphs of c
      
      
if b as number > 300 then
        set limitNum to 300
      else
        set limitNum to b as number
      end if
      
      
set cItem to items 1 thru limitNum of cList
      
      
set curDelim to AppleScript’s text item delimiters
      
set AppleScript’s text item delimiters to ", "
      
set ccItem to cItem as string
      
set AppleScript’s text item delimiters to curDelim
      
      
set c to ccItem
    end if
    
display dialog b & " Hits!" & return & c
  on error
    display dialog "0 hits" buttons {"OK"} default button 1
    
return 0
  end try
  
return b as number
end getHitWords

—ó^ǶÇΩÉfÅ[É^Ç™ÉnÉCÉXÉRÉAÇ…äYìñÇ∑ÇÈèÍçáÇ…ÇÕÅAǪÇÃé|ï\é¶ÇµÇ¬Ç¬ÉfÅ[É^Ç…ãLò^Ç∑ÇÈ

—åãâ Ç∆ǵǃéwíËÉRÅ[ÉXÇÃÉnÉCÉXÉRÉAÉfÅ[É^Çï‘ǵǃÇ≠ÇÈÇÃÇ≈ÅA
—Ç∆ÇËdžǶÇ∏ÅAÉnÉCÉXÉRÉAÇ…Ç»ÇÁÇ»Ç≥ǪǧǻÉfÅ[É^Çì¸ÇÍǃåƒÇ—èoÇπÇŒ
—ÉnÉCÉXÉRÉAÇÃämîFÇ™çsǶÇÈ

on writeHighScore(aScore, aLength, aPerson, aDate, aHist, aRule)
  set pPath to path to library folder from user domain
  
set pPath to (pPath as string) & "Preferences:"
  
set pPath to pPath as Unicode text
  
set pFile to pPath & prefFileName
  
  
tell application "Finder"
    if file "jp.piyomarusoft.length" of alias pPath exists then
      —Ç∑Ç≈Ç…ê›íËÉtÉ@ÉCÉãÇ™ë∂ç›Ç∑ÇÈèÍçá
      
try
        set hScore to read alias pFile as list –anAliasÇ…ÇÕÉtÉ@ÉCÉãÉGÉCÉäÉAÉXÇ™ì¸Ç¡ÇƒÇ¢ÇÈ
      on error
        —âΩÇÁÇ©ÇÃÉtÉ@ÉCÉãä÷åWÇÃÉGÉâÅ[Ç™î≠ê∂ǵÇΩèÍçáÇ÷ÇÃëŒèà
        
set hScore to emptyHscore
      end try
    else
      —Ç‹Çæê›íËÉtÉ@ÉCÉãÇ™ë∂ç›ÇµÇ»Ç¢èÍçá
      
—ïœêîÇÃèâä˙âª
      
–item1 is count up, item2 is Count down
      
set hScore to emptyHscore
    end if
  end tell
  
  
–display dialog aRule
  
  
if aRule is "Count Up" then
    set iSel1 to 1
  else
    set iSel1 to 2
  end if
  
  
set realLength to (aLength – 1)
  
  
if (item 1 of item realLength of item iSel1 of hScore) < aScore then
    –display dialog "You made High-Score" buttons {"OK"} default button 1
    
set item realLength of item iSel1 of hScore to {aScore, aPerson, aDate, aHist}
    
set aRes to write_to_file(hScore, pFile, false) of me
    
if aRes = false then
      display dialog "File I/O Error!" buttons {"OK"} default button 1
      
return
    else
      (*
      –
ÉtÉ@ÉCÉãÉ^ÉCÉvÇèëÇ´ä∑ǶǃÅAÉeÉLÉXÉgÉGÉfÉBÉ^Ç≈äJÇ©ÇÍÇÈDZÇ∆ÇñhÇÆÇΩÇflÇÃÉeÉXÉg
      do shell script "sync"
      –set anAlias to pFile as alias
      tell application "Finder"
        –set kind of file pFile to ""
        –set file type of file pFile to ""
        –set file creator of file pFile to ""
      end tell
      *)

    end if
  end if
  
  
set retList to item realLength of item iSel1 of hScore
  
return retList
  
end writeHighScore

—ÉtÉ@ÉCÉãÇÃí«ãLÉãÅ[É`ÉìÅuwrite_to_fileÅv
—í«ãLÉfÅ[É^ÅAí«ãLëŒè€ÉtÉ@ÉCÉãÅAbooleanÅitrueÇ≈í«ãLÅj
on write_to_file(this_data, target_file, append_data)
  try
    set the target_file to the target_file as text
    
set the open_target_file to open for access file target_file with write permission
    
if append_data is false then set eof of the open_target_file to 0
    
write this_data to the open_target_file starting at eof
    
close access the open_target_file
    
return true
  on error error_message
    try
      close access file target_file
    end try
    
return error_message
  end try
end write_to_file

★Click Here to Open This Script 

Posted in GAME | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

Pixelmator Pro AppleScriptコンテストで優勝

Posted on 10月 11, 2020 by Takaaki Naganoya

LateNight Software主催の「Pixelmator Pro AppleScriptコンテスト」で優勝しました。応募作品はPixelmatorもぐらたたきゲームです。

応募するにあたって、まず最初に、「まともにPixelmator Proの機能を活用する」か「Pixelmator Proの機能を無視する」かの2つの方向のどちらを採用するかを考えました。

100行以内というレギュレーションがけっこう厳しかったので、前者のアプローチだとすぐに限界に達するように思われました。それ以前に、使いこなしてもいない、ほぼ初見のアプリケーションを相手に有用なAppleScriptを書くのはほぼ無理といってよいでしょう。実際に対象のアプリケーションを使いこなして、そのアプリケーションが想定しているワークフローをこなしてみて、自分の意図するデータを作って、その作業の中で不満に思ったり効率がよくない点をAppleScriptで補っていくのが「普通のAppleScript」の書かれ方でしょう。

短期間でコンテストに投稿するとなると、ウケ狙いでアプリケーション全体の機能を理解せず一部の機能に着目してまったく関係のない用途のものを作る必要があると判断しました。

もうちょっと使いこなすための期間が長ければ、Pixelmator Proの機能を使い込んで、Pixelmator Proならではのソリューションも出てきたと思います。

なので、Pixelmator Pro AppleScriptコンテストの賞品であるところのPixelmator Pro v1.8を使えれば、「本来こういうものがあるべき」というScriptを書くことも可能だと思われます。

告知ページには、

To celebrate the introduction of AppleScript support in Pixelmator Pro 1.8 I’m giving away three copies of Pixelmator Pro. 

と、明記されているので「Pixelmator Pro」(macOS版)が賞品だと思っていたのですが、なぜか送られてきたのが「Pixelmator Photo」(iOS版)のコードで、何か手違いが生じただけだと思いたいのですが、どんなものなんでしょう? → 報告したらちゃんとPixelmator Proのコードが届きました。ねんのため

Pixelmatorもぐら叩きゲームについて


◀︎開発中のFMバトラー(1999年)

1999年のMacWorldExpo/Tokyoでデモを行った「FMバトラー」で、すでに「まったくゲーム用ではないアプリケーション(FileMaker Pro)を使ってゲームを作るという経験がありました。

基本的には、Pixelmator Pro書類上に並べたレイヤーの表示状態を制御して、パラパラアニメを表示。その一方でModifier Keyのキースキャンを行なって、ユーザーからの操作を受け取っています。

レギュレーションに「Script Debugger上で動作すること」という項目があり、Modifier Keyのスキャンであれば最前面にいないScript Debuggerでも問題なくスキャンできるため、対応が可能でした。

プログラムは、とにかく短く書くことが課題で、初期段階ではキースキャン部分だけで20行を超えていたものを大幅に短くしています。

もぐら叩きの本体部分もモグラが3段階で迫ってくるという設定を作り、step 1とstep 2の状態でユーザーからの入力があったら「お手つき」的な失敗を行ったという処理をしています。step 3の状態でのみもぐらの撃退が可能で、そのことをデータで表現しています。

set moleList to {{"Mole1", false, 20}, {"Mole2", false, 10}, {"Mole3", true, 20}}

良くも悪くも「Pixelmator Proでゲームを作ろう」という着想自体がすべてであり、プログラム自体はそれほど複雑でも高尚でもありません。

–> Download Piyomaru Whack-3-moles Game(実行に必要なPixelmator Pro書類)

プログラム自体を掲載していてもあまり参考にならないとは思いますが、本来作りたかった「もぐらが同時に3匹出てくるもぐら叩きゲーム」は見てみたいので、より高度なバージョンに書き換えた人がいればぜひ見せてください(^ー^;

最後に、Pixelmator Proもぐらたたきのグラフィックについて。8bit時代のピコピコゲーム(この部分、Google翻訳で非日本語話者に意味が通じるのだろうか?)を彷彿とさせる簡単さで「たいした手数はかけていない」ことが伝わることを重視しています。Pixelmator Proは写真の加工など高い能力を持ったグラフィックソフトなわけですが、あえてグラフィックソフトらしい写実的かつ写真をベースとした生々しいもぐらの絵を使うことは避けました。

また、あからさまにゲームであることがわかるよう背景も単純な色とし、一般的なPixelmator Proの利用シーンとは異なる状況下にあることをユーザーに知らせるように設計しました。絵心のある人ならもっと凝ったグラフィックを描いて表示するはずですが、そういう作り込み要素を排することで「自分にも作れそうだ」という印象を持ってもらうことを重視しています。あと、生き物の殺生を思わせるような生々しい表現(流血とか)は避けています。

AppleScript名:Mole Game v1.1.1a.scpt
–  Created by: Takaaki Naganoya
–  Created on: 2020/09/29
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit" — for NSEvent

set aScore to 0
set aMiss to 0
set maxMole to 3 —change mole attack number
set delayCount to 300 –delay speed. Faster machine needs larger number.

tell application "Pixelmator Pro"
  activate
  
if (exists of document "Piyomaru Whack-3-moles Game") = false then
    display dialog "There is no Piyomaru document" buttons {"OK"} default button 1 with icon 1
    
return
  end if
  
  
tell document "Piyomaru Whack-3-moles Game"
    set visible of layer "Mole1" to false
    
set visible of layer "Mole2" to false
    
set visible of layer "Mole3" to false
    
set visible of layer "Mole3Hit" to false
    
set visible of layer "Mole3Miss" to false
    
    
repeat 10 times
      tell layer "Title"
        set its visible to true
        
set keyRes to keyscanWait(delayCount, true) of me
        
set its visible to false
        
set keyRes to keyscanWait(delayCount, true) of me
        
if keyRes = true then exit repeat
      end tell
    end repeat
    
    
delay 1
    
    
set moleList to {{"Mole1", false, 20}, {"Mole2", false, 10}, {"Mole3", true, 20}}
    
repeat maxMole times
      repeat with i in moleList
        copy i to {layerName, hitLogic, delayTime}
        
set visible of layer layerName to true
        
set keyRes to keyscanWait((random number from 1 to delayTime) * delayCount, hitLogic) of me
        
set visible of layer layerName to false
        
        
if keyRes = true then
          set hitF to true
          
set aScore to aScore + 1
          
beep
          
set visible of layer "Mole3Hit" to true
          
delay 1
          
set visible of layer "Mole3Hit" to false
          
exit repeat
        else if keyRes = false then
          set aMiss to aMiss + 1
          
repeat 3 times
            set visible of layer "Mole3Miss" to true
            
delay 0.5
            
set visible of layer "Mole3Miss" to false
            
delay 0.5
          end repeat
          
set keyRes to true –To Skip See off action
          
exit repeat
        end if
      end repeat
      
      
if keyRes is not equal to true then
        set aMiss to aMiss + 1
        
repeat 3 times
          set visible of layer "Mole3Miss" to true
          
delay 0.5
          
set visible of layer "Mole3Miss" to false
          
delay 0.5
        end repeat
      end if
    end repeat
    
    
display dialog "Score:" & (aScore as string) & return & "Miss:" & (aMiss as string) with title "GAME OVER" buttons {"OK"} default button 1 with icon 1
    
  end tell
end tell

on keyscanWait(delayLoop, hitLogic)
  repeat delayLoop times
    set commandStatus to not ((((current application’s NSEvent’s modifierFlags() as integer) div (current application’s NSShiftKeyMask as integer)) mod 2) = 0)
    
if commandStatus = true then return hitLogic
  end repeat
  
return missing value
end keyscanWait

★Click Here to Open This Script 

Posted in GAME | Tagged 10.15savvy Pixelmator Pro | 1 Comment

Pixelmator Pro AppleScriptコンテストに「もぐら叩きゲーム」を応募

Posted on 9月 29, 2020 by Takaaki Naganoya

LateNight Software主催のPixelmator Pro AppleScriptコンテストに応募してみました。

応募内容は、「もぐら叩きゲーム」です。

Pixelmator Proというグラフィックソフトを使ってもぐら叩きゲーム。不可能ではありません。

Pixelmator ProのAppleScript用語辞書をながめていたら、レイヤーごとの表示/非表示を制御できることがわかったので、レイヤーをセルに見立ててパラパラアニメーション的なものを表示しつつCocoaの機能を使ってキースキャンするとできそうだと思っていました。作ってみたら、問題なくできてしまったと。

このもぐら1匹バージョンのもぐら叩きは1時間ぐらいで出来たのですが、3匹バージョンを試してみたところ、とても100行に収まりきらない感じだったので、3匹バージョン構想は破棄しました(時間かけるような内容でもないですし)。

コンテストのレギュレーションを詳細にチェックすると、1つの外部ファイルの読み込みはOKということになっており、表示用のPixelmator Pro書類がこれに該当するかどうか、という点が気になっていました。

こういうレギュレーションの機敏については、コンテスト開催側の胸先三寸で決められる部分なので、主催者側に判断をおまかせするしかありません。

あとは、環境に応じて時間待ちループ時間を大幅に変えないとダメそうです。AppleScriptObjCのプログラムは、HDDで動いているMac上だと極端に遅くなる傾向があるので、たまたま手元でmacOS 10.15.7を動かしている隔離環境(10.15のテスト環境)だとずいぶん動作が遅い(実際にはAppleScriptの実行が速すぎてウェイト入れまくっているんですが)印象を受けました。

このあたり、実行環境のパフォーマンスを計測して時間待ちの長さを適宜調節すべきなんですが、そういうパフォーマンス計測だけで数十行ぐらいは平気でかかってしまうので、100行以内というレギュレーションだとかなり無理な感じがします。Pixelmator Proに新規書類作成などのコマンドを送ってみて、どのぐらいの時間で処理できるかをその場で計測し、パフォーマンスを計ってウェイト時間を調整するべきなんでしょう。

ただ、コンテスト用のScriptだし、なるべくシンプルなものがいいんでしょう。音がないとさびしいので、iTunesなりMusic.appをコントロールしてBGMを鳴らそうと思っていたんですが、行数が足りなくてダメでした。

Pixelmator Pro向けには、KamenokoのデータをPixelmator Proのネイティブオブジェクトで内容を再現するものを試作して動かしています。ただ、1書類の内容を再構成すると40秒ぐらいかかるのと、Pixelmator Proのオブジェクトで再現するよりPDFベースでコピペしたほうが綺麗だったので、そのプログラムはお蔵入りしています。

この手の「とてもゲームが作れるようには見えない」アプリケーションを使って別のものを仕立てるのは、実は慣れたもので……

Mac World Expo/Tokyoでデモした「FMバトラー」(ファイルメーカーProをAppleScriptでコントロールしてホームページ対戦ゲームに仕立てたデモプログラム)など、これまでにもいろいろ作ってきました。

さらに、Cocoaの機能がAppleScriptから直接呼べるようになったので、複数キーの同時入力受付(正確には、ShiftやControlなどのModifier Keyのキースキャン)といったあからさまにゲームが作りやすそうな機能を利用できるようになったので、過去の無理やりつくったデモ用ゲーム風システムよりも、今回は作りやすかったといえます。

でも、真剣に「Pixelmator Proで込み入ったゲームを作れるんだろうか?」と試してみたところ、そこまでのパワーはありませんでした。オブジェクトの属性値の変更も、様子を見ながら限定されたものを操作するのは向いているものの、大量のオブジェクト操作を行うとアプリケーション側がついてきません。

きしくも、応募作品のもぐら叩きゲームは「奇跡的にちょうどいいバランス」で成立していることがわかりました。

後日、「外部プログラムの威力を利用して画像認識機能をPixelmator Proに足してみては?」と、Web APIとかdarknet(YOLO v3 model)を併用して画像認識した内容をレイヤー名に反映させたりと試していたところ……Pixelmator Proにやられました。

Pixelmator Proにはデフォルトで画像認識機能が入っていて、簡単な動物などの写真はインポートした瞬間に画像認識されて、それっぽいレイヤー名を自動で命名することが判明!

Pixelmator Proのバンドルパッケージを開けてみると、いくつもCore ML Modelのファイルが入っており、深層学習データを併用してさまざまな処理を行なっていることが伺われました。


▲Finder上からPixelmator Pro書類に画像をドラッグ&ドロップすると、その場で画像認識されて「それっぽい」レイヤー名が自動で割り振られる。「犬」とか「鳥」といった名前はPixelmator Proが自動でつけたもの


▲「どうせ認識しないだろ」と思って、ガンダムの画像をドロップしたら「おもちゃ」と認識。たった34MBの深層学習データでどうしたらここまで認識できるのやら。学習データだけでなく、別の手段も併用している???(画像取得元のURLの情報を取っているとか?)

もしかして、各種レイヤーの画像を随時(保存時にでも)画像認識して、ユーザーごとの画像⇄レイヤー名の関連性を学習して、再インポート時には過去の画像から類推して近いものを提案するのでは? などと思ってしまうところです。

これはオシャレです! センスよすぎです。さりげなさすぎて気づきませんでした。出しぬこうと悪だくみしてみたら、相手の方がはるかに上手だったということが判明したのでした。アプリケーションの機能で感動したのは久しぶりです。

Posted in GAME news | Tagged 10.15savvy Pixelmator Pro | Leave a comment

USBゲームコントローラーの情報を取得する

Posted on 3月 18, 2018 by Takaaki Naganoya

MacにUSB接続しているゲームコントローラーの情報を取得するAppleScriptです。

あくまでUSB接続しているコントローラーに限られ、同時にBluetooth接続のゲームコントローラー(PlayStation 3用のDual Shock3)があっても無視されるようです。

AppleScript名:USBゲームコントローラーの情報を取得する
— Created 2017-03-21 18:47:15 +0900 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "GameController"

set aController to current application’s GCController’s controllers()
–>  (NSArray) {​​​​​(_GCController) <GCController 0x6080004a2580 vendorName=’USB Game Device’ deviceHash=0xbe8dba5719e63307>​​​}

set aCon to aController’s firstObject()
if aCon = missing value then return
set aGameCon to aCon’s gamepad()
–>  (_GCMFiGamepadControllerProfile) <_GCMFiGamepadControllerProfile: 0x6000000df870>

set aExGameCon to aCon’s extendedGamepad()
–>  missing value

set aMicGameCon to aCon’s microGamepad()
–>  (_GCMFiGamepadControllerProfile) <_GCMFiGamepadControllerProfile: 0x6000000df870>

set aMotion to aCon’s motion()
–>  missing value

set aVendor to aCon’s vendorName() as string
–>  "USB Game Device"

set anAttached to aCon’s isAttachedToDevice()
–>  false

★Click Here to Open This Script 

Posted in GAME System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

電子書籍(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
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • 有害ではなくなっていたSpaces
  • 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