Loading plantmanager.py +58 −20 Original line number Diff line number Diff line Loading @@ -18,23 +18,9 @@ class Plant: def grow(self): if time.time() > self.timelastgrown + 1: self.timelastgrown = time.time() if self.water > 0: self.water -= 10 else: self.health -= 10 if self.health < 50 and self.height > 1: self.height -= 1 return if self.food > 0: self.food -= 1 else: self.health -= 10 if self.health < 50 and self.height > 1: self.height -= 1 return if self.health < 100: self.health += 10 if self.height < self.pot: self.consume() self.heal() if self.food > 0 and self.water > 0 and self.height < self.pot: self.height += 1 return Loading @@ -42,6 +28,32 @@ class Plant: attrs = {"Height": self.height, "Water": self.water, "Food": self.food, "Health": self.health} return attrs def add_water(self): self.water += 50 return def add_food(self): self.food += 50 return def consume(self): self.water -= self.height * 3 if self.water < 0: self.water = 0 self.food -= self.height if self.food < 0: self.food = 0 return def heal(self): self.health += 10 - math.ceil(math.pow(self.water - 75, 2) / 281.25) self.health += 10 - math.ceil(math.pow(self.food - 75, 2) / 281.25) if self.health > 100: self.health = 100 if self.health < 0: self.health = 0 return class Screen: def __init__(self, stdscr, plant): Loading @@ -50,7 +62,7 @@ class Screen: self.statuspos = curses.COLS - 7 curses.curs_set(False) curses.noecho() curses.cbreak() curses.halfdelay(1) self.stdscr.keypad(True) curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) self.potcolour = curses.color_pair(1) Loading @@ -59,11 +71,16 @@ class Screen: curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK) self.plantbadcolour = curses.color_pair(3) self.stdscr.clear() self.stdscr.addstr(1, 1, "[W] Add Water") self.stdscr.addstr(3, 1, "[F] Add Food") self.stdscr.addstr(curses.LINES - 4, 1, "[R] Restart") self.stdscr.addstr(curses.LINES - 3, 1, "[Q] Quit") return def update(self): self.drawplant() self.writeinfo() self.writemsg() self.stdscr.refresh() def writeinfo(self): Loading @@ -75,7 +92,7 @@ class Screen: line = 1 for k, v in attrs.items(): self.stdscr.addstr(line, self.statuspos - maxlen, "{0}:{1}{2: 4}".format(k, " " * (maxlen - len(k)), v)) line += 1 line += 2 return def drawplant(self): Loading @@ -95,13 +112,34 @@ class Screen: self.stdscr.addstr(line, plantloc, " ") return def writemsg(self, override=None): msg = " " if self.plant.water < 50: msg = "Your plant needs water!" elif self.plant.food < 50: msg = "Your plant needs food!" elif self.plant.height >= self.plant.pot - 2: msg = "You need a bigger pot!" msgleft = math.floor((curses.COLS - self.plant.pot) / 2) + self.plant.pot + 2 self.stdscr.addstr(curses.LINES - 2, msgleft, msg.ljust(curses.COLS - msgleft)) return def main(stdscr): plant = Plant() screen = Screen(stdscr, plant) screen.update() while plant.health > 0: time.sleep(1) c = stdscr.getch() if c == ord('w'): plant.add_water() if c == ord('f'): plant.add_food() if c == ord('q'): return if c == ord('r'): plant = Plant() screen = Screen(stdscr, plant) screen.update() plant.grow() stdscr.addstr(4, 3, "It died") Loading Loading
plantmanager.py +58 −20 Original line number Diff line number Diff line Loading @@ -18,23 +18,9 @@ class Plant: def grow(self): if time.time() > self.timelastgrown + 1: self.timelastgrown = time.time() if self.water > 0: self.water -= 10 else: self.health -= 10 if self.health < 50 and self.height > 1: self.height -= 1 return if self.food > 0: self.food -= 1 else: self.health -= 10 if self.health < 50 and self.height > 1: self.height -= 1 return if self.health < 100: self.health += 10 if self.height < self.pot: self.consume() self.heal() if self.food > 0 and self.water > 0 and self.height < self.pot: self.height += 1 return Loading @@ -42,6 +28,32 @@ class Plant: attrs = {"Height": self.height, "Water": self.water, "Food": self.food, "Health": self.health} return attrs def add_water(self): self.water += 50 return def add_food(self): self.food += 50 return def consume(self): self.water -= self.height * 3 if self.water < 0: self.water = 0 self.food -= self.height if self.food < 0: self.food = 0 return def heal(self): self.health += 10 - math.ceil(math.pow(self.water - 75, 2) / 281.25) self.health += 10 - math.ceil(math.pow(self.food - 75, 2) / 281.25) if self.health > 100: self.health = 100 if self.health < 0: self.health = 0 return class Screen: def __init__(self, stdscr, plant): Loading @@ -50,7 +62,7 @@ class Screen: self.statuspos = curses.COLS - 7 curses.curs_set(False) curses.noecho() curses.cbreak() curses.halfdelay(1) self.stdscr.keypad(True) curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) self.potcolour = curses.color_pair(1) Loading @@ -59,11 +71,16 @@ class Screen: curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK) self.plantbadcolour = curses.color_pair(3) self.stdscr.clear() self.stdscr.addstr(1, 1, "[W] Add Water") self.stdscr.addstr(3, 1, "[F] Add Food") self.stdscr.addstr(curses.LINES - 4, 1, "[R] Restart") self.stdscr.addstr(curses.LINES - 3, 1, "[Q] Quit") return def update(self): self.drawplant() self.writeinfo() self.writemsg() self.stdscr.refresh() def writeinfo(self): Loading @@ -75,7 +92,7 @@ class Screen: line = 1 for k, v in attrs.items(): self.stdscr.addstr(line, self.statuspos - maxlen, "{0}:{1}{2: 4}".format(k, " " * (maxlen - len(k)), v)) line += 1 line += 2 return def drawplant(self): Loading @@ -95,13 +112,34 @@ class Screen: self.stdscr.addstr(line, plantloc, " ") return def writemsg(self, override=None): msg = " " if self.plant.water < 50: msg = "Your plant needs water!" elif self.plant.food < 50: msg = "Your plant needs food!" elif self.plant.height >= self.plant.pot - 2: msg = "You need a bigger pot!" msgleft = math.floor((curses.COLS - self.plant.pot) / 2) + self.plant.pot + 2 self.stdscr.addstr(curses.LINES - 2, msgleft, msg.ljust(curses.COLS - msgleft)) return def main(stdscr): plant = Plant() screen = Screen(stdscr, plant) screen.update() while plant.health > 0: time.sleep(1) c = stdscr.getch() if c == ord('w'): plant.add_water() if c == ord('f'): plant.add_food() if c == ord('q'): return if c == ord('r'): plant = Plant() screen = Screen(stdscr, plant) screen.update() plant.grow() stdscr.addstr(4, 3, "It died") Loading