How can you achieve full 2d movement (diagonal as well as horizontal and vertical)? And, how do you find a 2d tilemap editor?

Edit by Merth: Moved to z80 subforum.
You have to be more specific. Are you talking about moving an object around on a screen, or are you talking about panning around on an image (or tilemap) that is bigger than the screen?
yes, moving an object around the screen.
I don't have a lot of experience with assembly, but I can give you the general technique with BASIC (and if you can apply it to assembly, it should work fine):

You want to use "GetKey" (in the PROGRAM:IO menu) to get the value of the last key pressed, and store it in a variable. No key was pressed, then GetKey returns 0. If you want to keep doing stuff but only move sometimes, then you get the key, and if it is 0, then you don't move. If you only want to do something when you move, then you can loop until the key is not zero, and then do something with that key. The value of each key (on the TI83+/84+) is a two-digit number where the first digit is the row (1_ for the top keys, 2_ for the first row under it, etc. and 10_ for the bottom row), and the second digit is the column (_1 for the left-most column, and _5 for the right-most column). One catch is that the left, up, and right keys are considered to be part of the 2nd row (making those 24,25,26), and down is part of the 3rd row (making it 34). Sometimes it is good to use GetKey just one time before a loop just to dump the last value (for example, perhaps the person pressed ENTER to start the program, and you want to ignore that key).

Here is some code which uses A and B for the (row,column) position, K for the key, and changes it according to key presses. The loop goes until they press 2ND (which is 21). The nice thing about "Repeat" loops is that they always go through at least once; and THEN it checks teh condition (for example, "Repeat K=21" will do all the code until the "End", and then will do it again and again until K is 21):


Code:
Getkey
Repeat K=21
Getkey -> K
If K=25
A-1 -> A
If K=34
A+1 -> A
If K=24
B-1 -> B
If K=26
B+1 -> B
End


There are ways to shorten that too: whenever you say something like "K=21", it turns into 1 if it is true and 0 if it is not. That means that you can put each comparison in parenthesis and actually add several values together. Thus, that code can be shortened to:


Code:

Getkey
Repeat K=21
Getkey -> K
A-(K=25)+(K=34) -> A
B-(K=24)+(K=26) -> B
End


Some points to consider:

- If you want to move by more than "1" unit at a time, then multiply by however much you want to move (e.g. "A+5 -> A", or perhaps "A-5(K=25)+5(K=34) -> A").

- If you want to keep the movement within certain boundaries, then you also check if the position is not too close to move that way (for example, "If K=34 and A<8" for "if you pressed DOWN and the row-position is not too far down (and 8 is too far)"). This can also be applied to the combination of statements, perhaps "A-(K=25 and A>0)+(K=34 and A<Cool".

- If you want to "wrap around" (when you move off the screen on one side, come back in on the opposite side), then you can move like you would, but then use an "If" to check if it is out of bounds and correct it. You can shorten this into smaller statements similar to how I did with combining keypresses:
"A+8(A<1)-8(A>Cool -> A" wraps A within 8 positions. Replace "8" with the actual size you want.

- The are multiple ways to move the screen around on a map. One way is to position and move things as if you could see the whole map all at once. You can then either compute the "screen position" from the position of something else, or store the screen position and move it around with everything else. Either way, whenever you display or draw stuff, you subtract the screen-position from the position of everything else to find where to draw it on the SCREEN (rather than on the whole map); and then you only draw things that are within the screen boundaries Smile

- Sorry, but there is no way to detect multiple keypresses at once in BASIC (but that are in assembly!). You can get around this either by using 8 keys (4 cardinal directions, and 4 for diagonals), or by storing values for which WAY to move ("momentum"), which would work JUST like moving something around on a screen; except that you are literally moving the "direction of movement". Then you have the position of the thing that is moving change according to that. The code would look something like this (assuming that A and B are the thing that moves [perhaps the screen], and C and D are the direction it is moving):


Code:
0 -> C
0 -> D
Getkey
Repeat K=21
Getkey -> K
C-(K=25 and C> -1)+(K=34 and C<1) -> C
D-(K=24 and D> -1)+(K=26 and D<1) -> D
A+C -> A
B+D -> B


There are MANY tools for tilemaps or sprites etc.; but I'm not familiar with any of them. Take a browse and see what you find in the archives (or perhaps someone else has more helpful info?)
As far as diagonal movement, you need to know how to perform direct input (can I assume you have read the relevant chapter in ASM in 28 Days?). By checking the arrow key bitmap separately for the up, down, left, and right keys, you can move both left and up, for example, in the same cycle. The pseudocode:


Code:
  initialize X
  initialize Y
OuterLoop:
  draw sprite at (X,Y)
InnerLoop:
  output reset to keypad
  nop \ nop
  output arrow key group to keypad
  nop \ nop
  input from keypad to K
  if K is $ff, so no bits are reset, jump to InnerLoop
  push af
    erase sprite at (X,Y)
  pop af
  push af
    call Go_Up if "up" bit in K is reset and Y is not at the top edge
  pop af
  push af
    call Go_Down if "down" bit in K is reset and Y is not at the bottom edge
  pop af
  push af
    call Go_Left if "left" bit in K is reset and X is not at the left edge
  pop af
  //no need to push af again to save it
    call Go_Right if "right" bit in K is reset and X is not at the right edge
  jump to OuterLoop
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement