Author Topic: [Axe game] 2048  (Read 21027 times)

0 Members and 1 Guest are viewing this topic.

Offline Toctave

  • LV2 Member (Next: 40)
  • **
  • Posts: 22
  • Rating: +2/-0
    • View Profile
[Axe game] 2048
« on: April 14, 2014, 09:39:41 am »
Hi guys!
I recently created a game in axe, you probably know it, it's 2048.
So here's the code with as much comments as I thought were needed, don't hesitate about asking me about a part of it if it's not clear enough.


Variables :
Pic** : Sprites and Bitmaps
I, J, K : Counters for the For( loops in movements subroutines
L1 : Pointer to the game table. I store the cells as their power of 2 (e. g. 2 is 1, 64 is 6, and 2048 is 11)
appvA : 20 bytes appvar to save the current game table, score, and high score
Q : Pointer to appvA
theta : If it's non zero, at least one cell has moved (helps to know when to create new cells)
N : limit variable, explained below
V : Value of the evaluated cell
T : Value of the targetted cell

Code: [Select]
.A2048
.Sprite for the Tiles
[FF818181818181FF]->Pic0
[FF818199998181FF]->Pic1
[FF8189B99D9181FF]->Pic2
[FF81FF8181FF81FF]->Pic3
[FF81BDA5A5BD81FF]->Pic4
[FFA5FFA5A5FFA5FF]->Pic5
[FF9999FFFF9999FF]->Pic6
[FFC3A59999A5C3FF]->Pic7
[FFC3BDBDBDBDC3FF]->Pic8
[FFFFC3DBDBC3FFFF]->Pic9
[FFFFFFE7E7FFFFFF]->Pic10
[FFABD5ABD5ABD5FF]->Pic11
[FFFFFFFFFFFFFFFF]->Pic12
.Logo Bitmap
Data(40,16)->Pic0A
[FFFFF0FF00818190818081819F8180F999999980F999999980819981818081998181809F99F999809F99F9998081818981808181898180FFFF8FFF807FFF87FF80000000000000000000000000000000]
.Help screen Bitmaps
Data(16,56)->Pic0B
[FF00810081609910992081408170FF000000FF0081008940B9509D7091108110FF000000FF008100FF7081508170FF508170FF000000FF008100BD23A564A527BD258177FF000000FF00A500FF66A511A522FF14A567FF000000FF0099009934FF45FF7799519971FF00000000000000]
Data(24,56)->Pic0C
[0000FF0000C304CEA50C2A99044E99048AA50EEEC30000FF0000000000FF0000C30CE6BD0288BD04CEBD082ABD0ECEC30000FF0000000000FF0000FF0E4CC308C2DB0C44DB0248C30CEEFF0000FF0000000000FF0000FF44C8FFCA2AE74A4EE74A82FFE4E2FF0000FF0000000000FF0000ABC48ED52AAAAB4AEED58A2AABE42ED50000FF0000000000FF0000FF84E6FFAAA8FFEAEEFF2A2AFF24CEFF0000FF000000000000000000]
DiagnosticOff
ExprOn
Full

Fill(L1,16,0)
0->I->J->K

.Default value for the NEW subroutine, see below
1->{L1+16}

.Create or load an appvar for the game table
UnArchive "appvA"
!If GetCalc("appvA"
GetCalc("appvA",20)->Q
R()
Else
GetCalc("appvA")->Q
End

.Recover last session
LOAD()

.Main Loop
Repeat getKey(15)
If getKey
If getKey(1)
DOWN()
End
If getKey(2)
LEFT()
End
If getKey(3)
RIGHT()
End
If getKey(4)
UP()
End
If getKey(55)
R()
End
If getKey(54)
H()
End
End
D()
End
If S>{Q+18}^^r
S->{Q+18}^^r
End
SAVE()
Return


Lbl DOWN
.By default, no move has been done
0->theta
.Check all columns (J)
For(J,0,3)
3->N
For(K,1,3)
.K is used to make a For( loop with I from 3 to 1
.X is the address of the target cell, it's by default the cell below the evaluated one
3-K->I+1->X
.If the evaluated cell contains a number
If {4*I+J+L1}->V!=0
.Main loop to find a target position
.Find a cell with the same value as the evaluated cell
.or an obstacle
.or X reaches the limit
Repeat {4*X+J+L1}->T=V or (T!=V and (T!=0)) or (X=N)
X++
End
{4*X+J+L1}->T
.If an obstacle was found, the target cell is one row upwards
If T!=V and (T!=0)
X--
End
.If any movement is made (target cell is different from evaluated cell)
If X!=I
.If target cell is empty
If {4*X+J+L1}=0
V->{4*X+J+L1}
Else
.If we're there, the target cell value needs to be increased
V+1->{4*X+J+L1}
.And the limit value goes upwards (see in the message for more information)
X-1->N
e^(V+1)+S->S
End
.Empty the evaluated cell and tell the program a move has been made
0->{4*I+J+L1}
theta++
End
End
End
End
.If at least one move has been made, create a new cell
If theta
NEW()
End
Return

Lbl LEFT
0->theta
For(J,0,3)
0->N
For(I,1,3)
If {4*J+I+L1}->V!=0
I-1->X
Repeat {4*J+X+L1}->T=V or (T!=V and (T!=0)) or (X=N)
X--
End
{4*J+X+L1}->T
If T!=V and (T!=0)
X++
End
If X!=I
If {4*J+X+L1}=0
V->{4*J+X+L1}
Else
V+1->{4*J+X+L1}
X+1->N
e^(V+1)+S->S
End
0->{4*J+I+L1}
theta++
End
End
End
End
If theta
NEW()
End
Return

Lbl RIGHT
0->theta
For(J,0,3)
3->N
For(K,1,3)
3-K->I+1->X
If {4*J+I+L1}->V!=0
Repeat {4*J+X+L1}->T=V or (T!=V and (T!=0)) or (X=N)
X++
End
If (T!=V and (T!=0))
X--
End
If X!=I
If {4*J+X+L1}=0
V->{4*J+X+L1}
Else
X-1->N
V+1->{4*J+X+L1}
e^(V+1)+S->S
End
0->{4*J+I+L1}
theta++
End
End
End
End
If theta
NEW()
End
Return

Lbl UP
0->theta
For(J,0,3)
0->N
For(I,1,3)
I-1->X
If {4*I+J+L1}->V!=0
Repeat {4*X+J+L1}->T=V or (T!=V and (T!=0)) or (X=N)
X--
End
{4*X+J+L1}->T
If T!=V and (T!=0)
X++
End
If X!=I
If {4*X+J+L1}=0
V->{4*X+J+L1}
Else
X+1->N
V+1->{4*X+J+L1}
e^(V+1)+S->S
End
0->{4*I+J+L1}
theta++
End
End
End
End
If theta
NEW()
End
Return

Lbl SAVE
Copy(L1,Q,16)
S->{Q+16}^^r
Return

Lbl LOAD
Copy(Q,L1,16)
{Q+16}^^r->S
Return

Lbl NEW
.L is set to 16 so the new cell position changes everytime
16->L
Repeat {L1+L}=0
rand^16->L
End
rand^6/5+1->{L1+L}
Return

Lbl R
0->S
Fill(L1,16,0
NEW()
NEW()
Return

Lbl H
.Clear the screen and draw the help bitmaps
ClrDraw
Tangent(20,5,Pic0B)
Tangent(52,5,Pic0C)
DispGraph^^r
Text(58,58,"by Toctave"
Repeat getKey
End
ClrDraw^^r^^r
Return


.Subroutine to easily create rectangles for the scores and game table itself, r1 to r4 is the same as for the Rect( command, r5 is used to tell how the corners will look
Lbl RECT
HLine({r2},{r1}-{r5},{r1}+{r3}+{r5}
HLine({r2}+{r4},{r1}-{r5},{r1}+{r3}+{r5}
VLine({r1},{r2}-{r5},{r2}+{r4}+{r5}
VLine({r1}+{r3},{r2}-{r5},{r2}+{r4}+{r5}
Return

.No need to explain here I think
Lbl D
Tangent(32,3,Pic0A
For(I,0,15)
L1+I->P
I^4*9+31->F
I/4*9+21->G
If {P}=1
Pt-On(F,G,Pic1)
ElseIf {P}=2
Pt-On(F,G,Pic2)
ElseIf {P}=3
Pt-On(F,G,Pic3)
ElseIf {P}=4
Pt-On(F,G,Pic4)
ElseIf {P}=5
Pt-On(F,G,Pic5)
ElseIf {P}=6
Pt-On(F,G,Pic6)
ElseIf {P}=7
Pt-On(F,G,Pic7)
ElseIf {P}=8
Pt-On(F,G,Pic8)
ElseIf {P}=9
Pt-On(F,G,Pic9)
ElseIf {P}=10
Pt-On(F,G,Pic10)
ElseIf {P}=11
Pt-On(F,G,Pic11)
ElseIf {P}=12
Pt-On(F,G,Pic12)
Else
Pt-On(F,G,Pic0)^^r
End
End
RECT(29,19,38,38,1
RECT(70,21,24,14,~1
RECT(70,38,24,14,~1
DispGraphClrDraw^^r

Text(72,22,"Score:"
Text(73,28,S>Dec
Text(75,39,"Best:"
Text(73,45,{Q+18}^^r>Dec


About the N variable :
The problem was that when going Left for example, with 3 as a limit, the program was doing this :
[8,4,4,0]=>[0,0,0,16]
with steps : [8,4,4,0]=>[8,4,0,4]=>[8,0,0,8]=>[0,0,0,16]
Now, it does the following :
[8,4,4,0]=>[0,0,8,8]
[8,4,4,0]=>[8,4,0,4]=>[8,0,0,8]=>Set the limit to the third cell (targetted cell was fourth, limit is target-1) => [0,0,8,8] (the 8 stopped at the third cell because the limit was reached


And here's a gif, as you can see the Text is laggy and causes the grayscale to look bad, and I have no idea how to fix this, if anyone could help that could be great.

Here's a gif without the text to show how fast it should be :



Controls :
Arrows to swipe in a direction
2nd to show the help screen
mode to reset the game
Clear to quit
« Last Edit: April 14, 2014, 09:42:42 am by Toctave »

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: [Axe game] 2048
« Reply #1 on: April 14, 2014, 09:43:38 am »
I see you use different pointers for your sprites. Instead you should use only one and use pointer offsets for that. Ie with only one command you do :
pt-on(x,y,id*8+PicX)

Offline TheCoder1998

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 434
  • Rating: +20/-2
  • my art is written in code, not in graphite
    • View Profile
    • My website :D
Re: [Axe game] 2048
« Reply #2 on: April 14, 2014, 09:45:59 am »
oh i had this issue too in my game,

i haven't read the whole code, but it has to do with updating the main, or back buffer iirc
just play around with clrdraw and you should be able to fix it


also, you  should introduce yourself to the forum here: http://www.omnimaga.org/introduce-yourself!/


I see you use different pointers for your sprites. Instead you should use only one and use pointer offsets for that. Ie with only one command you do :
pt-on(x,y,id*8+PicX)
or you could do that, i think that would fix it too

Edit Sorunome: Merged double-posts
« Last Edit: April 14, 2014, 10:56:37 am by TheCoder1998 »
my ticalc acc:

http://www.ticalc.org/archives/files/authors/113/11365.html

Spoiler For The Best Song Ever:


follow me on tumblr :)
www.rickdepizza.tumblr.com

check out my anilist :D
http://anilist.co/animelist/29701/Rickdepizza

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: [Axe game] 2048
« Reply #3 on: April 14, 2014, 09:47:54 am »
Double post = D:

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: [Axe game] 2048
« Reply #4 on: April 14, 2014, 09:55:49 am »
That game is looking nice so far! So many good-quality 2048 clones out there :)

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Toctave

  • LV2 Member (Next: 40)
  • **
  • Posts: 22
  • Rating: +2/-0
    • View Profile
Re: [Axe game] 2048
« Reply #5 on: April 14, 2014, 10:13:44 am »
Thanks Sorumone, Streetwalrus this seems like a good idea, however how should I initialize this PicX? By putting "-> {PicX + 1,2,3,4...}" instead of "-> Pic1, Pic2, Pic3, Pic4..."?
About the text issue, I think I know why it does this. It's because there's no way to write Text on the buffer only, and in a fast enough way. I think drawing Text is just horribly slow :( . What do you mean exactly by "play around with ClrDraw"?

Offline TheCoder1998

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 434
  • Rating: +20/-2
  • my art is written in code, not in graphite
    • View Profile
    • My website :D
Re: [Axe game] 2048
« Reply #6 on: April 14, 2014, 10:15:55 am »
i'm not quite sure how to say this, but i think you should look at when to update the main/back buffers
like not updating every freaking second, but only if there is something to actually update
my ticalc acc:

http://www.ticalc.org/archives/files/authors/113/11365.html

Spoiler For The Best Song Ever:


follow me on tumblr :)
www.rickdepizza.tumblr.com

check out my anilist :D
http://anilist.co/animelist/29701/Rickdepizza

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: [Axe game] 2048
« Reply #7 on: April 14, 2014, 10:16:24 am »
No, just remove all the pointers after all the sprites but the first. Then use Pic0 instead of PicX. ;)

Offline Toctave

  • LV2 Member (Next: 40)
  • **
  • Posts: 22
  • Rating: +2/-0
    • View Profile
Re: [Axe game] 2048
« Reply #8 on: April 14, 2014, 10:37:15 am »
Ok that's done, now I'm trying to solve that text problem.
On an unrelated note, how do I avoid Garbage Collecting everytime I compile my program?

Offline TheCoder1998

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 434
  • Rating: +20/-2
  • my art is written in code, not in graphite
    • View Profile
    • My website :D
Re: [Axe game] 2048
« Reply #9 on: April 14, 2014, 10:37:55 am »
have enough archive on your calc, which one are you using?
my ticalc acc:

http://www.ticalc.org/archives/files/authors/113/11365.html

Spoiler For The Best Song Ever:


follow me on tumblr :)
www.rickdepizza.tumblr.com

check out my anilist :D
http://anilist.co/animelist/29701/Rickdepizza

Offline Toctave

  • LV2 Member (Next: 40)
  • **
  • Posts: 22
  • Rating: +2/-0
    • View Profile
Re: [Axe game] 2048
« Reply #10 on: April 14, 2014, 10:39:48 am »
I have a TI-83+ (fr), and it says I have 86651 bytes of free archive...

Offline TheCoder1998

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 434
  • Rating: +20/-2
  • my art is written in code, not in graphite
    • View Profile
    • My website :D
Re: [Axe game] 2048
« Reply #11 on: April 14, 2014, 10:40:55 am »
then you shouldn't get so many garbage collect messages
it's not a big deal right?
my ticalc acc:

http://www.ticalc.org/archives/files/authors/113/11365.html

Spoiler For The Best Song Ever:


follow me on tumblr :)
www.rickdepizza.tumblr.com

check out my anilist :D
http://anilist.co/animelist/29701/Rickdepizza

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: [Axe game] 2048
« Reply #12 on: April 14, 2014, 10:41:30 am »
Try manually GCing.

Offline TheCoder1998

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 434
  • Rating: +20/-2
  • my art is written in code, not in graphite
    • View Profile
    • My website :D
Re: [Axe game] 2048
« Reply #13 on: April 14, 2014, 10:41:53 am »
oh yeah, that could help too
my ticalc acc:

http://www.ticalc.org/archives/files/authors/113/11365.html

Spoiler For The Best Song Ever:


follow me on tumblr :)
www.rickdepizza.tumblr.com

check out my anilist :D
http://anilist.co/animelist/29701/Rickdepizza

Offline Toctave

  • LV2 Member (Next: 40)
  • **
  • Posts: 22
  • Rating: +2/-0
    • View Profile
Re: [Axe game] 2048
« Reply #14 on: April 14, 2014, 10:45:23 am »
Try manually GCing.
Yeah sure I should try that WTF are they talking about...