This wouldnt be correct without some kind of scoring and remembering best scores etc and a little animation
// Project: 2048
// Created: 2020-03-01
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
// set window properties
SetWindowTitle( "2048" )
SetWindowSize( screenwidth, screenheight, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( screenwidth, screenheight ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
global totalsize=400
global size = 4
type tiledetails
number
txt
bounce
dontcalculateagainthisstep
endtype
global tiles as tiledetails[10,10] // max size
global gridsize
gridsize = totalsize/size
global gridstartingx
global gridstartingy
gridstartingy=screenheight/2-(size*gridsize)/2
gridstartingx=screenwidth/2-(size*gridsize)/2
global ScoreValue=0
global ScoreText
global BestScoreValue=0
global BestScoreText
global alreadywon=0
global framestep#, whattobeat
ScoreText = CreateText("Score: " + str(scorevalue))
SetTextSize(scoretext,64)
BestScoreText = CreateText("Best Score: " + str(Bestscorevalue))
SetTextSize(Bestscoretext,64)
Help = CreateText("")
SetTextSize(Help,32)
AddVirtualButton(1,900,200,128)
SetVirtualButtonText(1,"Reset")
AddVirtualButton(2,900,400,128)
SetVirtualButtonText(2,"Grid Size -")
AddVirtualButton(3,900,600,128)
SetVirtualButtonText(3,"Grid Size +")
resettiles()
bounce=1
framestep#=timer()
whattobeat = pow(2,(size*3)-1)
do
checkbuttons()
checkwin()
drawtiles()
checkmovements()
for x=0 to size-1
for y=0 to size-1
if tiles[x,y].bounce=1
if timer()-framestep#>.001
framestep#=timer()
c=Get2048Color(tiles[x,y].number)
DrawBox(gridstartingx + x * gridsize+4, gridstartingy + y * gridsize+4,(gridstartingx + x * gridsize)+gridsize-4, (gridstartingy + y * gridsize)+gridsize-4,c,c,c,c,1)
SetTextSize(tiles[x,y].txt,GetTextSize(tiles[x,y].txt)*1.1)
if GetTextSize(tiles[x,y].txt) > gridsize/3
SetTextSize(tiles[x,y].txt,gridsize/3)
tiles[x,y].bounce=0
endif
endif
endif
next
next
// bounce=bounce*-1
//next
SetTextString(scoretext, "Score: " + str(scorevalue))
SetTextPosition(scoretext,60,10)
SetTextString(bestscoretext, "Best Score: " + str(bestscorevalue))
SetTextPosition(bestscoretext,600,10)
whattobeat = pow(2,(size*3)-1)
SetTextString(Help,"Welcome to AGK 2048 - Use Cursors to move and try to reach " + str(whattobeat))
SetTextPosition(Help,screenwidth/2-GetTextTotalWidth(help)/2,screenheight-50)
Render2DFront()
Sync()
loop
function checkbuttons()
if GetVirtualButtonPressed(1) // reset
resettiles()
endif
if GetVirtualButtonPressed(2) // size -
dec size
gridsize = totalsize/size
gridstartingy=screenheight/2-(size*gridsize)/2
gridstartingx=screenwidth/2-(size*gridsize)/2
resettiles()
endif
if GetVirtualButtonPressed(3) // size +
inc size
gridsize = totalsize/size
gridstartingy=screenheight/2-(size*gridsize)/2
gridstartingx=screenwidth/2-(size*gridsize)/2
resettiles()
endif
endfunction
function checkmovements()
if GetRawKeyPressed(37) then shift(-1,0,1)
if GetRawKeyPressed(39) then shift(1,0,1)
if GetRawKeyPressed(38) then shift(0,-1,1)
if GetRawKeyPressed(40) then shift(0,1,1)
if GetRawKeyPressed(32) then addtiles()
if GetRawKeyPressed(13) then increment()
endfunction
function shift(xdir,ydir,add)
if xdir=-1 // go left
// shift
// columns shifting over -1
didweaddoneornot=0
repeat
nomoremoves=0
for y=0 to size-1
for x=1 to size step 1
if tiles[x-1,y].number=0 and tiles[x,y].number<>0
tiles[x-1,y].number = tiles[x,y].number
nomoremoves=1
didwemoveornot=1
tiles[x,y].number=0
endif
next
next
dontcombineagain=1
until nomoremoves=0
// combine
dontcombineagain=0
for y=0 to size-1
for x=1 to size step 1
if tiles[x-1,y].number = tiles[x,y].number and tiles[x,y].number<>0 and dontcombineagain=0
inc didwemoveornot
tiles[x-1,y].number = tiles[x,y].number * 2
tiles[x,y].number=0
// tiles[x-1,y].bounce=1
//SetTextSize(tiles[x-1,y].txt,12)
updatescores(tiles[x-1,y].number)
endif
next
next
// shift
// columns shifting over -1
repeat
nomoremoves=0
for y=0 to size-1
for x=1 to size step 1
if tiles[x-1,y].number=0 and tiles[x,y].number<>0
tiles[x-1,y].number = tiles[x,y].number
nomoremoves=1
inc didwemoveornot
tiles[x,y].number=0
endif
next
next
dontcombineagain=1
until nomoremoves=0
if didwemoveornot>0 then addtiles()
endif
if xdir=1 // go right
// columns shifting over +1
didwemoveornot=0
repeat
nomoremoves=0
for y=0 to size-1
for x=0 to size-2 step 1
if tiles[x+1,y].number=0 and tiles[x,y].number<>0
tiles[x+1,y].number = tiles[x,y].number
tiles[x,y].number=0
inc didwemoveornot
nomoremoves=1
endif
next
next
dontcombineagain=1
until nomoremoves=0
// combine
dontcombineagain=0
for y=0 to size-1
for x=size-1 to 0 step -1
if tiles[x+1,y].number = tiles[x,y].number and tiles[x,y].number<>0 and dontcombineagain=0
inc didwemoveornot
tiles[x+1,y].number = tiles[x+1,y].number * 2
tiles[x,y].number=0
//tiles[x+1,y].bounce=1
//SetTextSize(tiles[x+1,y].txt,12)
updatescores(tiles[x+1,y].number)
endif
next
next
// shift
repeat
nomoremoves=0
for y=0 to size-1
for x=0 to size-2 step 1
if tiles[x+1,y].number=0 and tiles[x,y].number<>0
tiles[x+1,y].number = tiles[x,y].number
tiles[x,y].number=0
inc didwemoveornot
nomoremoves=1
endif
next
next
dontcombineagain=1
until nomoremoves=0
if didwemoveornot>0 then addtiles()
endif
// up and down
if ydir=-1 // go up
// rows shifting over -1
didwemoveornot=0
dontcombineagain=0
repeat
nomoremoves=0
for x=0 to size-1
for y=1 to size-1
if tiles[x,y-1].number=0 and tiles[x,y].number<>0
tiles[x,y-1].number = tiles[x,y].number
tiles[x,y].number=0
nomoremoves=1
inc didwemoveornot
endif
next
next
dontcombineagain=1
until nomoremoves=0
dontcombineagain=0
for y=1 to size
for x=size to 0 step -1
if tiles[x,y-1].number = tiles[x,y].number and tiles[x,y].number<>0 and dontcombineagain=0
inc didwemoveornot
tiles[x,y-1].number = tiles[x,y-1].number * 2
tiles[x,y].number=0
//tiles[x,y-1].bounce=1
//SetTextSize(tiles[x,y-1].txt,12)
updatescores(tiles[x,y-1].number)
endif
next
next
dontcombineagain=0
repeat
nomoremoves=0
for x=0 to size-1
for y=1 to size-1
if tiles[x,y-1].number=0 and tiles[x,y].number<>0
tiles[x,y-1].number = tiles[x,y].number
tiles[x,y].number=0
inc didwemoveornot
nomoremoves=1
endif
next
next
dontcombineagain=1
until nomoremoves=0
if didwemoveornot>0 then addtiles()
endif
if ydir=1 // go down
// rows shifting over +1
didwemoveornot=0
dontcombineagain=0
repeat
nomoremoves=0
for x=0 to size-1 step 1
for y=0 to size-2
if tiles[x,y+1].number=0 and tiles[x,y].number<>0
tiles[x,y+1].number = tiles[x,y].number
tiles[x,y].number=0
inc didwemoveornot
nomoremoves=1
endif
next
next
dontcombineagain=1
until nomoremoves=0
//combine
dontcombineagain=0
for y=1 to size
for x=size to 0 step -1
if tiles[x,y+1].number = tiles[x,y].number and tiles[x,y].number<>0 and dontcombineagain=0
inc didwemoveornot
tiles[x,y+1].number = tiles[x,y+1].number * 2
tiles[x,y].number=0
//tiles[x,y+1].bounce=1
//SetTextSize(tiles[x,y+1].txt,12)
updatescores(tiles[x,y+1].number)
endif
next
next
//shift
repeat
nomoremoves=0
for x=0 to size-1 step 1
for y=0 to size-2
if tiles[x,y+1].number=0 and tiles[x,y].number<>0
tiles[x,y+1].number = tiles[x,y].number
tiles[x,y].number=0
inc didwemoveornot
nomoremoves=1
endif
next
next
dontcombineagain=1
until nomoremoves=0
if didwemoveornot>0 then addtiles()
endif
endfunction didwemoveornot
function updatescores(value)
inc scorevalue,value
if scorevalue>bestscorevalue then inc bestscorevalue,value
savesscore()
endfunction
function savesscore()
fr=OpenToWrite("score.dat")
WriteLine(fr,str(bestscorevalue))
CloseFile(fr)
endfunction
function loadscore()
if GetFileExists("score.dat")
fr=OpenToRead("score.dat")
bestscorevalue = val(ReadLine(fr))
CloseFile(fr)
else
// complete brand new setup
fw=OpenToWrite("score.dat")
WriteLine(fw,"0")
CloseFile(fw)
endif
endfunction
function drawtiles()
backcolor=MakeColor(255,198,153)
DrawBox(gridstartingx-50,gridstartingy-50,(gridstartingx+(size*gridsize))+50,(gridstartingy+(size*gridsize))+50,MakeColor(255,0,0),MakeColor(0,255,0),MakeColor(0,0,255),MakeColor(255,255,0),1)
// draw gridlines inbetween cells
c=MakeColor(100,100,100)
for x=0 to size
for y=0 to size
xx# =gridstartingx
xx2# =gridstartingx + ( size * gridsize )
yy# =gridstartingy + ( y * gridsize )-2.5
DrawBox(xx#,yy#,xx2#,yy#+5,c,c,c,c,1)
yy#=gridstartingy
yy2#=gridstartingy + ( size * gridsize )
xx#=gridstartingx + ( x * gridsize )-2.5
DrawBox(xx#,yy#,xx#+5,yy2#,c,c,c,c,1)
next
next
for x=0 to size-1
for y=0 to size-1
// reset to black cause dont want to see
SetTextColor(tiles[x,y].txt,255,198,153,255) //same as the background color
SetTextString(tiles[x,y].txt, str(tiles[x,y].number))
if tiles[x,y].number<>0
// only want to see the 2,4,8,16,32...... etc
c=MakeColor(200,200,0)
SetTextColor(tiles[x,y].txt,255,255,255,255)
c=Get2048Color(tiles[x,y].number)
DrawBox(gridstartingx + x * gridsize+9, gridstartingy + y * gridsize+9,(gridstartingx + x * gridsize)+gridsize-9, (gridstartingy + y * gridsize)+gridsize-9,c,c,c,c,1)
// for a=0 to 5
// c=MakeColor(200,200,200)
// DrawBox(gridstartingx + x * gridsize+a, gridstartingy + y * gridsize+a,(gridstartingx + x * gridsize)+gridsize-a, (gridstartingy + y * gridsize)+gridsize-a,c,c,c,c,0)
// next
// endif
SetTextVisible(tiles[x,y].txt,1)
else
c=MakeColor(200,200,0)
SetTextColor(tiles[x,y].txt,255,255,255,255)
DrawBox(gridstartingx + x * gridsize+5, gridstartingy + y * gridsize+5,(gridstartingx + x * gridsize)+gridsize-5, (gridstartingy + y * gridsize)+gridsize-5,c,c,c,c,1)
SetTextVisible(tiles[x,y].txt,0)
endif
// SetTextSize(tiles[x,y].txt,gridsize/2)
// if the tile is not currently bouncing
if tiles[x,y].bounce=0
SetTextSize(tiles[x,y].txt,gridsize/3)
endif
xx=gridstartingx+x*gridsize + gridsize/2 - (GetTextTotalWidth(tiles[x,y].txt)/2)
yy=gridstartingy+y*gridsize + gridsize/2 - (GetTextTotalHeight(tiles[x,y].txt)/2)
SetTextPosition(tiles[x,y].txt,xx,yy)
next
next
endfunction
function increment()
for x=0 to size-1
for y=0 to size-1
if tiles[x,y].number<>0
tiles[x,y].number = tiles[x,y].number * 2
SetTextString(tiles[x,y].txt,str(tiles[x,y].number))
endif
next
next
endfunction
function addtiles()
tilefound=0
repeat
tilenumberx = random(0,size-1)
tilenumbery = random(0,size-1)
if tiles[tilenumberx,tilenumbery].number=0
number2or4 = random(0,100)
if number2or4 < 95 // 95 % chance its a 2 or a 4
tiles[tilenumberx,tilenumbery].number=2
else
tiles[tilenumberx,tilenumbery].number=4
endif
SetTextString(tiles[tilenumberx,tilenumbery].txt, str(tiles[tilenumberx,tilenumbery].number))
SetTextSize(tiles[tilenumberx,tilenumbery].txt,12)
tiles[tilenumberx,tilenumbery].bounce=1
tilefound=1
endif
until tilefound=1
framestep#=timer()
endfunction
function checkwin()
count=0
win=0
for x=0 to size-1
for y=0 to size-1
if tiles[x,y].number=whattobeat
win=1
endif
next
next
if win=1 and alreadywon=0
repeat
print("WIN - Press 'space' key to continue")
sync()
until GetRawKeyPressed(32)
alreadywon=1
endif
endfunction
function checkgameover()
count=0
for x=0 to size-1
for y=0 to size-1
if tiles[x,y].number<>0
count=count+1
endif
next
next
if count=>(size*size)
// this can be changed to how we want
print("GAMEOVER")
drawtiles()
Render2DFront()
sync()
sleep(5000)
resettiles()
endif
endfunction
function resettiles()
loadscore()
if size>=9 then size=9
if size<=3 then size=3
// clear everything from the max tiles
for x=0 to 10
for y=0 to 10
DeleteText(tiles[x,y].txt)
next
next
for x=0 to size-1
for y=0 to size-1
DeleteText(tiles[x,y].txt)
tiles[x,y].number = 0
tiles[x,y].bounce = 0
tiles[x,y].txt = CreateText(str(tiles[x,y].number))
SetTextSize(tiles[x,y].txt,1)
next
next
addtiles()
addtiles()
endfunction
function Get2048Color(number)
if number=2 or number=4 then col=MakeColor(119,110,101)
if number=8 then col=MakeColor(242,177,121)
if number=16 then col=MakeColor(245,149,99)
if number=32 then col = MakeColor(246,124,95)
if number=64 then col = MakeColor(246,94,59)
if number=128 then col = MakeColor(237,207,114)
if number=256 then col = MakeColor(237,207,97)
if number=512 then col = MakeColor(237,200,80)
if number=1024 then col = MakeColor(237,197,63)
if number=2048 then col = MakeColor(237,194,46)
endfunction col
Have fun, been a nice little challenge all the same.
Moving onto something else soon