Log In  

Cart #10169 | 2015-04-24 | Code ▽ | Embed ▽ | No License
3

I'm not 100% sure this is working correctly but it was a good first try.

-- conway's game of life
-- by ian seyler

maxinit=1400

cls()
for t=1,maxinit do
 x=rnd(127)
 y=rnd(127)
 pset(x,y,15)
end

while(1) do
 for y=0,127 do
  for x=0,127 do
   neigh=0
   for tx=-1,1 do
    for ty=-1,1 do
     if(pget(x+tx,y+ty)==15) then
      neigh+=1
     end
    end
   end
   if(pget(x,y)==15) then
    neigh-=1
    if(neigh<2 or neigh>3) then
     pset(x,y,0)
    end
   elseif(pget(x,y)==0) then
    if(neigh==3) then
     pset(x,y,15)
    end
   end
  end
 end
end
P#10170 2015-04-23 22:36 ( Edited 2015-04-25 02:06)

Nice! It isn't 100% accurate since you're reading & writing to the same buffer as you loop through the pixels. This means that the update to one row affects the next one down.
To fix this you would need to buffer that row, or write to a separate buffer. I wonder if you could use the sprite memory for this?
Thanks for sharing your code :)

P#10172 2015-04-24 01:51 ( Edited 2015-04-24 05:51)

Ah, that makes sense. Ideally I would like to read the pixels from the screen and write new pixels to the back buffer. Then I could just call flip() and run through the screen pixels again.

How does the back buffer work? Should we be able to use it?

P#10179 2015-04-24 11:17 ( Edited 2015-04-24 15:17)

Well, what you could do is write all your cells to a table, then each time the table is finished, you iterate through the table and draw the cells.
But that would make the program quite a bit larger.

P#10200 2015-04-24 22:06 ( Edited 2015-04-25 02:06)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 14:24:02 | 0.010s | Q:15