Inferno #03
22 ноября 2002 |
|
Gamedev - WORM-255F. Python game code size of 255 bytes with the comments.
; WORM-255F tinygame ; By Shiru Otaku/ANGEL2 18.o9.2oo2 ; Mailto: shiru@mail.ru ; This programm is a game , "Python" in size 255 bytes. ; I am sure that more can be done in short, , But at 128 bytes for all lotion And she still does not fit:). ; This version uses some ; Sub 48basic rom (poll BREAK). ; This version uses a very simplistic ; RND - on the value of the register R. ; This version has sound effects through ; Hriper (integer 2). ; In this version, the squares, rabbits blink ; (Not FLASNem). ; In this version of the serpent's head is selected ; A different color. ; Game anyway that was on the screen before it ; Run (all the attributes ; Use the same INK and PAPER). ; Management - SINCLAIR JOY. ; Exit game - BREAK KEY (IY, and stack ; Not spoil). And the maximum length of a serpent - 255. WORMBUF EQU # C16F; buffer address serpent , (So cunning because , That the least significant byte , Addresses and , Used in , Initialize the new ; Game. WORMCOL EQU # 2D; color of the snake, only ; Odd (bit 0 ; Is set). RAMKCOL EQU # 09; color edge of the field, only ; Odd. ; Go ... ORG # C000 ; Start a new game - management here , And passed at startup, and ; Restart due gamovera. START ; Clear the playing field and at the same time ; Draw within a field - it is more profitable than ; Separately clear the screen and draw ; Frame (takes less). ; Within a field, we need to make it easier ; Define touch snake edge of the field at ; Color. It will be shorter than the check ; Value of the address of the head of the serpent ; Attributes (almost doubled). ; In HL - beginning of attributes. LD HL, # 5800 ; Painted over the top edge of the color line ; The playing field. LD B, # 20 LD A, RAMKCOL LD (HL), A INC HL DJNZ $ -2 And now paint the 22 lines - the first byte ; As a land of 30 bytes to 0, the last byte ; Line too as the edge. LD B, 22 SUDA LD (HL), A INC HL ; And if the outer loop goes on DJNZ, , Then the inner (30 bytes) - to register with, , We decrement it until an ; Carry flag. LD C, # 1E LD (HL), 0 INC HL DEC C JR NZ, $ -4 LD (HL), A INC HL DJNZ SUDA , And paint the bottom line, too, the color edge ; The playing field. LD B, # 20 LD (HL), A INC HL DJNZ $ -2 ; Initialize variables play. Set The length of a serpent equal to 4 cm. Realistically it will be 3 familiarity, 4-well-because the latter ; Element of the serpent - a zero color displays, , Eliminates the need for cleaning the screen And each cycle. LD A, 4 LD (WORMLEN), A ; Writes a variable color border deuce , Red (type gamover) ; Saving bytes through the use of RRA. ; Variables are needed to color the border ; Changed with a delay, but not immediately, though. RRA LD (BORDCOL), A ; Initialize buffer serpent. Here , Used the low byte address ; Buffer (stored in the HL) to , And enter it into memory as the least significant byte , Addresses the attributes and values for the cycle ; Simultaneously. ; Cycle at the same time is longer than required , (# 71 times), but this is not essential - but , Saving two bytes per cycle without DJNZ. LD HL, WORMBUF ADD A, L LD (HL), A INC HL LD (HL), # 59 INC HL DEC A JR NZ, $ -6 ; Writes to the variable direction ; Serpent command code DEC HL (# 2B) This means , That at the start of the game snake moves to the left. LD A, # 2B LD (WORMDIR), A ; Call subroutine output of the new ; Rabbit-rabbit. Sub-used , Because "that generation of a new rabbit needs ; Twice - at the start and when eaten ; Old rabbit. CALL GENKROL ; Now begins the main game loop. MAINLOOP ; As an address variable BORDCOL , Used a direct reference to the byte ; Operand LD A, n - allows ; Save extra bytes of code ; At this point we have to actually register A ; Store is not 0, and the color of the curb. BORDCOL = $ +1 LD A, 0 ; Colors border in that color. OUT (# FE), A , And enters into a variable BORDCOL unit - The next time a blue border , (As would HALTy - eyes have time to see ; Previous color). LD A, 1 LD (BORDCOL), A And the variable KROLADR - address the current ; Rabbit in the field of attributes - as ; Keep the team (LD HL, nn). KROLADR = $ +1 LD HL, 0 And the variable color rabbit is also stored in ; Team. It is necessary to flash the rabbit - We can not use FLASH ; Why we are not cleaned properly ; Screen area, and who knows what was on the ; Garbage (because we are not going to hope that all ; The right colors are set out ; BASIC-boot:) ; Initially, this variable is # 36 ; (Green INK and PAPER) KROLCOL = $ +1 LD A, # 36 LD (HL), A ; Draw a serpent. Its length is stored in ; Team LD B, n. It must be remembered that the length of ; Serpent actually 1 less than that stored in the ; This variable - after tail snake ; Draw one more familiarity with black. WORMLEN = $ +1 LD B, 0 LD HL, WORMBUF The color of the current familiarity serpent we store in ; Register A. Initially, it is zero, , Because we will bring the snake from the end to ; Top (it will save several ; Bytes per zatiralke tail). XOR A ; Copy the stack buffer address in the snake ; IX - is shorter than the load it there ; As LD IX, nn (in bytes). PUSH HL POP IX , Series O serpent. PRWORM0 ; Take the address of the buffer the serpent in the DE (the address in ; Of attributes, no origin - of ; Them long to count). LD E, (HL) INC HL LD D, (HL) INC HL ; Colors familiarity to that address color ; From the register A. LD (DE), A ; First familiarity (tail) colored ; Zero, and now we ship in the A color ; Proper carcass serpent (the loss of velocity on ; Extra commands in the loop are not fatal - ; Size is important to us:). LD A, WORMCOL ; Simultaneously with the conclusion we move the address ; Buffer serpent to its tail - if you do ; It separately with the conclusion of the program , Becomes longer. ; Because in IX, we kept the same as in ; HL - use the relative displacement, ; Still, even at zero bias bytes ; It is spent. LD (IX-2), E LD (IX-1), D INC IX INC IX DJNZ PRWORM0 ; After DE cycle was addressed head ; Serpent - if we want to highlight it ; A different color, then do it , (But does not look very nice, so that And you can not select, and save three ; Bytes). I have highlighted included brightness XOR 64 LD (DE), A , In case if the length of a serpent, we will increase ; Copy the last address of the head thereof in ; Next cell buffer (it indicates ; HL cycle after withdrawal). LD (HL), E INC HL LD (HL), D ; Swap HL and DE, as now we need to ; Find the new address of the head of ; Attributes, and it is more convenient to do ; Register HL. EX DE, HL ; In BC, we load the # 20 - adding to ; HL and BC could get a new address , While moving down. The eldest of the couple ; (B) we do not load, because it after ; Last DJNZ zero. LD C, # 1920 ; Logical to assume that the motion ; Up we need to subtract from HL BC. ; But we did not do that, because Team ; SUB HL, BC occupies two bytes of memory And instead of a single byte command ADD HL, BC. ; The problem is not superfluous byte, but in that ; New address, we compute ; Self-modifying code, and ; Upward movement we had to-be ; Modify two bytes (for the remaining ; Place; NOP), while for other ; Areas need to modify all ; One. Therefore, we are spending on three bytes ; Boot in a couple of DE values # FFE0. ; This value is a negative number , # 20, and if we add it to the HL, then , We actually do the same thing as And if taken away would-# 20. LD DE, # FFE0 ; WORMDIR variable points to an address in ; Memory, in which we are in the control unit ; Serpent write commands INC HL / DEC HL ; / ADD HL, BC / ADD HL, DE, depending on the ; Selected direction. WORMDIR = $ NOP And now return back to the address found ; DE, as it is we still need to ; Determine whether the death of the snake on ; Different circumstances and for the record ; To the last cell buffer when the serpent ; Survive. EX DE, HL ; Breath for seven interrupt - ; It to play was real, but at the same time , So that you can behold the flashing border ; At sedenii rabbits or death serpent. LD B, 7 HALT DJNZ $ -1 ;'s Change the color of the rabbit to a new (simulating ; FLASH) XOR `om. For this variable address ; Colored rabbit to take in HL, as us his ; Back to record again. ; Old color rabbit remains in register C ; For further checks LD HL, KROLCOL LD A, (HL) LD C, A XOR 9 LD (HL), A ; Now delve into the stomach to snake ; Know, have not eaten, we someone (a For example:). To begin with we learn, was not there- ; Eaten rabbit. LD A, (DE) ; So the rabbit or not? CP C JR NZ, CHKDEAD; Not that I know ... ; Similarly, rabbit! ; Well, then, would be to lengthen the serpent and ; Generate new rabbit. ; Extend. But there is one subtlety - the length of ; Serpent given us a single byte, and ; The case if a player - a giant of a large ; Joystick, you will need to provide ; The case when the length of a serpent was equal to 255. ; To not become it after a rabbit ; Serpent-invisible:) Because after INC (HL) ; Check the overflow flag, and if something - Restore the status quo. That is, And if the serpent was a length of 255, it is so and ; Left. LD HL, WORMLEN INC (HL) JR NZ, $ +3 DEC (HL) , Continues to lengthen the serpent - will increase , A pointer to the head of the serpent in the IX for two , (Ie one character). It should be noted ; That without these teams do not , (As it might seem) - because then IX , Would indicate a head snake, and the ; Previous cell buffer, and then the serpent - ; Khan. INC IX INC IX , We will invite to the stage of a new rabbit:). CALL GENKROL ; After GENKROL in register A is always zero - ; It and puts us in a place where shavali ; Rabbit - so that when testing for death ; Serpent not to take the remains of a rabbit for ; Something else LD (DE), A ; To celebrate at the mercy of a hare ; Fun Wink green border. LD A, 4 LD (BORDCOL), A , And along with Pickney:) Since peak to another ; Reasons we will not - will not ; Make pikalku a subroutine. When ; Work pikalki important value of the register A7 - It needed to keep the color of the curb (to , While there is a beeping, he has not changed). As ; Well what it's there now and there .. ; In case C we have the duration of Picanya ; (Outer loop). LD C, 70 ; To make a sound - it is necessary to change the value , The fourth bit in the port # FE from 0 to 1 XOR 16 OUT (# FE), A ; Make the delay between a change in values ; Bit, and length of the current value ; The outer loop, it was not just to , Peak, and with the change of frequency. LD B, C DJNZ $ DEC C JR NZ, $ -8 And now check, not whether we are dead- ; Chance (bite himself / edge of the screen). ; Check is most convenient using ; Team RRA (one byte), because the colors of the whole ; Tasteless - the walls, all sorts of snakes - we have , Is given an odd number. Accordingly, ; Of these items after the RRA established ; Carry flag. If we ate the rabbit - ; Flag is not established, because in pikalke we ; Entered on the register A is an even number , (Thrown down from least significant bit). CHKDEAD , Died? .. RRA JR NC, NODEAD; No !:).. ; Oops ... My zhuby ... ; On this occasion, to bring sound ; Breaking of teeth). It is similar to ; Previous pikalke, only the sound is ; Not a pure tone and noisy - we will ; Change the value of the fourth bit according ; From what we find in the register R. And do not ; Forget the junior three-bit register A ; To keep the number of 2 - red border. LD C, 250 LD A, R AND 18 OR 2 OUT (# FE), A LD B, C DJNZ $ DEC C JR NZ, $ -12 ; ... And the sound of crumbling jaw ; Run the game again ... JP START ; And if we do not podohli up to this point - , Then we need to write the new address of the head ; Snake at the end of the buffer. Only we must remember , That after a cycle output pointer to the address , (The one in the IX - HL we have long zaportili) , 2 more than necessary (INC'i something ; Perform), and if we are on elongated ; Eaten rabbit - then the value of IX as , Just what is necessary, but separate ; Zapisyvalki address will eat the extra bytes. ; In general, puts DE not to IX, and address , Is 2 less than (specify a relative ; Displacement). NODEAD LD (IX-1), D LD (IX-2), E ; Now we posproshat keyboard, rather than ; Poke-a player in her hard blunt ; Objects? .. Especially in the area of key , 67,890 (Sinclair joy)? .. LD BC, # EFFE ; In HL have addressed WORMDIR - byte to ; Be modified depending on the ; Poking a player (or leave unchanged, ; If the user is tired to engage in this nonsense). LD HL, WORMDIR IN A, (C) ; Check bits poluryada 67,890 will not ; By BIT n, r, and not with CP n - , Both of these methods guzzle two bytes, and we ; Need to check all poluryad. Because ; Advantageous to use single-byte ; Command RRA (check would have to in order ; Repetition bit keys, and once bit ; To miss - the 0 key, it is our ; Not used). RRA RRA JR C, $ +4 ; Button is pressed up (9) - fills in ; WORMDIR opcode ADD HL, DE LD (HL), # 19 RRA JR C, $ +4 ; Button is pressed down (8) - record in the ; WORMDIR opcode ADD HL, BC LD (HL), # 09 RRA JR C, $ +4 , Pressed the right button (7) - fills in ; WORMDIR opcode INC HL LD (HL), # 23 RRA JR C, $ +4 ; Button is pressed to the left (6) - fills in ; WORMDIR opcode DEC HL LD (HL), # 2B ; At last check, has not pressed, whether the player ; BREAK (and there are such grief:). ; Check by a reputable ; Routines ROM BASIC48. If not pressed - , Leaving the cycle, if you click ... CALL # 1F54 JP C, MAINLOOP ; ... Well, if pressed, and Song for saying good ... RET ; It - subroutine address generation ; Rabbits, she enters discharge the KROLADR, ; She checks - no, if anything bad things , Where the rabbit was going to appear, and so on. ; RND used very bad, just ; On the register R, but for this game is ; Justified (other ways to gobble up a lot , B). ; Algorithm for generating addresses in the right ; Range is also very stupid (but plows). GENKROL ; First Generate byte address - it , Must be within # 58 .. # 5A. ; This can be achieved, to generate the number of , 0 .. 2 (AND `th, and if you came out 3 - is ; Generate again). Then add to ; This number # 58, and receive byte ; Address. LD A, R LD H, 3 AND H CP H JR Z, GENKROL ADD A, # 58 LD H, A ; Now generate low byte address , Its value may be within , # 00 .. # FF, so that no additional ; Perversion is not required. LD A, R LD L, A , Check that the attribute is in ; Generated address, and if it is a serpent , Or the edge of the field - are generating the address again. LD A, (HL) CP 0 JR NZ, GENKROL ; Writes the generated address in the ; Variable KROLADR. LD (KROLADR), HL ; As you can see, the output from this ; Subroutine register A is always zero - This property routines used ; Above. RET ; That's all.
Other articles:
Similar articles:
В этот день... 21 November