2048: Difference between revisions

4,365 bytes added ,  5 years ago
Line 6,425:
size = 4
limit = 6
num = 0
numbers = [2,4,8,16,32,64]
flag = 0
x1 = 0
x2 = 0
y1 = 0
y2 = 0
button = newlist(size,size)
moveleft = []
Line 6,431 ⟶ 6,436:
moveup = []
movedown = []
myfilter2 = null
myfilter3 = null
winheight = 0
winwidth = 0
 
app = new qApp {
Line 6,436 ⟶ 6,445:
win = new qWidget() {
setWindowTitle('2048 Game')
setminimumwidth(300)
setminimumheight(300)
move(490,100)
resize(450,500)
for n = 1 to size
for m = 1 to size
colbutton[n][m] = 100new + n*40MyButton(win)
row = 100 + m*40next
next
arrow = new qLineedit(win)
mup = new qPushButton(win)
mdown = new qPushButton(win)
mleft = new qPushButton(win)
mright = new qPushButton(win)
newgame = new qPushButton(win)
myfilter = new qallevents(win)
myfilter.setResizeEvent("pResize()")
installeventfilter(myfilter)
myfilter3 = new qAllEvents(win) {
setMouseButtonPressEvent("pPress()")
setMouseButtonReleaseEvent("pRelease()")}
installeventfilter(myfilter3)
pResize()
show()
}
exec()
}
 
func pPress()
x1 = myfilter3.getglobalx()
y1 = myfilter3.getglobaly()
func pRelease()
x2 = myfilter3.getglobalx()
y2 = myfilter3.getglobaly()
xx1 = floor(x1/floor(winwidth/4)) - 2
xx2 = floor(x2/floor(winwidth/4)) - 2
yy1 = floor(y1/floor(winheight/8)) - 1
yy2 = floor(y2/floor(winheight/8)) - 1
bool = (yy1 <= size) and (yy2 <= size)
if (yy1 = yy2) and (xx2 < xx1) and bool
pleft()
ok
if (yy1 = yy2) and (xx1 < xx2) and bool
pright()
ok
if (xx1 = xx2) and (yy2 < yy1) and bool
pup()
ok
if (xx1 = xx2) and (yy1 < yy2) and bool
pdown()
ok
 
func pResize()
winwidth = win.width()
winheight = win.height()
arrow.close()
arrow = new qLineedit(win) {
setGeometry(0,7*floor(winheight/8),winwidth,floor(winheight/8))
arrow.setfocus(true)
myfilter2 = new qAllEvents(win) {
setkeypressevent("keypress()")
}
installeventfilter(myfilter2)
}
for n = 1 to size
for m = 1 to size
button[n][m].close()
col = (n-1)*floor(winwidth/4)
row = (m-1)*floor(winheight/8)
fontsize = 10 + (winheight/16)
fontsize2 = 10 + (winheight/50)
button[n][m] = new MyButton(win) {
setGeometry(col,row,40winwidth/4,40winheight/8)
setFont(new qFont("Verdana",fontsize,100,0))
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
setstylesheet('background-color:orange')
show()
}
next
next
newgame.close()
newgame = new qPushButton(win) {
setGeometry(1800,3807*floor(winheight/8),80winwidth,40floor(winheight/8))
setFont(new qFont("Verdana",fontsize2,100,0))
setstylesheet('background-color:violet')
settext('New Game')
setClickEvent('pbegin()')
show()
}
mup.close()
mup = new qPushButton(win) {
setGeometry(1400,3005*floor(winheight/8),40winwidth/4,40floor(winheight/8))
setFont(new qFont("Verdana",fontsize2,100,0))
settext('up')
setClickEvent('pup()')
show()
}
mdown.close()
mdown = new qPushButton(win) {
setGeometry(180floor(winwidth/4),3005*floor(winheight/8),40winwidth/4,40floor(winheight/8))
setFont(new qFont("Verdana",fontsize2,100,0))
settext('down')
setClickEvent('pdown()')
show()
}
mleft.close()
mleft = new qPushButton(win) {
setGeometry(2202*floor(winwidth/4),3005*floor(winheight/8),40winwidth/4,40floor(winheight/8))
setFont(new qFont("Verdana",fontsize2,100,0))
settext('left')
setClickEvent('pleft()')
show()
}
mright.close()
mright = new qPushButton(win) {
setGeometry(2603*floor(winwidth/4),3005*floor(winheight/8),40winwidth/4,40floor(winheight/8))
setFont(new qFont("Verdana",fontsize2,100,0))
settext('right')
setClickEvent('pright()')
show()
}
win.show()
pbegin()
 
show()
func keypress()
}
nKey = myfilter2.getkeycode()
exec()
switch nKey
}
on 16777234 pleft()
on 16777236 pright()
on 16777235 pup()
on 16777237 pdown()
off
 
func pbegin()
Line 6,490 ⟶ 6,588:
rn = random(size - 1) + 1
rm = random(size - 1) + 1
ran = randombutton[rn][rm].settext(limit - 1'2') + 1
button[rn][rm].settext(string(numbers[ran]))
next
arrow.setfocus(true)
 
func pdown()
if gameover()num = 1gameover()
if num return= size*size
ok flag = 1
pmovedown msgBox('You lost!')
newnum pbegin()
ok
if flag = 0
pmovedown()
newnum()
ok
 
func pup()
if gameover()num = 1gameover()
if num return= size*size
ok flag = 1
pmoveup msgBox('You lost!')
newnum pbegin()
ok
if flag = 0
pmoveup()
newnum()
ok
 
func pleft()
if gameover()num = 1gameover()
if num return= size*size
ok flag = 1
pmoveleft msgBox('You lost!')
newnum pbegin()
ok
if flag = 0
pmoveleft()
newnum()
ok
 
func pright()
if gameover()num = 1gameover()
if num return= size*size
ok flag = 1
pmoveright msgBox('You lost!')
newnum pbegin()
ok
if flag = 0
pmoveright()
newnum()
ok
 
func pmoveleft()
Line 6,543 ⟶ 6,661:
moveleft[p] = temp
del(moveleft,p+1)
exit
ok
ok
Line 6,582 ⟶ 6,699:
moveright[p] = temp
del(moveright,p-1)
exitif moveright[p-1] = temp
p = p - 1
ok
ok
ok
Line 6,620 ⟶ 6,739:
moveup[p] = temp
del(moveup,p+1)
exit
ok
ok
Line 6,658 ⟶ 6,776:
movedown[p] = temp
del(movedown,p+1)
exit
ok
ok
Line 6,687 ⟶ 6,804:
func gameover()
num = 0
flag = 0
for n = 1 to size
for m = 1 to size
Line 6,694 ⟶ 6,812:
next
next
ifreturn num = size*size
msgBox('You lost!')
return 1
ok
return
 
func msgBox(text) {
m = new qMessageBox(win) {
setWindowTitle('212048 Game')
setText(text)
show()
Line 6,718 ⟶ 6,832:
see "]" + nl
 
class MyButton from qPushButtonqLabel
func setText (cValue )
Super.setText(cValue)
switch cValue
2,468

edits