Skip to content

Commit

Permalink
added scaled demo for conways game of life
Browse files Browse the repository at this point in the history
  • Loading branch information
refaktor committed Dec 13, 2023
1 parent d13d6e4 commit 617a2ba
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contrib
4 changes: 2 additions & 2 deletions examples/ebitengine/life.rye
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
w: 320
h: 240
w: 100
h: 100

maxInitAliveCells: w * h / 10

Expand Down
91 changes: 91 additions & 0 deletions examples/ebitengine/life2.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
w: 80
h: 64
scale: 5

maxInitAliveCells: w * h / 7

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
}

set-layout-scale scale

ebitengine-run w * scale h * scale


0 comments on commit 617a2ba

Please sign in to comment.