.AlienInAcornsville LDY alienToMove \ Set Y to the number of the alien whose turn it is to \ move towards Acornsville in this iteration of the main \ loop, which we set in UpdateAliens BMI acrn1 \ If Y is negative, then there is no alien to move, so \ jump to acrn1 to return from the subroutine LDA alienState,Y \ If the alien's state is 27, then it is heading for the CMP #27 \ town, so jump to acrn7 if this is not the case BNE acrn7 \ The alien is heading towards the town. This happens \ in three stages: first, the alien takes off; then it \ flies towards the town; and finally it descends. When \ the alien's state is 27, it is working through the \ first two stages, so that's what we do now LDA yObjectHi+33 \ If the high byte of the alien's y-coordinate >= 12, CMP #12 \ then the alien is already flying high, so jump to BCS acrn2 \ acrn2 to move it closer to Acornsville LDA yObjectLo+33 \ Otherwise add 10 to the alien's yObject coordinate, ADC #10 \ so that it rises into the air, starting with the low STA yObjectLo+33 \ bytes BCC acrn1 \ If the addition of the low bytes overflowed, increment INC yObjectHi+33 \ the high byte .acrn1 RTS \ Return from the subroutine .acrn2 \ If we get here then the alien's yObjectHi coordinate \ is 12 or more, so it's already flying high LDX #0 \ Set X = 0, to use in the following to check either the \ x-coordinate (X = 0) or the z-coordinate (X = 80) STX T \ Set T = 0, which we use to record whether we move the \ alien along the x-axis or z-axis (or both) .acrn3 \ We either do the following with X = 0 or X = 80, which \ either checks the x-coordinate or the z-coordinate \ (as zObjectHi - xObjectHi is 80) \ \ The comments are for when X = 0, which checks the \ x-coordinate LDA xObjectHi+33,X \ If the high byte of the alien's x-coordinate is zero, BEQ acrn5 \ then we have already reached the right longitude for \ Acornsville, so jump to acrn5 LDA xObjectLo+33,X \ Subtract alienSpeed from the alien's x-coordinate, SEC \ starting with the low bytes, so the alien moves SBC alienSpeed \ towards Acornsville along the x-axis at the correct STA xObjectLo+33,X \ speed (which increases with later waves) BCS acrn4 \ If the subtraction of the low bytes underflowed, DEC xObjectHi+33,X \ decrement the high byte .acrn4 LDA #1 \ Set T = 1, to record the fact that we moved the alien STA T .acrn5 CPX #80 \ If X = 80 then we have checked both axes, so jump to BEQ acrn6 \ acrn6 to check the alien's altitude LDX #80 \ Otherwise jump back to acrn3 with X = 80 to check the BNE acrn3 \ alien's latitude in its z-coordinate (this BNE is \ effectively a JMP as X is never zero) .acrn6 LDA T \ If T <> 0 then we moved the alien along at least one BNE acrn1 \ of the axes, so jump to acrn1 to return from the \ subroutine as the alien is still in the process of \ flying to Acornsville LDA #28 \ Set the alien's state to 28, so the next time we call STA alienState,Y \ this routine, we jump straight to the following .acrn7 CMP #28 \ If the alien's state is not 28, then it isn't BNE acrn1 \ attacking the town, so jump to acrn1 to return from \ the subroutine \ The alien is heading towards the town. This happens \ in three stages: first, the alien takes off; then it \ flies towards the town; and finally it descends. When \ the alien's state is 28, it is on the final stage, so \ that's what we do now LDA hitTimer \ If hitTimer is non-zero, jump to acrn1 to return from BNE acrn1 \ the subroutine, so the alien doesn't move if we \ recently hit one of its comrades LDA yObjectLo+33 \ Subtract 10 from the alien's yObject coordinate, so SEC \ that it descends towards the town, starting with the SBC #10 \ low bytes STA yObjectLo+33 BCS acrn8 \ If the subtraction of the low bytes underflowed, DEC yObjectHi+33 \ decrement the high byte .acrn8 LDA yObjectHi+33 \ If the high byte of the alien's y-coordinate is zero, BNE acrn1 \ then it is still too high to attack, so jump to acrn1 \ to return from the subroutine LDA yObjectLo+33 \ If the low byte of the alien's y-coordinate is 10 or CMP #10 \ more, then it is still too high to attack, so jump to BCS acrn1 \ acrn1 to return from the subroutine JSR PrintTooLate \ Otherwise the alien is close enough to the town to \ wreak havoc... which means it's game over, so print \ the "TOO LATE!" message in the middle of the screen LDA #90 \ Delay for 90^3 loop iterations JSR Delay JSR TerminateGame \ Terminate the game TSX \ Remove the two bytes from the top of the stack that INX \ were put there by the JSR that called this routine, so INX \ we can jump to NewGame without leaving any trace on TXS \ the stack JMP NewGame \ Jump to NewGame to start a new gameName: AlienInAcornsville [Show more] Type: Subroutine Category: The Theme Summary: Move an alien towards Acornsville and check whether it has reached it yet (and if so, end the game) Deep dive: Aliens attack Acornsville!Context: See this subroutine in context in the source code References: This subroutine is called as follows: * MainLoop (Part 4 of 15) calls AlienInAcornsville
[X]
Subroutine Delay (category: Utility routines)
Delay for a specified number of loops
[X]
Subroutine NewGame (category: Setup)
Start a new game
[X]
Subroutine PrintTooLate (category: The Theme)
Print the "TOO LATE!" message at column 6, row 7 on-screen
[X]
Subroutine TerminateGame (category: Setup)
Terminate the current game
[X]
Label acrn1 is local to this routine
[X]
Label acrn2 is local to this routine
[X]
Label acrn3 is local to this routine
[X]
Label acrn4 is local to this routine
[X]
Label acrn5 is local to this routine
[X]
Label acrn6 is local to this routine
[X]
Label acrn7 is local to this routine
[X]
Label acrn8 is local to this routine
[X]
Variable alienSpeed in workspace Main variable workspace
The speed at which the aliens move, which starts at 10 for the first wave, then 14 for the second, 18 for the third, and 22 for all subsequent waves
[X]
Variable alienState (category: The Theme)
The state of each of the eight aliens
[X]
Variable alienToMove (category: The Theme)
The number of the alien to move towards Acornsville in this iteration of the main loop
[X]
Variable hitTimer (category: The Theme)
The time since we hit an alien, so we can time its explosion
[X]
Variable xObjectHi (category: 3D geometry)
High byte of the x-coordinate for an object
[X]
Variable xObjectLo (category: 3D geometry)
Low byte of the x-coordinate for an object
[X]
Variable yObjectHi (category: 3D geometry)
High byte of the y-coordinate for an object
[X]
Variable yObjectLo (category: 3D geometry)
Low byte of the y-coordinate for an object