Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: saintrunner on February 17, 2012, 06:57:25 pm

Title: BASIC movement
Post by: saintrunner on February 17, 2012, 06:57:25 pm
Code: [Select]
:ClrHome
:4→X
:5→Y
:
:Lbl A
:Output(X,Y,"@"
:While 1
:getKey→G
:While G=0
:getKey→G
:End
:If G=24
:Then
:Output(X,Y," "
:Y-1→Y
:Goto A
:End
:
:If G=25
:Then
:Output(X,Y," "
:X-1→X
:Goto A
:End
:
:If G=26
:Then
:Output(X,Y," "
:Y+1→Y
:Goto A
:End
:
:If G=34
:Then
:Output(X,Y," "
:X+1→X
:Goto A
:End
:
:
:If G=45
:Then
:ClrHome
:Stop
:End
:
:If G≠24 or 25 or 26 or 34
:Goto A
:End

can someone help me with this? I feel there is a more effective way to code movement. ???

Thanks

~saint~
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 17, 2012, 07:23:49 pm
This isn't the whole code, but this is how I generally do the key/movement portion.

Code: [Select]
ClrHome
4->X
5->Y
Repeat G=45
Repeat G
getkey->G
End
Output(X,Y,"
Y-(G=24)+(G=26->Y
X-(G=25)+(G=34->X
Output(X,Y,"@
End
ClrHome
Stop

I'm betting someone will come up with that one Δlist trick in response to this, but I coded BASIC mostly years before that trick was made popular, so I don't know how it works and if it's any faster.

(Note, don't use Goto inside Then/Else/While/Repeat/For. This causes memory leaks, which slows down the program then ERR:MEMORY after a while.)
Title: Re: BASIC movement
Post by: saintrunner on February 17, 2012, 07:27:14 pm
cool thanks :)

edit: hmmm doesn't seem to work
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 17, 2012, 07:38:06 pm
It doesn't? ??? What does happen?

Can you show me how you typed the code in wabbitemu? Just to make sure you typed it right.
Title: Re: BASIC movement
Post by: saintrunner on February 17, 2012, 07:42:15 pm
It works now thanks!

could you explain it?
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 17, 2012, 08:11:06 pm
It would be a bit hard to explain actually, is there any part in particular you might need help to understand ? According to IRC you understood the Y+stuff->Y part.
Title: Re: BASIC movement
Post by: saintrunner on February 17, 2012, 08:20:57 pm
just the ordering of the stuff has me slightly confused, but I bet if I look at it more, I will figure it out
Title: Re: BASIC movement
Post by: chattahippie on February 17, 2012, 08:36:58 pm
I'm betting someone will come up with that one Δlist trick in response to this, but I coded BASIC mostly years before that trick was made popular, so I don't know how it works and if it's any faster.
What is this trick? I've never heard of it ???
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 17, 2012, 09:07:34 pm
It involves storing 24, 25, 26 and 34 in a list then doing Δlist or something for the movement, which is even smaller than my code. It was on TI-BASIC Developer somewhere.
Title: Re: BASIC movement
Post by: chattahippie on February 17, 2012, 09:11:16 pm
Cool!  I was messing around with ∆List (I looked it up in TI-BASIC developer), trying to figure out said trick, and although I couldn't figure it out, I did manage to make a start to a snake game (the kind where you eat pellets to grow)

Not really anything significant (I've seen the game millions of times :P ), but I was amazed how easy it actually was to make (I had no clue beforehand how to make the tail) :D
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 17, 2012, 09:13:55 pm
If you manage to get the grasp of the trick in question, could you do some test or something? I wonder how its speed compares with my code.

As for snake I had an hard time making one before because no tutorial were available and the tail caused some glitches sometimes. For example when the apple/star appeared directly on the tail, it disappeared or something.
Title: Re: BASIC movement
Post by: chattahippie on February 17, 2012, 09:16:24 pm
Okay, I found it
Here it is, strait from TI-BASIC Developer:
Code: [Select]
:4→A
:8→B
:Repeat K=21
:getKey→K
:If Ans
:Output(A,B," // 1 space
:min(8,max(1,A+sum(Δlist(Ans={25,34→A
:min(16,max(1,B+sum(Δlist(K={24,26→B
:Output(A,Ans,"X
:End
Now, it is time to speed crunch!

Okay, here are my results:
DJ, your code is a little faster than theirs, and is 87 bytes (the version I made had no ClrHome and Stop)
The ∆List trick is 96 bytes, but also calculated if the character was at the border for almost no speed difference (slightly slower)

So, I would go with your version for without edge testing, but if you want to have testing for almost no speed drop, use the ∆List code
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 17, 2012, 09:25:35 pm
Actually I got rid of min(8,max(1, and min(16,max(1, because my code above had no detection for if the character goes out of the screen, but a better speed test would be to add the old method of checking to my code (if the char goes out, then it reverts the increase/decrease)
Title: Re: BASIC movement
Post by: chattahippie on February 17, 2012, 09:31:16 pm
Okay, I removed the min max from the List program, which shaved it down to 83 bytes, and they are both so fast, it's hard to tell which one is faster D:
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 17, 2012, 09:46:13 pm
Ah ok lol. X.x I guess the slowdown might not be too bad then. Plus by removing the stuff it's slightly smaller. I assume with collision detection it's much smaller if we compare both routines with it.
Title: Re: BASIC movement
Post by: chattahippie on February 17, 2012, 09:54:07 pm
Maybe, I think they would stay about the same.  The List code really doesn't have much collision detection anyways, just the min max trick

Anyways, I'm starting to understand just how brilliant this code is... it is amazing how it works :)

But isn't it faster to do Repeat Ans:getKey:End:Ans->G ?
Title: Re: BASIC movement
Post by: saintrunner on February 17, 2012, 10:05:23 pm
Can someone explain how I can do collision?
Title: Re: BASIC movement
Post by: chattahippie on February 17, 2012, 10:14:42 pm
Where it has either A+(getKey stuff)->A and B+(getkey stuff)->B, or the x and y versions,

just add
Code: [Select]
min(8,max(1, to the Y axis variable
and
Code: [Select]
min(16,max(1, to the X axis variable
should work :)
Title: Re: BASIC movement
Post by: saintrunner on February 17, 2012, 10:26:09 pm
um, can you show me that in the first code DJ posted? Thats the one I'm using
Title: Re: BASIC movement
Post by: chattahippie on February 17, 2012, 10:42:12 pm
Code: [Select]
ClrHome
4->X
5->Y
Repeat G=45
Repeat G
getkey->G
End
Output(X,Y,"
min(16,max(1,Y-(G=24)+(G=26->Y
min(8,max(1,X-(G=25)+(G=34->X
Output(X,Y,"@
End
ClrHome
Stop
There you go! :)

EDIT: if you need me to, I can explain it for you
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 17, 2012, 10:48:03 pm
Note that this is only for collision on the screen sides, though.
Title: Re: BASIC movement
Post by: chattahippie on February 17, 2012, 10:51:58 pm
Note that this is only for collision on the screen sides, though.
Yes, but it should work for any sized rectangle (aka no walls in the middle of the screen :P )
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 11:39:25 am
Well thanks, but I was looking for collision with predrawn things. Like if I had on the screen a question mark, I wouldn't want to run through it
Title: Re: BASIC movement
Post by: chattahippie on February 18, 2012, 12:18:41 pm
Here is a way to do that:
Code: [Select]
"
0ANYTHING000000  //Take out the newlines, of course, so the interpreter doesn't freak out
0             0
0             0
0     :)      0
00WORKS!!!1!100 ->Str1  //You can make the map bigger for more space to move

ClrHome
2->X
2->Y
Output(1,1,Str1
Repeat G=45
Repeat G
getkey->G
End
Output(X,Y,"
Y-(G=24)(sub(Str1,1,(X-1)16+Y-1) = "_")+(G=26)(sub(Str1,1,(X-1)16+Y+1) = "_->Y
X-(G=25)(sub(Str1,1,(X-2)16+Y) ="_")+(G=34)(sub(Str1,1,16X+Y+1) = "_->X
Output(X,Y,"@
End
ClrHome
Stop

I just kinda threw that together, but it should work.  It looks into Str1 to check if the space it is moving to is empty :)
It uses DJ's code, so you can easily modify it to this
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 12:20:52 pm
I'll try it out thanks
Title: Re: BASIC movement
Post by: Yeong on February 18, 2012, 12:27:34 pm
my ASCII world use something like that, I think.
However, mine has a 3 walkable tiles :D (And it's kinda slow :\ )
Title: Re: BASIC movement
Post by: BlakPilar on February 18, 2012, 12:29:56 pm
It involves storing 24, 25, 26 and 34 in a list then doing Δlist or something for the movement, which is even smaller than my code. It was on TI-BASIC Developer somewhere.

Do you mean Repeat max(K={24,25,26,34}) ?
Title: Re: BASIC movement
Post by: chattahippie on February 18, 2012, 12:31:05 pm
my ASCII world use something like that, I think.
However, mine has a 3 walkable tiles :D (And it's kinda slow :\ )

That sounds like more fun to program :P
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 12:31:45 pm
it didn't work ???
Title: Re: BASIC movement
Post by: Yeong on February 18, 2012, 12:33:09 pm
which part does it throw errors?
EDIT: maybe G is 45 BEFORE the loops starts. to prevent that, add DelVarG before Repeat G.
Title: Re: BASIC movement
Post by: BlakPilar on February 18, 2012, 12:34:14 pm
Did you try the matrix-based version I sent you? Or do you just want to do a string-based mapper?
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 12:34:19 pm
not sure, just says invalidDim
Title: Re: BASIC movement
Post by: Yeong on February 18, 2012, 12:35:02 pm
what are your x and y values right now?
Title: Re: BASIC movement
Post by: chattahippie on February 18, 2012, 12:35:20 pm
Set them both to 2
Title: Re: BASIC movement
Post by: BlakPilar on February 18, 2012, 12:36:24 pm
which part does it throw errors?
EDIT: maybe G is 45 BEFORE the loops starts. to prevent that, add DelVarG before Repeat G.

Repeat loops always go through once no matter what, so that shouldn't be the problem.
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 12:36:25 pm
they are
Title: Re: BASIC movement
Post by: Yeong on February 18, 2012, 12:36:37 pm
Did you try the matrix-based version I sent you? Or do you just want to do a string-based mapper?
Matrix tilemapping will be definitely faster. However, the size when the map gets kinda big..../me shudders


which part does it throw errors?
EDIT: maybe G is 45 BEFORE the loops starts. to prevent that, add DelVarG before Repeat G.
Repeat loops always go through once no matter what, so that shouldn't be the problem.
Crap. I totally forgot about that. D:

EDIT: Optimization and fix :D
chattahippie got his sub( argument mixed up XD
Code: [Select]
"
0ANYTHING000000  //Take out the newlines, of course, so the interpreter doesn't freak out
0             0
0             0
0     :)      0
00WORKS!!!1!100 ->Str1  //You can make the map bigger for more space to move

ClrHome
2->X
2->Y
Output(1,1,Str1
Repeat G=45
Repeat G
getkey->G
End
Output(X,Y,"
Y-(G=24 and sub(Str1,16X-17+Y,1) = "_")+(G=26 and sub(Str1,16X-15+Y,1) = "_->Y
X-(G=25 and sub(Str1,16X-32+Y,1) ="_")+(G=34 and sub(Str1,16X+Y+1,1) = "_->X
Output(X,Y,"@
End
ClrHome
Stop
Title: Re: BASIC movement
Post by: chattahippie on February 18, 2012, 12:43:34 pm
chattahippie got his sub( argument mixed up XD

I just noticed as well D:
getting rusty on my TIBASIC
Title: Re: BASIC movement
Post by: Yeong on February 18, 2012, 12:45:41 pm
getting rusty on my TIBASIC

That's why I practice TI-BASIC XD/me is rusty on his Axe. D:
Title: Re: BASIC movement
Post by: chattahippie on February 18, 2012, 12:48:13 pm
getting rusty on my TIBASIC

That's why I practice TI-BASIC XD/me is rusty on his Axe. D:

[dramatic music backing]lol, the thing is, I still practice TI-BASIC too! D:[/dramatic music backing]

I'm making a (simple) snake game as well as my axe game
but the thing is, Im not using sub( yet :P
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 01:21:11 pm
IT WORKS! Thanks so much! any way to make it so it tests certain 'tiles' ?


edit: bug....if you have a tile too your right you can't move down!
Title: Re: BASIC movement
Post by: chattahippie on February 18, 2012, 01:41:02 pm
IT WORKS! Thanks so much! any way to make it so it tests certain 'tiles' ?

Just check sub(Str1,(X-1)16+Y,1)
It will return the character you are standing on
and from there, you can have them do different things

But note, the code provided to check for collision will not allow you to stand on anything other than spaces, so you will have to adjust for that
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 01:42:12 pm
theres a bug, if you have a tile to your right you can't move down, also do I change the current subs with the above or do I just add it before?
Title: Re: BASIC movement
Post by: chattahippie on February 18, 2012, 02:04:54 pm
My code (and Yeong's "fixed" code :P ) had a small bug, this should work better:
Code: [Select]
"
0ANYTHING000000  //Take out the newlines, of course, so the interpreter doesn't freak out
0             0
0             0
0     :)      0
00WORKS!!!1!100 ->Str1  //You can make the map bigger for more space to move

ClrHome
2->X
2->Y
Output(1,1,Str1
Repeat G=45
Repeat G
getkey->G
End
Output(X,Y,"
Y-(G=24 and sub(Str1,16X-17+Y,1) = "_")+(G=26 and sub(Str1,16X-15+Y,1) = "_->Y
X-(G=25 and sub(Str1,16X-32+Y,1) ="_")+(G=34 and sub(Str1,16X+Y,1) = "_->X
Output(X,Y,"@

"example for event
If sub(Str1,16X-32+Y,1)="E
Then
"Event stuff here"
End

End
ClrHome
Stop
There was a +1 in the wrong place :P

For the example I put in there, it will check the tile above to see if it is an "E"
I wouldn't use this way for too many events though, it will cause a speed loss for each event you add
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 02:17:33 pm
Thanks so much :) you all have helped so much
Title: Re: BASIC movement
Post by: chattahippie on February 18, 2012, 02:20:22 pm
Thanks so much :) you all have helped so much

Glad to help :D

EDIT: 300 post!
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 02:21:26 pm
By the way I like your avatar
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 18, 2012, 02:22:48 pm
Did you try the matrix-based version I sent you? Or do you just want to do a string-based mapper?
Matrix tilemapping will be definitely faster. However, the size when the map gets kinda big..../me shudders
Yeah he might be forced to rely on XCOPY or Doors CS's Zcopy function. X.x. A 12x8 matrix in storage format (in the code) is between 250 and 350 KB large, so if your game got 200 maps (even less than Metroid II), it won't even get close to fit in RAM. In addition to that such matrix in matrix file format takes around 1 KB of RAM. They're much faster for collision detection, though.
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 02:24:53 pm
Did you try the matrix-based version I sent you? Or do you just want to do a string-based mapper?
Matrix tilemapping will be definitely faster. However, the size when the map gets kinda big..../me shudders
Yeah he might be forced to rely on XCOPY or Doors CS's Zcopy function. X.x. A 12x8 matrix in storage format (in the code) is between 250 and 350 KB large, so if your game got 200 maps (even less than Metroid II), it won't even get close to fit in RAM. In addition to that such matrix in matrix file format takes around 1 KB of RAM. They're much faster for collision detection, though.

if it doesn't fit in your RAM, couldn't you just use noshell and archive it?
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 18, 2012, 02:26:06 pm
It doesn,t work like that. You can't just run an archived BASIC program from another one and any BASIC file has to be under 22 KB large or so to even launch at all. Even with Noshell, DCS, Mirage, etc, you'll still get an ERR:ARCHIVED error.
Title: Re: BASIC movement
Post by: saintrunner on February 18, 2012, 02:28:15 pm
wow! it would be that big! well, I don't think if I made a BASIC game, I would use that many maps anyways :P And even if I did make a BASIC game, I doubt it would be ANY where close to how AMAZING your games are! You are my BASIC hero :)
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 18, 2012, 02:37:22 pm
Lol thanks. But yeah most of my games ended up pretty big since in the past so many other games were too short. D: (they usually used one pic per map)
Title: Re: BASIC movement
Post by: Yeong on February 18, 2012, 03:53:25 pm
My code (and Yeong's "fixed" code :P ) had a small bug
I only fixed your sub( stuff :P
Title: Re: BASIC movement
Post by: chattahippie on February 18, 2012, 04:05:44 pm
My code (and Yeong's "fixed" code :P ) had a small bug
I only fixed your sub( stuff :P

lol I know, I was joking, I know it was my fault
Title: Re: BASIC movement
Post by: jsj795 on February 18, 2012, 09:43:44 pm
be careful with making string-based map, because as the string gets longer, the more "bottom" you're in a map, the slower it gets since BASIC checks from the beginning of the string to the end of the string for the sub( routine
Title: Re: BASIC movement
Post by: DJ Omnimaga on February 18, 2012, 11:40:56 pm
Yeah true that's an issue too. I recommend splitting the maps in 128 character chunks or something. Smaller strings are easier to work with anyway since the PRGM editor is 16x7, so you don't need a map editor at all.