Moving a Character in Machine Code

Started by EgoTrip, 16:41, 08 December 10

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

EgoTrip

This is really stupid, I have done it before but cant seem to get it working any more.


All I want is to move a character (for arguments sake lets say a star, char 42) with the cursor keys around the screen in 8x8 blocks, using the firmware, and I cant seem to get something this simple working.


Help please!

arnoldemu

Quote from: EgoTrip on 16:41, 08 December 10
This is really stupid, I have done it before but cant seem to get it working any more.


All I want is to move a character (for arguments sake lets say a star, char 42) with the cursor keys around the screen in 8x8 blocks, using the firmware, and I cant seem to get something this simple working.


Help please!

txt set cursor for positioning and txt output for drawing should do it for the drawing part, remembering to draw space where the old position was.


then you could use km read char for reading the keys, or km test key.


I have made some example code that moves a sprite around and that uses firmware for checking keys.

http://www.cpctech.org.uk/source/shftspr.html

this may help?

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

EgoTrip

#2

I have already done what you suggested, it just doesn't work though, maybe I am missing something obvious.


What is the difference between read char and test key? I know one reads the char number and one the state of the key pressed, is that all or is there anything else? I used the test key in my attempt.


Edit: I would also like to add this is in Mode 1

EgoTrip

Actually sorry about all this, I just figured out what was wrong and it is all working now. All I needed to do was add in some PUSH AF and POP AF instructions.


Thanks for the help anyway!

EgoTrip

OK so its not working. After a while it crashes the computer.


Heres the code I have so far, yes its bloated and could be a lot better but I'm a novice.



org &3000


call init


.mainloop
call keys
jp mainloop


.keys
ld a,0
call &bb1e
jp nz,moveup
ld a,72
call &bb1e
jp nz,moveup
ld a,2
call &bb1e
jp nz,movedn
ld a,73
call &bb1e
jp nz,movedn
ld a,8
call &bb1e
jp nz,movelf
ld a,74
call &bb1e
jp nz,movelf
ld a,1
call &bb1e
jp nz,movert
ld a,75
call &bb1e
jp nz,movert
ld a,47
call &bb1e
jp nz,fire
ld a,76
call &bb1e
jp nz,fire
ld a,66
call &bb1e
jp nz,quit
ret


.moveup
ld a,(ypos)
cp 1
ret z
push af
call prblank
pop af
dec a
ld (ypos),a
call prchar
jp mainloop


.movedn
ld a,(ypos)
cp 25
ret z
push af
call prblank
pop af
inc a
ld (ypos),a
call prchar
jp mainloop


.movelf
ld a,(xpos)
cp 1
ret z
push af
call prblank
pop af
dec a
ld (xpos),a
call prchar
jp mainloop


.movert
ld a,(xpos)
cp 40
ret z
push af
call prblank
pop af
inc a
ld (xpos),a
call prchar
jp mainloop


.fire
ld a,7
call &bb5a
jp mainloop


.quit
pop af
ret


.prblank
ld a,(xpos)
ld h,a
ld a,(ypos)
ld l,a
call &bb75
ld a,32
call &bb5d
call &bd19
ret


.prchar
ld a,(xpos)
ld h,a
ld a,(ypos)
ld l,a
call &bb75
ld a,42
call &bb5d
call &bd19
ret


.xpos db 10
.ypos db 10


.init
ld a,10
ld (xpos),a
ld (ypos),a
ret


arnoldemu

#5
Quote from: EgoTrip on 17:13, 08 December 10
OK so its not working. After a while it crashes the computer.


Heres the code I have so far, yes its bloated and could be a lot better but I'm a novice.



org &3000


call init


.mainloop
call keys
jp mainloop


.keys
ld a,0
call &bb1e
jp nz,moveup
ld a,72
call &bb1e
jp nz,moveup
ld a,2
call &bb1e
jp nz,movedn
ld a,73
call &bb1e
jp nz,movedn
ld a,8
call &bb1e
jp nz,movelf
ld a,74
call &bb1e
jp nz,movelf
ld a,1
call &bb1e
jp nz,movert
ld a,75
call &bb1e
jp nz,movert
ld a,47
call &bb1e
jp nz,fire
ld a,76
call &bb1e
jp nz,fire
ld a,66
call &bb1e
jp nz,quit
ret


.moveup
ld a,(ypos)
cp 1
ret z
push af
call prblank
pop af
dec a
ld (ypos),a
call prchar
jp mainloop


.movedn
ld a,(ypos)
cp 25
ret z
push af
call prblank
pop af
inc a
ld (ypos),a
call prchar
jp mainloop


.movelf
ld a,(xpos)
cp 1
ret z
push af
call prblank
pop af
dec a
ld (xpos),a
call prchar
jp mainloop


.movert
ld a,(xpos)
cp 40
ret z
push af
call prblank
pop af
inc a
ld (xpos),a
call prchar
jp mainloop


.fire
ld a,7
call &bb5a
jp mainloop


.quit
pop af
ret


.prblank
ld a,(xpos)
ld h,a
ld a,(ypos)
ld l,a
call &bb75
ld a,32
call &bb5d
call &bd19
ret


.prchar
ld a,(xpos)
ld h,a
ld a,(ypos)
ld l,a
call &bb75
ld a,42
call &bb5d
call &bd19
ret


.xpos db 10
.ypos db 10


.init
ld a,10
ld (xpos),a
ld (ypos),a
ret



your jp mainloop is the main problem. the call keys is pushing return address onto the stack, then after you process a key, you do something and then jp mainloop back. You should be using ret here so it returns back ok.

replace all the jp mainloop in your key functions and then you should be ok.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

arnoldemu

Quote from: EgoTrip on 16:55, 08 December 10
I have already done what you suggested, it just doesn't work though, maybe I am missing something obvious.


What is the difference between read char and test key? I know one reads the char number and one the state of the key pressed, is that all or is there anything else? I used the test key in my attempt.


Edit: I would also like to add this is in Mode 1
read char translates the key and gives you a character that it pressed.
test key returns the key number.
I think read char is also dependant on the key repeat.
so you've chosen the best one.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

EgoTrip


Powered by SMFPacks Menu Editor Mod