!0.......^.........^.........^.. !B !P\H11\H07\H10\H00 T A K E A RUNNING J U M P !0.......^.........^.........^.. !2.......^.........^.........^.........^.........^.........^.... !P\H11\H02\H10\H00 If you've marvelled at the manic figures zipping around your video screen - then wonder no longer at how it's done. Improve your own arcade games by leaps and bounds with this zappy little routine from machine code maestro, Toni Baker. !1.......^.........^.........^.........^........ The object of this issue's exercise is to present a machine code routine which may be used in Basic programs for a wide variety of totally pointless - but fun - games. With it in operation you see a little figure on the screen, that at the touch of the left or right cursor keys, will happily run along the screen in a horizontal straight line. A touch of the up cursor key and the mischevious character will jump into the air and land back down again! Don't ask me what for - you're the programmer! A few simple PEEKs are all that are needed to tell the program where the figure is, and a couple of POKEs will move our friend to any part of the screen. Therefore, the Basic is totally interactive with the machine code. It is quite feasible to make some kind of video game out of this routine using no other machine code at all - so, if you feel that particular area is beyond you at present, there's still plenty for you to do in the meantime. !0.......^.........^.........^.. !B WHAT'S THE POINT !1.......^.........^.........^.........^........ If you don't understand machine code then a couple of points are worthy of explanation. The left-hand column of the machine code listing is all you need to feed into the computer. The other columns are just there to give additional information. However, you'll notice there are a couple of question marks in the left-hand column of the listing which are not part of the machine code; they are a warning that these are addresses which you'll need to work out for yourself. For instance, where it says CD???? in the left- hand column, and CALL FIGURE in the middle column, it means that the question marks in the left-hand column should be replaced by the address of the line of machine code which has the label FIGURE written in the second column. This address must be in hexa- decimal, and with the two component bytes in reverse order - so, for instance, if the line labelled FIGURE ends up at address 24962 then the question marks in the line CALL FIGURE will need to be replaced by 8261, since 24962 (decimal) equals 6182 (hex). If you start the program as a whole at address 6000 (hex) then this will actually be the case. The routine makes use of a couple of addresses in the system variables, which you can PEEK and POKE in Basic in order to make the game do whatever you want. These are: !2.......^.........^.........^.........^.........^.........^.... 23726 The y coordinate of the horizontal line along which the figure runs. 23727 The x coordinate of the figure. 23729 The height of the figure above the ground. !1.......^.........^.........^.........^........ In addition, there are three more addresses which you should not use in the course of the game, but which nonetheless must be POKEd with certain initial values before the game starts. These are: !2.......^.........^.........^.........^.........^.........^.... 23724 As 23726 initially 23725 As 23727 initially 23728 Must be POKEd with zero initially. (Note also that 23729 must also be POKEd with zero initially) !1.......^.........^.........^.........^........ Figure 1 shows the bare essentials of such a games. Lines 10, 20 and 50 POKE the initial values required into the system variables, but these addresses needn't be accessed again in Basic. Lines 30, 40 and 60 POKE the variables which are allowed to PEEK and POKE throughout the game. Lines 70 and 80 are 'the game'. The machine code will only move the figure by one square, if at all, and so between these moves you can put whatever you want, and this will define the game. !2.......^.........^.........^.........^.........^.........^.... !B !P\H11\H07\H10\H00\H13\H00 ------------------------------------------------ 10 POKE 23724,16 y coordinate of the figure. 20 POKE 23725,0 x coordinate of the figure. 30 POKE 23726,16 As line 10. 40 POKE 23727,0 As line 20. 50 POKE 23728,0 Reset all flags in A_FLAGS. 60 POKE 23729,0 Reset the height above the ground. 70 RANDOMIZE USR ????? This refers to the label START in the program. 80 GO TO 70 Figure 1. The bare essentials of a game, so that you can see the potential of the code in action. ------------------------------------------------ !0.......^.........^.........^.. !B TRICK OR THREAT? !1.......^.........^.........^.........^........ There is one other trick worth knowing about (one that in a way contradicts what was said earlier). The following routine will STOP the figure from falling down again after jumping and should be called whilst the player is in mid-air: !0.......^.........^.........^.. 1000 POKE 23726,PEEK 23724 1010 POKE 23728,0 1020 POKE 23729,0 1030 RETURN !1.......^.........^.........^.........^........ Happy programming - may silliness reign supreme! !0.......^.........^.........^.. !P\H11\H07\H10\H00\H13\H00 !B MOVING GRAPHICS DATA !2.......^.........^.........^.........^.........^.........^.... !P\H11\H07\H10\H00\H13\H00 FIGURES DEFB 00 01 03 03 01 00 01 3F Data for the left facing figure (1) DEFB 00 80 C0 C0 80 80 C0 E0 DEFB 01 02 02 04 04 38 00 00 DEFB 80 40 20 10 08 38 00 00 DEFB 00 01 03 03 01 00 01 3F Data for the left facing figure (2) DEFB 00 80 C0 C0 80 80 C0 E0 DEFB 01 01 02 04 0F 00 03 00 DEFB 80 80 80 80 F0 90 90 00 DEFB 00 01 13 0B 05 02 01 01 Data for the left facing figure DEFB 00 80 C0 C0 80 80 C0 A0 jumping. DEFB 01 01 02 04 02 01 03 00 DEFB C0 80 40 40 40 40 C0 00 DEFB 00 00 00 00 00 00 00 00 Data for the blank space. DEFB 00 00 00 00 00 00 00 00 DEFB 00 00 00 00 00 00 00 00 DEFB 00 00 00 00 00 00 00 00 DEFB 00 01 03 03 01 01 03 07 Date for the right facing figure(1) DEFB 00 80 C0 C0 80 00 80 FC DEFB 01 02 04 08 10 1C 00 00 DEFB 80 40 40 20 20 1C 00 00 DEFB 00 01 03 03 01 01 03 07 Date for the right facing figure(2) DEFB 00 80 C0 C0 80 00 80 FC DEFB 01 01 01 01 0F 09 09 00 DEFB 80 80 40 20 F0 00 C0 00 DEFB 00 01 03 03 01 01 03 05 Data for the right facing figure DEFB 00 80 C8 D0 A0 40 80 80 jumping. DEFB 03 01 02 02 02 02 03 00 DEFB 80 80 40 20 40 80 C0 00 This data should be entered at the end of the main machine code listing. You should then replace the question marks at the LD BC,FIGURES routine to point to the beginning of the moving graphics data. !2.......^.........^.........^.........^.........^.........^.... !B Machine code Assembler Comments ---------.---------.------------------.------------------------- FD5676 START LD D,(A_FLAGS) CBA2 RES 4,D Signal "No move made yet" CB4A BIT 1,D Is the figure jumping? 2033 JR NZ,UP/DOWN Go straight to 'up/down' routine if so 3EF7 LD A,#F7 Note: bit 3 reset DBFE IN A,(#FE) Scan segment 3 of the keyboard E610 AND #10 A:=00 if key '5' pressed, 10 otherwise 47 LD B,A B:=00 if key '5' pressed, 10 otherwise 3EEF LD A,#EF Note: bit 4 reset DBFE IN A,(#FE) Scan segment 4 of the keyboard E604 AND #04 A:=00 if key '8' pressed, 04 otherwise. B0 OR B A:=00 if both key '5'&'8' pressed; 04 if key '5' pressed, but not key '8'; 10 if key '8' pressed, but not key '5'; 14 if neither key pressed FE04 CP #04 Test for key '5' only 2814 JR Z,LEFT Move figure left if so FE10 CP #10 Test for key '8' only 201D JR NZ,UP/DOWN Go straight to 'up/down' routine unless pressed ---------.---------.------------------.------------------------- CBD2 RIGHT SET 2,D Signal figure facing right 3AAF5C LD A,(POS_X) A:=current x coordinate FE1E CP #1E Are we at the right of the screen? 2814 JR Z,UP/DOWN Exit if so FD3475 INC (POS_X) Otherwise change x coordinate CBE2 SET 4,D Signal figure has moved 180D JR UP/DOWN Go to the JUMP key test ---------.---------.------------------.------------------------- CB92 LEFT RES 2,D Signal figure facing left 3AAF5C LD A,(POS_X) A:=x coordinate of the figure A7 AND A Is it at left of screen? 2805 JR Z,UP/DOWN Exit if so FD3575 DEC (POS_X) Otherwise change x coordinate CBE2 SET 4,D Signal figure has moved ---------.---------.------------------.------------------------- 3EEF UP/DOWN LD A,#EF Note: bit 4 reset DBFE IN A,(#FE) Scan segment 4 of the keyboard E608 AND #08 A:=00 if key '7' pressed, 08 otherwise 2804 JR Z,J_PRESSED Jump if the JUMP key pressed CBAA RES 5,D Signal JUMP key not pressed 180A JR JUMP_TEST ---------.---------.------------------.------------------------- CB6A J_PRESSED BIT 5,D Was the key already pressed? CBEA SET 5,D Signal JUMP key pressed 2004 JR NZ,JUMP_TEST Exit unless key is pressed for the first time CBCA SET 1,D Signal figure is jumping CBB2 RES 6,D Signal figure moving upwards ---------.---------.------------------.------------------------- CB4A JUMP_TEST BIT 1,D Is the figure jumping? 281C JR Z,PRINTOUT Exit if not CBE2 SET 4,D Signal figure has moved CB72 BIT 6,D Is the figure moving up or down? 2809 JR Z,UP ---------.---------.------------------.------------------------- FD3577 DOWN DEC (JUMP_HGHT) Decrease the height above the ground 2011 JR NZ,PRINTOUT Jump unless the figure has reached the ground CB8A RES 1,D Signal figure no longer jumping 180D JR PRINTOUT ---------.---------.------------------.------------------------- 3AB15C UP LD A,(JUMP_HGHT) A:=height above the ground 3C INC A A:=new height 32B15C LD (JUMP_HGHT),A Store the new height FE04 CP #04 Are we at top of a jump? 2002 JR NZ,PRINTOUT Exit if not CBF2 SET 6,D Signal figure now moving downwards ---------.---------.------------------.------------------------- FD360200 PRINTOUT LD (TVFLAG),#00 Direct PRINT to upper part of screen 7A LD A,D A: contains all the flags CB4A BIT 1,D Is the figure jumping? 2804 JR Z,XXX E6FE AND #FE If so, then reset the animation flag 1802 JR YYY ---------.---------.------------------.------------------------- EE01 XXX XOR #01 Otherwise complement the animation flag ---------.---------.------------------.------------------------- 32B05C YYY LD (A_FLAGS),A Store amended flags CB62 BIT 4,D Has the figure moved? C8 RET Z Return if not ED5BAC5C LD DE,(LAST_POS) DE:=previous coordinates of the figure 3E03 LD A,#03 A:=code for a blank space CDA260 CALL FIGURE Print a blank space, overwriting prev figure ED5BAE5C LD DE,(POS) DE:=present position (with the figure assumed to be on the ground) 7B LD A,E A:=y coordinate of the ground FD9677 SUB (JUMP_HGHT) A:=true y coordinate of the figure 5F LD E,A DE:=true current coordinates ED53AC5C LD (LAST_POS),DE Store these coordinates 3AB05C LD A,(A_FLAGS) A: contains all the flags E607 AND #07 A:=code for the approp'ate figure to print ---------.---------.------------------.------------------------- 6F FIGURE LD L,A 2600 LD H,#00 HL:=code for the figure to print 29 ADD HL,HL 29 ADD HL,HL 29 ADD HL,HL 29 ADD HL,HL 29 ADD HL,HL Multiply by 20h 01C760 LD BC,FIGURES BC:=points to the data for the figures 09 ADD HL,BC HL:=points to the data for a specified figure 010800 LD BC,#0008 BC:=number of bytes per character square CD0000 CALL HALF_FIG Print top half of figure 1C INC E Change y coordinate in order to print bottom half of the figure ---------.---------.------------------.------------------------- 3E16 HALF_FIG LD A,"at control" D7 RST #10 PRINT AT 7B LD A,E D7 RST #10 E 7A LD A,D D7 RST #10 D CD0000 CALL FIG_SQ Print one character square of the figure. (Now to print the 2nd char square) ---------.---------.------------------.------------------------- 227B5C FIG_SQ LD (UDG),HL USR "A" points to the data to print 3E90 LD A,"Graphic A" D7 RST #10 Print one character of the figure 09 ADD HL,BC Point HL to the data for the next square C9 RET End of the routine !1.......^.........^.........^.........^........ !B -- from Your Spectrum #2 (Mar.1984) -- !$