A SPRITE is a character which can move freely about the screen, independently of any other characters, and no matter what the program happens to be doing at the time. It can move smoothly on and off the screen and pass in front of or behind other characters without disturbing them. They may be controlled either by hardware, software or a mixture of both. As you can imagine, a sprite is a handy thing to have around when writing games programs, but unfortunately the Amstrad does not support them. However don't despair, it is possible to simulate many of their properties. The best way would be to use machine code, which is extremely fast and would allow several sprites to be on the screen at once. Main problem there is that machine code is very difficult and time-consuming to write. Can it be done in Basic? Well if you only want one or two sprites the answer is yes. We can use Basic interrupts to jump to a subroutine at a specified interval and print the character at the graphics cursor, exclusively ORing it with the background so that anything else on the screen is not altered in any way when the sprite is subsequently removed. The XOR graphic option is set by printing one of the control characters, (see chapter 9 in the manual). After executing: PRINT CHR$ (23) ;CHR$ (1)the actual colour displayed of any point plotted or drawn is calculated by exclusively ORing the PEN number of the point with the PEN number of the background. Then:PRINT CHR$ (23) ;CHR$ (0)restores normal operation.To understand how this works and to be able to predict the resulting colour, we need to know a bit about binary. Our Amstrad can help us here, being an expert on the matter. The 16 colour mode 0 is really the only suitable one for sprites so we will concentrate on it. The pens are numbered 0 to 1 5 and in binary we need four digits or bits. To find out what the binary equivalent of a number is simply ask your Amstrad! PRINT BINS (number,4) where number' is between 0 and 15. XOR is a logical operator treating its arguments as bit patterns. To find out what XOR does ask the expert: PRINT 0 XOR 0 - gives 0 PRINT 0 XOR 1 - gives 1 PRINT 1 XOR 0 - gives 1 PRINT 1 XOR 1 - gives 0So to exclusively OR two numbers compare each bit and write down the result as above.What is 8 XOR 5? Old know-all will tell you it is 13. Is it right? 8........................1000 5........................0101 8 XOR 5.............1101To find out what 1101 is:PRINT &X1101 - gives 13So if you plot a point with graphics PEN 8 on top of a point with PEN 5 usinq the XOR option the result is PEN 13.What happens if you plot the point again with PEN 8? 13......................1101 8........................1000 13 XOR 8...........0101The original colour is restored. 0101 is 5.We can now predict the colour obtained when using the XOR option. After that struggle it's all downhill now - the rest is fairly straightforward. TAG will enable characters to be printed at the graphics cursor using graphics commands to position and colour them. By selecting our pens and inks carefully a character can appear to pass in front of or behind another character. If a ball is printed using PEN 8 on top of an object printed using PEN 5 the resulting colour is 13. If PEN 13 is set to the same colour as PEN 5 you cannot see the ball, so it appears to be behind the object. But if PEN 13 is set to the same colour as PEN 8 the ball can be seen, so it appears to be in front of the object. Program 1 shows how to move a ball across the screen, passing in front of and behind alternate stripes. First the colours are set. then seven stripes are printed using pens 1 to 7. The XOR option is set, the text and graphics cursors joined and the main loop entered. Sprites should move independently of the other characters and the program in general once set moving. To do this we need to use interrupts (see chapter 10 in the manual). An interrupt is a signal sent to the processor telling it to drop whatever it is doing and go and do something else, returning when it has finished. Four timers are available for use by the programmer. The timers are pretty fast, ticking away at 50 times a second. If we write our subroutine to move our sprite at line 1000. then we can say: EVERY 10 GOSUB 1000This causes the Amstrad to carry out the subroutine five times a second or every fifth of a second, no matter what it is doing. The delay can be any value allowing the sprite to be moved at any speed.Watch out if you are using very short delays as so much time may be spent moving the sprite that there isn't enough time to carry on with the rest of the program. Program II,shows how we can combine interrupts with our sprite moving routine. Program II: How it works : - 20 Sets the mode and border.
- 30-50 Sets the ink colours.
- 60-110 Prints the coloured stripes.
- 120 Sets the start position and direction.
- 130 Sets the colour.
- 140 Sets the XOR graphic option.
- 150 Enables the interrupt routine.
- 160-170 Defines the messages.
- 180-250 Scrolls the messages in opposite directions. 290 Joins the graphics and text cursor.
- 300-310 Works out the new coordinates.
- 320 Erases the old ball.
- 330 Prints the new ball.
- 340 Separates the cursors, updates position.
R.A.WADDILOVE, CWTA
| ★ AMSTRAD CPC ★ DOWNLOAD ★ |
|
CPCrulez[Content Management System] v8.732-desktop/c Page créée en 170 millisecondes et consultée 1560 foisL'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko. |
|
|