Showing posts with label background. Show all posts
Showing posts with label background. Show all posts

Friday, November 15, 2013

Petit Computer Journal #25


Background Maker

A while back, I did a BGMaker program. That's a program that takes in graphic screen (GRP0) and turn it into background (SCU0) and background tiles (BGU0, BGU1, BGU2,BGU3). I had suspected it that it fits into one screen, and it did. Here is my entry to the single screen programming challenge:


  1. 'BGMAKER BY HARRY HARDJONO
  2. CLS:CLEAR:DIM TL$[1024]:MT=-1
  3. OX=0:OY=0:RX=31:RY=23:P$="G2"
  4. ?"GRAPHIC?":INPUT P$
  5. LOAD "GRP0:"+P$,FALSE
  6. FOR Y=0 TO RY:FOR X=0 TO RX
  7. X1=X*8:X2=X1+7:Y1=Y*8:Y2=Y1+7
  8. G$="":FOR YY=Y1 TO Y2
  9. FOR XX=X1 TO X2
  10. G$=G$+HEX$(GSPOIT(XX,YY)%16)
  11. NEXT:NEXT
  12. CT=-1:FOR I=MT TO 0 STEP -1
  13. IF G$==TL$[I] THEN CT=I
  14. NEXT
  15. IF CT==-1 THEN GOSUB @SETCHR
  16. BGPUT 0,OX+X,OY+Y,CT,0,0,0
  17. NEXT:NEXT:SAVE "SCU0:T0"
  18. FOR I=0 TO FLOOR(MT/256)
  19. B$="BGU"+STR$(I)
  20. SAVE B$+":B"+STR$(I):NEXT:END
  21. @SETCHR
  22. MT=MT+1:TL$[MT]=G$:CT=MT
  23. BU$="BGU"+STR$(FLOOR(MT/256))
  24. CHRSET BU$,MT%256,G$:RETURN


I also did a version with no memory. I'm not going to show it here because it's not only complicated, but also very, very slow. In fact, at one point, I actually put in an array to do it. That's when I decided to do a version with arrays instead.

Anyway, if you look at the code above, you'll see that it only works on single graphic screen. That's not what I want to have. I want to have the full 64 by 64 background tile generator. As you can see, from the variables OX, OY, P$, even though they do nothing in this version, the original design does take the full background field into account.

Here is the updated BGMAKER program that includes all available background screen. You'll need six graphic screens. They're all loaded into GRP0, but the build is done on different parts of BG screen.

'BGMAKER BY HARRY HARDJONO
CLS:CLEAR:DIM TL$[1024]:MT=-1
@PICFILE
DATA "G1","G2","G3","G6","G5","G4"
RESTORE @PICFILE

All this does is some initialization. I put the file names into DATA statements because I'm too lazy to modify the code. So I simply replaced all INPUT P$ into READ P$.


  1. 'LOAD GRPS
  2. OX=0:OY=0:RX=31:RY=23:READ P$
  3. LOAD "GRP0:"+P$,FALSE
  4. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER


Okay, you can see the pattern here. OX, OY are relative shifting or original X,Y coordinate. RX,RY are the size or width and height of the screen. I mentioned changing INPUT P$ into READ P$. Once I loaded the graphic into screen 0, I shifted the background to the appropriate place and call the @TILER subroutine. Since OX,OY are zeros, I guess I don't need to shift the background (using BGOFS), but I'm doing it here for consistency.


  1. OX=32:OY=0:RX=31:RY=23:READ P$
  2. LOAD "GRP0:"+P$,FALSE
  3. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER

  4. OX=0:OY=24:RX=31:RY=23:READ P$
  5. LOAD "GRP0:"+P$,FALSE
  6. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER

  7. OX=32:OY=24:RX=31:RY=23:READ P$
  8. LOAD "GRP0:"+P$,FALSE
  9. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER

  10. OX=0:OY=48:RX=31:RY=15:READ P$
  11. LOAD "GRP0:"+P$,FALSE
  12. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER

  13. OX=32:OY=48:RX=31:RY=15:READ P$
  14. LOAD "GRP0:"+P$,FALSE
  15. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER


