GreenBLocks puzzle game v1.1, for iPad.

Post Reply
User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

GreenBLocks puzzle game v1.1, for iPad.

Post by Dav »

Here's another attempt of making of a game like I was playing online, which was called 'Numberhood". You move the blocks close to each other, to make them turn green, clear the board. It's not as easy as it sounds. The number in each block tells how many other blocks it needs to touching to turn green. IF touching too many, block turns red, if just right amount it turns green. Arrange the blocks so all on board turns green.

This game isn't as polished or finished as I'd like, but I don't have that much coding time these days. I pretty much followed the levels of the original game. I coded this only as a programming exercise, but posting it in case some of you would enjoy playing it.

- Dav

Code: Select all

'GreenBlocks.txt v1.1
'A 'Numberhood' like puzzle game for ipad
'Coded by Dav April/2018

'v1.1: fixed bug that showed wrong symbol sometimes.
'.     added support for the 0 number.
'.     added more levels. 15 now.
'.     optimized code
'.     renamed game

'GOAL:

'Group blocks together to turn them green.
'When all blocks turn green, level is completed.

'HOW TO PLAY:

'First Select a block to move to an empty spot.
'Then select the spot to move it to. The numbers in the
'block say how many other blocks should be touching it.
'When the correct number is touching it, it will
'turn green (ie: 1 means only one block should touch it)
'If too many are touching it, it will turn red.
'Dark circled blocks can't be moved (level 6 and up).
'Turn all blocks on board green to clear the level.

'The Reatart button is a level redo, it resets it.

'There are 15 levels.

if lowstr$(device_type$())<>"ipad" then
  print "Sorry, game for iPad only right now."
  end
end if

option base 1
get orientation ort
set orientation vertical
get screen size sw,sh

level = 1
maxlevel =15

'======
restart:
'======

graphics
graphics clear .2,.2,.2
set buttons custom

'draw background texture
draw alpha .3
draw font size 100 
draw color 0,0,0
'a litte background
for x=1 to sw step 61
   for y =1 to sh step 102
      draw text chr$(9619) at x,y
   next y
next x
draw alpha 1

grid=9 !size=sw/grid
dim bm(grid*grid), bx(grid*grid),by(grid*grid),bv$(grid*grid)
dim fc(grid*grid) 'fill color of button
dim bcon$(grid*grid)

set buttons font size 36

draw color 0,0,0
fill color 1,1,.8
button "restart" text "Restart" at 1,grid

draw color 1,1,1
w$="Level "&level&" of "&maxlevel
draw font size 36
draw text w$ at grid*25, grid*2

draw font size 100
set buttons font size 74

x$=chr$(11035)

draw color 0,0,0
fill color .8,.8,.8

'init default button values, and generate x/y values...
bc=1
for r= 1 to grid
   for c = 1 to grid
     x=(r*size) ! y=(c*size)
     button str$(bc) text x$ at x-size,y size size,size
     bx(bc)=x-size!by(bc)=y  'button x/y position
     fc(bc)=0 'fill color of button
     bv$(bc)=x$ 'fill board with blocks
     bcon$(bc) = "udlr" 'button connections
     bm(bc)=0
     bc=bc+1
   next c
next r

'define connection for each button (left,right,up,down)

'adjust connections of corner buttons
bcon$(1)="rd"!bcon$(grid)="rd"
bcon$((grid*grid)-(grid-1))="ld"
bcon$(grid*grid)="ul"

'left side
for s=2 to grid-1
  bcon$(s)="udr"
next s

'right side
for s=((grid*grid)-(grid-2))to (grid*grid-1)
  bcon$(s)="udl"
next s

gosub setlevel

'===
main:
'===

selected=0
gosub updateboard

second:

