昨日の続き。
なんで昨日いきなりライフゲームなんぞ実装したかと言うと、なんかglitchに使えそうだったからです。寝る前にふと思いついちゃいました。てなわけで今日ぐっちゃぐちゃになりましたけど、割と構造化を意識した感じのコードにしてたんです。コードグリッチ!
以下詳細。

やべー超楽しい(俺が)。
機能・フロー:
1) たんぶらうざのフィードを取得し、画像を全件取得。
2) ライフゲームを回す
3) ライフゲーム下、各ノードが特定の条件下で周囲のノードが持つピクセルを自分にコピーするようにする。>自分が生きている場合、周囲の生きているノードから適当にコピー
4) クリックで画像変更
ダウンロード:
lifegame_glitching.app.zip(mac)
lifegame_glitching.exe.zip(win)未動作確認
# ビデオ見てもらうとわかるけどめっちゃ重いソフトウェアなので、しばらく待って変化が無ければクリックしてみたり終了してみたり。ネットワークにも影響されます。
ソース:
import processing.xml.*;
import java.util.regex.*;String feed_url = "http://mao.s151.xrea.com/tumbrowser/index.xml";
int width = 400;
int height = 600;
int px_size = 1;XMLElement feed;
String[] image_urls;
int image_idx = 0;
int image_idx_max = 0;
PImage image_src;Bug[] buf_bugs;
Bug[][] bugs;void setup()
{
size(width, height);init_image_url();
init_image_src();fill(255);
bugs = new Bug[width / px_size][height / px_size];
buf_bugs = new Bug[8];for(int x = 0; x < width / px_size; x++){
for(int y = 0; y < height / px_size; y++){
bugs[x][y] = new Bug(x * px_size, y * px_size);
}
}
frameRate(2);
}void init_image_src(){
println(image_idx);
image_src = loadImage(image_urls[image_idx]);
image_src.loadPixels();
if(image_idx == image_idx_max){
image_idx = 0;
}
else{
image_idx++;
}
float w = width;
float h = height;
float iw = image_src.width;
float ih = image_src.height;
float scale;
int x1, y1, x2, y2;
if(w / iw > h / ih){
scale = w / iw;
float rest = scale * ih - h;
x1 = 0;
x2 = width;
y1 = floor(-rest / 2);
y2 = floor(rest / 2) + height;
}
else{
scale = h / ih;
float rest = scale * iw - w;
x1 = floor(-rest / 2);
x2 = floor(rest / 2) + width;
y1 = 0;
y2 = height;
}
imageMode(CORNERS);
image(image_src, x1, y1, x2, y2);
}void init_image_url(){
XMLElement feed = new XMLElement(this, feed_url);
XMLElement[] contents = feed.getChildren("channel/item/content:encoded");
image_urls = new String[contents.length];
Pattern pattern_url = Pattern.compile("http://data.tumblr.com/[^¥.]+...."); // cannot escape quotesimage_idx_max = contents.length - 1;
for(int i = 0 ; i < contents.length ; i++){
String content = contents[i].getContent();
Matcher matcher_url = pattern_url.matcher(content);
if(matcher_url.find()){
image_urls[i] = matcher_url.group(0);
}
println(image_urls[i]);
}
}void draw()
{
int[] idx_buf;
int x_m, x_p, y_m, y_p;
for(int x = 0; x < width / px_size; x++){
for(int y = 0; y < height / px_size; y++){
x_m = v(x - 1, width / px_size);
x_p = v(x + 1, width / px_size);
y_m = v(y - 1, height / px_size);
y_p = v(y + 1, height / px_size);
buf_bugs[0] = bugs[x_m][y_m];
buf_bugs[1] = bugs[x][y_m];
buf_bugs[2] = bugs[x_p][y_m];
buf_bugs[3] = bugs[x_m][y];
buf_bugs[4] = bugs[x_p][y];
buf_bugs[5] = bugs[x_m][y_p];
buf_bugs[6] = bugs[x][y_p];
buf_bugs[7] = bugs[x_p][y_p];bugs[x][y].update(buf_bugs);
}
}
for(int x = 0; x < width / px_size; x++){
for(int y = 0; y < height / px_size; y++){
bugs[x][y].draw();
}
}
}void mouseReleased(){
background(0);
init_image_src();
}int v(int i, int max){
if(i >= 0){
if(i < max){
return i;
}
else{
return 0;
}
}
else{
return max - 1;
}
}class Bug {
public int idx, idy;
int live_flag, buf_live_flag;
int bug_color = 0;
Bug living_bug = this;
Bug(int x, int y){
idx = x;
idy = y;
buf_live_flag = live_flag = (random(0, 3) < 1) ? 1 : 0;
}
public int living(){
return buf_live_flag;
}
public void update(Bug[] bugs){
int count_living = 0;
for(int i = 0 ; i < 8 ; i++){
if(bugs[i].living() == 1){
count_living++;
living_bug = bugs[i];
}
}
if(live_flag == 1 && (count_living < 2 || count_living > 3)){
live_flag = 0;
}
else if(live_flag == 0 && count_living == 3){
live_flag = 1;
}
}
public void draw(){
if(live_flag == 1){
int cidx = living_bug.idx;
int cidy = living_bug.idy;
imageMode(CORNER);
copy(cidx, cidy, px_size, px_size, idx, idy, px_size, px_size);
}
buf_live_flag = live_flag;
}
}
コメント (17)
やるね。これ。
投稿者: NZM | 2008年01月31日 04:16
thx
つかproce55ingおもしろいよ
http://processing.org/
かなり手軽にお絵描き出来るので、プレゼン用に動的フィギュア作成とかにも使えるかも。
物理エンジンライブラリ:
http://www.cs.princeton.edu/~traer/physics/
投稿者: negipo | 2008年01月31日 09:03
いや、すごい面白そう。
一段落したらやってみよう。
投稿者: NZM | 2008年01月31日 23:08
おうけ。出来たら見せて
てかブログ書いてよ
投稿者: negipo | 2008年02月01日 01:15
うん、できたら見せる。
ついでにJAVAも勉強しよう。
ブログはつける気がおこらない。
投稿者: NZM | 2008年02月01日 09:32
processingの記法ってJavaとちょっと違う気がする
エクスポートするときJavaのソースコードも一緒にできるから、見てみると良いかも
投稿者: negipo | 2008年02月01日 10:14
http://tetraleaf.com/p5_reference_alpha/environment/index.html
このJAVA modeってやつとか使えるといいよね。
ってかJAVAで開発されてるんじゃないの?
投稿者: NZM | 2008年02月01日 18:35
基本的にはちょっと特殊なIDEの附属したjavaのライブラリじゃないのかな?よくわかんないけど。
投稿者: negipo | 2008年02月02日 01:48
http://crystal.jemmy.co.jp/
これ、お前の彼女だろ?
投稿者: NZM | 2008年02月06日 23:59
あ、あれ?・・・・・・何かすげー似てるような
てかちょっと検索したら50歳じゃねーか
これ見たら怒るのかなあ
投稿者: negipo | 2008年02月07日 01:03
NZMさんのことだから、絶対人間じゃない(鬼太郎にでてくる妖怪とかだろう)と思って、すごい恐る恐るリンクをクリックしたら、あらびっくり。
逆に大きな喜びを感じてしまいましたよ・・・
たとえ50歳でも人間だし、ましてやエステ会のカリスマ(?)ですよ!
投稿者: 匿名 | 2008年02月08日 15:02
なんだか君が不憫で仕方ないよ
明日からよろしくお願い致します
投稿者: negipo | 2008年02月08日 17:08
これまでに俺がした形容のなかで最もましな自信ある。
投稿者: NZM | 2008年02月08日 18:50
僕の彼女の心をぐたぐたにしておきながらいばるなよw
投稿者: negipo | 2008年02月08日 20:02
まじっすか!?
投稿者: なかたたくほろ | 2008年02月29日 10:01
まじっすか!?
投稿者: なかたたくほろ | 2008年02月29日 10:01
まじっす
投稿者: negipo | 2008年02月29日 11:41