// "Snake" // // This macro set demonstrates how to use the arrow keys // as shortcuts, how to generate and use a custom color LUT, // how to create an image centered on the screen and how to // display HTML formatted messages in dialog boxes. // Arrow key support was added in ImageJ 1.54t. In earlier // versions, use use the 'w', 'a', 's' and 'd' keys. // // To run the game, install these macros and use the // Plugins>Macros>New Game command, or press 'n'. // // Hint: Green and cyan are good, red and black frame are bad. var SIZE = 128; // image Size var GREEN = 1; // food var RED = 2; // poison var WHITE = 3; // image background var CYAN = 4; // door to next level var TEXT = 5; // backgroung text color var FRAME = 6; // black image frame var d,x,y,score,level; showMessage ("Snake", "Install these macros and start a new game by pressing 'n'.\n" + "On ImageJ 1.54t or later, use the arrow keys to play.\n" + "On earlier versions, use the 'w', 'a', 's' and 'd' keys.\n" ); exit; macro "New Game [n]" { while (level<=10) { initPlayBoard(); exitLevel=false; while(!exitLevel) { checkCurrentDirection(); if (getPixel(x,y)==GREEN) youAteSomethingGood(); else if ((getPixel(x,y)==RED) || ((getPixel(x,y)==FRAME)&&(d!=""))) youAteSomethingBad(); else if (getPixel(x,y)==CYAN) exitLevel=true; else if (getPixel(x,y)==0) setPixel(x,y,255); else setPixel(x,y,0); wait(150-level*8); updateDisplay(); } beep; level++; } level--; showMessage( "Congratulations!
"+
"Level: "+level+" "+
"Level: "+level+"
"+
"Score: "+score+""
);
close;
}
// arrow key support added in ImageJ 1.54t
macro "Up [up]" {d="U";}
macro "Down [down]" {d="D";}
macro "Left [left]" {d="L";}
macro "Right [right]" {d="R";}
macro "Up (w key) [w]" {d="U";}
macro "Down (s key) [s]" {d="D";}
macro "Right (a key) [a]" {d="L";}
macro "Left (d key) [d]" {d="R";}
function initPlayBoard() {
reuse = nImages>0;
if (reuse) reuse = startsWith(getTitle, "Level ");
if (reuse)
rename("Level "+level+" Score "+score);
else {
call("ij.gui.ImageWindow.centerNextImage");
newImage("Level "+level,"8-bit",SIZE,SIZE,1);
run("In [+]"); run("In [+]"); run("In [+]"); run("In [+]");
}
x=SIZE/2;y=x;d="";
r=newArray(0, 0, 255, 255, 0, 220, 1);
g=newArray(0, 200, 0, 255, 255, 230, 1);
b=newArray(0, 0, 0, 255, 255, 255, 1);
setLut(r,g,b); // a custom LUT with just 6 colors.
setColor(WHITE); fillRect(0,0,SIZE,SIZE); // the white image bckgd
setColor(FRAME); drawRect(0,0,SIZE,SIZE); // the black image frame
setColor(TEXT); // the backgroung text color
setFont("Monospaced",32,"bold no-smoothing");
setJustification("center");
drawString("ImageJ",SIZE/2,SIZE/3);
drawString("Snake",SIZE/2,2*SIZE/3);
drawString("Game",SIZE/2,SIZE);
addItem(15, GREEN); // food
addItem(30, RED); // poison
addItem( 1, CYAN); // door to next level
setPixel(x,y,0); // sets start position to center
}
function addItem(f,type) {
setColor(type);
for (i=0;i
"+
"Score: "+score+""
);
close;
exit;
}
function youAteSomethingGood() {
for (i=x-1;i<=x+1;i++) {
for (j=y-1;j<=y+1;j++) {
if (getPixel(i,j)==1)
setPixel(i,j,WHITE);
}
}
score++;
rename("Level "+level+" Score "+score);
beep();
}