Nothing fancy, got bored. Thought maybe someone might find a use for this.
I've been trying a few different approaches lately for a Bejeweled game. Rather than organizing the gems as part of the grid map, this example keeps them separate and independent of the grid. Might seem odd at first, but once you start to add in movement animation and other effects, this actually becomes easier to manage than locking the gems into a 2D grid-like array.
As for this code snippet? Right-click to drop a block. Each block will detect collision with blocks under it. So when you left-click to remove a block on the bottom, everything above it will automatically cascade down at an accelerating velocity. That's basically all it does.
Rem Project: Dark Basic Pro Project
Rem Created: Sunday, February 24, 2013
Rem ***** Main Source File *****
set display mode 1024, 768, 32
#CONSTANT OBJSIZE = 64
tick as dword = timer()
lastTick as dword
type thing
acceleration as float
x as integer
y as float
gem as integer
endtype
dim gems() as thing
sync on
`sync rate 60
do
cls
lastTick = tick
tick = timer()
delta# = (tick-lastTick)*.01
if mouseclick() = 1 and flag=0
flag = 1
tx = mousex()/64
ty = mousey()/64
if getGem(tx, ty) = -1 then addGem(tx*64, ty*64)
endif
if mouseclick() = 2 and flag = 0
flag = 1
my = mousey() / 64
selectedGem = getGem(mousex()/64, my)
if selectedGem > -1 then removeGem(selectedGem)
endif
if mouseclick() = 0 then flag = 0
selectedGem = getGem(mousex()/64, mousey()/64)
rem draw map
ink rgb(64,64,64),0
box 0, 0, 640, 640
ink rgb(48,48,48),0
for x = 1 to 10
line x*64,0,x*64,640
next x
for y = 1 to 10
line 0,y*64,640,y*64
next y
gemCount = array count(gems())
for i = 0 to gemCount
inc gems(i).acceleration, 20*delta#
inc gems(i).y, gems(i).acceleration*delta#
for j = 0 to gemCount
if i <> j
x = gems(i).x
y = gems(i).y
x2 = gems(j).x
y2 = gems(j).y
if overlap(x, y, x+64, y+64, x2, y2, x2+64, y2+64) = 1
gems(i).y = y2-64
gems(i).acceleration = 0
endif
endif
next j
if gems(i).y+64 >= 640
gems(i).y = 576
gems(i).acceleration = 0
endif
next i
rem draw gems
ink rgb(0,192,128),0
for i = 0 to gemCount
x = gems(i).x
y = gems(i).y
box x+4, y+4, x+60, y+60
next i
sync
loop
function overlap(x1, y1, x2, y2, x3, y3, x4, y4)
if x1 < x4 and x2 > x3 and y1 < y4 and y2 > y3 then exitfunction 1
endfunction 0
function addGem(x, y)
array insert at bottom gems()
gems().x = x
gems().y = y
endfunction
function removeGem(i)
array delete element gems(), i
endfunction
function getGem(mapX, mapY)
gemCount = array count(gems())
for i = 0 to gemCount
if gems(i).x / 64 = mapX and int(gems(i).y) / 64 = mapY then exitfunction i
next i
endfunction -1
"You're all wrong. You're all idiots." ~Fluffy Rabbit