#!/usr/bin/env python3 # # 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. """A quick game of life goal is to collect all the food before the fish's energy reaches 0 """ __all__ = ['main_c', 'Grid', 'Fish'] __author__ = "Cody A. Taylor ( codemister99@yahoo.com )" __version__ = "2.0" from random import randint import curses class Fish(object): """Simple object to simulate fish movement, energy level, and eating You can set Xmax and Ymax to define the grid, and the x,y is placed center """ def __init__(self, Ymax, Xmax, energy): """Make the fish Xmax - x axis max Ymax - y axis max energy - starting energy """ self.y = Ymax // 2 self.x = Xmax // 2 self._Ymax = Ymax self._Xmax = Xmax self.energy = energy self.char = ">" def __str__(self): """returns the representational characters of the fish >>> fish = Fish(4, 4, 10) >>> fish.move_right() >>> str(fish) '>' >>> fish.move_left() >>> str(fish) '<' """ return self.char def __repr__(self): """returns x, y positions and energy >>> fish = Fish(4, 4, 10) >>> fish 'y: 2, x: 2, energy: 10' """ return "y: {0.y!r}, x: {0.x!r}, energy: {0.energy!r}".format(self) def move_right(self): """Move the fish to the right one Test to make sure it can move: add 1 to x subtract one from energy set the direction char """ if self.x < self._Xmax and not self.is_dead(): self.x += 1 self.energy -= 1 self.set_char(">") def move_left(self): """Move the fish to the left one Test to make sure it can move: subtract 1 from x subtract one from energy set the direction char """ if self.x > 0 and not self.is_dead(): self.x -= 1 self.energy -= 1 self.set_char("<") def move_up(self): """Move the fish to the up one Test to make sure it can move: subtract 1 from y subtract one from energy set the direction char """ if self.y > 0 and not self.is_dead(): self.y -= 1 self.energy -= 1 self.set_char("^") def move_down(self): """Move the fish to the down one Test to make sure it can move: add 1 to y subtract one from energy set the direction char """ if self.y < self._Ymax and not self.is_dead(): self.y += 1 self.energy -= 1 self.set_char("v") def eat(self, value=2): """Adds value to energy""" self.energy += value def is_dead(self): """True is energy is less than 1""" if self.energy <= 0: return True return False def set_char(self, direction): """Used internally to set the direction chars Set to 'X' if fish is dead """ self.char = direction if self.is_dead(): self.char = "X" class Grid(object): """Provides a square pond in which the Fish class can use >>> grid = Grid("F", 10, 20) """ def __init__(self, food_char, max_y, max_x): """Make the grid using lists [[" ", food_char, " ", " "], [food_char, " ", " ", " "]] \ one y list / \^x item/ """ self.food_char = food_char _grid = [] for y in range(max_y + 1): _grid.append(["|"]) for x in range(max_x + 1): if randint(0, max_x) == x: _grid[y].append(food_char) else: _grid[y].append(" ") _grid[y].append("|") self.grid = _grid def no_food(self): """Returns False if any food_char in found in the grid""" for y in self.grid: if self.food_char in y: return False return True def found_food(self, y, x): """If a food_char is at this Y,X location, set that to " " and return True""" x += 1 if self.grid[y][x] == self.food_char: self.grid[y][x] = " " return True return False def main_c(stdscr): """A ncurses main""" fish = Fish(6, 12, 15) grid = Grid("F", 6, 12) curses.curs_set(0) stdscr.addstr(0, 0, "+-------------+") stdscr.addstr(8, 0, "+-------------+") while True: for y in range(len(grid.grid)): stdscr.addstr(y + 1, 0, "".join(grid.grid[y])) if grid.found_food(fish.y, fish.x): fish.eat() if grid.no_food(): stdscr.addstr(10, 0, " Winner !!! ") elif fish.is_dead(): stdscr.addstr(10, 0, " The fish died ! ") else: stdscr.addstr(10, 0, "{0!r} ".format(fish)) stdscr.addstr(fish.y + 1, fish.x + 1, str(fish)) stdscr.refresh() if fish.is_dead() or grid.no_food(): break char = stdscr.getch() if char == ord("q") or char == ord(" "): break elif char == curses.KEY_UP: stdscr.delch(fish.y + 1, fish.x + 1) fish.move_up() elif char == curses.KEY_DOWN: stdscr.delch(fish.y + 1, fish.x + 1) fish.move_down() elif char == curses.KEY_LEFT: stdscr.delch(fish.y + 1, fish.x + 1) fish.move_left() elif char == curses.KEY_RIGHT: stdscr.delch(fish.y + 1, fish.x + 1) fish.move_right() else: continue if __name__ == "__main__": curses.wrapper(main_c)