This commit is contained in:
marcinljablonski 2019-04-29 03:49:45 +02:00
parent b87c673279
commit b7198cb910
6 changed files with 256 additions and 0 deletions

55
Cucumber.py Normal file
View File

@ -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

11
Plant.py Normal file
View File

@ -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")

17
Point.py Normal file
View File

@ -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]

55
Tomato.py Normal file
View File

@ -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

26
Trac.py Normal file
View File

@ -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

92
traktor.py Normal file
View File

@ -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")