Quote: "What I need is the code that works, and the explanations on why it works."
Here is the code that works. I'm setting the sprites as non-visible rather than deleting since that was your preference.
The reason the delete function works is because it does exactly what I suggested in English (not code) above.
// Project: match3
// Author: Scraggle
// Created: 2020-06-28
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "match3" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
#constant MARGIN_LEFT 50
#constant MARGIN_TOP 130
#constant BOARD_SIZE 8
#constant CELL_SIZE 70
#constant QTY_STYLES 5
#constant HORIZONTAL 0
#constant VERTICAL 1
type tCell
style as integer
spr as integer
txt as integer[2]
qtyMatches as integer[1]
checked as integer[1]
endtype
global board as tCell[BOARD_SIZE, BOARD_SIZE]
global image as integer[QTY_STYLES]
image[0] = LoadImage("pizza.png")
image[1] = LoadImage("croissant.png")
image[2] = LoadImage("doughnut.png")
image[3] = LoadImage("icecream.png")
image[4] = LoadImage("slice.png")
image[5] = LoadImage("cupcake.png")
InitialiseBoard()
GetMatches()
ShowMatches()
//info Text
info as integer[1]
info[0] = CreateText("Horizontal matches")
info[1] = CreateText("Vertical matches")
SetTextColor(info[0], 64, 128, 224, 255)
SetTextColor(info[1], 64, 224, 64, 255)
for k = 0 to 1
SetTextSize(info[k], 40)
SetTextPosition(info[k], 0, 55 + k*30)
next k
///////////// M A I N L O O P S T A R T ////////////////
repeat
Print("Press <Space> or <left click> to randomise board")
Print("Press <Delete> or <right click> to delete matches")
if GetRawKeyPressed(32) or GetPointerPressed()
RandomiseBoard()
GetMatches()
ShowMatches()
endif
if GetRawKeyPressed(46) or GetRawMouseRightPressed()
DeleteMatches()
endif
Sync()
until GetRawKeyPressed(27)
end
///////////// M A I N L O O P E N D ////////////////
function DeleteMatches()
for y = 0 to BOARD_SIZE
for x = 0 to BOARD_SIZE
//Horizontal
matches = board[x,y].qtyMatches[HORIZONTAL]
if matches >= 3
for k = 0 to matches-1
SetSpriteVisible(board[x+k,y].spr, 0)
next k
endif
//Vertical
matches = board[x,y].qtyMatches[VERTICAL]
if matches >= 3
for k = 0 to matches-1
SetSpriteVisible(board[x,y+k].spr, 0)
next k
endif
next x
next y
endfunction
function GetMatches()
ClearMatches()
for y = 0 to BOARD_SIZE
for x = 0 to BOARD_SIZE
for k = 0 to 1
if board[x,y].checked[k] = 0
match = CheckMatches(k, x,y)
if match > board[x,y].qtyMatches[k]
board[x,y].qtyMatches[k] = match
endif
endif
next k
next x
next y
endfunction
function CheckMatches(k, x, y)
match = 1
if k = HORIZONTAL
while x < BOARD_SIZE
if board[x,y].style = board[x+1,y].style
board[x,y].checked[k] = 1
inc match
inc x
else
exitfunction match
endif
endwhile
exitfunction match
else
while y < BOARD_SIZE
if board[x,y].style = board[x,y+1].style
board[x,y].checked[k] = 1
inc match
inc y
else
exitfunction match
endif
endwhile
endif
endfunction match
function ClearMatches()
for y = 0 to BOARD_SIZE
for x = 0 to BOARD_SIZE
for k = 0 to 1
board[x,y].qtyMatches[k] = 1
board[x,y].checked[k] = 0
next k
next x
next y
endfunction
function ShowMatches()
for y = 0 to BOARD_SIZE
for x = 0 to BOARD_SIZE
for k = 0 to 1
SetTextString(board[x,y].txt[k], str(board[x,y].qtyMatches[k]))
SetTextVisible(board[x,y].txt[k], board[x,y].qtyMatches[k] >= 3)
next k
next x
next y
endfunction
function InitialiseBoard()
for y = 0 to BOARD_SIZE
for x = 0 to BOARD_SIZE
board[x,y].spr = CreateSprite(0)
SetSpriteSize(board[x,y].spr, CELL_SIZE, CELL_SIZE)
SetSpritePosition(board[x,y].spr, MARGIN_LEFT + x*CELL_SIZE, MARGIN_TOP + y*CELL_SIZE)
for k = 0 to 1
board[x,y].txt[k] = CreateText("0")
SetTextColor(board[x,y].txt[k], 255, 255, 255, 255)
SetTextSize(board[x,y].txt[k], CELL_SIZE/1.5)
SetTextBold(board[x,y].txt[k], 1)
next k
SetTextPosition(board[x,y].txt[0], MARGIN_LEFT + x*CELL_SIZE + CELL_SIZE/4, MARGIN_TOP + y*CELL_SIZE + CELL_SIZE/5)
SetTextPosition(board[x,y].txt[1], MARGIN_LEFT + x*CELL_SIZE + CELL_SIZE/2, MARGIN_TOP + y*CELL_SIZE + CELL_SIZE/5)
SetTextColor(board[x,y].txt[0], 64, 128, 224, 255)
SetTextColor(board[x,y].txt[1], 64, 224, 64, 255)
next x
next y
RandomiseBoard()
endfunction
function RandomiseBoard()
for y = 0 to BOARD_SIZE
for x = 0 to BOARD_SIZE
board[x,y].style = Random(0, QTY_STYLES)
SetSpriteImage(board[x,y].spr, image[board[x,y].style])
SetSpriteVisible(board[x,y].spr, 1)
next x
next y
endfunction
Go through that code and try to learn what everything is doing and why. Only when you feel you understand it fully should you move forward with the next step.
It was never my intention to write a match 3 game or write a tutorial, but that seems to be happening. I'll keep helping if you need it but I'm reluctant to continue providing complete code. I'd rather see what you are doing and point out where it can be improved.