2048: Difference between revisions

Content added Content deleted
Line 6,419: Line 6,419:
<lang ring>
<lang ring>
# Project : 2048 Game
# Project : 2048 Game
# Date : 2018/09/07
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>


load "stdlib.ring"
load "stdlib.ring"
load "guilib.ring"
load "guilib.ring"

C_GAMETITLE = '2048 Game'
C_WINDOWBACKGROUND = "background-color: gray;"
if isMobile()
C_LABELFONTSIZE = "font-size:120px;"
C_BUTTONFONTSIZE = "font-size:160px;"
else
C_LABELFONTSIZE = "font-size:50px;"
C_BUTTONFONTSIZE = "font-size:80px;"
ok
C_PLAYERSCORESTYLE = "color:white;background-color:rgb(50,50,50);border-radius:17px;" + C_LABELFONTSIZE
C_NEWGAMESTYLE = 'color:white;background-color:rgb(50,50,50);border-radius:17px;' + C_LABELFONTSIZE
C_EMPTYBUTTONSTYLE = 'border-radius:17px;background-color:silver;' + C_BUTTONFONTSIZE
C_BUTTON2STYLE = 'border-radius:17px;color:black; background-color: yellow ;' + C_BUTTONFONTSIZE
C_BUTTON4STYLE = 'border-radius:17px;color:black; background-color: violet ;' + C_BUTTONFONTSIZE
C_BUTTON8STYLE = 'border-radius:17px;color:white; background-color: purple ;' + C_BUTTONFONTSIZE
C_BUTTON16STYLE = 'border-radius:17px;color:white; background-color: blue ;' + C_BUTTONFONTSIZE
C_BUTTON32STYLE = 'border-radius:17px;color:white; background-color: red ;' + C_BUTTONFONTSIZE
C_BUTTON64STYLE = 'border-radius:17px;color:black; background-color: lightgray ;' + C_BUTTONFONTSIZE
C_BUTTON128STYLE = 'border-radius:17px;color:black; background-color: white ;' + C_BUTTONFONTSIZE
C_BUTTON256STYLE = 'border-radius:17px;color:white; background-color: black ;' + C_BUTTONFONTSIZE
C_BUTTON512STYLE = 'border-radius:17px;color:white; background-color: Purple ;' + C_BUTTONFONTSIZE
C_BUTTON1024STYLE = 'border-radius:17px;color:black; background-color: Yellow ;' + C_BUTTONFONTSIZE
C_BUTTON2048STYLE = 'border-radius:17px;color:white; background-color: Green ;' + C_BUTTONFONTSIZE
C_LAYOUTSPACING = 10
C_PLAYERSCORE = 'Player Score : '


