From b7198cb91008a6a113197713959a936f89951765 Mon Sep 17 00:00:00 2001 From: marcinljablonski Date: Mon, 29 Apr 2019 03:49:45 +0200 Subject: [PATCH] . --- Cucumber.py | 55 ++++++++++++++++++++++++++++++++ Plant.py | 11 +++++++ Point.py | 17 ++++++++++ Tomato.py | 55 ++++++++++++++++++++++++++++++++ Trac.py | 26 +++++++++++++++ traktor.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 256 insertions(+) create mode 100644 Cucumber.py create mode 100644 Plant.py create mode 100644 Point.py create mode 100644 Tomato.py create mode 100644 Trac.py create mode 100644 traktor.py diff --git a/Cucumber.py b/Cucumber.py new file mode 100644 index 0000000..c9b32ec --- /dev/null +++ b/Cucumber.py @@ -0,0 +1,55 @@ +from Plant import Plant + +class Cucumber(Plant): + def __init__(self): + self.__is_alive = True + self.__ttl = 40 + self.__hydration = 40 + self.__soil_level = 40 + self.__dehydration_ratio = 0.8 + self.__desoil_ratio = 0.7 + self.__max_soil_lvl = 40 + self.__max_hydration_lvl = 40 + self.__ready = 11 + + def get_symbol(self): + if not self.__is_alive: + return ('x', None) + elif self.__hydration > (self.__max_hydration_lvl / 2) \ + and self.__soil_level > (self.__max_soil_lvl / 2): + return ('o', "green") if (self.__ttl > self.__ready) \ + else ('O', "green") + else: + return ('o', "yellow") if (self.__ttl > self.__ready) \ + else ('O', "yellow") + + def tick(self, n): + self.decrease_ttl(n) + self.decrase_hydration(n) + self.decrease_soillevel(n) + + def decrease_ttl(self, n): + self.__ttl -= n + if self.__ttl == 0: + self.__is_life == False + + + def increase_hydration(self, n): + self.__hydration += n + if self.__hydration > 40: + self.__is_life = False + + def decrase_hydration(self, n): + self.__hydration -= n * self.__dehydration_ratio + if self.__hydration < 1: + self.__is_life = False + + def increase_soillevel(self, n): + self.__soil_level += n + if self.__soil_level > 40: + self.__is_life = False + + def decrease_soillevel(self, n): + self.__soil_level -= n * self.__desoil_ratio + if self.__soil_level < 1: + self.__is_life = False diff --git a/Plant.py b/Plant.py new file mode 100644 index 0000000..6258368 --- /dev/null +++ b/Plant.py @@ -0,0 +1,11 @@ +class Plant(): + + def get_symbol(self): + raise NotImplementedError("Please Implement this method") + + def tick(self, n): + raise NotImplementedError("Please Implement this method") + + def get_stats(self): + #np. touple'a z info czy żyje, ile ma wody i nawozu + raise NotImplementedError("Please Implement this method") diff --git a/Point.py b/Point.py new file mode 100644 index 0000000..0262e58 --- /dev/null +++ b/Point.py @@ -0,0 +1,17 @@ +class Point: + def __init__(self, cord): + self.x = cord[0] + self.y = cord[1] + + def get_cord(self): + return (self.x, self.y) + + def get_x(self): + return self.x + + def get_y(self): + return self.y + + def set_cord(self, cord): + self.x = cord[0] + self.y = cord[1] diff --git a/Tomato.py b/Tomato.py new file mode 100644 index 0000000..66674ef --- /dev/null +++ b/Tomato.py @@ -0,0 +1,55 @@ +from Plant import Plant + +class Tomato(Plant): + def __init__(self): + self.__is_alive = True + self.__ttl = 38 + self.__hydration = 41 + self.__soil_level = 41 + self.__dehydration_ratio = 0.7 + self.__desoil_ratio = 0.8 + self.__max_soil_lvl = 40 + self.__max_hydration_lvl = 40 + self.__ready = 10 + + def get_symbol(self): + if not self.__is_alive: + return ('x', None) + elif self.__hydration > (self.__max_hydration_lvl / 2) \ + and self.__soil_level > (self.__max_soil_lvl / 2): + return ('p', "green") if (self.__ttl > self.__ready) \ + else ('P', "green") + else: + return ('p', "yellow") if (self.__ttl > self.__ready) \ + else ('P', "yellow") + + def tick(self, n): + self.decrease_ttl(n) + self.decrase_hydration(n) + self.decrease_soillevel(n) + + def decrease_ttl(self, n): + self.__ttl -= n + if self.__ttl == 0: + self.__is_life == False + + + def increase_hydration(self, n): + self.__hydration += n + if self.__hydration > 40: + self.__is_life = False + + def decrase_hydration(self, n): + self.__hydration -= n * self.__dehydration_ratio + if self.__hydration < 1: + self.__is_life = False + + def increase_soillevel(self, n): + self.__soil_level += n + if self.__soil_level > 40: + self.__is_life = False + + def decrease_soillevel(self, n): + self.__soil_level -= n * self.__desoil_ratio + if self.__soil_level < 1: + self.__is_life = False diff --git a/Trac.py b/Trac.py new file mode 100644 index 0000000..fcd73c3 --- /dev/null +++ b/Trac.py @@ -0,0 +1,26 @@ +from Point import Point + +class Trac: + def __init__(self, rotation, position): + self.__position = Point(position.get_cord()) + self.__rotation = rotation + + def get_symbol(self): + if self.__rotation == 'S': + return 'v' + elif self.__rotation == 'N': + return '^' + elif self.__rotation == 'E': + return '>' + else: + return '<' + + # def set_rotation(self, rotation): + # self.__rotation = rotation + + # def set_position(self, position): + # self.__position.set_cord(position) + + def get_position(self): + return self.__position + diff --git a/traktor.py b/traktor.py new file mode 100644 index 0000000..2bf0dc2 --- /dev/null +++ b/traktor.py @@ -0,0 +1,92 @@ +import string +import sys +import os +import time +import random +from Tomato import Tomato +from Cucumber import Cucumber +from Plant import Plant +from Point import Point +from Trac import Trac + +string.ascii_letters = 'oOpPx' +string.ascii_numbers = '01' +OKGREEN = '\033[92m' +OKBLUE = '\033[94m' +OKRED = '\033[91m' +ENDC = '\033[0m' + + +def initialize_field(): + field = [] + for i in range(11): + row = [] + for j in range(7): + if i == 0 or i == 10 or j == 0 \ + or j == 3 or j == 6: + # row.append((Point((i, j)), None)) + row.append(None) + elif i < 3: + # row.append((Point((i, j)), Cucumber())) + row.append(Cucumber()) + else: + # row.append((Point((i, j)), Tomato())) + row.append(Tomato()) + field.append(row) + return field + + +def print_field(field, tractor): + sys.stdout.write(OKBLUE) + sys.stdout.write("$") + sys.stdout.write(" ") + sys.stdout.write("\n") + + for x, row in enumerate(field): + for y, i in enumerate(row): + if not i: + if tractor.get_position().get_x() == x \ + and tractor.get_position().get_y() == y: + sys.stdout.write(OKBLUE) + sys.stdout.write(tractor.get_symbol()) + sys.stdout.write(" ") + else: + sys.stdout.write(" ") + else: + symbol = i.get_symbol() + if symbol[1] == "green": + sys.stdout.write(OKGREEN) + sys.stdout.write(symbol[0]) + sys.stdout.write(" ") + else: + sys.stdout.write(OKRED) + sys.stdout.write(symbol[0]) + sys.stdout.write(" ") + + sys.stdout.write("\n") + +def update_state(field): + for row in field: + for i in row: + if i: + i.tick(1) + + + +if __name__ == "__main__": + + field = initialize_field() + tractor = Trac('S', Point((0,0))) + + sys.stdout.write(ENDC) + i = 40 + while i>=0: + if sys.platform == "win32": + os.system("cls") + else: + os.system("clear") + print_field(field, tractor) + update_state(field) + time.sleep(0.4) + i = i-1 + print("The end of time")