THE GRAPHICS – THE SPRITES (4TH PART)

da | Jan 15, 2024 | MSX BASIC | 0 comments

Here we are at the most important part of the graphics, the one you were waiting for with enthusiasm! How to draw objects or characters on the screen and then make them move: THE SPRITES. Before starting the explanation, let's remember what I said when I explained the SCREEN. Remember that the 2nd option was to define the size of the sprites. I repeat; if we want to work with 8X8 sprites, we will use this option with 0 or 1, that is, the first with normal dimensions and the second doubled. Instead, option 2 and 3 applies to the creation of 16X16 sprites, 2 normal, 3 doubled. I quickly explain the characteristics and their behavior. The sprites can be used in screens 1,2 and 3 and they all have the same functionality, no matter the division into pixels of the screen, they work on planes outside of the latter. We can create 256 8X8 sprites (0-255) and 64 16X16 sprites (0-63). But on both the first and second methods that we will use, we can only display 32. This is because each sprite has its own plane and we cannot insert more than one, if we do so, the second one called will cover the first. The sprite planes are numbered from 0 to 31 and the one with the lowest value will have priority over all the others that come after it. To make you understand better, if a sprite with plane 0 overlaps another with a value of one, the second will be hidden. A big flaw of MSX1 is that we cannot use more than 4 sprites on the same line, if we do, all those with higher floor numbers will not be displayed. In conclusion, we can use as many as 32 sprites on the screen but no more than 4 on the same horizontal line. Each plane has a grid of 256X192 pixels, this means that we can position our sprite on one of the points mentioned. If we exceed 255 horizontal pixels, the sprite will appear on the other side of the screen, while vertically, exceeding the 191st pixel we will no longer see it, because even if we see 192 pixels vertically, there are another 64 hidden, in reality the grid it is 256X256. Let's remember that when we use the doubled sprites, the 8X8 one will become 16 pixels, while the 16X16 one will be 32, so be careful when calculating collisions. To create a sprite the MSX has an appropriate command, called SPRITE$. When we set the usual parameter on the SCREEN, depending on what we have put, it will create a matrix of 8X8 or 16X16. Each sprite will have its own identification number. The first will be SPRITE$(0), the second SPRITE$(1) and so on. Let's start by understanding how to load the 8X8 matrix of the sprite. The codes to be entered inside it are from right to left, there are eight lines made up of 8 boxes. If we put 1 in the box the pixel will be turned on, if vice versa it is zero it will be turned off. The codes will be loaded into an alphanumeric variable via the CHR$ function and can be decimal, binary, hexadecimal or even octal. I'll make a small diagram to be more understandable, the face of a Martian.

8 7 6 5 4 3 2 1
|1|0|0|0|0|0|0|1| 1
|0|1|0|1|1|0|1|0| 2
|0|0|1|1|1|1|0|0| 3
|0|1|1|1|1|1|1|0| 4
|1|1|0|1|1|0|1|1| 5
|1|1|1|1|1|1|1|1| 6
|0|1|1|1|1|1|1|0| 7
|0|0|1|1|1|1|0|0| 8

10 SCREEN 2.0: COLOR 15.1.1: CLS: ' We activate the graphic screen with 8X8 sprite grid
20 S$=”” :' We initialize the alphanumeric variable. We will need it to load the data
30 FOR I=0 TO 7:' Loop to insert data into the variable
40 READ S: S$=S$+CHR$(S) : ' Reads a piece of data and puts it into the variable with CHR$
50 NEXT I: SPRITE$(0)=S$: ' Close the loop and create sprite number 0 with the data from the S$ variable.
60 DATA 129,90,60,126,219,255,126,60: ' Sprite data in decimal (but could be binary or hexadecimal)

Our sprite has been created. Now we can display it on the screen with the PUTSPRITE command. Its syntax is: PUTSPRITE FloorNumber, (Oriz.Coord., Vert.Coord.), ColorCode, SpriteNumber. Numeropiano, as you already know, is the sprite viewing page. Horizontal and vertical coordinates are the point where we want our sprite to appear. Color code is the color we want to give it. We can use the available colors of the MSX, i.e. 16 (0 to 15). If we give the code 0 the sprite will be invisible. The sprite can only have one color. And finally Sprite number is what we give when it is created with SPRITE$, in our case it is zero. Now let's make the sprite appear in the center of the screen, adding a line to our program.


