Jump to content

2048: Difference between revisions

Line 6,419:
<lang ring>
# Project : 2048 Game
# Date : 2018/09/07
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
 
load "stdlib.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
Line 6,448 ⟶ 6,477:
processevents()
win = new qWidget() {
setWindowTitle('2048 Game'C_GAMETITLE)
setgeometry(100,100,800,600,700)
setminimumwidth(300)
setminimumheight(300)
if not isMobile()
grabkeyboard()
setstylesheetgrabkeyboard('background-color:white')
ok
setstylesheet(C_WINDOWBACKGROUND)
move(490,100)
for n = 1 to size
Line 6,472 ⟶ 6,503:
winheight = win.height()
for n = 1 to size + 2
LayoutButtonRow[n] = new QHBoxLayout() {
setSpacing(C_LAYOUTSPACING)
setSpacing(3) }
}
next
for n = 1 to size
Line 6,479 ⟶ 6,511:
button[n][m] { temp = text() }
buttonsave[n][m] = temp
buttonsave[n][m] = temp
fontsize = 10 + (winheight/25)
fontsize2 = 10 + (winheight/50)
button[n][m] = new MyButton(win) {
setFont(new qFont("Verdana",fontsize,100,0))
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
setstylesheet('background-color:orange'C_EMPTYBUTTONSTYLE)
show()
}
next
next
Line 6,497 ⟶ 6,525:
button[n][m].settext(temp)
next
LayoutButtonRow[n].setSpacing(C_LAYOUTSPACING)
next
playerscore.close()
playerscore {
setGeometry(0,4*floor(winheight/6),winwidth,floor(winheight/6))
setFont(new qFont("Verdana",fontsize2,100,0))
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
settext('Play Score: 'C_PLAYERSCORE + nScore)
setStylesheet(C_PLAYERSCORESTYLE)
show()
}
}
 
newgame.close()
newgame {
setGeometry(0,5*floor(winheight/6),winwidth,floor(winheight/6))
setFont(new qFont("Verdana",fontsize2,100,0))
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
setstylesheet('background-color:violet'C_NEWGAMESTYLE)
settext('New Game')
myfilter4 = new qallevents(newgame)
Line 6,522 ⟶ 6,547:
LayoutButtonRow[size+2].AddWidget(newgame)
LayoutButtonMain = new QVBoxLayout() {
setSpacing(C_LAYOUTSPACING)
setSpacing(3)
for n = 1 to size+2
AddLayout(LayoutButtonRow[n])
win.show()
win.show()
next
next }
}
win.setLayout(LayoutButtonMain)
win.show()
Line 6,538 ⟶ 6,564:
x1 = myfilter3.getglobalx()
y1 = myfilter3.getglobaly()
 
func pRelease()
x2 = myfilter3.getglobalx()
y2 = myfilter3.getglobaly()
xx1difx = floor(x1/floor(winwidth/4))x2 - 2x1
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()
nKey = myfilter2.getkeycode()
Line 6,574 ⟶ 6,598:
for n = 1 to size
for m = 1 to size
button[n][m].setStylesheet('background-color: orange'C_EMPTYBUTTONSTYLE)
button[n][m].settext('')
next
Line 6,592 ⟶ 6,616:
button[rn2][rm2].settext(numbers[rand][2])
nScore = 0
playerscore.settext('Play Score: 'C_PLAYERSCORE)
 
func pMoveInDirection cFunc
func pdown()
num = gameover()
if num = size*size
Line 6,602 ⟶ 6,626:
ok
if flag = 0
pmovedowncall cFunc()
sleep(0.5)
newnum()
ok
 
func pdown()
pMoveInDirection(:pMoveDown)
 
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()
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()
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()
Line 6,657 ⟶ 6,654:
movetilesleft(n,moveleft)
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)
Line 6,666 ⟶ 6,702:
if temp != '0' and temp != ''
nScore = nScore + temp
playerscore.settext('Play Score: 'C_PLAYERSCORE + nScore)
flag = 1
moveleft[p] = temp
Line 6,678 ⟶ 6,714:
for n = len(moveleft) + 1 to size
if n <= size
button[n][nr].setStylesheet('background-color: orange'C_EMPTYBUTTONSTYLE)
button[n][nr].settext('')
ok
next
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)
Line 6,706 ⟶ 6,729:
if temp != '0' and temp != ''
nScore = nScore + temp
playerscore.settext('Play Score: 'C_PLAYERSCORE + nScore)
flag = 1
moveright[p] = temp
Line 6,718 ⟶ 6,741:
for n = 1 to size - len(moveright)
if n <= size
button[n][nr].setStylesheet('background-color: orange'C_EMPTYBUTTONSTYLE)
button[n][nr].settext('')
ok
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)
Line 6,745 ⟶ 6,756:
if temp != '0' and temp != ''
nScore = nScore + temp
playerscore.settext('Play Score: 'C_PLAYERSCORE + nScore)
flag = 1
moveup[p] = temp
Line 6,757 ⟶ 6,768:
for n = len(moveup) + 1 to size
if n <= size
button[nr][n].setStylesheet('background-color: orange'C_EMPTYBUTTONSTYLE)
button[nr][n].settext('')
ok
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)
Line 6,784 ⟶ 6,782:
if temp != '0' and temp != ''
nScore = nScore + temp
playerscore.settext('Play Score: 'C_PLAYERSCORE + nScore)
flag = 1
movedown[p] = temp
Line 6,796 ⟶ 6,794:
for n = size - len(movedown) to 1 step -1
if n <= size
button[nr][n].setStylesheet('background-color: orange'C_EMPTYBUTTONSTYLE)
app.processevents()
button[nr][n].settext('')
Line 6,847 ⟶ 6,845:
Super.setText(cValue)
switch cValue
on '2' setStyleSheet('foreground-color:blue; background-color: yellow'C_BUTTON2STYLE)
on '4' setStylesheet('foreground-color:white; background-color: violet'C_BUTTON4STYLE)
on '8' setStylesheet('foreground-color:white; background-color: blue'C_BUTTON8STYLE)
on '16' setStylesheet('foreground-color:black; background-color: green'C_BUTTON16STYLE)
on '32' setStylesheet('foreground-color:white; background-color: red'C_BUTTON32STYLE)
on '64' setStylesheet('foreground-color:white; background-color: gray'C_BUTTON64STYLE)
on '128' setStylesheet('foreground-color:violet; background-color: white'C_BUTTON128STYLE)
on '256' setStylesheet(C_BUTTON256STYLE)
on '512' setStylesheet(C_BUTTON512STYLE)
on '1024' setStylesheet(C_BUTTON1024STYLE)
on '2048' setStylesheet(C_BUTTON2048STYLE)
off
</lang>
2,468

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.