As you can see, only the first line is being changed. I could have just put the whole thing into a subroutine. However, I'm not doing it because of the possibility that I don't have the whole 6 graphics to play with. If I only have four graphic screens, for example, it's easy for me to just comment out the necessary lines. This is judgement call, so you can certainly do it different. Whatever works.


  1. @SAVEBG
  2. SAVE "SCU0:T0"
  3. FOR I=0 TO FLOOR(MT/256)
  4. B$="BGU"+STR$(I)
  5. SAVE B$+":B"+STR$(I)
  6. NEXT 
  7. WAIT 300


Even though this is labelled as a subroutine. This is just a piece of code. It's not being called by GOSUB. I simply put the label there because I don't need to use comment. I could have, but simply decided not to.


  1. FOR I=0 TO 99
  2. X=RND(512):Y=RND(512)
  3. BGOFS 0,X,Y,60:WAIT 60
  4. NEXT
  5. WAIT 300


This is simply the scrolling of the background. You see 60 at the end of BGOFS call. This is equivalent of one second. So, I scroll for one second, while waiting for one second. What happens if I don't wait? Then the scroll will be reset immediately.


  1. BGCLR
  2. CHRINIT "BGU0"
  3. CHRINIT "BGU1"
  4. CHRINIT "BGU2"
  5. CHRINIT "BGU3"
  6. GCLS

  7. END


This code is necessary to reset the states to the original state. Otherwise, you will see that your screen is overlaid with your background. Of course, during debugging, that's quite problematic for me, since oftentimes the program does not run to completion, so I end up doing these commands by hand.


  1. @TILER
  2. FOR Y=0 TO RY:FOR X=0 TO RX
  3. X1=X*8:X2=X1+7:Y1=Y*8:Y2=Y1+7
  4. GOSUB @TILESTR
  5. CT=-1:FOR I=MT TO 0 STEP -1
  6. IF G$==TL$[I] THEN CT=I
  7. NEXT
  8. IF CT==-1 THEN GOSUB @SETCHR
  9. BGPUT 0,OX+X,OY+Y,CT,0,0,0
  10. NEXT:NEXT
  11. RETURN


There's some confusion here. I should have put the X1,X2,Y1,Y2 line into @TILESTR as it serves no purpose here. TL$[I] is the 64 hexadecimal string that contains the tile info. What the program does is simply set CT (Current Tile) to -1. Whenever the tile is found, CT is set to I. If the tile is not found, then CT will contain the value of -1. Then we know to store the current string (G$) into the tile array. After that, it's trivial to just set CT into the background.

Notice that I'm not doing mirror comparison. The documentation mentioned X,Y rotation. I'm ignoring that here. Actually, those aren't X,Y rotation. Those are mirror flip. If you want to you may want to modify the code to include that check. It's easy enough to do.


  1. @TILESTR
  2. G$=""
  3. FOR YY=Y1 TO Y2
  4. FOR XX=X1 TO X2
  5. G$=G$+HEX$(GSPOIT(XX,YY)%16)
  6. NEXT:NEXT
  7. RETURN


I have to make a decision here. Graphic screens can handle 256 colors. BG tiles can only handle 16 colors. What do I do? Well, if you notice, BG colors can actually have 240 colors. 256 colors-16 transparent colors. You can differentiate the different palettes, but I simply decided not to.

If you want to differentiate the palettes, then you want to store FLOOR(GSPOIT(XX,YY)/16) value and enter it into G$ so that it doesn't match equivalent pattern but different palette.

Another factor to consider is the X,Y mirror. Now you see why there's X1,X2,Y1,Y2. This is done for ease of mirroring. Normal: Y1-Y2/X1-X2. X Mirror: Y1-Y2/X2-X1. Y Mirror: Y2-Y1/X1-X2. XY Mirror: Y2-Y1/X2-X1. Note that I haven't done so, so the above is not guaranteed to work. Making it work is left as homework for the readers. ;)


  1. @SETCHR
  2. MT=MT+1:IF MT>1023 THEN MT=1023
  3. TL$[MT]=G$:CT=MT
  4. BU$="BGU"+STR$(FLOOR(MT/256))
  5. CHRSET BU$,MT%256,G$
  6. RETURN


Nothing to it. This simply increment the MT (MaxTile) counter and set the character to the appropriate BGUX bank.

