10/9/2014
Uploaded a new version of my match3 code along with a compiled version.
This is a working version, it will combine all matches 3 and above that are touching each other and the same colour, limit gem swapping to neighbouring gems, undo swap if no match is found. Sorry if some code is confusing, I'm confused. Made in AppGameKit v2 Alpha 5.1
4/9/2014
Old Code
Been wanting to do this for awhile, and been workin on this since monday. This is not a game its just some code for matchin gems, maybe give some people ideas.
xres = 1024
yres = 768
SetVirtualResolution(xres,yres)
//Change these to what you like
columns = 10
rows = 10
gemspacing = 40
numberOfColors = 4
boardOffsetX = 350
boardOffsetY = 0
numberofchecks = 30
Type _gems
sprite
color
falling
x
y
matched
EndType
numberofgems = columns * rows
Dim gems[numberofgems] as _gems
for i2 = 1 to rows
for i = 1 to columns
gemcount = gemcount + 1
gems[gemcount].sprite = createsprite(0)
SetSpriteSize(gems[gemcount].sprite, gemspacing -(gemspacing/10),gemspacing-(gemspacing/10))
SetSpritePositionByOffset(gems[gemcount].sprite, (gemspacing * i)+boardOffsetX, (gemspacing * i2)+boardOffsetY)
gems[gemcount].color = random(1,numberOfColors)
SetGemColor(gemcount, gems[gemcount].color)
next i
next i2
Type _gameboard
currentgem
color
endType
dim board[columns,rows] as _gameboard
dim matches[numberofgems,numberofgems]
currentgem=1
currentboardx = 1
currentboardy = 1
//Testing controls
// Spacebar to find any matching lists
// + - To cycle through Gems and Lists
//WASD To cycle through the Board Tiles
do
//Find Gem Positions
for i = 1 to numberofgems
gems[i].x = GetSpriteXByOffset(gems[i].sprite)
gems[i].y = GetSpriteYByOffset(gems[i].sprite)
next i
//Assign Gems to Board Array
for i = 1 to numberofgems
gemboardx = (gems[i].x-boardOffsetX)/gemspacing
gemboardy = (gems[i].y-boardOffsetY)/gemspacing
board[gemboardx,gemboardy].color = gems[i].color
board[gemboardx,gemboardy].currentgem = i
next i
if listdone = 0
match=0
//X Matches
for i2 = 1 to rows
for i = 2 to columns-1
if board[i,i2].color = board[i-1,i2].color and board[i,i2].color = board[i+1,i2].color
match=match+1
matches[match,1] = board[i-1,i2].currentgem
matches[match,2] = board[i,i2].currentgem
matches[match,3] = board[i+1,i2].currentgem
endif
next i
next i2
//Y Matches
for i2 = 2 to rows-1
for i = 1 to columns
if board[i,i2].color = board[i,i2-1].color and board[i,i2].color = board[i,i2+1].color
match=match+1
matches[match,1] = board[i,i2-1].currentgem
matches[match,2] = board[i,i2].currentgem
matches[match,3] = board[i,i2+1].currentgem
endif
next i
next i2
endif
if GetRawKeyPressed( 32 )
gosub fixlists
listdone=1
endif
print("Matches: " + str(match))
Print( "Mouse X: " + str(GetPointerX()))
print( "Mouse Y: " + str(GetPointerY()))
print( "Current Gem/List: "+str(currentgem))
Print( "Gem X: " + str(gems[currentgem].x))
print( "Gem Y: " + str(gems[currentgem].y))
Print( "Current Board X: " + str(currentboardx))
print( "Current Board Y: " + str(currentboardy))
if GetRawKeyPressed( 187 ) then currentgem=currentgem+1
if GetRawKeyPressed( 189 ) then currentgem=currentgem-1
if currentgem > numberofgems then currentgem =1
if currentgem < 1 then currentgem = numberofgems
if GetRawKeyPressed( 68 ) then currentboardx=currentboardx+1
if GetRawKeyPressed( 65 ) then currentboardx=currentboardx-1
if currentboardx > columns then currentboardx =1
if currentboardx < 1 then currentboardx = columns
if GetRawKeyPressed( 87 ) then currentboardy=currentboardy+1
if GetRawKeyPressed( 83 ) then currentboardy=currentboardy-1
if currentboardy > columns then currentboardy =1
if currentboardy < 1 then currentboardy = columns
Print( "Board Gem: " + str(board[currentboardx,currentboardy].currentgem))
print( "Board Color: " + str(board[currentboardx,currentboardy].color))
printc("List1: ")
for t = 1 to 15
if matches[currentgem,t] > 0
printc(str(matches[currentgem,t])+",")
endif
next t
Print( " " )
Print( ScreenFPS() )
if goingdark = 1
flashin = flashin +1
else
flashin = flashin -1
endif
if flashin < 1 then goingdark = 1
if flashin > 200 then goingdark = 0
for i = 1 to numberofgems
if gems[i].matched = 1
SetSpriteColoralpha(gems[i].sprite,255-flashin)
endif
next i
Sync()
loop
fixlists:
//Ceck to see if any of the lists contain the same numbers
for t = 1 to 10 //Go through the lists a few times to make sure
for list1 = 1 to match-1
if matches[list1, 1] >0
for list2 = list1+1 to match
amatch = 0
for l1 = 1 to numberofchecks
for l2 = 1 to numberofchecks
if matches[list1, l1] = matches[list2, l2] and matches[list1, l1] > 0 and matches[list2, l2] > 0
amatch = amatch + 1
endif
next l2
next l1
if amatch > 0 then gosub combinelists
next list2
endif
next list1
next t
//Set gem to matched
for t = 1 to match
for t2 = 1 to numberofchecks
if matches[t, t2] > 0
gems[matches[t, t2]].matched = 1
endif
next t2
next t
return
combinelists:
//Combine lists with matched numbers
for cl1 = 1 to numberofchecks
for cl2 = 1 to numberofchecks
//If list 1 and list 2 have the same number erase the one in list 2
if matches[list1, cl1] = matches[list2, cl2]
matches[list2, cl2] = 0
endif
//Combine remaining entries in list 2 to empty slot in list 1
if matches[list1, cl1] = 0 and matches[list2, cl2] > 0
matches[list1, cl1] = matches[list2, cl2]
matches[list2, cl2] = 0
endif
next cl2
next cl1
return
function SetGemColor(gemnumber, color)
if gems[gemnumber].color = 1
SetSpriteColor(gems[gemnumber].sprite, 255,0,0,255)
endif
if gems[gemnumber].color = 2
SetSpriteColor(gems[gemnumber].sprite, 255,133,0,255)
endif
if gems[gemnumber].color = 3
SetSpriteColor(gems[gemnumber].sprite, 155, 155, 0,255)
endif
if gems[gemnumber].color = 4
SetSpriteColor(gems[gemnumber].sprite,133,255,0,255)
endif
if gems[gemnumber].color = 5
SetSpriteColor(gems[gemnumber].sprite,0,0,255,255)
endif
if gems[gemnumber].color = 6
SetSpriteColor(gems[gemnumber].sprite,138,43,226,255)
endif
if gems[gemnumber].color = 7
SetSpriteColor(gems[gemnumber].sprite,75,0,130,255)
endif
EndFunction
Still need to do a better job of optimising it.
yo