Sunday Game Engine 公開

Sunday Game Engine正式公開しました。 - 高橋直樹の仕事と日常の日記
と、言うわけで寝る前に一つ、何も操作できないけど見てるだけのライフゲームを作成してみました。

root.lua

gui.create(64*16,64*16)
gui.caption("Life Game")

field_now = {}
field_next = {}
for i = 1, 64 do
	field_now[i] = {}
	field_next[i] = {}
end
-- 初期化
for x = 1, 64 do
	for y = 1, 64 do
		field_now[x][y] = math.random(2)
	end
end

font.define({name="MS ゴシック", width=16, height=16})

texture.create(1, 16, 16)
texture.fill(1, 0)
texture.create(2, 16, 16)

font.totexture(2, 0, 0, string.byte("@"), 0xffffffff)

generation = 1
while true do -- ループ
	-- 世代数の表示
	gui.caption("Life Game / generation : "..tostring(generation))
	
	
	-- 表示
	draw.setblend(0)
	draw.beginscene()
	for x = 1, 64 do
		for y = 1, 64 do
			draw.all(field_now[x][y], (x-1)*16, (y-1)*16, 255)
		end
	end
	draw.endscene()

	-- 計算
	for x = 1, 64 do
		for y = 1, 64 do
			local count = 0
			for dx = -1, 1 do
				for dy = -1, 1 do
					if dx == 0 and dy == 0 then
					else
						if ( x + dx ) > 0 and ( x + dx ) < 65 then
							if ( y + dy ) > 0 and ( y + dy ) < 65 then
								count = count + field_now[x+dx][y+dy] - 1
							end
						end
					end
				end
				if count == 3 then
					field_next[x][y] = 2
				elseif count == 2 and field_now[x][y] == 2 then
					field_next[x][y] = 2
				else
					field_next[x][y] = 1
				end
			end
		end
	end

	-- コピー
	for x = 1, 64 do
		for y = 1, 64 do
			field_now[x][y] = field_next[x][y]
		end
	end

	-- クリック待ち
	gui.doevents()
	l,r,w,ld,rd=gui.getclick()
	if l then break end
	
	generation = generation + 1
end

思ったこと

ウィンドウのサイズを自由に決められると言うのは素敵なことだなあ。

P.S.

指摘を受けてバグを修正。
それと、SGEを使ってはまった点についていくつか。

gui.create()は最初に行うこと
他のtexture.xxxとかが全部初期化されてしまうので、texture.create()→gui.create()の順でやるとエラーになる。
入力待ちループには必ずgui.doevents()を挟むこと
まともに動かなくなるぞ!

まずはそんなとこかねえ。