#!/usr/bin/env python
"""A minimal video game. Use arrow keys. Resize terminal to adjust difficulty."""

# Modifications by Lauren and Kevin, 12 Sept 2007
# Look for MOD comments.

__author__ = 'Nick Montfort <nickm@nickm.com>'
__version__ = '0.1'

from random import random
from time import sleep

# Initialize the curses library to allow printing at a coordinate
import curses
import math
scr = curses.initscr()
curses.noecho(); curses.cbreak(); scr.keypad(1); scr.nodelay(1)
(height, width) = scr.getmaxyx()

# Initialize game variables
collided = False
tendency = .4 # Higher, up to 1, means more motion
x = int(width/2)
y = int(height/2) # Initally, center the escaper
centerx = x
centery = y 
# MOD: Tracking 500 coordinates to create a bigger trail cloud
recent = [(y,x)] * 500 # A list of recent escaper positions
iterations = 0 # Equal to the score
bg = 1
bottom = "Press the SPACEBAR to quit!"

# Main loop
while not collided:
    iterations += 1

# MOD, pause the game and invert the background color every 1000 times through the loop
    if (iterations % 1000) == 0:
        #scr.erase()
	scr.addstr(y,x,str("FREEZE!"))
        scr.bkgd(ord(' '), curses.A_REVERSE) # Invert the background
        scr.refresh()
        i = 0
        while i < 10000000: i += 1
        scr.bkgd(ord(' '))
        scr.refresh()

    displacement = int(4 * random()) # Move zero to three units
    swerve = random()
    if swerve < tendency:
        if swerve < tendency * .5:
            if swerve < tendency * .25: x += displacement
            else: x -= displacement
        else:
            if swerve > tendency * .75: y += displacement
            else: y -= displacement

    scr.erase()

# MOD: Create a tail of descending character sizes 
    trailcount = 0 
    for (doty, dotx) in recent:
        if trailcount < int((len(recent) * 0.2)):
            character = "."
        else:
	    if (trailcount < int((len(recent) * 0.6))):
                character = "o"
	    else:
                character = "O"
        scr.addstr(doty, dotx, character, curses.A_DIM) # For each previous position, a dot
        trailcount += 1

    recent.insert(0, (y, x)) # Add the current position as a recent one
    recent.pop() # Remove the oldest item on the recent list
    inkey = scr.getch() # Get whatever key (if any) is pressed
    if inkey == curses.KEY_LEFT: x += 5 + int(5 * random())   # Kick the escaper in the
    if inkey == curses.KEY_RIGHT: x -= 5 + int(5 * random())  # opposite direction
    if inkey == curses.KEY_DOWN: y -= 4 + int(4 * random())   # of the arrow key pressed
    if inkey == curses.KEY_UP: y += 4 + int(4 * random())
    if (inkey == 32): collided = True
    if x <= 0 or y <= 0 or x >= width - 1 or y >= height - 1: # Lost!
        collided = True # The escaper has hit the edge of the window
        scr.bkgd(ord(' '), curses.A_REVERSE) # Invert the background
    else:
        scr.addstr(y, x, "@", curses.A_BOLD) # Draw the still-contained escaper

# MOD: Keep score at the top of the screen
    scr.addstr(1,(width - 10 - int(math.log10(iterations))),"Score: " + str(iterations), curses.A_BOLD)
    scr.addstr((height - 1),1,bottom,curses.A_BOLD)
    bottom = "Press the SPACEBAR to quit!"	
    scr.refresh()
    sleep(.01)

# Pause with window reversed, then finish with curses and print the score
sleep(1)
scr.keypad(0); curses.nocbreak(); curses.echo(); curses.endwin()
print "\nSCORE:", iterations
