07-28-2026, 07:28 AM
Just a simple "Whack-A-Squre" game ...
Code:
'===================
' Whack-A-Square
'===================
set window "Whack-A-Square", 300,300,false,2
set redraw off
set mouse off
randomize time()
constant SCORE_PER_LEVEL = 3
constant MAX_LEVEL = 6
visible score, level, isPlaying, activeMoleIdx
visible popDelayTimer, moleUpFrames
visible holes_x=[], holes_y=[], holeCount
visible px=[], py=[], pvx=[], pvy=[], plife=[]
visible particleCount
level = 1
isPlaying = 1
moleUpFrames = 0
particleCount = 0
createLayout(1)
'-----------
' MAIN LOOP
'-----------
while not keydown(KEY_ESCAPE, true)
mx = mousex()
my = mousey()
click = mousebutton(0,true) 'left click
if isPlaying then
if activeMoleIdx >= 0 then
moleUpFrames = moleUpFrames + 1
duration = (1000 - level * 80) / 16.67
if duration < 350 / 16.67 then duration = 350 / 16.67
if moleUpFrames >= duration then
activeMoleIdx = -1
popDelayTimer = (800 - level * 50) / 16.67
if popDelayTimer < 200 / 16.67 then popDelayTimer = 200 / 16.67
endif
else
if popDelayTimer > 0 then
popDelayTimer = popDelayTimer - 1
else
activeMoleIdx = rnd(holeCount)
moleUpFrames = 0
endif
endif
endif
whacked = false
if click and isPlaying then whacked = checkWhack(mx,my)
' Grass background
set color 39,174,96; cls
' UI
set color 255,255,255
set caret 10,10; write level
for i = 0 to holeCount - 1
drawHole(holes_x[i], holes_y[i])
next
if not whacked and activeMoleIdx >= 0 then
drawMole(holes_x[activeMoleIdx], holes_y[activeMoleIdx])
endif
' Cursor
set color 255, 0, 0, 150
draw rect mx - 8, my - 8, 50, 50, true
updateParticles()
drawParticles()
redraw
fwait 60
wend
'-----------
' FUNCTIONS
'-----------
function createLayout(count)
holeCount = count
holes_x=[]
holes_y=[]
cols1 = 1
if count > 1 and count <= 4 then cols1 = 2
if count > 4 then cols1 = 3
rows1 = (count + cols1 - 1) / cols1
totalW = cols1 * 80 + (cols1 - 1) * 15
totalH = rows1 * 80 + (rows1 - 1) * 15
startX = (320 - totalW) / 2 + 40
startY = (320 - totalH) / 2 + 40
for i = 0 to count-1
row = i / cols1
col = i % cols1
holes_x[i] = startX + col * (80 + 15)
holes_y[i] = startY + row * (80 + 15)
next
endfunc
function drawHole(x, y)
set color 40,40,40
draw rect x - 36, y - 36, 72, 72, true
endfunc
function drawMole(x, y)
set color 139, 90, 43
draw rect x-25, y-25, 50, 50, true
endfunc
function checkWhack(mx, my)
for i = 0 to holeCount - 1
dx = mx - holes_x[i]
dy = my - holes_y[i]
dist2 = dx * dx + dy * dy
if dist2 < 1600 then ' 40*40
if i = activeMoleIdx and activeMoleIdx >= 0 then
activeMoleIdx = -1
spawnExplosion(holes_x[i], holes_y[i])
score = score + 1
if score % SCORE_PER_LEVEL = 0 and level < MAX_LEVEL then
level = level + 1
createLayout(level)
elseif score % SCORE_PER_LEVEL = 0 and level = MAX_LEVEL then
set color 255,255,255
set caret 10,30
wln "MISSION ACCOMPLISHED"
redraw
temp = rln()
end
endif
' timing reset
moleUpFrames = 0
popDelayTimer = (800 - level * 50) / 16.67
if popDelayTimer < 200 / 16.67 then popDelayTimer = 200 / 16.67
return true
endif
endif
next
return false
endfunc
function spawnExplosion(x, y)
count = 16 ' Fixed burst count
max_particles = 32
for i = 0 to count - 1
if particleCount >= max_particles then break
idx = particleCount
particleCount = particleCount + 1
px[idx] = x
py[idx] = y
pvx[idx] = (rnd(101) - 50) / 12
pvy[idx] = (rnd(101) - 50) / 12
plife[idx] = 25
next
endfunc
function updateParticles()
i = 0
while i < particleCount
plife[i] = plife[i] - 1
px[i] = px[i] + pvx[i]
py[i] = py[i] + pvy[i]
pvy[i] = pvy[i] + 0.25 ' Gravity
pvx[i] = pvx[i] * 0.96 ' Air resistance
if plife[i] <= 0 then
particleCount = particleCount - 1
if i < particleCount then
px[i] = px[particleCount]
py[i] = py[particleCount]
pvx[i] = pvx[particleCount]
pvy[i] = pvy[particleCount]
plife[i] = plife[particleCount]
endif
else
i = i + 1
endif
wend
endfunc
function drawParticles()
particle_size = 8
if particleCount <= 0 then return
set color 220, 130, 50
half = particle_size / 2
for i = 0 to particleCount - 1
draw rect int(px[i]) - half, int(py[i]) - half, particle_size, particle_size, true
next
endfunc

