######################################################################
Program  : Child's Play
Author   : David A. Gershman
 Email   : dagershman { a t } dagertech { d 0 t } net
 Twitter : @dagershman
License  : CCA 4.0 (Creative Commons Attribution 4.0 Int. License)
         : To view a copy of this license, visit
         : http://creativecommons.org/licenses/by/4.0/ or send a letter to
         : Creative Commons, PO Box 1866, Mountain View
	 : CA 94042, USA.
	 :
	 : ( I do this to ensure the code can be used/shared among
	 :   *anyone* for any reason.  If this is an issue for the
	 :   contest, please let me know. )
######################################################################

######################################################################
# Background
######################################################################

Welcome to Earth's Battle Armada Squadron Intercept Coalition
(BASIC).  You have been selected to defend Earth's outer air-space
against the most vile, childish, and underhanded of enemies...
the "Copy Catter Dirty Ratters"!!

The Copy Catters not only mimic your every move, but have also hacked
Earth's cyber systems and cloned our ships!  As a member of the elite
C=64 enforcers, you have been chosen to fly one of our
state-of-the-art starships out in front of the Armada to battle these
enemies.

However, they *are* Copy Catter Dirty Ratters!!!  If *you* move left,
*they* move left.  If *you* move right, *they* move right.  And,
because they've cloned our starship technology, you must fly a ship
without weapons and without defenses.  This will prevent them from
firing upon you AND prevent them from defending against Armada photon
torpedos.  HOWEVER, you too will be vulnerable to Armada torpedos!

Your mission is fly in front of the Armada and navigate left and
right which your enemy will mimic.  The Armada will be firing their
photon torpedos behind you.  You need to maneuver out of the way of
torpedos but then maneuver back so your enemy gets hit!

Once your enemy is hit 15 times, they are destroyed and you win the
day!  But be careful...if *you* get hit 15 times, *you* will be
destroyed and all life on Earth will be infinitely replicated!

######################################################################
# Submission Contents
######################################################################

  - README.txt             : This instruction file.
  - childs_play.basicv2    : ASCII-based source code fed into VICE's 'petcat'
  - childs_play_src.png    : Screen shot of the source code within VICE
                             with abbreviations showing 80column limits
  - childs_play.prg        : VICE emulator's "petcat" utility's output
  - childs_play.d64        : 1541 floppy disk image containing
                             the game named as "CHILDS PLAY"

######################################################################
# Emulator Usage/Loading/Execution
######################################################################

"Child's Play" was developed on a Linux host using the VICE C64 Emulator
('x64' binary).  To execute the program, it is presumed the reader/user
has knowledge of VICE and .prg/.d64 files.

To run the program, the reader/user may mount the provided .d64 file
using the VICE emulator, or drag-n-drop the .prg file onto an already
running "x64" VICE emulation window.

If drag-n-drop is used, VICE will automatically load and run the
program.

If using the .d64 floppy disk image, after mounting the disk, issue
the following commands:

    load "*",8 <ENTER>

followed by

    run <ENTER>

######################################################################
# Play Instructions
######################################################################

Upon executing the program, two "ships" will soon be displayed.  Use
the 'j' and 'k' keys to move the ships left and right, respectively.
You are the ship on the "bottom" with Armada torpedos being fired up
the screen.

When a torpedo hits a ship, it changes color.  After 15 hits (15 color
changes), the corresponding ship is destroyed (turns black) and the
game ends.

Winning the game requires the player to get the top ship (Copy
Catters) destroyed first.

######################################################################
# Program Breakdown
######################################################################

Line 0 : for i = 0 to 24:print tab(9)"{white}B"tab(30)"{white}B":next:v=53248:m=v+39:n=m+1:pokem-7,0:pokem-6,0:s=12288

     # Print the initial side-bars and clear the screen at the same time.
     for i = 0 to 24
       print tab(9)"{white}B"tab(30)"{white}B"
     next
     
     v=53248    # Initialize 'v' to start of Sprite RAM
     m=v+39     # Set m to Sprite 0's color register
     n=m+1      # Set n to Sprite 1's color register

     poke m-7,0  # Set border color to black (m-7 == v+39-7 == 52348 + 39 - 7 == 52380)
     poke m-6,0  # Set background color to black (m-6 ==> 53281)
     s = 12288   # Set 's' to location for Sprite data

Line 1 : pokem,1:poken,1:gosub8:poke2040,192:poke2041,193:pokev,100:pokev+1,84:pokev+2,100:pokev+3,132

     poke m, 1      # Set Sprite 0's color to white
     poke n, 1      # Set Sprite 1's color to white
     gosub 8        # Gosub routine to "build_sprites"
     poke 2040,192  # Set Sprite 0's pointer for 12288
     poke 2041,193  # Set Sprite 1's pointer for 12288 + 64
     
     poke v, 100    # Set Sprite 0 and 1's X, Y starting positions
     poke v+1, 84
     poke v+2, 100
     poke v+3, 132