And there you go. A very easy to do Background Maker. Here is the whole completed source code for your convenience. I suppose I should do the QR. Well, maybe sometimes in the future, or maybe not. Easy enough for you to do it by hand.



  1. 'BGMAKER BY HARRY HARDJONO
  2. CLS:CLEAR:DIM TL$[1024]:MT=-1
  3. @PICFILE
  4. DATA "G1","G2","G3","G6","G5","G4"
  5. RESTORE @PICFILE

  6. 'LOAD GRPS
  7. OX=0:OY=0:RX=31:RY=23:READ P$
  8. LOAD "GRP0:"+P$,FALSE
  9. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
  10. OX=32:OY=0:RX=31:RY=23:READ P$
  11. LOAD "GRP0:"+P$,FALSE
  12. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
  13. OX=0:OY=24:RX=31:RY=23:READ P$
  14. LOAD "GRP0:"+P$,FALSE
  15. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
  16. OX=32:OY=24:RX=31:RY=23:READ P$
  17. LOAD "GRP0:"+P$,FALSE
  18. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
  19. OX=0:OY=48:RX=31:RY=15:READ P$
  20. LOAD "GRP0:"+P$,FALSE
  21. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
  22. OX=32:OY=48:RX=31:RY=15:READ P$
  23. LOAD "GRP0:"+P$,FALSE
  24. BGOFS 0,OX*8,OY*8,60:GOSUB @TILER

  25. @SAVEBG
  26. SAVE "SCU0:T0"
  27. FOR I=0 TO FLOOR(MT/256)
  28. B$="BGU"+STR$(I)
  29. SAVE B$+":B"+STR$(I)
  30. NEXT 
  31. WAIT 300

  32. FOR I=0 TO 99
  33. X=RND(512):Y=RND(512)
  34. BGOFS 0,X,Y,60:WAIT 60
  35. NEXT
  36. WAIT 300

  37. BGCLR
  38. CHRINIT "BGU0"
  39. CHRINIT "BGU1"
  40. CHRINIT "BGU2"
  41. CHRINIT "BGU3"
  42. GCLS

  43. END

  44. @TILER
  45. FOR Y=0 TO RY:FOR X=0 TO RX
  46. X1=X*8:X2=X1+7:Y1=Y*8:Y2=Y1+7
  47. GOSUB @TILESTR
  48. CT=-1:FOR I=MT TO 0 STEP -1
  49. IF G$==TL$[I] THEN CT=I
  50. NEXT
  51. IF CT==-1 THEN GOSUB @SETCHR
  52. BGPUT 0,OX+X,OY+Y,CT,0,0,0
  53. NEXT:NEXT
  54. RETURN

  55. @TILESTR
  56. G$=""
  57. FOR YY=Y1 TO Y2
  58. FOR XX=X1 TO X2
  59. G$=G$+HEX$(GSPOIT(XX,YY)%16)
  60. NEXT:NEXT
  61. RETURN

  62. @SETCHR
  63. MT=MT+1:IF MT>1023 THEN MT=1023
  64. TL$[MT]=G$:CT=MT
  65. BU$="BGU"+STR$(FLOOR(MT/256))
  66. CHRSET BU$,MT%256,G$
  67. RETURN



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


Friday, August 30, 2013

Raspberry Pi Journal #15

Changing Desktop Background

I found that the default raspberry pi logo on a white background is too bright for my taste. So, I decide to change it a bit. The background or wallpaper can be easily changed by doing right click on the background itself. A set of menu will come up.

Choose Desktop Preferences. You can load any picture that you have and have it as your wallpaper.

Desktop Grub is darker color, but I've since replaced it with Saber Dollfie.

And if perchance you have a different menu, try this from command line:

pcmanfm --desktop-pref
pcmanfm -w file

Type "man pcmanfm" will tell you all the different options, and I can tell you it works!

And should you want a script to randomly pick a PNG picture to use as wallpaper, I managed to cobble this line:


PIC=$(ls *.png | shuf -n1);pcmanfm -w $PIC

And finally, here is my final script. Notice, I end the script with a specific image.


#!/bin/bash

for ((walltick=100;$walltick;walltick=$walltick-1))
do 
  PIC=$(ls wallpaper/*.jpg | shuf -n1)
  pcmanfm -w $PIC
  sleep 100
done
pcmanfm -w saber.png
sleep 5
exit 0