do
  for t=1 to (grid*grid)

    'only poll non blocked buttons
    if bv$(t)<>x$ then
      if button_pressed(str$(t)) then 

       'only non movable  umbers
       if bm(t)=0 then

       'if first choice...
       if selected=0 then
          'only select a number button
          if bv$(t)<>"" then
             selected=1
             'highlight button here (redraw)
             fill color .7,.9,1
             button str$(t) text swap$(t) at bx(t),by(t) size size,size
             notes set "120:sg7"!notes play
             selbtn=t 'save picked butn number
             goto second  'now get second choice
          end if
       else  'making second choice
          'only move to blank block
          if bv$(t)="" then
            bv$(t)=bv$(selbtn)!bv$(selbtn)=""
            goto main
          else
            'clear previous highlighted btn
            selected=0!gosub updateboard!selected=1

            'hightlight butn here (redraw)
             fill color .7,.9,1
             button str$(t) text swap$(t) at bx(t),by(t) size size,size
            selbtn=t
            notes set "120:sg7"!notes play
            goto second
          end if
       end if
       end if
    end if
   end if
  next t

  if button_pressed("restart") then goto restart

  if button_pressed("quit") then
     set orientation ort ! end
  end if

until 0

end


'==========
updateboard:
'==========

for s=1 to grid * grid

   'only update non block buttons
   if bv$(s)<>x$ then

     'define fill color for each button
     if bv$(s)="" then
       fc(s)=0
     else
       num=0 'defaults
       fc(s)=1 

       'look left
       if instr(bcon$(s), "l") then
         peek=1
         if bv$(s-grid)=x$ then peek=0
         if bv$(s-grid)="" then peek=0
         if peek=1 then num=num+1
       end if
       'look right
       if instr(bcon$(s),"r") then
         peek=1
         if bv$(s+grid)=x$ then peek=0
         if bv$(s+grid)="" then peek=0
         if peek=1 then num=num+1
       end if
       'look up
       if instr(bcon$(s),"u") then
         peek=1
         if bv$(s-1)=x$ then peek=0
         if bv$(s-1)="" then peek=0
         if peek=1 then num=num+1
       end if
       'look down
       if instr(bcon$(s),"u") then
         peek=1
         if bv$(s+1)=x$ then peek=0
         if bv$(s+1)="" then peek=0
         if peek=1 then num=num+1
       end if

       if num=val(bv$(s)) then fc(s)=3 'green
       if num>val(bv$(s)) then fc(s)=2 'red

     end if

     'finally draw the button
     if fc(s)=0 then fill color 1,1,1 'blank
     if fc(s)=1 then fill color .9,.8,.6 'orange
     if fc(s)=2 then fill color 1,.2,.2 'red
     if fc(s)=3 then fill color .2,1,.2 'green
     button str$(s) text swap$(s) at bx(s),by(s) size size,size
   end if
next s

'check if all are green
done=1 'default is done
for s=1 to grid * grid
   'only check number buttons
   if bv$(s)<>x$ then
     if bv$(s)<>"" then
      if fc(s)<>3 then 
         done=0 !break
      end if
     end if
   end if
next s

if done = 1 then
   level=level+1
   notes set "7:s(c3eg)(e4cg3)(g4ec)(c5g4e)"
   notes play 
   pause 1

   fill color 0,1,0
   for i=1 to grid*grid
      if bv$(i)=x$ then goto skipp
      if bv$(i)="" then goto skipp
      button str$(i) text chr$(9989) at bx(i),by(i) size size,size
      pause .05
      skipp:
   next i

   pause 1

   if level>maxlevel then end   'all levels done? end

   goto restart

end if

return


setlevel:

if level=1 then
  bv$(23)="1"!bv$(32)=""
  bv$(41)=""!bv$(50)="1"
end if

if level=2 then
  bv$(23)="1"!bv$(32)=""!bv$(41)="4"!bv$(40)=""!bv$(39)="1"
  bv$(42)=""!bv$(43)="1"!bv$(50)=""!bv$(59)="1"
end if

if level=3 then
   bv$(31)="1"!bv$(32)=""!bv$(33)="2"
   bv$(40)="1"!bv$(41)="2"!bv$(42)=""
   bv$(49)=""!bv$(50)=""!bv$(51)=""
end if

if level=4 then
   bv$(31)="2"!bv$(32)="1"!bv$(33)=""
   bv$(40)=""!bv$(41)="2"!bv$(42)=""
   bv$(49)="2"!bv$(50)="1"!bv$(51)=""
end if

if level=5 then
   bv$(39)="2"!bv$(40)="1"!bv$(41)=""!bv$(42)="1"!bv$(43)=""
   bv$(31)=""!bv$(32)="1"!bv$(33)=""
   bv$(23)="3"!bv$(49)=""!bv$(50)="1"!bv$(51)=""!bv$(59)="3"
end if

