#!/usr/bin/env python
"Displays a few swimming fishes using curses."

# Fishurses, Displays a few swimming fishes using curses.
# Copyright (C) 2009  Joakim Soderlund
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import curses
import random
from time import sleep

__author__    = "Joakim Soderlund"
__license__   = "GPLv3"
__copyright__ = "Copyright (C) 2009  Joakim Soderlund"
__date__      = "2009-03-08"
__version__   = "1.1"

fishRange = (10, 30)
fishSleep = 0.25

if hasattr(__builtins__, "xrange"):
	range = xrange

class fish():
	"A simple fish."
	def __init__(self, xpos, ypos):
		self.xPos = xpos
		self.yPos = ypos
		self.fish = "><(((*>"
	
	def delFish(self, window):
		for x in range(self.xPos-len(self.fish), self.xPos+1):
			try:
				window.addch(self.yPos, x, " ")
			except:
				pass
				
	def addFish(self, window):
		strPos = 0
		for x in range(self.xPos-len(self.fish), self.xPos+1):
			try:
				window.addch(self.yPos, x, self.fish[strPos])
			except:
				pass
			finally:
				strPos += 1
		
def main():
	"Starts Fishurses."
	try:
		stdscr = curses.initscr()
		curses.curs_set(0)
		curses.noecho()
		curses.cbreak()
		stdscr.keypad(1)
		fishList = []
		fishNew = 0		
		while True:
			if fishNew <= 0:
				fishList.append(fish(0, random.randrange(stdscr.getbegyx()[0], stdscr.getmaxyx()[0]+1)))
				fishNew = random.randrange(fishRange[0], fishRange[1])
			for f in fishList:
				if f.yPos < stdscr.getmaxyx()[0]+1 and f.xPos < stdscr.getmaxyx()[1]+len(f.fish)+1:
					f.delFish(stdscr)
					f.xPos += 1
					f.addFish(stdscr)
				else:
					del fishList[fishList.index(f)]
			stdscr.refresh()
			fishNew -= 1
			sleep(fishSleep)
			
	except KeyboardInterrupt:
		pass
		
	finally:
		stdscr.keypad(0)
		curses.curs_set(1)
		curses.echo()
		curses.nocbreak()
		curses.endwin()

if __name__ == "__main__":
	main()


