Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whack-A-Square
#1
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
Reply
#2
Just a "simple" whack-a-sqaure? Maybe at the beginning... Still a cool test of eye-hand coordination... lol
Logic is the beginning of wisdom.

Live long and prosper.
Reply
#3
(07-28-2026, 12:26 PM)johnno56 Wrote: Just a "simple" whack-a-sqaure? Maybe at the beginning... Still a cool test of eye-hand coordination... lol

Simple but fun Smile

A long, long time ago I programmed a rather large game based on whack-a-mole, not very proud of it, but still: https://www.bigfishgames.com/super-smash...t1l11.html . There are three main games, one where you pop balloons, one where you throw cakes on people's faces and one where you punch mechanical clowns, all whack-a-mole:ish.
Reply
#4
Too simple. Easiest way to win is to "stick" to one square and "wait" until it's highlighted... lol
Logic is the beginning of wisdom.

Live long and prosper.
Reply
#5
(08-01-2026, 06:41 AM)johnno56 Wrote: Too simple. Easiest way to win is to "stick" to one square and "wait" until it's highlighted... lol

Yeah , should be a goal, a time limit and you should lose points on bad clicks Smile
Reply
#6
Ouch! With "my" eye and hand coordination, I would end up with a negative score... lol
Logic is the beginning of wisdom.

Live long and prosper.
Reply
#7
(08-01-2026, 12:35 PM)johnno56 Wrote: Ouch! With "my" eye and hand coordination, I would end up with a negative score... lol

This "Whack-a-Mole" kind games promotes brain health, memory, and concentration. They may serve as a hand and eye coordination theraphy for managing neurodegenerative conditions  Big Grin Big Grin Big Grin
Reply
#8
Interesting... How did you know that my brain was unhealthy. I haven't told anyone. My memory issues. I don't remember the last time I forgot anything... Concentration. I don't have any problem with.. oh, look. A butterfly!

At my age, my brain is functioning as it should, but thanks anyway for the therapeutic advice, George. Much appreciated. Big Grin
Logic is the beginning of wisdom.

Live long and prosper.
Reply
#9
(08-02-2026, 01:02 AM)johnno56 Wrote: Interesting... How did you know that my brain was unhealthy. I haven't told anyone. My memory issues. I don't remember the last time I forgot anything... Concentration. I don't have any problem with.. oh, look. A butterfly!

At my age, my brain is functioning as it should, but thanks anyway for the therapeutic advice, George. Much appreciated. Big Grin

Cool Cool Cool
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)