Friday, October 25, 2013

Petit Computer Journal #22


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:


  1. Process Input
  2. 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.



  1. ACLS
  2. SPSET 1,68,0,0,0,0
  3. SPOFS 1,128,96
  4. SPSET 10,40,0,0,0,0
  5. SPANIM 10,2,6
  6. SPOFS 10,-50,-50


  7. @INIT
  8. PD=0:'PD=PLAYER DIRECTION. 0=STOP.1234=UDLR
  9. BB=0

  10. @LOOP
  11. ' PROCESS INPUT
  12. VSYNC 1:TX=TCHX:TY=TCHY:TS=TCHST:TT=TCHTIME
  13. B=BUTTON(0)
  14. SPREAD(1),PX,PY

  15. IF (B AND 1) THEN PD=1
  16. IF (B AND 2) THEN PD=2
  17. IF (B AND 4) THEN PD=3
  18. IF (B AND 8) THEN PD=4
  19. IF (B AND 16) THEN PA=1 ELSE PA=0:'PLAYER ACTION: SHOOT

  20. 'ACT ON INPUT
  21. 'THIS SECTION IS THE IDLE SECTION
  22. IF (SPCHK(1) AND 1) GOTO @LOOP ELSE GOSUB @PMOVE
  23. IF (SPCHK(10) AND 1) THEN BB=1 ELSE BB=0

  24. IF !BB THEN SPOFS 10,-50,-50
  25. IF PA AND !BB THEN GOSUB @PSHOOT:PD=0
  26. GOTO @LOOP

  27. @PMOVE
  28. IF PD>4 OR PD<0 THEN PD=0
  29. ON PD GOTO @MSTOP,@MUP,@MDOWN,@MLEFT,@MRIGHT
  30. @MSTOP
  31. SPCHR 1,68
  32. SPANIM 1,1,0
  33. RETURN
  34. @MUP
  35. SPCHR 1,76
  36. SPANIM 1,4,15
  37. SPOFS 1,PX,PY-16,60
  38. RETURN
  39. @MDOWN
  40. SPCHR 1,68
  41. SPANIM 1,4,15
  42. SPOFS 1,PX,PY+16,60
  43. RETURN
  44. @MLEFT
  45. SPCHR 1,72
  46. SPANIM 1,4,15
  47. SPOFS 1,PX-16,PY,60
  48. RETURN
  49. @MRIGHT
  50. SPCHR 1,64
  51. SPANIM 1,4,15
  52. SPOFS 1,PX+16,PY,60
  53. RETURN

  54. @PSHOOT
  55. IF PD>4 OR PD<0 THEN RETURN
  56. ON PD GOTO @PSTOP,@PUP,@PDOWN,@PLEFT,@PRIGHT
  57. @PSTOP
  58. GOTO @PDOWN
  59. RETURN
  60. @PUP
  61. SPCHR 10,46
  62. SPOFS 10,PX,PY,0
  63. SPOFS 10,PX,PY-160,160
  64. RETURN
  65. @PDOWN
  66. SPCHR 10,42
  67. SPOFS 10,PX,PY,0
  68. SPOFS 10,PX,PY+160,160
  69. RETURN
  70. @PLEFT
  71. SPCHR 10,44
  72. SPOFS 10,PX,PY,0
  73. SPOFS 10,PX-160,PY,160
  74. RETURN
  75. @PRIGHT
  76. SPCHR 10,40
  77. SPOFS 10,PX,PY,0
  78. SPOFS 10,PX+160,PY,160
  79. RETURN


No comments:

Post a Comment