Game of Life

BlitzPlus Forums/BlitzPlus Programming/Game of Life

Heliotrope(Posted 2013) [#1]
Recently I was making a Conway's Game of Life sim. I think that I got the rules right. The problem is that the display keeps flashing. The cause of this is the 'Cls' at line 13 which is fixed by moving it to line 41. I thought that this would fix the problem(it fixed the blinking) but it also made the game lag, skip frames and the rules to break.

The code...

SetBuffer BackBuffer ()
Graphics 600,600,16,2
Dim arr(299,299,2)
	For x = 0 To 299
		For y = 0 To 299
			arr (x,y,0) = Rnd (0,1)
		Next
	Next
Repeat
	Cls
	For x = 1 To 298
		For y = 1 To 298
		
		If arr (x - 1,y - 1,0) = 1 Then surr = surr + 1
		If arr (x - 1,y,0) = 1 Then surr = surr + 1
		If arr (x - 1,y + 1,0) = 1 Then surr = surr + 1
		If arr (x,y - 1,0) = 1 Then surr = surr + 1
		If arr (x,y + 1,0) = 1 Then surr = surr + 1
		If arr (x + 1,y - 1,0) = 1 Then surr = surr + 1
		If arr (x + 1,y,0) = 1 Then surr = surr + 1
		If arr (x + 1,y + 1,0) = 1 Then surr = surr + 1
		
		If surr < 2 Or surr > 3 Then arr(x,y,1) = 0
		If surr = 3 And arr(x,y,0) = 0 Then arr(x,y,1) = 1
		
		surr = 0
		Next
	Next
	x = 0
	y = 0
	For x = 0 To 299
		For y = 0 To 299
		
		arr (x,y,0) = arr (x,y,1)
		arr (x,y,2) = arr (x,y,1)
		Next
	Next 
	
	For x = 0 To 299
		For y = 0 To 299
		If arr (x,y,2) = 1 Then Rect x * 6,y * 6,6,6
		Next
	Next
	
	count = count + 1
	Color 0,255,0
	Text 0,0,"Step: " + count
	Color 255,255,255
	
	Flip  
	Delay 50
	
Until KeyHit (1)
End 


Can anyone see the problem?


xlsior(Posted 2013) [#2]
Try just putting the CLS right after the flip -- at that point you'll start with a clean slate for the next iteration?


Heliotrope(Posted 2013) [#3]
No, CLS just after the flip still has the annoying blinking and just before the flip has a blank screen with occasional glimpses of squares.
Update - putting the CLS after the delay has the same effect of line 13 and changing the VWAIT of the flip seams to have no effect.


xlsior(Posted 2013) [#4]
try flipping the first two lines:

First initialize the graphics mode, THEN tell it to draw to the backbuffer.


Midimaster(Posted 2013) [#5]
upps, xlsior was faster...


Midimaster(Posted 2013) [#6]
your problem is because you wrote the SETBUFFER before GRAPHICS.
With this each FLIP jumps between a invalid and the correct backbuffer.
Always define the buffer after defining the graphic device.

this works:

Graphics 600,600

SetBuffer BackBuffer()

FPS=CreateTimer(20)
Dim arr(300,300,3)
.....
Repeat
	For x = 1 To 298
	.....
	.....
	Text 0,0,"Step: " + count +" " + MilliSecs()
	Color 255,255,255
	Flip  0
	WaitTimer FPS
	Cls
Until KeyHit (1)
End



Heliotrope(Posted 2013) [#7]
Thanks xlsior and midimaster, that explains years of failed graphics progarms.
I always though that the setbuffer went before graphics.


Heliotrope(Posted 2013) [#8]
Also one last thing, is there a way to convert an .exe file into a screensaver as this program would make an awesome screensaver.
idea - if the conversion is possible what about pacman or space invaders that plays itself as a screen saver.


xlsior(Posted 2013) [#9]
Yes, there is.

A screensaver really is just a .exe renamed to .scr, with a couple of minor tweaks -- I don't remember the specifics, but basically there are two command line parameters that your program needs to support. One of them triggers the 'demo' mode where it starts immediately, the other should bring up the configuration/settings screen.

Optionally, there's a flag that tells the program to run and embed itself into a canvas which is what gets shown in the small preview window.

I'm fairly sure that there are full fledged instructions and possibly even a framework for screensavers for blitzmax that has been posted to the forums in the past.
Other than possibly embedding the preview canvas, you should be able to create a full-blown compliant screensaver in Blitzplus with that info as well.


Heliotrope(Posted 2013) [#10]
Thanks xlsior, one last thing is never one last thing.
I have made the screensaver one but have not yet converted it and,
I am making a game of life that you can interact with and I was wondering
if you could change the size of the array so that nothing will ever go off the array.
The gliders reach the edge and are turned into squares.

Update - solved it - You copy the data to a second array and then make the first again, only with two extra rows. then copy the data back with an offset of one.


Heliotrope(Posted 2013) [#11]
Hi again, is there a command in blitz that allows you to create a text file? I swear that I've seen it before!


virtlands(Posted 2013) [#12]
It's elementary.

To create a text file, use any of the following::

fp = WriteFile(filename$)
.... Writeline(fp, " string ");
.... Writeline(fp, " string ");
closefile(fp)

--- I shall check out your game of life soon.


Heliotrope(Posted 2013) [#13]
Virtlands, I hope to have it finished soon.


Heliotrope(Posted 2013) [#14]
Hi all,
As of this post most of the functionality in the code is done. The areas that i am having trouble with are the colour sliders, zoom function, mouse pan and then array resizing. Apart from that it is done. if someone could help me with these areas that would be great.

Here is the code.



Heliotrope(Posted 2013) [#15]
Hi again guys,
With the screensaver idea, is there anything more that I have to do other than rename the .exe file to a .scr file?


Blitzplotter(Posted 2016) [#16]
Just happened across this - nice work ;)