hey guys,
i programmed the game of life for fun tonight,
read up what it is here : http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
#CONSTANT FIELDS 65
#CONSTANT ALIVE 1
#CONSTANT DEAD 2
global SCREENW as integer
global SCREENH as integer
load dll "user32.dll",1
SCREENW = call dll(1,"GetSystemMetrics",0)
SCREENH = call dll(1,"GetSystemMetrics",1)
delete dll 1
set display mode SCREENW, SCREENH, 32
sync on : sync rate 0 : sync
type tField
on as boolean
changed as boolean
nb as integer :`lebendige Nachbarn
endType
dT as float
updateTime as float
fieldSize = SCREENH/FIELDS
fieldX = (SCREENW-fieldSize*FIELDS)/2
fieldY = (SCREENH-fieldSize*FIELDS)/2
dim field(FIELDS-1, FIELDS-1) as tField
dim tempField(FIELDS-1, FIELDS-1) as boolean
//deadImage
box 0, fieldSize/2, fieldSize, fieldSize, rgb(128, 30, 100), rgb(220, 80, 80), rgb(128, 30, 100), rgb(220, 80, 80)
ink 0xffee8866, 0 : box 0, 0, fieldSize, fieldSize/2
ink 0xaa0077 : line 0, 0, fieldSize-1, 0 : line 0, 0, 0, fieldSize-1
line 0, fieldSize-1, fieldSize-1, fieldSize-1 : line fieldSize-1, 0, fieldSize-1, fieldSize-1
get image DEAD, 0, 0, fieldSize, fieldSize, 1
//aliveImage
box 0, fieldSize/2, fieldSize, fieldSize, rgb(30, 128, 100), rgb(80, 220, 80), rgb(30, 128, 100), rgb(80, 220, 80)
ink 0xff88ee66, 0 : box 0, 0, fieldSize, fieldSize/2
ink 0x00aa77 : line 0, 0, fieldSize-1, 0 : line 0, 0, 0, fieldSize-1
line 0, fieldSize-1, fieldSize-1, fieldSize-1 : line fieldSize-1, 0, fieldSize-1, fieldSize-1
get image ALIVE, 0, 0, fieldSize, fieldSize, 1
sprite DEAD, 0, 0, DEAD : set sprite DEAD, 0, 0 : hide sprite DEAD
sprite ALIVE, 0, 0, ALIVE : set sprite ALIVE, 0, 0 : hide sprite ALIVE
//Vorbereitung
for x = 0 to FIELDS-1
for y = 0 to FIELDS-1
field(x, y).on = 0
field(x, y).changed = 1
next y
next x
edit = 1
cls 0x221111 : ink 0xff55dd99, 0
//Mainloop
do
dT = (timer()-oldTime)*0.001
oldTime = timer()
if edit = 1
if mouseclick()=2
for x = 0 to FIELDS-1
for y = 0 to FIELDS-1
if field(x, y).on
field(x, y).on = 0
field(x, y).changed = 1
endif
next y
next x
endif
mx = (mousex()-FIELDX)/fieldSize
my = (mousey()-FIELDY)/fieldSize
mc = mouseclick()
if mx >= 0 and mx <= FIELDS-1
if my >= 0 and my <= FIELDS-1
if mc = 1
field(mx, my).on = 1
field(mx, my).changed = 1
endif
if mc = 3
field(mx, my).on = 0
field(mx, my).changed = 1
endif
endif
endif
if spacekey() then edit = 0
else
if mouseclick() then edit = 1
endif
inc updateTime, dT
if updateTime > 0.03
if edit = 0
//process
for x = 0 to FIELDS-1
for y = 0 to FIELDS-1
field(x, y).nb = 0
for x2 = -1 to 1
for y2 = -1 to 1
if x2 <> 0 or y2 <> 0
if x+x2 >= 0 and x+x2 <= FIELDS-1
if y+y2 >= 0 and y+y2 <= FIELDS-1
inc field(x, y).nb, field(x+x2, y+y2).on
endif
endif
endif
next y2
next x2
if field(x, y).on
if field(x, y).nb < 2 or field(x, y).nb > 3
tempField(x, y) = 0
field(x, y).changed = 1
endif
else
if field(x, y).nb = 3
tempField(x, y) = 1
field(x, y).changed = 1
endif
endif
next y
next x
for x = 0 to FIELDS-1
for y = 0 to FIELDS-1
if field(x, y).changed
field(x, y).on = tempField(x, y)
endif
next y
next x
endif
//draw
for x = 0 to FIELDS-1
for y = 0 to FIELDS-1
if field(x, y).changed
if field(x, y).on
paste sprite ALIVE, fieldX+x*fieldSize, fieldY+y*fieldSize
else
paste sprite DEAD, fieldX+x*fieldSize, fieldY+y*fieldSize
endif
field(x, y).changed = 0
endif
next y
next x
text 0, 0, "mousebutton1 : draw"
text 0, 14, "mousebutton2 : clear"
text 0, 28, "spacebar : simulate"
updateTime = 0
endif
sync : loop
Like it says draw with your left mousebutton, delete everything with your right mousebutton and hit the spacebar to start the simulation!
It's pretty fun to play around with it
edit/And don't forget that you can change the FIELD-Constant which defines the edge-length of the square, on my PC even something like 300 runs fairly smooth