Hello Everyone,
This is coming from a little program written for fun in this thread:
http://forum.thegamecreators.com/?m=forum_view&t=188948&b=7
When the grid gets over a certain size, it drags the system down really slow! I have taken out the size restrictions so try making a grid that is 75x75 with 10 colors and just watch the timer, you will see what I mean. It isn't doing the calculations on the fly for what color or size all the squares are since they are held in an array. Squares are pretty basic and I thought they would be drawn much faster than that even in a double loop (look at the drawboards() function). Any ideas?
Rem Project: flood game
Rem Created: Sunday, September 04, 2011
Rem ***** Main Source File *****
rem create custom types to hold info
sync on:sync rate 60
type Rect
rLeft as integer
rRight as integer
rTop as integer
rBottom as integer
endtype
type mButton
bRect as rect
Pressed as integer
bcolor as dword
Caption as string
ColorPressed as dword
endtype
type mSquare
bRect as rect
bcolor as dword
endtype
type mGameBoard
bRect as rect
endtype
rem create some variables to hold stuff
rem arrays are created in the InitialMe function after user input!!!!
global NumColors as integer
global XNum as integer
global YNum as integer
global GameStarted as integer
global TimePast as dword
global TurnCounter as integer
global GameTimer as dword
global TimeHolder as dword
global ScreenChanged as integer
savemouse as integer
rem intialize the startup variables and such
randomize timer()
GameStarted = 0
savemouse = 0
rem ******************MAIN LOOP******************************************
do
if escapekey() = 1 then end
if GameStarted = 0 then InitialMe()
GameStarted = 1
m_xy = mousecollision()
if mouseclick() = 1 and savemouse<>1 then buttonaction(m_xy)
if mouseclick()<> 1 then savemouse = 0 else savemouse = 1
rem screenchanged check added to speed up large boards by not redrawing everytime
rem if ScreenChanged = 1
drawboards()
rem ENDIF
updateinfo()
checkwin()
ScreenChanged = 0
sync
LOOP
end
REM ***********************************************************************
function ChangeColors(i as integer,j as integer,newcolor as dword, oldcolor as dword)
if i>=1 and i<=XNum and j>=1 and j<=YNum and allsquares(i,j).bcolor<>newcolor and allsquares(i,j).bcolor = oldcolor
allsquares(i,j).bcolor = newcolor
if i<XNum then changecolors(i+1,j,newcolor,oldcolor)
if i>1 then changecolors(i-1,j,newcolor,oldcolor)
if j<YNum then changecolors(i,j+1,newcolor,oldcolor)
if j>1 then changecolors(i,j-1,newcolor,oldcolor)
ENDIF
ENDFUNCTION
function UpdateInfo()
GameTimer = (Timer()-TimeHolder)/1000
ink rgb(255,255,255),rgb(255,255,255)
text 20, 60, "Number of Moves: " + str$(TurnCounter)
text 20, 80, "Time Played: " + str$(GameTimer)
sync
ENDFUNCTION
function checkwin()
basecolor as dword
colorcount as integer
basecolor = allsquares(1,1).bcolor
for i = 1 to XNum
for j=1 to YNum
if allsquares(i,j).bcolor = basecolor then colorcount = colorcount+1
NEXT
NEXT
if colorcount = XNum*YNum
cls
SET TEXT SIZE 30
CENTER TEXT (SCREEN WIDTH()/2),(SCREEN HEIGHT()/2), "!!!You Win!!!"
set TEXT size 12
CENTER TEXT (SCREEN WIDTH()/2),((SCREEN HEIGHT()/2)+60),"Press any key to continue"
sync
WAIT KEY
initialme()
ENDIF
ENDFUNCTION
function drawboards()
cls
rem draw it all to screen
rem draw the squares
for i = 1 to XNum
for j = 1 to YNum
drawSquare(i,j)
next
NEXT
rem draw the buttons
buttonNum = 5 + NumColors
for i = 1 to buttonnum
drawbutton(i)
NEXT
ENDFUNCTION
function ButtonAction(BNum as integer)
if BNum = 1 then InitialMe()
if BNum = 2
cls
SET TEXT SIZE 14
print "This is an example of a simple 'Flood It' game where you try to flood the screen with color"
print "The rules are pretty simple. Color changes starting from the top left box and spread outward."
print "If the surrounding boxes share color then they a change as well. The amount of playing space"
print "will eventually become filled with 1 color. When that happens, you win!"
print ""
print "The more colors you choose (up to 15 are possibe!) the more difficult the game. The greater"
print "the size of the playing grid (up to 25x25 is possible!) the more difficult the game. If the"
print "color selection appears to be running slow either reduce your grid size or hold down the"
print "left mouse button until selection is made"
print "What can you master?"
print ""
print "Press any key to return to your game"
sync
wait key
ENDIF
if BNum >= 5
for i = 1 to XNum
for j = 1 to YNum
changecolors(i,j,allbuttons(BNum).bcolor,allsquares(1,1).bcolor)
NEXT
NEXT
endif
TurnCounter = TurnCounter+1
ScreenChanged = 1
ENDFUNCTION
rem checks the current mouse location to see if it is on a button
function MouseCollision()
returned = 0
numbuttons = 5+NumColors
for i = 1 to numbuttons
if mousex()>AllButtons(i).brect.rleft then a = 1 else a = 0
if mousex()<AllButtons(i).brect.rright then b = 1 else b = 0
if mousey()>AllButtons(i).brect.rtop then c = 1 else c = 0
if mousey()<AllButtons(i).brect.rbottom then d = 1 else d = 0
returned = a+b+c+d
if returned = 4
returned = i
exit
else
returned = 0
ENDIF
NEXT i
ENDFUNCTION returned
function InitialMe()
BlockWidth as integer
BlockHeight as integer
tsquares as integer
ThisBoard as mGameboard
cls
sync
rem get user input for what they want their board to look like
input "How many colors in play? (less colors = easier game) "; NumColors
print "What grid size would you like to play? (smaller dimensions = easier game) "
input "X value: "; XNum
input "Y value: "; YNum
rem if XNum > 25 then XNum = 25
if XNum <= 0 then XNum = 1
rem if YNum > 25 then YNum = 25
if YNum <= 0 then YNum = 1
rem if NumColors > 15 then NumColors = 15
sync
cls
rem !!!!!!!!!!!!!arrays used throughout the code!!!!!!!!!!!!!!!!!!!
buttonNum = 5 + NumColors
dim AllButtons(buttonNum) as mbutton
dim AllSquares(XNum,YNum) as msquare
dim RandColors(NumColors)
rem ************************************************************************************************************************
rem set the initial playing board dimensions based on screen size, right half is playing area, left half is buttons and data
thisboard.brect.rleft = screen width()/2
thisboard.brect.rright = thisboard.brect.rleft+(SCREEN WIDTH()/2)
thisboard.brect.rtop = 10
thisboard.brect.rbottom = thisboard.brect.rtop+(SCREEN HEIGHT() - 20)
rem set the block size on the board
BlockWidth = (SCREEN WIDTH()/2)/XNum
BlockHeight = (thisboard.brect.rbottom - thisboard.brect.rtop)/YNum
tsquares = XNum * YNum
rem setup the random colors and the board arrays
for a = 1 to NumColors
RandColors(a) = rgb(rnd(254)+1,rnd(254)+1,rnd(254)+1)
NEXT
rem setup some initial variables to hold information
x=1
y=1
leftmarker = thisboard.brect.rleft
topmarker = thisboard.brect.rtop
rem setup the data for the squares: color and placement
for b = 1 to tsquares
if x>XNum then x = 1: y=y+1: topmarker = topmarker+blockheight: leftmarker = thisboard.brect.rleft
AllSquares(x,y).bcolor = RandColors(rnd((NumColors -1))+1)
AllSquares(x,y).brect.rleft = leftmarker
AllSquares(x,y).brect.rright = leftmarker+BlockWidth
AllSquares(x,y).brect.rtop = topmarker
AllSquares(x,y).brect.rbottom = topmarker + BlockHeight
x=x+1
leftmarker = leftmarker+BlockWidth
NEXT
rem **************************************************************************************************************************
rem setup left side of screen - buttons and data
TurnCounter = 0
ScreenChanged = 0
TimeHolder = Timer()
rem setup start/reset button
AllButtons(1).pressed = 0
AllButtons(1).bcolor = rgb(140,157,188)
AllButtons(1).colorpressed = rgb(107,147,220)
AllButtons(1).caption = "New Game"
AllButtons(1).brect.rleft = 20
AllButtons(1).brect.rright = 100
AllButtons(1).brect.rtop = 20
AllButtons(1).brect.rbottom = 40
rem setup help button
AllButtons(2).pressed = 0
AllButtons(2).bcolor = rgb(140,157,188)
AllButtons(2).colorpressed = rgb(107,147,220)
AllButtons(2).caption = "Help"
AllButtons(2).brect.rleft = 120
AllButtons(2).brect.rright = 200
AllButtons(2).brect.rtop = 20
AllButtons(2).brect.rbottom = 40
rem setup color change buttons
blockwidth = 30
blockheight = 30
leftmarker = 20
topmarker = 100
for i = 5 to (NumColors+4)
if (leftmarker + blockwidth+10)>= (SCREEN WIDTH()/2)-10 then topmarker = topmarker+blockheight+10:leftmarker = 20
AllButtons(i).pressed = 0
AllButtons(i).bcolor = RandColors(i-4)
AllButtons(i).brect.rleft = leftmarker
AllButtons(i).brect.rright = leftmarker + blockwidth
AllButtons(i).brect.rtop = topmarker
AllButtons(i).brect.rbottom = topmarker+blockheight
leftmarker = leftmarker+blockwidth+10
nEXT
ScreenChanged = 1
rem ***************************************************************************************************************************
ENDFUNCTION
function DrawButton(tb as integer)
box AllButtons(tb).brect.rleft,AllButtons(tb).brect.rtop,AllButtons(tb).brect.rright,AllButtons(tb).brect.rbottom, AllButtons(tb).bcolor,AllButtons(tb).bcolor,AllButtons(tb).bcolor,AllButtons(tb).bcolor
xval = AllButtons(tb).brect.rleft + ((AllButtons(tb).brect.rright - AllButtons(tb).brect.rleft)-text width(AllButtons(tb).caption))/2
yval = AllButtons(tb).brect.rtop + ((AllButtons(tb).brect.rbottom - AllButtons(tb).brect.rtop)- text size())/2
if AllButtons(tb).caption <> "" then text xval, yval , AllButtons(tb).caption
ENDFUNCTION
function DrawSquare(x as integer,y as integer)
box AllSquares(x,y).brect.rleft,AllSquares(x,y).brect.rtop,AllSquares(x,y).brect.rright,AllSquares(x,y).brect.rbottom, AllSquares(x,y).bcolor,AllSquares(x,y).bcolor,AllSquares(x,y).bcolor,AllSquares(x,y).bcolor
ENDFUNCTION