size = 4
size = 4
Line 6,448: Line 6,477:
processevents()
processevents()
win = new qWidget() {
win = new qWidget() {
setWindowTitle('2048 Game')
setWindowTitle(C_GAMETITLE)
setgeometry(100,100,800,600)
setgeometry(100,100,600,700)
setminimumwidth(300)
setminimumwidth(300)
setminimumheight(300)
setminimumheight(300)
if not isMobile()
grabkeyboard()
setstylesheet('background-color:white')
grabkeyboard()
ok
setstylesheet(C_WINDOWBACKGROUND)
move(490,100)
move(490,100)
for n = 1 to size
for n = 1 to size
Line 6,472: Line 6,503:
winheight = win.height()
winheight = win.height()
for n = 1 to size + 2
for n = 1 to size + 2
LayoutButtonRow[n] = new QHBoxLayout() {
LayoutButtonRow[n] = new QHBoxLayout() {
setSpacing(C_LAYOUTSPACING)
setSpacing(3) }
}
next
next
for n = 1 to size
for n = 1 to size
Line 6,479: Line 6,511:
button[n][m] { temp = text() }
button[n][m] { temp = text() }
buttonsave[n][m] = temp
buttonsave[n][m] = temp
buttonsave[n][m] = temp
fontsize = 10 + (winheight/25)
fontsize2 = 10 + (winheight/50)
button[n][m] = new MyButton(win) {
button[n][m] = new MyButton(win) {
setFont(new qFont("Verdana",fontsize,100,0))
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
setstylesheet('background-color:orange')
setstylesheet(C_EMPTYBUTTONSTYLE)
show()
show()
}
}
next
next
next
next
Line 6,497: Line 6,525:
button[n][m].settext(temp)
button[n][m].settext(temp)
next
next
LayoutButtonRow[n].setSpacing(C_LAYOUTSPACING)
next
next
playerscore.close()
playerscore {
playerscore {
setGeometry(0,4*floor(winheight/6),winwidth,floor(winheight/6))
setGeometry(0,4*floor(winheight/6),winwidth,floor(winheight/6))
setFont(new qFont("Verdana",fontsize2,100,0))
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
settext('Play Score: ' + nScore)
settext(C_PLAYERSCORE + nScore)
setStylesheet(C_PLAYERSCORESTYLE)
show()
show()
}
}

newgame.close()
newgame {
newgame {
setGeometry(0,5*floor(winheight/6),winwidth,floor(winheight/6))
setGeometry(0,5*floor(winheight/6),winwidth,floor(winheight/6))
setFont(new qFont("Verdana",fontsize2,100,0))
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
setstylesheet('background-color:violet')
setstylesheet(C_NEWGAMESTYLE)
settext('New Game')
settext('New Game')
myfilter4 = new qallevents(newgame)
myfilter4 = new qallevents(newgame)
Line 6,522: Line 6,547:
LayoutButtonRow[size+2].AddWidget(newgame)
LayoutButtonRow[size+2].AddWidget(newgame)
LayoutButtonMain = new QVBoxLayout() {
LayoutButtonMain = new QVBoxLayout() {
setSpacing(C_LAYOUTSPACING)
setSpacing(3)
for n = 1 to size+2
for n = 1 to size+2
AddLayout(LayoutButtonRow[n])
AddLayout(LayoutButtonRow[n])
win.show()
win.show()
next
next }
}
win.setLayout(LayoutButtonMain)
win.setLayout(LayoutButtonMain)
win.show()
win.show()
Line 6,538: Line 6,564:
x1 = myfilter3.getglobalx()
x1 = myfilter3.getglobalx()
y1 = myfilter3.getglobaly()
y1 = myfilter3.getglobaly()

func pRelease()
func pRelease()
x2 = myfilter3.getglobalx()
x2 = myfilter3.getglobalx()
y2 = myfilter3.getglobaly()
y2 = myfilter3.getglobaly()
xx1 = floor(x1/floor(winwidth/4)) - 2
difx = x2 - x1
dify = y2 - y1
xx2 = floor(x2/floor(winwidth/4)) - 2
if fabs(difx) > fabs(dify)
yy1 = floor(y1/floor(winheight/6)) - 1
if difx < 0
yy2 = floor(y2/floor(winheight/6)) - 1
pleft()
bool = (yy1 <= size) and (yy2 <= size)
else
if (yy1 = yy2) and (xx2 < xx1) and bool
pRight()
pleft()
ok
ok
else
if (yy1 = yy2) and (xx1 < xx2) and bool
if dify < 0
pright()
pUp()
ok
else
if (xx1 = xx2) and (yy2 < yy1) and bool
pDown()
pup()
ok
ok
ok
if (xx1 = xx2) and (yy1 < yy2) and bool
pdown()
ok

func keypress()
func keypress()
nKey = myfilter2.getkeycode()
nKey = myfilter2.getkeycode()
Line 6,574: Line 6,598:
for n = 1 to size
for n = 1 to size
for m = 1 to size
for m = 1 to size
button[n][m].setStylesheet('background-color: orange')
button[n][m].setStylesheet(C_EMPTYBUTTONSTYLE)
button[n][m].settext('')
button[n][m].settext('')
next
next
Line 6,592: Line 6,616:
button[rn2][rm2].settext(numbers[rand][2])
button[rn2][rm2].settext(numbers[rand][2])
nScore = 0
nScore = 0
playerscore.settext('Play Score: ')
playerscore.settext(C_PLAYERSCORE)


func pMoveInDirection cFunc
func pdown()
num = gameover()
num = gameover()
if num = size*size
if num = size*size
Line 6,602: Line 6,626:
ok
ok
if flag = 0
if flag = 0
pmovedown()
call cFunc()
sleep(0.5)
sleep(0.5)
newnum()
newnum()
ok
ok

func pdown()
pMoveInDirection(:pMoveDown)


func pup()
func pup()
pMoveInDirection(:pMoveUp)
num = gameover()
if num = size*size
flag = 1
msgBox('You lost!')
pbegin()
ok
if flag = 0
pmoveup()
sleep(0.5)
newnum()
ok


func pleft()
func pleft()
pMoveInDirection(:pMoveLeft)
num = gameover()
if num = size*size
flag = 1
msgBox('You lost!')
pbegin()
ok
if flag = 0
pmoveleft()
sleep(0.5)
newnum()
ok


func pright()
func pright()
pMoveInDirection(:pMoveRight)
num = gameover()
if num = size*size
flag = 1
msgBox('You lost!')
pbegin()
ok
if flag = 0
pmoveright()
sleep(0.5)
newnum()
ok


func pmoveleft()
func pmoveleft()
Line 6,657: Line 6,654:
movetilesleft(n,moveleft)
movetilesleft(n,moveleft)
next
next

func pmoveright()
for n = 1 to size
moveright = []
for m = size to 1 step -1
button[m][n] {temp = text()}
if temp != ''
add(moveright,temp)
ok
next
movetilesright(n,moveright)
next
return

func pmoveup()
for n = 1 to size
moveup = []
for m = 1 to size
button[n][m] {temp = text()}
if temp != ''
add(moveup,temp)
ok
next
movetilesup(n,moveup)
next
return

func pmovedown()
for n = 1 to size
movedown = []
for m = size to 1 step -1
button[n][m] {temp = text()}
if temp != ''
add(movedown,temp)
ok
next
movetilesdown(n,movedown)
next
return


func movetilesleft(nr,moveleft)
func movetilesleft(nr,moveleft)
Line 6,666: Line 6,702:
if temp != '0' and temp != ''
if temp != '0' and temp != ''
nScore = nScore + temp
nScore = nScore + temp
playerscore.settext('Play Score: ' + nScore)
playerscore.settext(C_PLAYERSCORE + nScore)
flag = 1
flag = 1
moveleft[p] = temp
moveleft[p] = temp
Line 6,678: Line 6,714:
for n = len(moveleft) + 1 to size
for n = len(moveleft) + 1 to size
if n <= size
if n <= size
button[n][nr].setStylesheet('background-color: orange')
button[n][nr].setStylesheet(C_EMPTYBUTTONSTYLE)
button[n][nr].settext('')
button[n][nr].settext('')
ok
ok
next
next
return
return

func pmoveright()
for n = 1 to size
moveright = []
for m = size to 1 step -1
button[m][n] {temp = text()}
if temp != ''
add(moveright,temp)
ok
next
movetilesright(n,moveright)
next
return


func movetilesright(nr,moveright)
func movetilesright(nr,moveright)
Line 6,706: Line 6,729:
if temp != '0' and temp != ''
if temp != '0' and temp != ''
nScore = nScore + temp
nScore = nScore + temp
playerscore.settext('Play Score: ' + nScore)
playerscore.settext(C_PLAYERSCORE + nScore)
flag = 1
flag = 1
moveright[p] = temp
moveright[p] = temp
Line 6,718: Line 6,741:
for n = 1 to size - len(moveright)
for n = 1 to size - len(moveright)
if n <= size
if n <= size
button[n][nr].setStylesheet('background-color: orange')
button[n][nr].setStylesheet(C_EMPTYBUTTONSTYLE)
button[n][nr].settext('')
button[n][nr].settext('')
ok
ok
next
next


func pmoveup()
for n = 1 to size
moveup = []
for m = 1 to size
button[n][m] {temp = text()}
if temp != ''
add(moveup,temp)
ok
next
movetilesup(n,moveup)
next
return


func movetilesup(nr,moveup)
func movetilesup(nr,moveup)
Line 6,745: Line 6,756:
if temp != '0' and temp != ''
if temp != '0' and temp != ''
nScore = nScore + temp
nScore = nScore + temp
playerscore.settext('Play Score: ' + nScore)
playerscore.settext(C_PLAYERSCORE + nScore)
flag = 1
flag = 1
moveup[p] = temp
moveup[p] = temp
Line 6,757: Line 6,768:
for n = len(moveup) + 1 to size
for n = len(moveup) + 1 to size
if n <= size
if n <= size
button[nr][n].setStylesheet('background-color: orange')
button[nr][n].setStylesheet(C_EMPTYBUTTONSTYLE)
button[nr][n].settext('')
button[nr][n].settext('')
ok
ok
next
next

func pmovedown()
for n = 1 to size
movedown = []
for m = size to 1 step -1
button[n][m] {temp = text()}
if temp != ''
add(movedown,temp)
ok
next
movetilesdown(n,movedown)
next
return


func movetilesdown(nr,movedown)
func movetilesdown(nr,movedown)
Line 6,784: Line 6,782:
if temp != '0' and temp != ''
if temp != '0' and temp != ''
nScore = nScore + temp
nScore = nScore + temp
playerscore.settext('Play Score: ' + nScore)
playerscore.settext(C_PLAYERSCORE + nScore)
flag = 1
flag = 1
movedown[p] = temp
movedown[p] = temp
Line 6,796: Line 6,794:
for n = size - len(movedown) to 1 step -1
for n = size - len(movedown) to 1 step -1
if n <= size
if n <= size
button[nr][n].setStylesheet('background-color: orange')
button[nr][n].setStylesheet(C_EMPTYBUTTONSTYLE)
app.processevents()
app.processevents()
button[nr][n].settext('')
button[nr][n].settext('')
Line 6,847: Line 6,845:
Super.setText(cValue)
Super.setText(cValue)
switch cValue
switch cValue
on '2' setStyleSheet('foreground-color:blue; background-color: yellow')
on '2' setStyleSheet(C_BUTTON2STYLE)
on '4' setStylesheet('foreground-color:white; background-color: violet')
on '4' setStylesheet(C_BUTTON4STYLE)
on '8' setStylesheet('foreground-color:white; background-color: blue')
on '8' setStylesheet(C_BUTTON8STYLE)
on '16' setStylesheet('foreground-color:black; background-color: green')
on '16' setStylesheet(C_BUTTON16STYLE)
on '32' setStylesheet('foreground-color:white; background-color: red')
on '32' setStylesheet(C_BUTTON32STYLE)
on '64' setStylesheet('foreground-color:white; background-color: gray')
on '64' setStylesheet(C_BUTTON64STYLE)
on '128' setStylesheet('foreground-color:violet; background-color: white')
on '128' setStylesheet(C_BUTTON128STYLE)
on '256' setStylesheet(C_BUTTON256STYLE)
on '512' setStylesheet(C_BUTTON512STYLE)
on '1024' setStylesheet(C_BUTTON1024STYLE)
on '2048' setStylesheet(C_BUTTON2048STYLE)
off
off
</lang>
</lang>