refactor code

This commit is contained in:
Dominik Cupał 2021-04-13 10:40:56 +02:00
commit c4a1c40f4a
5 changed files with 69 additions and 23 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/python3
import copy
from queue import Queue
from threading import Event
import pygame
@ -17,7 +18,7 @@ class App:
self.__clock = None
self.__board = Board()
self.__tractor = Tractor(self.__board)
self.__bot_is_running = False
self.__bot_is_running = Event()
self.__moves = None
def initialize(self):
@ -39,16 +40,16 @@ class App:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.__tractor.move_up()
print(self.__tractor)
self.__tractor.direction_up()
if keys[pygame.K_DOWN]:
self.__tractor.move_down()
print(self.__tractor)
self.__tractor.direction_down()
if keys[pygame.K_LEFT]:
self.__tractor.move_left()
print(self.__tractor)
self.__tractor.direction_left()
if keys[pygame.K_RIGHT]:
self.__tractor.move_right()
self.__tractor.direction_right()
if keys[pygame.K_m]:
self.__tractor.move()
print(self.__tractor)
if keys[pygame.K_w]:
@ -79,8 +80,8 @@ class App:
print(f"Bfs is failed")
else:
print(f"Bfs is succeed")
self.__bot_is_running = True
self.__tractor.move_by_bfs_handler(self.__moves)
self.__bot_is_running.set()
self.__tractor.move_by_bfs_handler(self.__moves, self.__bot_is_running)
def update_screen(self):
@ -91,7 +92,7 @@ class App:
def get_moves_by_bfs(self):
x, y = self.__tractor.get_position()
node = Node(None, x, y, self.__tractor.get_direction(), 0, "initial state", "movement")
node = Node(None, x, y, self.__tractor.get_direction(), 0, "movement", "initial state")
board = copy.deepcopy(self.__board)
self.__moves = Graphsearch.bfs(Queue(), Queue(), node,
lambda n=node, b=board: Graphsearch.succ(n, b),
@ -105,7 +106,7 @@ class App:
for event in pygame.event.get():
self.event_handler(event)
if not self.__bot_is_running:
if not self.__bot_is_running.is_set():
self.keys_pressed_handler()
self.loop_handler()

View File

@ -12,6 +12,12 @@ class BaseField:
img = pygame.image.load(self._img_path)
img = pygame.transform.rotate(img, angle)
# def draw_field(self, screen: pygame.Surface, pos_x: int, pos_y: int, is_centered: bool = False, size: tuple = None,
# angle: int = 0):
# pre_img = pygame.image.load(self._img_path)
# if angle == 90:
# pre_img = pygame.transform.flip(pre_img, True, False)
# img = pygame.transform.rotate(pre_img, angle)
scale = pygame.transform.scale(img, (FIELD_SIZE, FIELD_SIZE))
rect = img.get_rect()

View File

@ -28,6 +28,7 @@ class Tractor(BaseField):
self.__move = FIELD_SIZE
self.__board = board
self.__harvested_corps = []
self.__fuel = 10
def draw(self, screen: pygame.Surface):
self.draw_field(screen, self.__pos_x + FIELD_SIZE / 2, self.__pos_y + FIELD_SIZE / 2,
@ -35,11 +36,11 @@ class Tractor(BaseField):
# Key methods handlers
def move(self):
if self.__direction == 0.0:
if self.__direction == D_EAST:
self.move_right()
elif self.__direction == 90.0:
elif self.__direction == D_NORTH:
self.move_up()
elif self.__direction == 180.0:
elif self.__direction == D_WEST:
self.move_left()
else:
self.move_down()
@ -60,6 +61,41 @@ class Tractor(BaseField):
if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH:
self.__pos_x += self.__move
def direction_up(self):
self.__direction = 0
def direction_right(self):
self.__direction = 270
def direction_down(self):
self.__direction = 180
def direction_left(self):
self.__direction = 90
# def move(self):
# if self.__fuel > 0:
# self.__fuel -= 1
#
# if self.__direction == 0:
# if self.__pos_y - self.__move >= 0:
# self.__pos_y -= self.__move
#
# elif self.__direction == 270:
# if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH:
# self.__pos_x += self.__move
#
# elif self.__direction == 180:
# if self.__pos_y + self.__move + FIELD_SIZE <= HEIGHT:
# self.__pos_y += self.__move
#
# elif self.__direction == 90:
# if self.__pos_x - self.__move >= 0:
# self.__pos_x -= self.__move
# else:
# print("Run out of fuel!")
def rotate_left(self):
self.__direction = (self.__direction - 90.0) % 360.0
@ -91,6 +127,7 @@ class Tractor(BaseField):
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:
@ -178,13 +215,13 @@ class Tractor(BaseField):
def get_direction(self):
return self.__direction
def move_by_bfs_handler(self, moves: list[tuple[str, str]]):
thread = threading.Thread(target=self.move_by_bfs, args=(moves,))
def move_by_bfs_handler(self, moves: list[tuple[str, str]], is_running: threading.Event):
thread = threading.Thread(target=self.move_by_bfs, args=(moves, is_running), daemon=True)
thread.start()
def move_by_bfs(self, moves: list[tuple[str, str]]):
def move_by_bfs(self, moves: list[tuple[str, str]], is_running: threading.Event):
print(moves)
print(f"Length of Moves {len(moves)} - {3**len(moves)}")
print(f"Length of Moves {len(moves)} - {3 ** len(moves)}")
while len(moves) > 0:
movement, action = moves.pop(0)
# do action
@ -211,6 +248,7 @@ class Tractor(BaseField):
self.rotate_right()
time.sleep(TIME_OF_MOVING)
is_running.clear()
@staticmethod
def move_is_correct(x: int, y: int, direction: float) -> Union[(int, int), None]:
@ -259,3 +297,4 @@ class Tractor(BaseField):
obj = get_class("app.fields", choosen_type)
board.get_fields()[x][y] = obj()
return obj()
# return f"Position: {x}:{y} - {type(self.__board.get_fields()[x][y]).__name__}\nFuel: {self.__fuel}\n"

View File

@ -44,10 +44,10 @@ SUNFLOWER = 'sunflower'
FIELD_TYPES = (SAND, CLAY, GRASS, CORN, SUNFLOWER)
# Directions
D_NORTH = 90.0
D_EAST = 0.0
D_SOUTH = 270.0
D_WEST = 180.0
D_NORTH = 0.0
D_EAST = 270.0
D_SOUTH = 180.0
D_WEST = 90.0
# Goal Test
AMOUNT_OF_CROPS = 5

Binary file not shown.

Before

Width:  |  Height:  |  Size: 516 B

After

Width:  |  Height:  |  Size: 1.4 KiB