5. Loop with Player input + background process
Last post, we were dealing with idle loop and how we process the button inputs and the corresponding code handlers. This time, I'm going to show you a clearer form to handle player input.
One of the most asked question is how to move the player sprite UDLR, and shoot. I'm going to show you how you should structure your code and divide it into two parts:
- Process Input
- Act on Input
While we are processing input, we want it very, very quick. This means that we do not act on any of the input at all. We simply store the information and that's it.
Later on, depending on the variables set, we can process the data. I'm using an extra variable to determine whether or not the player is shooting, and whether or not the bullet is being shot.
This is a very simple structure. If you can use array for directions, then I suggest that you use it. Even loading array using DATA statements is preferable to hand-coding it, like I'm doing here. Even though it doesn't take much time at all, copying and pasting, using array is the neater and better solution. Easier to maintain, too.
We'll take a look at multi-threading process next.
- ACLS
- SPSET 1,68,0,0,0,0
- SPOFS 1,128,96
- SPSET 10,40,0,0,0,0
- SPANIM 10,2,6
- SPOFS 10,-50,-50
- @INIT
- PD=0:'PD=PLAYER DIRECTION. 0=STOP.1234=UDLR
- BB=0
- @LOOP
- ' PROCESS INPUT
- VSYNC 1:TX=TCHX:TY=TCHY:TS=TCHST:TT=TCHTIME
- B=BUTTON(0)
- SPREAD(1),PX,PY
- IF (B AND 1) THEN PD=1
- IF (B AND 2) THEN PD=2
- IF (B AND 4) THEN PD=3
- IF (B AND 8) THEN PD=4
- IF (B AND 16) THEN PA=1 ELSE PA=0:'PLAYER ACTION: SHOOT
- 'ACT ON INPUT
- 'THIS SECTION IS THE IDLE SECTION
- IF (SPCHK(1) AND 1) GOTO @LOOP ELSE GOSUB @PMOVE
- IF (SPCHK(10) AND 1) THEN BB=1 ELSE BB=0
- IF !BB THEN SPOFS 10,-50,-50
- IF PA AND !BB THEN GOSUB @PSHOOT:PD=0
- GOTO @LOOP
- @PMOVE
- IF PD>4 OR PD<0 THEN PD=0
- ON PD GOTO @MSTOP,@MUP,@MDOWN,@MLEFT,@MRIGHT
- @MSTOP
- SPCHR 1,68
- SPANIM 1,1,0
- RETURN
- @MUP
- SPCHR 1,76
- SPANIM 1,4,15
- SPOFS 1,PX,PY-16,60
- RETURN
- @MDOWN
- SPCHR 1,68
- SPANIM 1,4,15
- SPOFS 1,PX,PY+16,60
- RETURN
- @MLEFT
- SPCHR 1,72
- SPANIM 1,4,15
- SPOFS 1,PX-16,PY,60
- RETURN
- @MRIGHT
- SPCHR 1,64
- SPANIM 1,4,15
- SPOFS 1,PX+16,PY,60
- RETURN
- @PSHOOT
- IF PD>4 OR PD<0 THEN RETURN
- ON PD GOTO @PSTOP,@PUP,@PDOWN,@PLEFT,@PRIGHT
- @PSTOP
- GOTO @PDOWN
- RETURN
- @PUP
- SPCHR 10,46
- SPOFS 10,PX,PY,0
- SPOFS 10,PX,PY-160,160
- RETURN
- @PDOWN
- SPCHR 10,42
- SPOFS 10,PX,PY,0
- SPOFS 10,PX,PY+160,160
- RETURN
- @PLEFT
- SPCHR 10,44
- SPOFS 10,PX,PY,0
- SPOFS 10,PX-160,PY,160
- RETURN
- @PRIGHT
- SPCHR 10,40
- SPOFS 10,PX,PY,0
- SPOFS 10,PX+160,PY,160
- RETURN
No comments:
Post a Comment