70 PUTSPRITE 0,(124,92),15,0:' It will appear at the given coordinates with the color white(15)
80 GOTO 80
To create a 16X16 sprite instead, MSX uses 4 8X8 grids. The loading sequence is: the first 8 top left, the second bottom left, the third top right and the last bottom right. And exactly like this:
1 3
2 4
Since I drew a Martian earlier, now we'll make his spaceship.

 1                3
----------------------------------
|0|0|0|0|1|1|1|1||1|1|1|1|0|0|0|0| |0|0|0|1|1|1|1|1||1|1|1|1|1|0|0|0|
|0|0|0|1|1|1|0|0||0|0|1|1|1|0|0|0|
|0|0|0|1|1|0|0|0||0|0|0|1|1|0|0|0|
|0|0|0|1|1|0|0|0||0|0|0|1|1|0|0|0| |0|0|0|1|1|0|0|0||0|0|0|1|1|0|0|0| |0|1|1|1|1|1|0|0||0|0|1|1|1|1|1|0|
|1|1|1|1|1|1|1|1||1|1|1|1|1|1|1|1|
----------------------------------
|1|0|1|1|1|1|1|1||1|1|1|1|1|1|0|1|
|1|1|1|1|1|1|1|1||1|1|1|1|1|1|1|1|
|0|1|1|0|1|1|1|1||1|1|1|1|0|1|1|0|
|0|1|1|1|1|1|0|1||1|0|1|1|1|1|1|0|
|0|0|1|1|1|1|1|1||1|1|1|1|1|1|0|0|
|0|0|0|1|1|0|0|0||0|0|0|1|1|0|0|0|
|0|0|1|1|0|0|0|0||0|0|0|0|1|1|0|0|
|0|1|1|0|0|0|0|0||0|0|0|0|0|1|1|0|
----------------------------------
2 4

Now, let's vary the program made above, so that it can read the 16X16 sprite
10 SCREEN 2,2: COLOR 15,1,1: CLS: ' As above but with 16X16 sprite grid
20 S$=””
30 FOR I=0 TO 31:' This time the data to read will be 32
40 READ S: S$=S$+CHR$(S)
50 NEXT I: SPRITE$(0)=S$
55' *** Spaceship Data ***
60 DATE 15,31,28,24,24,24,124,255
70 DATA 191,255,111,125,63,24,48,96
80 DATA 240,248,56,24,24,24,62,255
90 DATA 253,255,246,190,252,24,12,6
100 PUTSPRITE 0,(124,92),15,0
110 GOTO 110


As you have seen, little changes, instead of reading 8 data, it reads 32, everything else remains the same.
Let's make our object move on the screen. This applies to both programs.
20 S$=””: X=124: Y=92: ' Let's modify this line by adding the sprite movement variables
100 I$=INKEY$: IF I$=”” THEN 100:' The INKEY$ function has been activated. It stays on this line if no key is pressed

110 IF I$=”W” OR I$=”w” THEN Y=Y-1:' Move the sprite 1 pixel up
120 IF I$=”S” OR I$=”s” THEN Y=Y+1:' Move the sprite 1 pixel down
130 IF I$=”Q” OR I$=”q” THEN X=X-1:' Move the sprite 1 pixel to the left
140 IF I$=”E” OR I$=”e” THEN X=X+1:' Move the sprite 1 pixel to the right
150 PUTSPRITE 0,(X,Y),15,0:' Having the variables, the command will make the sprite appear at the indicated points. This happens so quickly that we will only notice the movement of the latter.


160 GOTO 100: Postpones execution to 100, where it will wait for a key press.


This method is just to help you understand how to move a sprite. Later I will explain how to do this with the cursor keys.
Since I went on too long, I'll continue in the next episode. I'll talk about collisions, movement with the cursor keys (as mentioned above) and some other tricks you'll need to start creating small games.
Thanks to everyone and see you next time.

0 Comments

Submit a Comment

Your email address will not be published. I campi obbligatori sono contrassegnati *

LEZIONE 26 – INTERVAL ON, RND ED ALTRI

LESSON 26 – INTERVAL ON, RND AND OTHERS

Good morning everyone, In this episode I will talk about an important interrupt, especially for games, when we want to make an enemy suddenly appear or a useful object, but also to create a stopwatch or a clock and many other things. The COMMAND exploits the...

LEZIONE 26 – INTERVAL ON, RND ED ALTRI

LESSON 24 – THE DATA MANAGED BY THE MSX (PART 3)

Kind regards to everyone. Today we will close this topic, I will talk about direct or random access files. But first, as I mentioned in the last episode, I will give a small explanation about the program. First of all, the first thing to say is that I used a command which...