if level=6 then 
  bv$(13)="1"!bv$(14)="2"!bv$(15)=""
  bv$(22)=""!bv$(23)="4"!bv$(24)="1"
  bv$(31)=""!bv$(32)="1"!bv$(33)=""
  bv$(40)="1"!bv$(41)="4"!bv$(42)="1"
  bv$(49)=""!bv$(50)="1"!bv$(51)=""
  bv$(58)=""!bv$(59)="4"!bv$(60)="1"
  bv$(67)="1"!bv$(68)="2"!bv$(69)=""
  bm(23)=1!bm(41)=1!bm(59)=1
end if

if level=7 then
  bv$(21)=""!bv$(22)="2"!bv$(23)="2"!bv$(24)=""
  bv$(30)="2"!bv$(31)="3"!bv$(32)=""!bv$(33)=""
  bv$(39)=""!bv$(40)="2"!bv$(41)="2"!bv$(42)="2"!bv$(43)=""
  bv$(49)=""!bv$(50)=""!bv$(51)="3"!bv$(52)="2"
  bv$(58)=""!bv$(59)="2"!bv$(60)="2"!bv$(61)=""
  bm(31)=1!bm(41)=1!bm(51)=1
end if

if level=8 then
  bv$(11)="2"!bv$(12)="2"!bv$(13)="1"
  bv$(20)=""!bv$(21)="4"!bv$(22)="1"
  bv$(29)=""!bv$(30)=""!bv$(31)=""
  bv$(38)=""!bv$(39)="1"!bv$(40)="2"!bv$(41)=""!bv$(42)=""
  bv$(47)=""!bv$(48)=""!bv$(49)=""!bv$(50)=""!bv$(51)=""
  bv$(56)=""!bv$(57)=""!bv$(58)="2"!bv$(59)="2"!bv$(60)=""
  bv$(67)="2"!bv$(68)="1"!bv$(69)="2"
  bm(21)=1!bm(40)=1!bm(59)=1
end if

if level=9 then
  bv$(23)="2"!bv$(24)=""!bv$(31)=""!bv$(32)="2"!bv$(33)=""
  bv$(40)="2"!bv$(41)="4"!bv$(42)="2"
  bv$(49)="3"!bv$(50)="1"!bv$(51)="2"
  bv$(58)="4"!bv$(59)="2"!bv$(60)="2"
  bm(41)=1!bm(49)=1!bm(51)=1
end if

if level=10 then
  bv$(13)="1"!bv$(14)="4"!bv$(15)="2"
  bv$(21)=""!bv$(22)="3"!bv$(23)="3"!bv$(24)=""!bv$(25)=""
  bv$(30)=""!bv$(31)=""!bv$(32)="2"!bv$(33)=""!bv$(34)=""
  bv$(40)="2"!bv$(41)="1"!bv$(42)=""
  bv$(48)=""!bv$(49)=""!bv$(50)="2"!bv$(51)=""!bv$(52)=""
  bv$(57)=""!bv$(58)="1"!bv$(59)="1"!bv$(60)=""!bv$(61)=""
  bv$(67)="1"!bv$(68)="1"!bv$(69)="2"
  bm(22)=1!bm(32)=1!bm(50)=1
end if

if level=11 then
  bv$(21)="1"!bv$(22)="0"!bv$(23)="3"!bv$(24)="2"!bv$(25)="3"
  bv$(30)=""!bv$(31)=""!bv$(32)="2"!bv$(33)=""!bv$(34)=""
  bv$(39)=""!bv$(40)=""!bv$(41)="1"!bv$(42)=""!bv$(43)=""
  bv$(48)=""!bv$(49)=""!bv$(50)="3"!bv$(51)=""!bv$(52)=""
  bv$(57)="2"!bv$(58)="2"!bv$(59)="3"!bv$(60)="0"!bv$(61)="2"
end if

if level=12 then
  bv$(22)="0"!bv$(23)="0"!bv$(24)="0"
  bv$(31)="0"!bv$(32)=""!bv$(33)=""
  bv$(40)=""!bv$(41)=""!bv$(42)=""
  bv$(49)=""!bv$(50)=""!bv$(51)="0"
  bv$(58)="0"!bv$(59)="0"!bv$(60)="0"
end if

