Traktor/source/area/tractor.py

206 lines
7.8 KiB
Python
Raw Permalink Normal View History

2024-06-09 21:34:39 +02:00
from NN.neural_network import clear_text_area
from crop_protection_product import CropProtectionProduct
2024-06-09 21:34:39 +02:00
from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, WIDTH
from area.field import fieldX, fieldY, tiles
2024-04-09 15:02:58 +02:00
import pygame
2024-04-14 17:20:27 +02:00
import time
2024-03-07 18:01:12 +01:00
class Tractor:
x = None
y = None
2024-04-10 01:52:13 +02:00
direction = None #direction takes values in the range of 1 to 4 (1->North, 2->East etc...)
2024-03-07 18:01:12 +01:00
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")
2024-03-07 18:01:12 +01:00
# etc
def __init__(self, x, y, direction, tractor_start, tractor_end):
2024-03-07 18:01:12 +01:00
self.x = x
self.y = y
2024-04-10 01:52:13 +02:00
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
2024-04-09 15:02:58 +02:00
self.direction = direction
2024-04-10 01:52:13 +02:00
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 #
2024-04-10 01:52:13 +02:00
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()
2024-04-09 15:02:58 +02:00
2024-06-09 21:34:39 +02:00
def work_on_field(self, screen, tile, ground, plant1):
results = []
if plant1 is None:
tile.randomizeContent()
# sprobuj zasadzic cos
2024-03-25 01:02:03 +01:00
print("Tarctor planted something")
2024-06-09 21:34:39 +02:00
results.append("Tarctor planted something")
elif plant1.growth_level == 100:
tile.plant = None
2024-03-25 01:02:03 +01:00
ground.nutrients_level -= 40
ground.water_level -= 40
print("Tractor collected something")
2024-06-09 21:34:39 +02:00
results.append("Tractor collected something")
else:
2024-03-25 01:02:03 +01:00
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)
2024-06-09 21:34:39 +02:00
results.append(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)
2024-06-09 21:34:39 +02:00
results.append(t)
ground.weed = False
2024-03-25 01:02:03 +01:00
if ground.water_level < plant1.water_requirements:
ground.water_level += 20
print("Tractor watered the plant")
2024-06-09 21:34:39 +02:00
results.append("Tractor watered the plant")
2024-03-25 01:02:03 +01:00
if ground.nutrients_level < plant1.nutrients_requirements:
ground.nutrients_level += 20
print("Tractor added some nutrients")
2024-06-09 21:34:39 +02:00
results.append("Tractor added some nutrients")
clear_text_area(screen, WIDTH-90, 100, 400, 100)
for idx, result in enumerate(results):
display_work_results(screen, result, (WIDTH-90, 100 + idx * 30))
2024-04-09 15:02:58 +02:00
def move(self):
if self.direction == DIRECTION_EAST:
2024-04-10 01:52:13 +02:00
self.rect.x += TILE_SIZE
2024-04-09 15:02:58 +02:00
elif self.direction == DIRECTION_WEST:
2024-04-10 01:52:13 +02:00
self.rect.x -= TILE_SIZE
2024-04-09 15:02:58 +02:00
elif self.direction == DIRECTION_NORTH:
2024-04-10 01:52:13 +02:00
self.rect.y -= TILE_SIZE
2024-04-09 15:02:58 +02:00
elif self.direction == DIRECTION_SOUTH:
2024-04-10 01:52:13 +02:00
self.rect.y += TILE_SIZE
2024-04-09 15:02:58 +02:00
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:
2024-04-14 14:28:40 +02:00
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
2024-04-14 14:28:40 +02:00
return "move east"
elif node.get_direction() == DIRECTION_WEST and node.get_x() - TILE_SIZE >=fieldX:
2024-04-14 14:28:40 +02:00
return "move west"
elif node.get_direction() == DIRECTION_NORTH and node.get_y() - TILE_SIZE >=fieldY:
2024-04-14 14:28:40 +02:00
return "move north"
elif node.get_direction() == DIRECTION_SOUTH and node.get_y() + TILE_SIZE < 760: #last tile on the south has y:727
2024-04-14 14:28:40 +02:00
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
2024-04-14 14:28:40 +02:00
2024-04-09 15:02:58 +02:00
def draw_tractor(self, win):
imageTractor = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
2024-04-10 01:52:13 +02:00
win.blit(imageTractor, (self.rect.x, self.rect.y))
pygame.display.flip()
def store_tile_image(self, tile):
return pygame.image.load(tile.image).convert_alpha()
def restore_tile_image(self, screen, tile):
image = pygame.image.load(tile.image).convert_alpha()
image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE))
screen.blit(image, (tile.x, tile.y))
pygame.display.update()
#translates move_list generated by bfs into the actual movement:
2024-04-14 17:20:27 +02:00
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))
tile_images = {}
for tile in tiles:
tile_images[(tile.x, tile.y)] = tractor.store_tile_image(tile)
2024-04-14 17:20:27 +02:00
pygame.display.update()
for move in move_list:
# WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE))
current_tile = None
for tile in tiles:
if tile.x == tractor.rect.x and tile.y == tractor.rect.y:
current_tile = tile
break
if current_tile:
tractor.restore_tile_image(WIN, current_tile)
2024-04-14 17:20:27 +02:00
if move == "move":
tractor.move()
elif move == "rotate_right":
tractor.rotate_to_right()
elif move == "rotate_left":
tractor.rotate_to_left()
2024-04-14 17:20:27 +02:00
tractor.draw_tractor(WIN)
pygame.display.update()
2024-06-10 11:21:58 +02:00
time.sleep(0.35)
2024-06-09 21:34:39 +02:00
#displays results of the "work_on_field" function next to the field:
def display_work_results(screen, text, position):
font = pygame.font.Font(None, 30)
displayed_text = font.render(text, 1, (255,255,255))
screen.blit(displayed_text, position)
pygame.display.update()