Highlights for v1.3 features.
- 25 levels.
- Now saves game progress.
- Restart level button
- SLOWDOWN saves battery.
Hope you enjoy this new version!
- Dav
NOTE: The game is currently set to 25 levels, but you can change that by adjusting maxlevel value to any number you want. Anything over 50 just gets too frustrating for me, but it will work at 50 or even higher.
Code: Select all
'GRIDFILL puzzle game v1.3
'Fill the grid with all one color.
'Use bottom buttons to choose color.
'Complete all 25 levels to win the game.
'
'Coded by Dav, September/2016
'Some modifications by Ducthman (Thanks)
'NEW for v1.3
'
'. - Added more levels. 25 in total.
'.
'. - Game progress now saved to a file in
'. the current dir called gridfill.dat.
'.
'. - Replaced BUTTONS method in grid board
'. with FILL RECT instead. Much faster.
'.
'. - Changed the arrow to bigger emoji one.
'.
'. - Added restart level button at bottom.
'.
'. - Added happy music when game won.
'.
'. - Fixed midnight bug in clock display.
'.
'. - Added SLOWDOWN to save battery.
'.
'. - Added happy win music & color flashing
'GAME FOR IPAD ONLY RIGHT NOW....
maxlevel=25 'How many levels the game has.
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 buttons custom
set buttons font size 56
set toolbar off
'=== read data file, make new if not there
level=1
if file_exists("gridfill.dat") then
file "gridfill.dat" read lev
if lev < 1 or lev > maxlevel then goto makenew
level=lev
else
makenew:
file "gridfill.dat" write level
end if
'=== make message boxes
If level <>1 then gosub SetParameters
gosub makemessages
'=== draw background image
option base 0
gosub drawbackground
option base 1
firstrun=1
'level=25 'level override for testing
'======
newgame:
'======
'=== update config file
file "gridfill.dat" delete
file "gridfill.dat" write level
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 136,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 100,800 size 80,80
fill color 0,1,0
button "grn" text "" at 200,800 size 80,80
fill color 0,0,1
button "blu" text "" at 300,800 size 80,80
fill color 1,1,0
button "yel" text "" at 400,800 size 80,80
fill color 1,0,1
button "pur" text "" at 500,800 size 80,80
fill color 1,.5,0
button "org" text "" at 600,800 size 80,80
'draw quit & restart buttons
set buttons font size 26
draw color 1,1,1
fill color 0,0,0
button "quit" text "Quit" at 675,900
button "restart" text "Restart" at 550,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 button_pressed("restart") then goto newgame
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$="Failed!"!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
slowdown
until forever
end
'==========================================
' GOSUBS AND FUNCTIONS
'==========================================
'==========
updateboard:
'==========
size=650/rows
'updates board based on btn() values
nm=0
for r= 1 to rows
for c = 1 to columns
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)
fill rect x+65-size,y+90-size to x+65-4,y+90-4
pause dly
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
gosub cyclecolors
button "win" delete
gosub gridhide
level=level+1
'=== if all levels done...
if level > maxlevel then
gosub gridhide
'=== show happy face
gosub drawface
gosub playhappymusic
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:
'=======
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 1,1,1
set buttons font size 56
button ">" text chr$(10162) 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 step 2
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.3" 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 -40
button ">" text chr$(10162) 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 1,1,1
for m=90 to 800 step 40
button ">" text chr$(10162) 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
if hr=0 then hr=12
tm$=str$(hr)&":"&min$&" "&m$
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:
'===========
gosub SetParameters
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:
'=======
refresh off
'=== redraw background
sprite "bg" show
sprite "bg" stamp
sprite "bg" hide
gosub drawinfobar
refresh on
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
'=============
playhappymusic:
'=============
'happy music
c$="46:i(c4aff3c5)(ff4a)(ca5ff4)(c7a6)(a6f)(c5c4af)"
c$=c$&"i(c4aff3c5)(ff4a)(ca5ff4)(c7a6)(a6f)(c5c4af)"
c$=c$&"i(a#gecc5)(ege4)q(a#5gc5e4)(a#5d6g4e)"
c$=c$&"i(ega#5d6)(c6a6f5)q(caf)s(c6a5)(a#g)(af)(ge)"
c$=c$&"i(c4aff3c5)(ff4a)(ca5ff4)(c7a6)(a6f)(c5c4af)"
c$=c$&"i(c4aff3c5)(ff4a)(ca5ff4)(c7a6)(a6f)(c5c4af)"
c$=c$&"i(a#gecc5)(ege4)q(a#5gc5e4)(e5ca#4gc)h(f5ca3f)"
'play song
notes set c$ ! notes play
'loop until music stops playing
do! until notes_time() => notes_length()
return
'==========
cyclecolors:
'==========
size=650/rows
for j=0 to 5
for r= 1 to rows
for c = 1 to columns
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)
fill rect x+65-size,y+90-size to x+65-4,y+90-4
pause dly
next c
next r
next j
return
'=============
SetParameters:
'=============
rows=4+level ! columns=4+level ! turnmax=rows*2-2
'compute delay for board flooding routine
dly=.009
for h= 1 to level
dly=dly-.0007
next h
return