if level=13 then
  bv$(22)=""!bv$(23)=""!bv$(24)=""!bv$(25)=""
  bv$(30)="0"!bv$(31)=""!bv$(32)="1"!bv$(33)="2"!bv$(34)="3"
  bv$(39)="0"!bv$(40)=""!bv$(41)="1"!bv$(42)="2"!bv$(43)="3"
  bv$(48)="0"!bv$(49)=""!bv$(50)="1"!bv$(51)="2"!bv$(52)="3"
  bv$(58)=""!bv$(59)=""!bv$(60)=""!bv$(61)=""
end if

if level=14 then
  bv$(13)="1"!bv$(15)="2"
  bv$(22)="0"!bv$(24)="0"
  bv$(30)=""!bv$(31)="3"!bv$(32)="3"!bv$(33)="1"!bv$(34)=""
  bv$(39)=""!bv$(40)="2"!bv$(41)="3"!bv$(42)="2"!bv$(43)=""
  bv$(48)=""!bv$(49)=""!bv$(50)="3"!bv$(51)=""!bv$(52)=""
  bv$(57)=""!bv$(58)=""!bv$(59)=""!bv$(60)=""!bv$(61)=""
  bv$(66)="3"!bv$(67)="2"!bv$(68)="2"!bv$(69)="3"!bv$(70)=""
  bm(32)=1!bm(41)=1!bm(50)=1
end if

if level=15 then
  bv$(12)="2"!bv$(13)=""!bv$(14)="2"!bv$(15)=""!bv$(16)="2"
  bv$(21)=""!bv$(22)="2"!bv$(23)=""!bv$(24)="2"!bv$(25)=""
  bv$(30)="2"!bv$(31)=""!bv$(32)="0"!bv$(33)=""!bv$(34)="2"
  bv$(39)="2"!bv$(40)="0"!bv$(41)="0"!bv$(42)="0"!bv$(43)="2"
  bv$(48)="2"!bv$(49)=""!bv$(50)="0"!bv$(51)=""!bv$(52)="2"
  bv$(57)=""!bv$(58)="2"!bv$(59)=""!bv$(60)="2"!bv$(61)=""
  bv$(66)="2"!bv$(67)=""!bv$(68)="2"!bv$(69)=""!bv$(70)="2"
  bm(41)=1
end if

return

def swap$(n)
     a$=""
     if .bv$(n)="0" then 
        a$=chr$(9675)!if .bm(n)=1 then a$=chr$(9679)
     end if
     if .bv$(n)= "1" then 
        a$=chr$(10112)!if .bm(n)=1 then a$=chr$(10122)
     end if
     if .bv$(n)= "2" then 
        a$=chr$(10113)!if .bm(n)=1 then a$=chr$(10123)
     end if
     if .bv$(n)= "3" then 
        a$=chr$(10114)!if .bm(n)=1 then a$=chr$(10124)
     end if
     if .bv$(n)= "4" then 
        a$=chr$(10115)!if .bm(n)=1 then a$=chr$(10125)
     end if
     swap$=a$
end def

Last edited by Dav on Wed Apr 18, 2018 5:21 pm, edited 4 times in total.

User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: GreenBLocks puzzle game v1.1, for iPad.

Post by Dav »

I updated the puzzle game, fixed a bug, added more levels, and added the number 0 block (those blocks can't touch any other block) Cleaned up code a bit. Renamed game.

The BUTTON command is so useful for making simple puzzle games. One my favorite sB tool to use.

- Dav

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: GreenBLocks puzzle game v1.1, for iPad.

Post by GeorgeMcGinn »

Dav,

Another addictive and great game! Played it for hours!

Thanks
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: GreenBLocks puzzle game v1.1, for iPad.

Post by Dav »

Thanks, George.. Glad you enjoyed it! I made another one today. AS soon as I clean it up a little I'll post it.

- Dav

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: GreenBLocks puzzle game v1.1, for iPad.

Post by Dutchman »

A nice game, but it took a while before I noticed how to play.
It is indeed not simple but a challenge. Am curious if I'll come to level 15 :lol:
Thanks
screen shot.JPG
screen shot.JPG (70.29 KiB) Viewed 3752 times

User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: GreenBLocks puzzle game v1.1, for iPad.

Post by Dav »

THanks for trying it out Dutchman.

Yeah, I didn't know how to explain the game very well.

Thanks for the screen shot too.

- Dav

Post Reply