SzybciorSmartTraktor/agent.py
2022-04-28 09:21:32 +02:00

78 lines
3.2 KiB
Python

import pygame
import settings
class Instance:
def __init__(self, tank_capacity, direction):
size = settings.Field.size
self.rect = pygame.Rect(0, 0, size(), size())
self.action = ''
self.tank_capacity = tank_capacity
self.direction = direction
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 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'