Traktor/app/tractor.py
2021-04-11 19:48:44 +02:00

156 lines
4.8 KiB
Python

#!/usr/bin/python3
import random
import pygame
import os
import time
from threading import Thread
from typing import Union
from app.base_field import BaseField
from app.board import Board
from app.utils import get_class
from app.fields import CROPS, PLANTS, SOILS, Sand, Clay
from config import *
from app.fields import Plant, Soil, Crops
class Tractor(BaseField):
def __init__(self, board: Board):
super().__init__(os.path.join(RESOURCE_DIR, f"{TRACTOR}.{PNG}"))
self.__pos_x = (int(HORIZONTAL_NUM_OF_FIELDS / 2) - 1) * FIELD_SIZE
self.__pos_y = (int(VERTICAL_NUM_OF_FIELDS / 2) - 1) * FIELD_SIZE
self.__angle = 0.0
self.__move = FIELD_SIZE
self.__board = board
self.__harvested_corps = []
def draw(self, screen: pygame.Surface):
self.draw_field(screen, self.__pos_x + FIELD_SIZE / 2, self.__pos_y + FIELD_SIZE / 2,
is_centered=True, size=(FIELD_SIZE, FIELD_SIZE), angle=self.__angle)
# Key methods handlers
def move(self):
if self.__angle == 0.0:
self.move_right()
elif self.__angle == 90.0:
self.move_up()
elif self.__angle == 180.0:
self.move_left()
else:
self.move_down()
def move_up(self):
if self.__pos_y - self.__move >= 0:
self.__pos_y -= self.__move
def move_down(self):
if self.__pos_y + self.__move + FIELD_SIZE <= HEIGHT:
self.__pos_y += self.__move
def move_left(self):
if self.__pos_x - self.__move >= 0:
self.__pos_x -= self.__move
def move_right(self):
if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH:
self.__pos_x += self.__move
def rotate_left(self):
self.__angle = (self.__angle - 90.0) % 360.0
def rotate_right(self):
self.__angle = (self.__angle + 90.0) % 360.0
def hydrate(self):
if self.check_field(Sand):
field = self.get_field_from_board()
if not field.is_hydrated and field.is_sowed:
print('Hydrate soil')
self.irrigate_sand(field)
elif self.check_field(Plant):
field = self.get_field_from_board()
if not field.is_hydrated:
print("Hydrate plant")
self.irrigate_plants(field)
def sow(self):
field = self.get_field_from_board()
if self.check_field(Sand) and not field.is_sowed:
print('Sow')
field.is_sowed = True
def harvest(self):
if self.check_field(Crops):
print('Harvest')
field = self.get_field_from_board()
self.harvest_crops(field)
self.get_result_of_harvesting()
def fertilize(self):
if self.check_field(Clay):
print('Fertilize soil')
field = self.get_field_from_board()
self.fertilize_clay(field)
################################################################################
def fertilize_clay(self, field: Clay):
field.is_fertilized = True
self.do_action((Sand.__name__,))
def irrigate_plants(self, field: Plant):
field.is_hydrated = True
self.do_time_action(CROPS)
def irrigate_sand(self, field: Sand):
field.is_hydrated = True
self.do_time_action(PLANTS)
def harvest_crops(self, field: Crops):
self.__harvested_corps.append(type(field).__name__)
self.do_action(SOILS)
def do_action(self, TYPE: tuple):
choosen_type = random.choice(TYPE)
obj = get_class("app.fields", choosen_type)
x, y = self.get_position()
self.__board.get_fields()[x][y] = obj()
def do_time_action(self, TYPE: tuple):
thread = Thread(target=self.do_time_action_handler, args=(TYPE,), daemon=True)
thread.start()
def do_time_action_handler(self, TYPE: tuple):
time.sleep(TIME_OF_GROWING)
self.do_action(TYPE)
def check_field(self, class_name: Union[type(Plant), type(Crops), type(Soil)]):
if isinstance(self.get_field_from_board(), class_name):
return True
return False
def get_field_from_board(self):
x, y = self.get_position()
return self.__board.get_fields()[x][y]
def get_position(self):
x = self.__pos_x // FIELD_SIZE
y = self.__pos_y // FIELD_SIZE
return x, y
def get_result_of_harvesting(self):
crops = set(self.__harvested_corps)
result = 0.0
for crop in crops:
amount = self.__harvested_corps.count(crop)
print(f"{amount} x {crop}")
result += amount * get_class("app.fields", crop).price
print(f"Price for collected crops: {result:.2f}")
def __str__(self):
x, y = self.get_position()
return f"Position: {x}:{y} - {type(self.__board.get_fields()[x][y]).__name__}"