[TI BASIC] Optimizing
Moderator: MaxCoderz Staff
Can someone help me optimize my snake game more please?
Code: Select all
Goto S
Lbl N
Repeat A-N+B-O and not([A](N,O
randInt(1,15->N
randInt(1,18->O
End
Pt-On(O,N,3
Pt-Off(O,N
1+Z->Z
Text(23,77,Z
End
Repeat K=45 or Z=253 or [A](A,B
getKey->K
If K=22
Pause
If 24=K(C!=6
4->C
If 25=K(C!=2
8->C
If 26=K(C!=4
6->C
If 34=K(C!=8
2->C
C->[A](A,B
A-iPart(.4(5-C->A
B+(C=6)-(C=4->B
A-15((A=16)-not(A->A
B-18((B=19)-not(B->B
Pt-Off(E,D,2
Pt-On(B,A,2
If N=A(O=B
Goto N
[A](D,E->F
0->[A](D,E
D-iPart(.4(5-F->D
E+(F=6)-(F=4->E
D-15((D=16)-not(D->D
E-18((E=19)-not(E->E
End
Text(-1,27,15,"GAMEOVER
If Z>LVH(1
Then
Z->LVH(1
Text(-1,35,12,"HIGHSCORE
End
DelVar [A]
Pause
ClrDraw
RecallGDB 1
Disp
Stop
Lbl S
Disp "LOADING...
StoreGDB 1
1/4->Xmin
95/4->Xmax
1/4->Ymin
63/4->Ymax
FnOff
AxesOff
{15,18->dim([A]
Fill(0,[A]
1->A:1->B:6->C
1->D:1->E:0->Z
1->dim(LVH
ClrDraw
Horizontal Ymin
Horizontal Ymax
Vertical Xmin
Vertical 75/4
Vertical Xmax
Text(3,77,"HIGH
Text(9,77,LVH(1
Text(17,77,"YOUR
Repeat 1
Goto N
Goto S
Lbl N
Repeat A-N+B-O and not([A](N,O
randInt(1,15->N
randInt(1,18->O
End
Pt-On(O,N,3
Pt-Off(O,N
1+Z->Z
Text(23,77,Z
End
Repeat K=45 or Z=253 or [A](A,B
getKey->K
If K=22
Pause
If 24=K(C!=6
4->C
If 25=K(C!=2
8->C
If 26=K(C!=4
6->C
If 34=K(C!=8
2->C
C->[A](A,B
A-iPart(.4(5-C->A
B+(C=6)-(C=4->B
A-15((A=16)-not(A->A
B-18((B=19)-not(B->B
Pt-Off(E,D,2
Pt-On(B,A,2
If N=A(O=B
Goto N
[A](D,E->F
0->[A](D,E
D-iPart(.4(5-F->D
E+(F=6)-(F=4->E
D-15((D=16)-not(D->D
E-18((E=19)-not(E->E
End
Text(-1,27,15,"GAMEOVER
If Z>LVH(1
Then
Z->LVH(1
Text(-1,35,12,"HIGHSCORE
End
DelVar [A]
Pause
ClrDraw
RecallGDB 1
Disp
Stop
Lbl S
Disp "LOADING...
StoreGDB 1
1/4->Xmin
95/4->Xmax
1/4->Ymin
63/4->Ymax
FnOff
AxesOff
{15,18->dim([A]
Fill(0,[A]
1->A:1->B:6->C
1->D:1->E:0->Z
1->dim(LVH
ClrDraw
Horizontal Ymin
Horizontal Ymax
Vertical Xmin
Vertical 75/4
Vertical Xmax
Text(3,77,"HIGH
Text(9,77,LVH(1
Text(17,77,"YOUR
Repeat 1
Goto N
First...Always calculate everything out...don't do this...
1/4->Xmin
95/4->Xmax
1/4->Ymin
63/4->Ymax
...
Vertical 75/4
Actually divide those numbers and use that value. When you make the calculator divide in a program you are adding exra steps...and in basic you want to eliminate as many steps as possible.
Second: No gotos/laels, especially not in loops. I'll try to explain: Everytime the program reachesa goto in a loop, I think it stores that location in the memory. So these stored locations keep adding up in the memory...then you get an error memory. Also, labels are bad because the goto statement will search for the label from the top of the code down, so that will slow things down.
-to eliminate the labels/gotos I would recomend creating two programs (assuming you want to keep the same format) Take theone label and make a seperate program for it, and just call it where you would call a label.
I don't really have the time to delve into the program more deeply, but those, especially the second one, should help.
Lbl N
Repeat A-N+B-O and not([A](N,O
randInt(1,15->N
randInt(1,18->O
End
Pt-On(O,N,3
Pt-Off(O,N
1+Z->Z
Text(23,77,Z
End
Repeat K=45 or Z=253 or [A](A,B
getKey->K
If K=22
Pause
If 24=K(C!=6
4->C
If 25=K(C!=2
8->C
If 26=K(C!=4
6->C
If 34=K(C!=8
2->C
C->[A](A,B
A-iPart(.4(5-C->A
B+(C=6)-(C=4->B
A-15((A=16)-not(A->A
B-18((B=19)-not(B->B
Pt-Off(E,D,2
Pt-On(B,A,2
If N=A(O=B
Goto N
[A](D,E->F
0->[A](D,E
D-iPart(.4(5-F->D
E+(F=6)-(F=4->E
D-15((D=16)-not(D->D
E-18((E=19)-not(E->E
End
Text(-1,27,15,"GAMEOVER
If Z>LVH(1
Then
Z->LVH(1
Text(-1,35,12,"HIGHSCORE
End
DelVar [A]
Pause
ClrDraw
RecallGDB 1
Disp
Stop
Lbl S
Disp "LOADING...
StoreGDB 1
1/4->Xmin
95/4->Xmax
1/4->Ymin
63/4->Ymax
FnOff
AxesOff
{15,18->dim([A]
Fill(0,[A]
1->A:1->B:6->C
1->D:1->E:0->Z
1->dim(LVH
ClrDraw
Horizontal Ymin
Horizontal Ymax
Vertical Xmin
Vertical 75/4
Vertical Xmax
Text(3,77,"HIGH
Text(9,77,LVH(1
Text(17,77,"YOUR
Repeat 1
Goto N
First...Always calculate everything out...don't do this...
1/4->Xmin
95/4->Xmax
1/4->Ymin
63/4->Ymax
...
Vertical 75/4
Actually divide those numbers and use that value. When you make the calculator divide in a program you are adding exra steps...and in basic you want to eliminate as many steps as possible.
Second: No gotos/laels, especially not in loops. I'll try to explain: Everytime the program reachesa goto in a loop, I think it stores that location in the memory. So these stored locations keep adding up in the memory...then you get an error memory. Also, labels are bad because the goto statement will search for the label from the top of the code down, so that will slow things down.
-to eliminate the labels/gotos I would recomend creating two programs (assuming you want to keep the same format) Take theone label and make a seperate program for it, and just call it where you would call a label.
I don't really have the time to delve into the program more deeply, but those, especially the second one, should help.
No, I made it so it doesn't do that.MooK wrote: Second: No gotos/laels, especially not in loops. I'll try to explain: Everytime the program reachesa goto in a loop, I think it stores that location in the memory. So these stored locations keep adding up in the memory...then you get an error memory. Also, labels are bad because the goto statement will search for the label from the top of the code down, so that will slow things down.
-to eliminate the labels/gotos I would recomend creating two programs (assuming you want to keep the same format) Take theone label and make a seperate program for it, and just call it where you would call a label.
Read the last couple posts on this thread page: http://joepnet.com/hosted/maxcoderz/php ... .php?t=422
I can see that where 1/4 and .25, .25 would be better.Mook wrote:Actually divide those numbers and use that value. When you make the calculator divide in a program you are adding exra steps...and in basic you want to eliminate as many steps as possible.
Also, I did stuff like 95/4 instead of 23.75 just to save a couple of extra bytes.
It is currently 596 bytes...I am trying to get it down to below 550. Is there a better method of creating a snake game that is just as fast as this one and is also mcuh smaller?
I really hate using a matrix. It takes up like...2450ish space.
i dont see why they need to take up so much space. But if I use strings, it is really slow. Lists are also the same as matrices, takes lots of space.
Thanks for the help,
Virtuoso
-
- Calc Wizard
- Posts: 526
- Joined: Tue 08 Mar, 2005 1:37 am
- Location: who wants to know?
- Contact:
Text(3,77,"HIGH
Text(9,77,LVH(1
Text(17,77,"YOUR
Code: Select all
Text(4,77,"HIGH",LVH(1),"YOUR
- dysfunction
- Calc Master
- Posts: 1454
- Joined: Wed 22 Dec, 2004 3:07 am
- Location: Through the Aura
-
- Calc Wizard
- Posts: 526
- Joined: Tue 08 Mar, 2005 1:37 am
- Location: who wants to know?
- Contact:
Thanks a lot. Every small optimization helps my program even more. I also did not know that the "text(-1" was not compatible with the regular 83. So getting rid of it will also save 2 more bytes.
I do not get that part though.Darkerline wrote:Also, your Xmin, Ymin, Xmax, Ymax seem to be in fourths. If you multiplied all graph-screen coordinates by 4, you could probably save size.
-
- Calc Wizard
- Posts: 526
- Joined: Tue 08 Mar, 2005 1:37 am
- Location: who wants to know?
- Contact:
Ok. Suppose you had code like this:
you can change it to
Code: Select all
1/4->Xmin
95/4->Xmax
1/4->Ymin
63/4->Ymax
Vertical 75/4
PtOn(6,10
Code: Select all
1->Xmin
95->Xmax
1 -> Ymin
63-> Ymax
Vertical 75
PtOn(24,40
Oh, the thing is that i store the snake's positions in the matrix. So I set the window so like....
um...
say i want to put a 4 in the matrix position 5,5
I just have to plot the point 5x5 and it will be where I want. I dont have to change anything about the "5 and 5" to plot it where I want the points.
That is why the window is set in a weird way, so I dont have to do anything to the matrix position to plot the points.
I hope I explained it at least a little well. My own explaination kinda confuses me.
Thanks,
Virtuoso
um...
say i want to put a 4 in the matrix position 5,5
I just have to plot the point 5x5 and it will be where I want. I dont have to change anything about the "5 and 5" to plot it where I want the points.
That is why the window is set in a weird way, so I dont have to do anything to the matrix position to plot the points.
I hope I explained it at least a little well. My own explaination kinda confuses me.
Thanks,
Virtuoso
-
- Calc Wizard
- Posts: 526
- Joined: Tue 08 Mar, 2005 1:37 am
- Location: who wants to know?
- Contact:
If anyone could help me optimize this (for speed, not size), it would be a big help. This program moves around an image depending on the button that is pressed. Again...I am looking for speed; I will sacrifice space fore speed.
DelVar YDelVar X
While 1
getKey->A
If Ans:Then
Text(Y,X,"
Text(Y+4,X,"
X+7(A=26)-7(A=24->X
Y+9(A=34)-9(A=25->Y
If X>86 or X<0:X+7(X<=1)-7(X>=87->X
If Y<0 or Y>45:Y+9(Y<=1)-9(Y>=46->Y
prgmPERSON
End
End
I was told that If:then statement end commands are much faster than if:statement commands...so would I change it to
If X>86 or X<0:then
X+7(X<=1)-7(X>=87->X
End
If Y<0 or Y>45:Then
Y+9(Y<=1)-9(Y>=46->Y
End
(I am trying to avoid the extra storing if the X and Y are in range)
Thanks for any help.
DelVar YDelVar X
While 1
getKey->A
If Ans:Then
Text(Y,X,"
Text(Y+4,X,"
X+7(A=26)-7(A=24->X
Y+9(A=34)-9(A=25->Y
If X>86 or X<0:X+7(X<=1)-7(X>=87->X
If Y<0 or Y>45:Y+9(Y<=1)-9(Y>=46->Y
prgmPERSON
End
End
I was told that If:then statement end commands are much faster than if:statement commands...so would I change it to
If X>86 or X<0:then
X+7(X<=1)-7(X>=87->X
End
If Y<0 or Y>45:Then
Y+9(Y<=1)-9(Y>=46->Y
End
(I am trying to avoid the extra storing if the X and Y are in range)
Thanks for any help.
(I just changed X+7(A=26)-7(A=24->X to X+7(Ans=26)-7(Ans=24->X That part is much faster now.)
*edit*
I was thinking...would this work?
X+7(A=26)-7(A=24->X
If Ans>86 or Ans<0:Ans+7(Ans<=1)-7(Ans>=87->X
Y+9(A=34)-9(A=25->Y
If Ans<0 or Ans>45:Ans+9(Ans<=1)-9(Ans>=46->Y
*edit again*
I was thinking again...Since prgmPERSON uses the X and Y variables...and since I call it after setting the Y values, could I just replace the Ys in prgmPERSON with Ans?
*edit*
Implimented the first thing, now I am going to try the ans in prgmPERSON.
*edit*
yeah that worked. So...to recap...I am trying to optimize this
DelVar YDelVar X
While 1
getKey->A
If Ans:Then
Text(Y,X,"
Text(Y+4,X,"
X+7(Ans=26)-7(Ans=24->X
If Ans>86 or Ans<0:Ans+7(Ans<=1)-7(Ans>=87->X
Y+9(Ans=34)-9(Ans=25->Y
If Ans<0 or Ans>45:Ans+9(Ans<=1)-9(Ans>=46->Y
prgmPERSON (I put Ans instead of Y vars in this sub program)
End
End
The only other two things I can think of to make it faster:
-Would it help if I moved the Text commands so they are right after I store the X or Y vals, and then use ans for either X or Y?
-Would it be faster to use
If Ans<0 or Ans>45:Then
Ans+9(Ans<=1)-9(Ans>=46->Y
End
OR
Ans+9(Ans<=1)-9(Ans>=46->Y
(I would also apply the faster method for the X boundry checking part)
*sigh* *edit* (I am leaving for college visits tomorrow, so I am trying to get this optimized as much as possible before I go...)
Here is my code, if you see a way to optimize it...
DelVar YDelVar X
While 1
getKey->A
If Ans:Then
Text(Y,X,"
Text(Y+4,X,"
X+7(Ans=26)-7(Ans=24->X
If Ans>86 or Ans<0:Then
Ans+7(Ans<=1)-7(Ans>=87->X
End
A
Y+9(Ans=34)-9(Ans=25->Y
If Ans<0 or Ans>45:Then
Ans+9(Ans<=1)-9(Ans>=46->Y
End
prgmPERSON
End
End
*edit*
I was thinking...would this work?
X+7(A=26)-7(A=24->X
If Ans>86 or Ans<0:Ans+7(Ans<=1)-7(Ans>=87->X
Y+9(A=34)-9(A=25->Y
If Ans<0 or Ans>45:Ans+9(Ans<=1)-9(Ans>=46->Y
*edit again*
I was thinking again...Since prgmPERSON uses the X and Y variables...and since I call it after setting the Y values, could I just replace the Ys in prgmPERSON with Ans?
*edit*
Implimented the first thing, now I am going to try the ans in prgmPERSON.
*edit*
yeah that worked. So...to recap...I am trying to optimize this
DelVar YDelVar X
While 1
getKey->A
If Ans:Then
Text(Y,X,"
Text(Y+4,X,"
X+7(Ans=26)-7(Ans=24->X
If Ans>86 or Ans<0:Ans+7(Ans<=1)-7(Ans>=87->X
Y+9(Ans=34)-9(Ans=25->Y
If Ans<0 or Ans>45:Ans+9(Ans<=1)-9(Ans>=46->Y
prgmPERSON (I put Ans instead of Y vars in this sub program)
End
End
The only other two things I can think of to make it faster:
-Would it help if I moved the Text commands so they are right after I store the X or Y vals, and then use ans for either X or Y?
-Would it be faster to use
If Ans<0 or Ans>45:Then
Ans+9(Ans<=1)-9(Ans>=46->Y
End
OR
Ans+9(Ans<=1)-9(Ans>=46->Y
(I would also apply the faster method for the X boundry checking part)
*sigh* *edit* (I am leaving for college visits tomorrow, so I am trying to get this optimized as much as possible before I go...)
Here is my code, if you see a way to optimize it...
DelVar YDelVar X
While 1
getKey->A
If Ans:Then
Text(Y,X,"
Text(Y+4,X,"
X+7(Ans=26)-7(Ans=24->X
If Ans>86 or Ans<0:Then
Ans+7(Ans<=1)-7(Ans>=87->X
End
A
Y+9(Ans=34)-9(Ans=25->Y
If Ans<0 or Ans>45:Then
Ans+9(Ans<=1)-9(Ans>=46->Y
End
prgmPERSON
End
End
-
- Calc Wizard
- Posts: 526
- Joined: Tue 08 Mar, 2005 1:37 am
- Location: who wants to know?
- Contact: