Gridfill color puzzle game using buttons (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:

Gridfill color puzzle game using buttons (iPad)

Post by Dav »

This is a "Globs" type puzzle game. It uses buttons totally for all game play. I wanted to see how easy it would be to make this kind of game in smart basic, and it was pretty easy using a recursive flood fill routine and colored buttons.

To play the game, try to fill the grid with all one color in a given number of moves. There are 10 levels in the game to beat. it gets harder to win as the grid gets larger. Right now it is for iPad only.

- Dav

EDIT: v1.2 changes include 10 levels with larger grid sizes. Smart Basic v4.7 code.

Code: Select all

'GRIDFILL puzzle game v1.2
'Fill the grid with all one color.
'Use bottom buttons to choose color.
'Do all 10 levels to win the game.
'Made using smart BASIC v4.7
'Coded by Dav, JUNE/2015

'GAME FOR IPAD ONLY RIGHT NOW....

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

'=== set up screen
graphics
graphics clear 0,0,0
set orientation top
set toolbar off

'=== make message boxes
gosub makemessages

'=== draw background image
option base 0
gosub drawbackground
option base 1


set buttons custom
set buttons font size 56

level=1
firstrun=1

'======
newgame:
'======

draw color 0,0,0

sw=screen_width()
sh=screen_height()

gosub drawinfobar

if level =1 then
   rows=4 ! columns=4 ! size=125 ! turnmax=7
end if
if level=2 then
   rows=5 ! columns=5 ! size=107 ! turnmax=8
end if
if level=3 then
   rows=6 ! columns=6 ! size=95 ! turnmax=10
end if
if level=4 then
   rows=7 ! columns=7 ! size=83 ! turnmax=12
end if
if level=5 then
   rows=8 ! columns=8 ! size=75 ! turnmax=14
end if
if level=6 then
   rows=9 ! columns=9 ! size=68 ! turnmax=16
end if
if level=7 then
   rows=10 ! columns=10 ! size=62 ! turnmax=18
end if
if level=8 then
   rows=11 ! columns=11 ! size=57 ! turnmax=20
end if
if level=9 then
   rows=12 ! columns=12 ! size=55 ! turnmax=22
end if
if level=10 then
   rows=13 ! columns=13 ! size=50 ! turnmax=24
end if

dim btn(rows,columns)

turns=0
red=0!grn=1!blu=2!yel=3!pur=4!org=5

'show current game level
   set buttons font size 26
   draw color 0,0,0
   fill color 1,1,1
   b$="Level "&str$(level)
   dx=sw-(text_width(b$))-48
   button "level" text b$ at 110,10
   draw color 0,0,0

'generate random board colors
for r= 1 to rows
   for c = 1 to columns
     btn(r,c)=rnd(6)
   next c
next r

gosub gridhide

if firstrun=1 then 
   showmsg ("newgame")
   firstrun=0
end if

gosub updateboard


'draw color changing buttons
fill color 1,0,0
button "red" text "" at 80,800 size 80,80
fill color 0,1,0
button "grn" text "" at 180,800 size 80,80
fill color 0,0,1
button "blu" text "" at 280,800 size 80,80
fill color 1,1,0
button "yel" text "" at 380,800 size 80,80
fill color 1,0,1
button "pur" text "" at 480,800 size 80,80
fill color 1,.5,0
button "org" text "" at 580,800 size 80,80

'draw quit button
set buttons font size 26
draw color 1,1,1
fill color 0,0,0
button "quit" text "Quit" at 675,900
draw color 0,0,0

clr=btn(1,1)! gosub drawarrow

gosub drawturns

do

  'get color press
  do 
    if button_pressed("red") then
      clr=red ! if btn(1,1)<>clr then break
    end if
    if button_pressed("grn") then
      clr=grn ! if btn(1,1)<>clr then break
    end if
    if button_pressed("blu") then
      clr=blu ! if btn(1,1)<>clr then break
    end if
    if button_pressed("yel") then
      clr=yel ! if btn(1,1)<>clr then break
    end if
    if button_pressed("pur") then
      clr=pur ! if btn(1,1)<>clr then break
    end if
    if button_pressed("org") then
      clr=org ! if btn(1,1)<>clr then break
    end if
    if button_pressed("quit") then end

    if time()>60 then
      gosub drawinfobar
      time reset
    end if

  until 0

  old=btn(1,1) 

  turns=turns+1
  gosub drawturns
  gosub drawarrow
  gosub movearrowup

  floodfill(old,clr, 1,1)

  notes set "126:c4"!notes play

  gosub updateboard

  gosub movearrowdown

  gosub checkforwin

  'if turns over, game over
  if turns >turnmax-1 then
    set buttons font size 72
    draw color 1,1,1
    fill color 0,0,0
    tx$="Sorry!"!tl=text_width(tx$)+20
    button "lose" text tx$ at sw/2-tl,sh/2-100
    notes set "111:h(d3fa)(dga#)(dfa)"
    notes play ! pause 3
    button "lose" delete
    gosub gridhide
    showmsg("retry")
    goto newgame
  end if

until forever


end


'==========================================
' GOSUBS AND FUNCTIONS
'==========================================


'==========
updateboard:
'==========
'updates board based on btn() values

nm=0
for r= 1 to rows
   for c = 1 to columns
     'bn$=str$(r)&str$(c)
     j= btn(r,c) ! nm=nm+1
     if j=0 then fill color 1,0,0
     if j=1 then fill color 0,1,0
     if j=2 then fill color 0,0,1
     if j=3 then fill color 1,1,0
     if j=4 then fill color 1,0,1
     if j=5 then fill color 1,.5,0
     x=(r*size) ! y=(c*size)
     button str$(nm) text "" at x,y+15 size size,size
   next c
next r

return


'==========
checkforwin:
'==========
'sees if grid is all one color (win)

t=0
for y=1 to columns
   for x= 1 to rows
     if btn(x,y)= clr then t=t+1
   next x
next y
'if it's completed...
if t = (columns*rows) then
  set buttons font size 72
  draw color 0,0,0
  fill color 1,1,1
  tx$="Good!"!tl=text_width(tx$)+20
  button "win" text tx$ at sw/2-tl,sh/2-100
  notes set "7:s(c3eg)(e4cg3)(g4ec)(c5g4e)"
  notes play ! pause 1.5
  button "win" delete
  gosub gridhide
  level=level+1
  '=== if all levels done...
  if level > 10 then 
     gosub gridhide
     '=== show happy face
     gosub drawface
     notes set "90:i(eg#b)r(df#a)w(eg#b)"
     notes play ! pause 3
     showmsg("won")
     pause 2
     '=== redraw background
     sprite "bg" show
     sprite "bg" stamp
     sprite "bg" hide
     '=== start over
     level=1
     firstrun= 1
     goto newgame
  end if
  showmsg("nextlevel")
  goto newgame
end if

return

'=======
drawface:
'=======
'draws a happy face

sw=screen_width()
sh=screen_height()-100
fill color 1,1,0
fill circle sw/2,sh/2 size 200
fill color 0,0,0
fill circle sw/2-75,sh/2-75 size 50
fill circle sw/2+75,sh/2-75 size 50
fill color 1,1,1
fill circle sw/2-75,sh/2-75 size 15
fill circle sw/2+75,sh/2-75 size 15
fill color 0,0,0
fill circle sw/2,sh/2+20 size 20
draw size 25
draw color 0,0,0
draw arc sw/2,sh/2,125,3.14,0,1
return


'========
drawturns:
'========
  set buttons font size 26
  draw color 0,0,0
  fill color 1,1,1
  tt$=str$(turns)&"/"&str$(turnmax)
  button "turns" text tt$ at 10,10
return


'========
drawarrow:
'========

fill alpha 0
draw color 0,0,0
set buttons font size 56
button "▶︎" text "▶︎" at 1,800 size 70,70
draw color 0,0,0
fill alpha 1

return


'=============
drawbackground:
'=============
'draws a rainbow background

dim rd(961),gn(961),bu(961)

for b = 0 to 255
   rd(i)=255!gn(i)=b!bu(i)=0!i=i+1
next b
for r = 255 to 0 step -1
   rd(i)=r!gn(i)=255!bu(i)=0!i=i+1
next r
for y = 0 to 255
   rd(i)=0!gn(i)=255-y!bu(i)=y!i=i+1
next y
for z = 0 to 192
   rd(i)=0!gn(i)=z!bu(i)=255!i=i+1
next z

draw alpha .4
for y = 0 to 960
   draw color rd(y)/255,gn(y)/255,bu(y)/255
   draw line 0,y to screen_width(),y
next y
_line(0,750,screen_width(),752,1,0,0,.3)


draw alpha 1
shadow on
draw color .5,.5,1
draw font size 24
draw text "GRIDFILL v1.2" at 275,915
sw=screen_width()!sh=screen_height()
sprite "bg" scan 0,0,sw,sh
sprite "bg" at 0,0
sprite "bg" show
sprite "bg" stamp
sprite "bg" hide

return

'==========
movearrowup:
'==========
fill alpha 0
draw color 1,1,1
if clr=0 then draw color 1,0,0
if clr=1 then draw color 0,1,0
if clr=2 then draw color 0,0,1
if clr=3 then draw color 1,1,0
if clr=4 then draw color 1,0,1
if clr=5 then draw color 1,.5,0
for m=800 to 50 step -60
  button "▶︎" text "▶︎" at 1,m size 70,70
  pause .01
next m
draw color 0,0,0

fill alpha 1
return


'============
movearrowdown:
'============

fill alpha 0
draw color 0,0,0
for m=50 to 800 step 60
  button "▶︎" text "▶︎" at 1,m size 70,70
  pause .01
next m
draw color 0,0,0
fill alpha 1
gosub drawarrow
return

'==========
drawinfobar:
'==========
fill color 0,0,0
draw color 1,1,1
fill alpha 0
bat$=str$(battery_level())&"%"
set buttons font size 20
button "bat" text bat$ at sw-text_width(bat$)-90,1
ampm$="AM" ! hr=current_hour()
min$=str$(current_minute())
if len(min$)=1 then min$="0"&min$
if hr>12 then
  hr=hr-12 ! ampm$="PM"
end if
tm$=str$(hr)&":"&min$&" "&ampm$
button "time" text tm$ at ((sw/2)-text_width(tm$)/2),1
'draw battery graphic
fill alpha 1
draw size 1
fill rect sw-70,10 to sw-20,24
draw rect sw-70,10 to sw-20,24
f=battery_level()/2
fill color 1,1,1
fill rect sw-70,10 to sw-70+f,24
fill rect sw-20, 14 to sw-17, 19
draw color 0,0,0

return


'===========
makemessages:
'===========

refresh off

'=== make new game box
_line (0,0,300,300,1,1,1,1)
_line (5,5,295,295,1,0,0,1)
_line (0,0,300,300,0,0,0,0)
_text ("NEW GAME",65,20,"",36,1,1,1)
_text ("Fill the grid with",25,80,"",22,0,1,1)
_text ("one color using the",25,110,"",22,0,1,1)
_text ("big color buttons.",25,140,"",22,0,1,1)
_text ("You have 7 turns.", 25,170,"",22,0,1,1)
_line (100,225,200,275,1,1,1,1)
_line (100,225,200,275,0,0,0,0)
_text ("GO!",125,234,"",36,0,0,1)
sprite "newgame" scan 0,0,302,302
sprite "newgame" at -2000,-2000
sprite "newgame" show

graphics clear 0,0,0

'make next level box
_line (0,0,300,300,1,1,1,1)
_line (5,5,295,295,1,0,0,1)
_line (0,0,300,300,0,0,0,0)
_text ("YOU DID IT!",33,20,"",36,1,1,1)
_text ("Good job. Now for a",25,80,"",22,0,1,1)
_text ("larger grid to fill",25,110,"",22,0,1,1)
_text ("up. Number of turns",25,140,"",22,0,1,1)
_text ("will be increased.", 25,170,"",22,0,1,1)
_line (100,225,200,275,1,1,1,1)
_line (100,225,200,275,0,0,0,0)
_text ("GO!",125,234,"",36,0,0,1)
sprite "nextlevel" scan 0,0,302,302
sprite "nextlevel" at -2000,-2000
sprite "nextlevel" show

graphics clear 0,0,0

'make retry message
_line (0,0,300,300,1,1,1,1)
_line (5,5,295,295,1,0,0,1)
_line (0,0,300,300,0,0,0,0)
_text ("OUT OF TURNS",25,20,"",36,1,1,1)
_text ("Sorry, you ran out",25,80,"",22,0,1,1)
_text ("of turns. But don't",25,110,"",22,0,1,1)
_text ("worry, you can have",25,140,"",22,0,1,1)
_text ("another try...", 25,170,"",22,0,1,1)
_line (100,225,200,275,1,1,1,1)
_line (100,225,200,275,0,0,0,0)
_text ("RETRY",108,238,"",28,0,0,1)
sprite "retry" scan 0,0,302,302
sprite "retry" at -2000,-2000
sprite "retry" show

graphics clear 0,0,0

'make you won message
_line (0,0,300,300,1,1,1,1)
_line (5,5,295,295,1,0,0,1)
_line (0,0,300,300,0,0,0,0)
_text (" YOU WON!",25,25,"",42,1,1,1)
_text (" Congratulations!!",25,80,"",22,0,1,1)
_text ("You have solved all",25,110,"",22,0,1,1)
_text ("levels. You are now",25,140,"",22,0,1,1)
_text ("a GRIDFILL master!!", 25,170,"",22,0,1,1)
_line (100,225,200,275,1,1,1,1)
_line (100,225,200,275,0,0,0,0)
_text ("Restart",108,238,"",20,0,0,1)
sprite "won" scan 0,0,302,302
sprite "won" at -2000,-2000
sprite "won" show

graphics clear 0,0,0
refresh on

return

'================
def showmsg(msg$)
'show a message box we made

'find center of screen
'the -150 is half size of our box
xm = screen_width()/2-150
xy = screen_height()/2-200

'play a sfx for box exploding in...
notes set "9:tc6d6e6f6g6a6b6c7"
notes play

'scale in the message box sprite
for t = 1 to 100 step 2
  sprite msg$ alpha t/100
  sprite msg$ at xm,xy scale t/100
  pause .005
next t

'wait for user to click close

do 
  tx = touch_x(0) ! ty = touch_y(0)
  if tx>xm+100 and tx<xm+200 then
     if ty>xy+225 and ty<xy+275 then
        'play a click sfx
        notes set "108:c7" ! notes play
        pause .2 ! break
     end if
  end if
until 0

'play sfx for box going away
notes set "9:tc7b6a6g6f6e6d6c6"
notes play

'dissapear box,scale it out...
for t = 100 to 0 step -2
  sprite msg$ alpha t/100
  sprite msg$ at xm,xy scale t/100
  pause .005
next t

end def


'=================================
def _line (x1,y1,x2,y2,fill,r,g,b)
'Combined draw rect,draw line, fill rect
'to this one function, like Qbasic does it.
'Draws a line or box, hollow or filled.
if fill=1 then
  fill color r,g,b
  fill rect x1,y1 to x2,y2
else
  draw color r,g,b
  draw rect x1,y1 to x2,y2
end if
end def


'======================================
def _text (text$,x,y,font$,size, r,g,b)
'just a one-line way to draw text
if font$<>"" then draw font name font$
if size>0 then draw font size size
draw color r,g,b
draw text text$ at x,y
end def


'=======
gridhide:
'=======
nm=0
for r= 1 to rows
   for c = 1 to columns
     nm=nm+1
     button str$(nm) delete
   next c
next r
return


'===========================
def FloodFill(old, clr, x, y)
	If .btn(x,y) <> old Then
		Return
	Else
		.btn(x,y) = clr
	End if
	If x > 1 Then FloodFill(old, clr, x-1, y)
	If x < .rows Then FloodFill(old, clr, x + 1, y)
	If y > 1 Then FloodFill(old, clr, x, y-1)
	If y < .columns Then FloodFill(old, clr, x, y+1)
End def
'==========================

Attachments
gridfill1.jpg
gridfill1.jpg (58.75 KiB) Viewed 4043 times
gridfill2.jpg
gridfill2.jpg (38.38 KiB) Viewed 4043 times
Last edited by Dav on Fri Jun 19, 2015 6:05 pm, edited 8 times in total.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4787
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Gridfill color game using buttons (iPad)

Post by Mr. Kibernetik »

Great game!

I would suggest to use SLOWDOWN command in main waiting loop to minimize energy consumption - and you can play much longer on a battery.

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: Gridfill color game using buttons (iPad)

Post by Dav »

Thanks, Mr. Kibernetik. II'm still using smart basic v4.7. I will update and look into using the new SLOWDOWN command. It's really fun making things using smart basic!

I have posted a new version of the game with some fixes And improvements. The grid is larger, the arrow button moves, and I added a new version of my infobar routine that uses transparent button to place info so it will update on top of graphic display without having to clear the background.

I have found some things with this game using buttons. For one, the arrow button has to be set MUCH further down the screen than others. At 70,1685. I wonder why?

button "▶︎" text "▶︎" at 1,10 size 70,1675

Also, When I increased the grid larger than 10x11 the buttons start disappearing. Have I reached a limit to number of buttons?

Thanks for trying out the game!

- Dav

User avatar
Mr. Kibernetik
Site Admin
Posts: 4787
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Gridfill color game using buttons (iPad)

Post by Mr. Kibernetik »

Dav wrote:Also, When I increased the grid larger than 10x11 the buttons start disappearing. Have I reached a limit to number of buttons?
Very interesting...

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: Gridfill color game using buttons (iPad)

Post by Dav »

Dav wrote: ..., the arrow button has to be set MUCH further down the screen than others. At 70,1685. I wonder why?
I figured out why... I had the buttons x,y and size parameters switched around by mistake. lol:
The code is corrected now.

EDIT: I also figured out why buttons started disappearing on larger grids - it is not a smart basic problem. My button naming method was duplicating button names. I fixed it by making sure each button gets a unique name and now I can use larger grids without error.

- Dav

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: Gridfill color game using buttons (iPad)

Post by Dav »

I had some time today to finish this little color puzzle game. It has 10 levels to beat now. The grid size gets larger as levels get higher, from 4x4 to 13x13. I also included a couple screenshots of the game. It plays okay for me on my iPad Mini running smart BASIC v4.7.

- Dav

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

Re: Gridfill color puzzle game using buttons (iPad)

Post by Dutchman »

Very nice game :D
It took me a lot of trials before I passed levels 6 and 7.
Higher levels took less effort.
It was frustrating however that the player has to restart at level 1 if he stopped the game.
Therefore I have made some changes so that I could preset the desired level.
The modified file is attached.
It is not completely correct however. The message after the final win shows the wrong number of turns.
Maybe you could adapt the game.
The game could be even more attractive if the actual status is stored in file and restored after restart.
And it turned out that my 'Sea Combat" game became a lot more attractive after I added player selection and showed personal high-scores.
So if you have some spare time ……… :P
Gridfill changed.txt
(12.13 KiB) Downloaded 295 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: Gridfill color puzzle game using buttons (iPad)

Post by Dav »

Thanks, Dutchman. GoOd suggestion. I will work on it and add a level save and maybe a couple other things. Thanks for trying out the game.

- Dav

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

Re: Gridfill color puzzle game using buttons (iPad)

Post by Dutchman »

I have adapted the game for usage with Launcher.
See viewtopic.php?f=20&t=1499

Due to usage of the exit-label to quit, it was quite simple to realise :D
Launcher can be disabled by changing to 'Launcher=0' at the beginning of the program

Code: Select all

'GRIDFILL puzzle game v1.3 fit fot Launcher 2.0
'Fill the grid with all one color.
'Use bottom buttons to choose color.
'Do all 10 levels to win the game.
'Made using smart BASIC v4.7
'Coded by Dav, JUNE/2015

'GAME FOR IPAD ONLY RIGHT NOW....

LEVEL=7 'set your start level

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

'=== set up screen
graphics
graphics clear 0,0,0
set orientation vertical
set toolbar off

'=== make message boxes
If level<>1 THEN GOSUB SetParameters
gosub makemessages

'=== draw background image
option base 0
gosub drawbackground
option base 1


set buttons custom
set buttons font size 56

firstrun=1

'======
newgame:
'======

draw color 0,0,0

sw=screen_width()
sh=screen_height()

gosub drawinfobar
gosub SetParameters
dim btn(rows,columns)

turns=0
red=0!grn=1!blu=2!yel=3!pur=4!org=5

'show current game level
   set buttons font size 26
   draw color 0,0,0
   fill color 1,1,1
   b$="Level "&str$(level)
   dx=sw-(text_width(b$))-48
   button "level" text b$ at 110,10
   draw color 0,0,0

'generate random board colors
for r= 1 to rows
   for c = 1 to columns
     btn(r,c)=rnd(6)
   next c
next r

gosub gridhide

if firstrun=1 then 
   showmsg ("newgame")
   firstrun=0
end if

gosub updateboard


'draw color changing buttons
fill color 1,0,0
button "red" text "" at 80,800 size 80,80
fill color 0,1,0
button "grn" text "" at 180,800 size 80,80
fill color 0,0,1
button "blu" text "" at 280,800 size 80,80
fill color 1,1,0
button "yel" text "" at 380,800 size 80,80
fill color 1,0,1
button "pur" text "" at 480,800 size 80,80
fill color 1,.5,0
button "org" text "" at 580,800 size 80,80

'draw quit button
set buttons font size 26
draw color 1,1,1
fill color 0,0,0
button "quit" text "Quit" at 675,900
draw color 0,0,0

clr=btn(1,1)! gosub drawarrow

gosub drawturns

do

  'get color press
  do 
    if button_pressed("red") then
      clr=red ! if btn(1,1)<>clr then break
    end if
    if button_pressed("grn") then
      clr=grn ! if btn(1,1)<>clr then break
    end if
    if button_pressed("blu") then
      clr=blu ! if btn(1,1)<>clr then break
    end if
    if button_pressed("yel") then
      clr=yel ! if btn(1,1)<>clr then break
    end if
    if button_pressed("pur") then
      clr=pur ! if btn(1,1)<>clr then break
    end if
    if button_pressed("org") then
      clr=org ! if btn(1,1)<>clr then break
    end if
    if button_pressed("quit") then exit

    if time()>60 then
      gosub drawinfobar
      time reset
    end if

  until 0

  old=btn(1,1) 

  turns=turns+1
  gosub drawturns
  gosub drawarrow
  gosub movearrowup

  floodfill(old,clr, 1,1)

  notes set "126:c4"!notes play

  gosub updateboard

  gosub movearrowdown

  gosub checkforwin

  'if turns over, game over
  if turns >turnmax-1 then
    set buttons font size 72
    draw color 1,1,1
    fill color 0,0,0
    tx$="Sorry!"!tl=text_width(tx$)+20
    button "lose" text tx$ at sw/2-tl,sh/2-100
    notes set "111:h(d3fa)(dga#)(dfa)"
    notes play ! pause 3
    button "lose" delete
    gosub gridhide
    showmsg("retry")
    goto newgame
  end if

until forever

exit:
IF Launch$="launcher" THEN
{/- Launcher.sb}
ENDIF
SET TOOLBAR ON
end


'==========================================
' GOSUBS AND FUNCTIONS
'==========================================


'==========
updateboard:
'==========
'updates board based on btn() values

nm=0
for r= 1 to rows
   for c = 1 to columns
     'bn$=str$(r)&str$(c)
     j= btn(r,c) ! nm=nm+1
     if j=0 then fill color 1,0,0
     if j=1 then fill color 0,1,0
     if j=2 then fill color 0,0,1
     if j=3 then fill color 1,1,0
     if j=4 then fill color 1,0,1
     if j=5 then fill color 1,.5,0
     x=(r*size) ! y=(c*size)
     button str$(nm) text "" at x,y+15 size size,size
   next c
next r

return


'==========
checkforwin:
'==========
'sees if grid is all one color (win)

t=0
for y=1 to columns
   for x= 1 to rows
     if btn(x,y)= clr then t=t+1
   next x
next y
'if it's completed...
if t = (columns*rows) then
  set buttons font size 72
  draw color 0,0,0
  fill color 1,1,1
  tx$="Good!"!tl=text_width(tx$)+20
  button "win" text tx$ at sw/2-tl,sh/2-100
  notes set "7:s(c3eg)(e4cg3)(g4ec)(c5g4e)"
  notes play ! pause 1.5
  button "win" delete
  gosub gridhide
  level=level+1
  '=== if all levels done...
  if level > 10 then 
     gosub gridhide
     '=== show happy face
     gosub drawface
     notes set "90:i(eg#b)r(df#a)w(eg#b)"
     notes play ! pause 3
     showmsg("won")
     pause 2
     '=== redraw background
     sprite "bg" show
     sprite "bg" stamp
     sprite "bg" hide
     '=== start over
     level=1
     firstrun= 1
     gosub setparameters
     goto newgame
  end if
  showmsg("nextlevel")
  goto newgame
end if

return

'=======
drawface:
'=======
'draws a happy face

sw=screen_width()
sh=screen_height()-100
fill color 1,1,0
fill circle sw/2,sh/2 size 200
fill color 0,0,0
fill circle sw/2-75,sh/2-75 size 50
fill circle sw/2+75,sh/2-75 size 50
fill color 1,1,1
fill circle sw/2-75,sh/2-75 size 15
fill circle sw/2+75,sh/2-75 size 15
fill color 0,0,0
fill circle sw/2,sh/2+20 size 20
draw size 25
draw color 0,0,0
draw arc sw/2,sh/2,125,3.14,0,1
return


'========
drawturns:
'========
  set buttons font size 26
  draw color 0,0,0
  fill color 1,1,1
  tt$=str$(turns)&"/"&str$(turnmax)
  button "turns" text tt$ at 10,10
return


'========
drawarrow:
'========

fill alpha 0
draw color 0,0,0
set buttons font size 56
button "▶︎" text "▶︎" at 1,800 size 70,70
draw color 0,0,0
fill alpha 1

return


'=============
drawbackground:
'=============
'draws a rainbow background

dim rd(961),gn(961),bu(961)

for b = 0 to 255
   rd(i)=255!gn(i)=b!bu(i)=0!i=i+1
next b
for r = 255 to 0 step -1
   rd(i)=r!gn(i)=255!bu(i)=0!i=i+1
next r
for y = 0 to 255
   rd(i)=0!gn(i)=255-y!bu(i)=y!i=i+1
next y
for z = 0 to 192
   rd(i)=0!gn(i)=z!bu(i)=255!i=i+1
next z

draw alpha .4
for y = 0 to 960
   draw color rd(y)/255,gn(y)/255,bu(y)/255
   draw line 0,y to screen_width(),y
next y
_line(0,750,screen_width(),752,1,0,0,.3)


draw alpha 1
shadow on
draw color .5,.5,1
draw font size 24
draw text "GRIDFILL v1.2" at 275,915
sw=screen_width()!sh=screen_height()
sprite "bg" scan 0,0,sw,sh
sprite "bg" at 0,0
sprite "bg" show
sprite "bg" stamp
sprite "bg" hide

return

'==========
movearrowup:
'==========
fill alpha 0
draw color 1,1,1
if clr=0 then draw color 1,0,0
if clr=1 then draw color 0,1,0
if clr=2 then draw color 0,0,1
if clr=3 then draw color 1,1,0
if clr=4 then draw color 1,0,1
if clr=5 then draw color 1,.5,0
for m=800 to 50 step -60
  button "▶︎" text "▶︎" at 1,m size 70,70
  pause .01
next m
draw color 0,0,0

fill alpha 1
return


'============
movearrowdown:
'============

fill alpha 0
draw color 0,0,0
for m=50 to 800 step 60
  button "▶︎" text "▶︎" at 1,m size 70,70
  pause .01
next m
draw color 0,0,0
fill alpha 1
gosub drawarrow
return

'==========
drawinfobar:
'==========
fill color 0,0,0
draw color 1,1,1
fill alpha 0
bat$=str$(battery_level())&"%"
set buttons font size 20
button "bat" text bat$ at sw-text_width(bat$)-90,1
ampm$="AM" ! hr=current_hour()
min$=str$(current_minute())
if len(min$)=1 then min$="0"&min$
if hr>12 then
  hr=hr-12 ! ampm$="PM"
end if
tm$=str$(hr)&":"&min$&" "&ampm$
button "time" text tm$ at ((sw/2)-text_width(tm$)/2),1
'draw battery graphic
fill alpha 1
draw size 1
fill rect sw-70,10 to sw-20,24
draw rect sw-70,10 to sw-20,24
f=battery_level()/2
fill color 1,1,1
fill rect sw-70,10 to sw-70+f,24
fill rect sw-20, 14 to sw-17, 19
draw color 0,0,0

return


'===========
makemessages:
'===========

refresh off

'=== make new game box
_line (0,0,300,300,1,1,1,1)
_line (5,5,295,295,1,0,0,1)
_line (0,0,300,300,0,0,0,0)
_text ("NEW GAME",65,20,"",36,1,1,1)
_text ("Fill the grid with",25,80,"",22,0,1,1)
_text ("one color using the",25,110,"",22,0,1,1)
_text ("big color buttons.",25,140,"",22,0,1,1)
_text ("You have "&turnmax&" turns.", 25,170,"",22,0,1,1)
_line (100,225,200,275,1,1,1,1)
_line (100,225,200,275,0,0,0,0)
_text ("GO!",125,234,"",36,0,0,1)
sprite "newgame" scan 0,0,302,302
sprite "newgame" at -2000,-2000
sprite "newgame" show

graphics clear 0,0,0

'make next level box
_line (0,0,300,300,1,1,1,1)
_line (5,5,295,295,1,0,0,1)
_line (0,0,300,300,0,0,0,0)
_text ("YOU DID IT!",33,20,"",36,1,1,1)
_text ("Good job. Now for a",25,80,"",22,0,1,1)
_text ("larger grid to fill",25,110,"",22,0,1,1)
_text ("up. Number of turns",25,140,"",22,0,1,1)
_text ("will be increased.", 25,170,"",22,0,1,1)
_line (100,225,200,275,1,1,1,1)
_line (100,225,200,275,0,0,0,0)
_text ("GO!",125,234,"",36,0,0,1)
sprite "nextlevel" scan 0,0,302,302
sprite "nextlevel" at -2000,-2000
sprite "nextlevel" show

graphics clear 0,0,0

'make retry message
_line (0,0,300,300,1,1,1,1)
_line (5,5,295,295,1,0,0,1)
_line (0,0,300,300,0,0,0,0)
_text ("OUT OF TURNS",25,20,"",36,1,1,1)
_text ("Sorry, you ran out",25,80,"",22,0,1,1)
_text ("of turns. But don't",25,110,"",22,0,1,1)
_text ("worry, you can have",25,140,"",22,0,1,1)
_text ("another try...", 25,170,"",22,0,1,1)
_line (100,225,200,275,1,1,1,1)
_line (100,225,200,275,0,0,0,0)
_text ("RETRY",108,238,"",28,0,0,1)
sprite "retry" scan 0,0,302,302
sprite "retry" at -2000,-2000
sprite "retry" show

graphics clear 0,0,0

'make you won message
_line (0,0,300,300,1,1,1,1)
_line (5,5,295,295,1,0,0,1)
_line (0,0,300,300,0,0,0,0)
_text (" YOU WON!",25,25,"",42,1,1,1)
_text (" Congratulations!!",25,80,"",22,0,1,1)
_text ("You have solved all",25,110,"",22,0,1,1)
_text ("levels. You are now",25,140,"",22,0,1,1)
_text ("a GRIDFILL master!!", 25,170,"",22,0,1,1)
_line (100,225,200,275,1,1,1,1)
_line (100,225,200,275,0,0,0,0)
_text ("Restart",108,238,"",20,0,0,1)
sprite "won" scan 0,0,302,302
sprite "won" at -2000,-2000
sprite "won" show

graphics clear 0,0,0
refresh on

return

'================
def showmsg(msg$)
'show a message box we made

'find center of screen
'the -150 is half size of our box
xm = screen_width()/2-150
xy = screen_height()/2-200

'play a sfx for box exploding in...
notes set "9:tc6d6e6f6g6a6b6c7"
notes play

'scale in the message box sprite
for t = 1 to 100 step 2
  sprite msg$ alpha t/100
  sprite msg$ at xm,xy scale t/100
  pause .005
next t

'wait for user to click close

do 
  tx = touch_x(0) ! ty = touch_y(0)
  if tx>xm+100 and tx<xm+200 then
     if ty>xy+225 and ty<xy+275 then
        'play a click sfx
        notes set "108:c7" ! notes play
        pause .2 ! break
     end if
  end if
until 0

'play sfx for box going away
notes set "9:tc7b6a6g6f6e6d6c6"
notes play

'dissapear box,scale it out...
for t = 100 to 0 step -2
  sprite msg$ alpha t/100
  sprite msg$ at xm,xy scale t/100
  pause .005
next t

end def


'=================================
def _line (x1,y1,x2,y2,fill,r,g,b)
'Combined draw rect,draw line, fill rect
'to this one function, like Qbasic does it.
'Draws a line or box, hollow or filled.
if fill=1 then
  fill color r,g,b
  fill rect x1,y1 to x2,y2
else
  draw color r,g,b
  draw rect x1,y1 to x2,y2
end if
end def


'======================================
def _text (text$,x,y,font$,size, r,g,b)
'just a one-line way to draw text
if font$<>"" then draw font name font$
if size>0 then draw font size size
draw color r,g,b
draw text text$ at x,y
end def


'=======
gridhide:
'=======
nm=0
for r= 1 to rows
   for c = 1 to columns
     nm=nm+1
     button str$(nm) delete
   next c
next r
return


'===========================
def FloodFill(old, clr, x, y)
   If .btn(x,y) <> old Then
      Return
   Else
      .btn(x,y) = clr
   End if
   If x > 1 Then FloodFill(old, clr, x-1, y)
   If x < .rows Then FloodFill(old, clr, x + 1, y)
   If y > 1 Then FloodFill(old, clr, x, y-1)
   If y < .columns Then FloodFill(old, clr, x, y+1)
End def
'==========================
SetParameters:
if level =1 then
   rows=4 ! columns=4 ! size=125 ! turnmax=7
end if
if level=2 then
   rows=5 ! columns=5 ! size=107 ! turnmax=8
end if
if level=3 then
   rows=6 ! columns=6 ! size=95 ! turnmax=10
end if
if level=4 then
   rows=7 ! columns=7 ! size=83 ! turnmax=12
end if
if level=5 then
   rows=8 ! columns=8 ! size=75 ! turnmax=14
end if
if level=6 then
   rows=9 ! columns=9 ! size=68 ! turnmax=16
end if
if level=7 then
   rows=10 ! columns=10 ! size=62 ! turnmax=18
end if
if level=8 then
   rows=11 ! columns=11 ! size=57 ! turnmax=20
end if
if level=9 then
   rows=12 ! columns=12 ! size=55 ! turnmax=22
end if
if level=10 then
   rows=13 ! columns=13 ! size=50 ! turnmax=24
end if
RETURN
Last edited by Dutchman on Fri Jan 20, 2023 12:32 pm, edited 1 time in total.

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

Re: Gridfill color puzzle game using buttons (iPad)

Post by Dutchman »

The code in the previous post has been adapted to Launcher 2.0

Post Reply