Loading plantmanager.py +39 −22 Original line number Diff line number Diff line Loading @@ -10,37 +10,42 @@ class Plant: self.height = 0 self.water = 100 self.food = 100 self.pot = 10 self.pot = 5 self.timelastgrown = time.time() self.health = 50 self.screen = None return def grow(self): if time.time() > self.timelastgrown + 1: if time.time() > self.timelastgrown + 1 and self.health > 0: self.timelastgrown = time.time() self.consume() self.heal() if self.food > 0 and self.water > 0 and self.height < self.pot: self.height += 1 if self.health <= 0: self.screen.update("Your plant died.") return self.screen.update() return def getattr(self): attrs = {"Height": self.height, "Water": self.water, "Food": self.food, "Health": self.health} attrs = {"Health": self.health, "Water": self.water, "Food": self.food} return attrs def add_water(self): self.water += 50 self.water += 35 return def add_food(self): self.food += 50 self.food += 35 return def consume(self): self.water -= self.height * 3 self.water -= self.height if self.water < 0: self.water = 0 self.food -= self.height self.food -= math.ceil(self.height / 3) if self.food < 0: self.food = 0 return Loading @@ -54,11 +59,17 @@ class Plant: self.health = 0 return def repot(self): self.pot *= 2 self.consume() return class Screen: def __init__(self, stdscr, plant): self.stdscr = stdscr self.plant = plant self.plant.screen = self self.statuspos = curses.COLS - 7 curses.curs_set(False) curses.noecho() Loading @@ -73,14 +84,15 @@ class Screen: self.stdscr.clear() self.stdscr.addstr(1, 1, "[W] Add Water") self.stdscr.addstr(3, 1, "[F] Add Food") self.stdscr.addstr(5, 1, "[P] Re-pot") self.stdscr.addstr(curses.LINES - 4, 1, "[R] Restart") self.stdscr.addstr(curses.LINES - 3, 1, "[Q] Quit") return def update(self): def update(self, msg_override=None): self.drawplant() self.writeinfo() self.writemsg() self.writemsg(msg_override) self.stdscr.refresh() def writeinfo(self): Loading @@ -97,6 +109,9 @@ class Screen: def drawplant(self): line = curses.LINES - 2 if self.plant.pot >= curses.COLS: self.stdscr.addstr(line, 0, "#" * curses.COLS, self.potcolour) else: self.stdscr.addstr(line, 0, "{0}{1}{0}".format(" " * math.floor((curses.COLS - self.plant.pot) / 2), "#" * self.plant.pot), self.potcolour) plantloc = math.floor(curses.COLS / 2) Loading @@ -105,6 +120,7 @@ class Screen: plantcolour = self.plantbadcolour for i in range(self.plant.height): line -= 1 if line >= 0: self.stdscr.addstr(line, plantloc, "|", plantcolour) for i in range(line): line -= 1 Loading @@ -114,38 +130,39 @@ class Screen: def writemsg(self, override=None): msg = " " if self.plant.water < 50: if override is not None: msg = override elif self.plant.water < 50: msg = "Your plant needs water!" elif self.plant.food < 50: msg = "Your plant needs food!" elif self.plant.water > 150: msg = "Don't over-water!" elif self.plant.food > 150: msg = "Don't over-feed!" 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)) self.stdscr.addstr(curses.LINES - 3, curses.COLS - 24, msg.rjust(23)) return def main(stdscr): plant = Plant() screen = Screen(stdscr, plant) screen.update() while plant.health > 0: while True: c = stdscr.getch() if c == ord('w'): plant.add_water() if c == ord('f'): plant.add_food() if c == ord('p'): plant.repot() 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") stdscr.refresh() time.sleep(1) return if __name__ == "__main__": Loading Loading
plantmanager.py +39 −22 Original line number Diff line number Diff line Loading @@ -10,37 +10,42 @@ class Plant: self.height = 0 self.water = 100 self.food = 100 self.pot = 10 self.pot = 5 self.timelastgrown = time.time() self.health = 50 self.screen = None return def grow(self): if time.time() > self.timelastgrown + 1: if time.time() > self.timelastgrown + 1 and self.health > 0: self.timelastgrown = time.time() self.consume() self.heal() if self.food > 0 and self.water > 0 and self.height < self.pot: self.height += 1 if self.health <= 0: self.screen.update("Your plant died.") return self.screen.update() return def getattr(self): attrs = {"Height": self.height, "Water": self.water, "Food": self.food, "Health": self.health} attrs = {"Health": self.health, "Water": self.water, "Food": self.food} return attrs def add_water(self): self.water += 50 self.water += 35 return def add_food(self): self.food += 50 self.food += 35 return def consume(self): self.water -= self.height * 3 self.water -= self.height if self.water < 0: self.water = 0 self.food -= self.height self.food -= math.ceil(self.height / 3) if self.food < 0: self.food = 0 return Loading @@ -54,11 +59,17 @@ class Plant: self.health = 0 return def repot(self): self.pot *= 2 self.consume() return class Screen: def __init__(self, stdscr, plant): self.stdscr = stdscr self.plant = plant self.plant.screen = self self.statuspos = curses.COLS - 7 curses.curs_set(False) curses.noecho() Loading @@ -73,14 +84,15 @@ class Screen: self.stdscr.clear() self.stdscr.addstr(1, 1, "[W] Add Water") self.stdscr.addstr(3, 1, "[F] Add Food") self.stdscr.addstr(5, 1, "[P] Re-pot") self.stdscr.addstr(curses.LINES - 4, 1, "[R] Restart") self.stdscr.addstr(curses.LINES - 3, 1, "[Q] Quit") return def update(self): def update(self, msg_override=None): self.drawplant() self.writeinfo() self.writemsg() self.writemsg(msg_override) self.stdscr.refresh() def writeinfo(self): Loading @@ -97,6 +109,9 @@ class Screen: def drawplant(self): line = curses.LINES - 2 if self.plant.pot >= curses.COLS: self.stdscr.addstr(line, 0, "#" * curses.COLS, self.potcolour) else: self.stdscr.addstr(line, 0, "{0}{1}{0}".format(" " * math.floor((curses.COLS - self.plant.pot) / 2), "#" * self.plant.pot), self.potcolour) plantloc = math.floor(curses.COLS / 2) Loading @@ -105,6 +120,7 @@ class Screen: plantcolour = self.plantbadcolour for i in range(self.plant.height): line -= 1 if line >= 0: self.stdscr.addstr(line, plantloc, "|", plantcolour) for i in range(line): line -= 1 Loading @@ -114,38 +130,39 @@ class Screen: def writemsg(self, override=None): msg = " " if self.plant.water < 50: if override is not None: msg = override elif self.plant.water < 50: msg = "Your plant needs water!" elif self.plant.food < 50: msg = "Your plant needs food!" elif self.plant.water > 150: msg = "Don't over-water!" elif self.plant.food > 150: msg = "Don't over-feed!" 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)) self.stdscr.addstr(curses.LINES - 3, curses.COLS - 24, msg.rjust(23)) return def main(stdscr): plant = Plant() screen = Screen(stdscr, plant) screen.update() while plant.health > 0: while True: c = stdscr.getch() if c == ord('w'): plant.add_water() if c == ord('f'): plant.add_food() if c == ord('p'): plant.repot() 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") stdscr.refresh() time.sleep(1) return if __name__ == "__main__": Loading