#!/usr/bin/python
# FUNK DAS LICHT! 2.9 -- The Pythonified Licht Hunt
# thp <thp at thpinfo com> Mon, 04 Dec 2006 02:29:54 +0100

import curses
import random

def setup():
    global stdscr
    stdscr = curses.initscr()
    curses.start_color()
    curses.init_pair( 1, curses.COLOR_BLUE, curses.COLOR_BLACK)
    curses.init_pair( 2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
    curses.init_pair( 3, curses.COLOR_BLACK, curses.COLOR_WHITE)
    curses.noecho()
    curses.cbreak()
    curses.curs_set( 0)
    stdscr.keypad(1)

def teardown():
    global stdscr
    curses.nocbreak()
    stdscr.keypad(0)
    curses.echo()
    curses.curs_set( 1)
    curses.endwin()

class FunkObject(object):
    def __init__( self, x = 0, y = 0, c = '?', color = 1, winsize = (0,0)):
        self.x = x
        self.y = y
        self.c = c
        self.color = color
        self.max_x = winsize[1] - 2 # 1 for zerobased, + 1 for border
        self.max_y = winsize[0] - 2 # 1 for zerobased, + 1 for border
        self.min_x = 1
        self.min_y = 1

    def meets( self, other):
        return self.x == other.x and self.y == other.y

    def move( self, x=0, y=0):
        if x == 0 and y == 0:
            self.x = self.x + random.randint( -1, 1)
            self.y = self.y + random.randint( -1, 1)
        else:
            self.x = self.x + x
            self.y = self.y + y

        self.x = max( self.min_x, min( self.x, self.max_x))
        self.y = max( self.min_y, min( self.y, self.max_y))

    def display( self, win):
        win.addch( self.y, self.x, self.c, curses.color_pair( self.color))

    def clear( self, win):
        win.addch( self.y, self.x, ' ')

setup()

licht = FunkObject( 10, 10, '*', 1, stdscr.getmaxyx())
player = FunkObject( 3, 3, 'o', 2, stdscr.getmaxyx())

endmsg = 'Funkie Funked! After fercking %d moves :)'
moves = 0

stdscr.border()
stdscr.addstr( 0, 5, ' FUNK DAS LICHT! 2.9 ', curses.color_pair( 3))
while not player.meets( licht):
    player.display( stdscr)
    licht.display( stdscr)
    c = stdscr.getch()
    moves = moves+1
    player.clear( stdscr)
    licht.clear( stdscr)

    if c == curses.KEY_DOWN:
        player.move( 0, 1)
    elif c == curses.KEY_UP:
        player.move( 0, -1)
    elif c == curses.KEY_LEFT:
        player.move( -1, 0)
    elif c == curses.KEY_RIGHT:
        player.move( 1, 0)
    elif c == ord('q'):
        endmsg = 'You cowardly quit the game after %d moves.'
        break

    licht.move()

stdscr.clear()
stdscr.border()
stdscr.addstr( 3, 3, 'FUNK DAS LICHT! 2.9 -- The Pythonified Licht Hunt', curses.color_pair( 1))
stdscr.addstr( 6, 3, endmsg % moves, curses.color_pair( 2))
stdscr.addstr( 9, 3, 'Press the \"q\" key to exit FDL2.9', curses.color_pair( 3))

while stdscr.getch() != ord('q'):
    pass

teardown()