Line 2 : geta$:u=peek(v):u=u-(a$="k")*8:u=u+(a$="j")*8:pokev,u:pokev+2,u:gosub7

     get a$           # Get a key if pressed
     u=peek(v)        # Get sprite's X coord.
     u=u-(a$="k")*8   # If 'k' was pressed, calculate position 8 pixels right
     u=u+(a$="j")*8   # If 'j' was pressed, calculate position 8 pixels left
     poke v,u         # Set Sprite 0's new X position
     poke v+2,u       # Set Sprite 1's new X position
     gosub 7          # Scroll the screen 

Line 3 : c=peek(v+31):o=peek(m):p=peek(n):o=(o-(c=1orc=3))and15:p=(p-(c>1))and15:pokem,o:poken,p

     c=peek(v+31)             # v+31==53279, C=64 Sprite-to-Background collision register
     o=peek(m)                # Get Sprite 0's color
     p=peek(n)                # Get Sprite 1's color
     o=(o-(c=1orc=3))and15    # Increase Sprite 0's color if collision with "torpedo"
     p=(p-(c>1))and15         # Increase Sprite 1's color if collision with "torpedo"
     poke m,o                 # Set Sprite 0's new color
     poke n,p                 # Set Sprite 1's new color

Line 4 : ifo=0thenprint"{clr}{white}game over, you win!":print"you killed the combatant!":end
Line 5 : ifp=0thenprint"{clr}{white}game over, you lost.":print"you got killed.":end

     # (above code)
     # Check each sprite and if a sprite's color is black, it was
     #   destroyed.  Notify user and end.

Line 6 : u=u-(112-u)*(u<112):u=u+(u-245)*(u>245):pokev,u:pokev+2,u:goto2

     # Since no win/loss, we continue play.
     
     u=u-(112-u)*(u<112)  # Restrict Sprites' X coordinate to pos 112 on the left
     u=u+(u-245)*(u>245)  # Restrict Sprites' X coordinate to pos 245 on the right
                          # Doing this prevents from having to
                          #   deal with another register for
                          #   positions > 255

     poke v,u             # Update both Sprites' X positions to prevent going
     poke v+2,u           #   "out of bounds"

     goto 2               # Continue play and check for a key press again.

Line 7 : pokev+21,3:c=33*-(rnd(0)>0.8):printtab(9);"{white}B";spc(rnd(0)*18);"{red}"chr$(c)tab(30)"{white}B":return

     # This is the "scroll_screen" routine
     poke v+21,3
                         # Enable Sprite's 0 and 1.  Repetitive,
                         #   yes.  But I had room on this line
                         #   for it. :)  
     c=33*-(rnd(0)>0.8)  # Should we print a space or a
                         #  torpedo('!').  This gives us
                         #  torpedo's about 80% of the time.
                         #  Allows torpedos to be more sparse.
                         #  Lowering the 0.8 value decreases the 
                         #  number of likely torpedoes on the screen.
                   
     printtab(9);"{white}B";spc(rnd(0)*18);"{red}"chr$(c)tab(30)"{white}B"
     
                         # Print the left border, random spaces,
                         #  optionally the torpedo, and finally 
                         #  the right border.
                   
     return              # Return

Line 8 : fori=0to62:pokes+i,0:pokes+64+i,0:next:data189,189,189,125,189,190,62,102,124,30,36,120

     # The "build_sprites" routine - line 1
     fori=0to62:pokes+i,0:pokes+64+i,0:next
        # Clear out all Sprite 0's and 1's data.
        
     data189,189,189,125,189,190,62,102,124,30,36,120
        # First piece of 21 bytes of sprite data

Line 9 : fori=0to20:readx:pokes+i,x:poke12414-i,x:next:data30,102,120,30,129,120,28,129,56:return

         # The "build_sprites" routine - line 2
         for i = 0 to 20
            # I only use 21 bytes of the 63 for a sprite.
            # This allowed me to keep the lines/data smaller.
                            
     read x            # Read a byte for the sprite
     poke s+i,x        # Build Sprite 0 normally, from the start.
     poke 12414-i,x    # Buils Sprite 1 in reverse, from the "back".
                       #  This is what allows me to use the same data
                       #  for 2 sprites, in opposite directions AND
                       #  why the Copy Catters "cloned" our ships! :)
     next              # Next sprite data
     
     data30,102,120,30,129,120,28,129,56
        # Rest of the 21 bytes of sprite data
	
     return            # Return from "build_sprites"

######################################################################
# End of README
######################################################################
