SzybciorSmartTraktor/agent.py
2022-05-12 21:49:22 +02:00

124 lines
4.2 KiB
Python

import pygame
import settings
class Instance:
def __init__(self, tank_capacity, direction, seeds, feritizer, water, carrots, potatoes, wheat):
size = settings.Field.size
self.rect = pygame.Rect(0, 0, size(), size())
self.action = ''
self.tank_capacity = tank_capacity
self.direction = direction
self.seeds = seeds
self.feritizer = feritizer
self.water = water
self.carrots = carrots
self.potatoes = potatoes
self.wheat = wheat
def coordinates(self):
return {
'x': self.x(),
'y': self.y()
}
def x(self):
return int(self.rect.x / settings.Field.size())
def y(self):
return int(self.rect.y / settings.Field.size())
def get_tank_capacity(self):
return self.tank_capacity
def set_tank_capacity(self, fuel_units):
self.tank_capacity = fuel_units
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_seeds(self):
return self.seeds
def set_seeds(self, seeds):
self.seeds = seeds
def get_feritizer(self):
return self.feritizer
def set_feritizer(self, feritizer):
self.feritizer = feritizer
def get_water(self):
return self.water
def set_water(self, water):
self.water = water
def get_carrots(self):
return self.carrots
def set_carrots(self, carrots):
self.carrots = carrots
def get_potatoes(self):
return self.potatoes
def set_potatoes(self, potatoes):
self.potatoes = potatoes
def get_wheat(self):
return self.wheat
def set_wheat(self, wheat):
self.wheat = wheat
def move(self, action):
key_pressed = pygame.key.get_pressed()
height = settings.Pygame.height()
width = settings.Pygame.width()
tile_size = settings.Field.size()
if key_pressed[pygame.K_UP] and self.direction == 4 and self.rect.x > 0 and action is None:
self.rect.x -= tile_size
elif key_pressed[pygame.K_UP] and self.direction == 2 and self.rect.x < width - tile_size and action is None:
self.rect.x += tile_size
elif key_pressed[pygame.K_UP] and self.direction == 1 and self.rect.y > 0 and action is None:
self.rect.y -= tile_size
elif key_pressed[pygame.K_UP] and self.direction == 3 and self.rect.y < height - tile_size and action is None:
self.rect.y += tile_size
elif action == 'f' and self.direction == 4 and self.rect.x > 0:
self.rect.x -= tile_size
elif action == 'f' and self.direction == 2 and self.rect.x < width - tile_size:
self.rect.x += tile_size
elif action == 'f' and self.direction == 1 and self.rect.y > 0:
self.rect.y -= tile_size
elif action == 'f' and self.direction == 3 and self.rect.y < height - tile_size:
self.rect.y += tile_size
elif key_pressed[pygame.K_SPACE]:
return 'open_window'
return 'none'
def rotate(self, action):
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LEFT] and self.direction == 2 or action == 'l' and self.direction == 2:
self.direction = 1
elif key_pressed[pygame.K_LEFT] and self.direction == 1 or action == 'l' and self.direction == 1:
self.direction = 4
elif key_pressed[pygame.K_LEFT] and self.direction == 4 or action == 'l' and self.direction == 4:
self.direction = 3
elif key_pressed[pygame.K_LEFT] and self.direction == 3 or action == 'l' and self.direction == 3:
self.direction = 2
elif key_pressed[pygame.K_RIGHT] and self.direction == 1 or action == 'r' and self.direction == 1:
self.direction = 2
elif key_pressed[pygame.K_RIGHT] and self.direction == 4 or action == 'r' and self.direction == 4:
self.direction = 1
elif key_pressed[pygame.K_RIGHT] and self.direction == 3 or action == 'r' and self.direction == 3:
self.direction = 4
elif key_pressed[pygame.K_RIGHT] and self.direction == 2 or action == 'r' and self.direction == 2:
self.direction = 3
return 'none'