Traktor/source/area/tractor.py

162 lines
6.2 KiB
Python

from crop_protection_product import CropProtectionProduct
from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, FIELD_WIDTH, FIELD_HEIGHT, WIDTH, HEIGHT
from area.field import fieldX, fieldY, tiles
import pygame
import time
class Tractor:
x = None
y = None
direction = None #direction takes values in the range of 1 to 4 (1->North, 2->East etc...)
image = None
cypermetryna = CropProtectionProduct("pests", "cereal")
diflufenikan = CropProtectionProduct("weeds", "cereal")
spirotetramat = CropProtectionProduct("pests", "fruit")
oksadiargyl = CropProtectionProduct("weeds", "fruit")
spinosad = CropProtectionProduct("pests", "vegetable")
metazachlor = CropProtectionProduct("weeds", "vegetable")
# etc
def __init__(self, x, y, direction, tractor_start, tractor_end):
self.x = x
self.y = y
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
self.direction = direction
self.image = pygame.image.load('resources/images/tractor_right.png').convert_alpha()
self.tractor_start = tractor_start #important for bfs - prevents from spawning obstacles on these positions
self.tractor_end = tractor_end #
if (self.direction==1):
self.image = pygame.image.load('resources/images/tractor_up.png').convert_alpha()
elif (self.direction==3):
self.image = pygame.image.load('resources/images/tractor_down.png').convert_alpha()
elif (self.direction==4):
self.image = pygame.image.load('resources/images/tractor_left.png').convert_alpha()
def work_on_field(self, tile, ground, plant1):
if plant1 is None:
tile.randomizeContent()
# sprobuj zasadzic cos
print("Tarctor planted something")
elif plant1.growth_level == 100:
tile.plant = None
ground.nutrients_level -= 40
ground.water_level -= 40
print("Tractor collected something")
else:
plant1.try_to_grow(50,50) #mozna dostosowac jeszcze
ground.nutrients_level -= 11
ground.water_level -= 11
if ground.pest:
# traktor pozbywa sie szkodnikow
if plant1.plant_type == self.cypermetryna.plant_type:
t = "Tractor used Cypermetryna"
elif plant1.plant_type == self.spirotetramat.plant_type:
t = "Tractor used Spirotetramat"
elif plant1.plant_type == self.spinosad.plant_type:
t = "Tractor used Spinosad"
print(t)
ground.pest = False
if ground.weed:
# traktor pozbywa się chwastow
if plant1.plant_type == self.diflufenikan.plant_type:
t = "Tractor used Diflufenikan"
elif plant1.plant_type == self.oksadiargyl.plant_type:
t = "Tractor used Oksadiargyl"
elif plant1.plant_type == self.metazachlor.plant_type:
t = "Tractor used Metazachlor"
print(t)
ground.weed = False
if ground.water_level < plant1.water_requirements:
ground.water_level += 20
print("Tractor watered the plant")
if ground.nutrients_level < plant1.nutrients_requirements:
ground.nutrients_level += 20
print("Tractor added some nutrients")
def move(self):
if self.direction == DIRECTION_EAST:
self.rect.x += TILE_SIZE
elif self.direction == DIRECTION_WEST:
self.rect.x -= TILE_SIZE
elif self.direction == DIRECTION_NORTH:
self.rect.y -= TILE_SIZE
elif self.direction == DIRECTION_SOUTH:
self.rect.y += TILE_SIZE
def rotate_to_right(self):
if self.direction == 4:
self.direction = 1
else:
self.direction += 1
self.image = pygame.transform.rotate(self.image, -90)
def rotate_to_left(self):
if self.direction == 1:
self.direction = 4
else:
self.direction -= 1
self.image = pygame.transform.rotate(self.image, 90)
#checks if we can move further and if we won't get out of boundaries - for bfs:
def can_it_move_node(node):
if node.get_direction() == DIRECTION_EAST and node.get_x() + TILE_SIZE < 830: #last tile on the west has y:797
return "move east"
elif node.get_direction() == DIRECTION_WEST and node.get_x() - TILE_SIZE >=fieldX:
return "move west"
elif node.get_direction() == DIRECTION_NORTH and node.get_y() - TILE_SIZE >=fieldY:
return "move north"
elif node.get_direction() == DIRECTION_SOUTH and node.get_y() + TILE_SIZE < 760: #last tile on the south has y:727
return "move south"
else:
return False
#checks if there's an obstacle on the given coordinates (important for bfs):
def is_obstacle(self, x, y):
for tile in tiles:
if tile.x == x and tile.y == y:
ground = tile.ground
if ground.obstacle and self.tractor_start != (x, y) and self.tractor_end != (x,y):
return True
return False
def draw_tractor(self, win):
imageTractor = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
win.blit(imageTractor, (self.rect.x, self.rect.y))
pygame.display.flip()
#translates move_list generated by bfs into the actual movement:
def do_actions(tractor, WIN, move_list):
trail = pygame.image.load("resources/images/background.jpg").convert_alpha()
trail = pygame.transform.scale(trail, (TILE_SIZE, TILE_SIZE))
pygame.display.update()
for move in move_list:
WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE))
if move == "move":
tractor.move()
elif move == "rotate_right":
tractor.rotate_to_right()
elif move == "rotate_left":
tractor.rotate_to_left()
tractor.draw_tractor(WIN)
pygame.display.update()
time.sleep(1)