-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from rokostik/ebitengine-game-of-life
Add ebitengine game of life example
- Loading branch information
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
w: 320 | ||
h: 240 | ||
|
||
maxInitAliveCells: w * h / 10 | ||
|
||
world: list produce w * h { } { .concat false } | ||
|
||
loop maxInitAliveCells { | ||
x: random-integer w | ||
y: random-integer h | ||
change\nth! world 1 + x + y * w true | ||
} | ||
|
||
pixels: list produce w * h * 4 { } { .concat 0 } | ||
|
||
|
||
neighbourCount: fn { x y } { | ||
c: 0 | ||
neg: 0 - 1 ; TODO loading literal -1 doesn't work | ||
ixs: vals { neg 0 1 } | ||
for ixs { :i | ||
for ixs { :j | ||
if any { not i = 0 not j = 0 } { | ||
x2: x + i | ||
y2: y + j | ||
if all { not x2 < 0 not y2 < 0 not w <= x2 not h <= y2 } { | ||
if world .nth 1 + x2 + y2 * w { | ||
inc! 'c | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return c | ||
} | ||
|
||
on-update { | ||
print "on-update" | ||
next: list produce w * h { } { .concat false } | ||
|
||
loop w { :x1 | ||
x: x1 - 1 | ||
loop h { :y1 | ||
y: y1 - 1 | ||
pop: neighbourCount x y | ||
ix: 1 + x + y * w | ||
either pop < 2 { | ||
change\nth! next ix false | ||
} { | ||
either all { any { pop = 2 pop = 3 } world .nth ix } { | ||
change\nth! next ix true | ||
} { | ||
either pop > 3 { | ||
change\nth! next ix false | ||
} { if pop = 3 { | ||
change\nth! next ix true } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
world: next | ||
} | ||
|
||
on-draw { :screen | ||
print "on-draw" | ||
loop w * h { :i1 | ||
i: i1 - 1 | ||
ix: 1 + i * 4 | ||
either world .nth 1 + i { | ||
change\nth! pixels ix 255 | ||
change\nth! pixels ix + 1 255 | ||
change\nth! pixels ix + 2 255 | ||
change\nth! pixels ix + 3 255 | ||
} { | ||
change\nth! pixels ix 0 | ||
change\nth! pixels ix + 1 0 | ||
change\nth! pixels ix + 2 0 | ||
change\nth! pixels ix + 3 0 | ||
} | ||
} | ||
|
||
screen .write-pixels pixels | ||
} | ||
|
||
ebitengine-run w h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters