From d8f1101bc67c206f7d6af7e5daee9a06324bd049 Mon Sep 17 00:00:00 2001 From: JakubR Date: Wed, 5 May 2021 13:48:48 +0200 Subject: [PATCH] minor changes in main; code refactoring --- main.py | 78 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 29 deletions(-) diff --git a/main.py b/main.py index 9ea5ced..5153d31 100644 --- a/main.py +++ b/main.py @@ -10,11 +10,12 @@ import project_constants as const import minefield as mf import searching_algorithms.bfs as bfs from display_assets import blit_graphics -from ui.input_box import * -from ui.button import * -from ui.text_box import * -def main(): +from ui.input_box import * +from ui.button import * +from ui.text_box import * + +def main(): pygame.init() pygame.display.set_caption(const.V_NAME_OF_WINDOW) @@ -28,44 +29,66 @@ def main(): # create an instance of Minefield, pass necessary data minefield = mf.Minefield(const.MAP_RANDOM_10x10) - # get sequence of actions found by BFS algorithm + # setting flags for program + running = True + in_menu = True - - running = False + # counting coordinates for ui components + ib_width, ib_height = 65, 48 + bt_width, bt_height = 140, 32 + ib_y = 230 + ib1_x = const.SCREEN.get_width() / 2 - ib_width - 5 + ib2_x = const.SCREEN.get_width() / 2 + 5 + bt_x = const.SCREEN.get_width() / 2 - bt_width / 2 + bt1_y = ib_y + ib_height + 10 + bt2_y = bt1_y + bt_height + 10 # basic font for user typed - input1=InputBox(position=(200,200),dimensions=(140,32),label="x") - input2 = InputBox(position=(200, 250), dimensions=(140, 32),label="y") - randombutton = Button(position=(200,350),dimensions=(140,32),text="random") - okbutton = Button(position=(200,300),dimensions=(140,32),text="ok") - while True: + input1 = InputBox(position=(ib1_x, ib_y), dimensions=(ib_width, ib_height), label="x", input_centered=True) + input2 = InputBox(position=(ib2_x, ib_y), dimensions=(ib_width, ib_height), label="y", input_centered=True) + random_button = Button(position=(bt_x, bt1_y), dimensions=(bt_width, bt_height), text="random") + ok_button = Button(position=(bt_x, bt2_y), dimensions=(bt_width, bt_height), text="ok") - while running== False: + # initializing goal position + to_x, to_y = 0, 0 + + while running: + + while running and in_menu: const.SCREEN.fill((255, 255, 255)) - events= pygame.event.get() - input1.run(const.SCREEN,pygame.mouse.get_pos(),events) + events = pygame.event.get() + + for event in events: + if event.type == pygame.QUIT: + running = False + + input1.run(const.SCREEN, pygame.mouse.get_pos(), events) input2.run(const.SCREEN, pygame.mouse.get_pos(), events) - okbutton.draw(const.SCREEN, pygame.mouse.get_pos()) - randombutton.draw(const.SCREEN, pygame.mouse.get_pos()) + ok_button.draw(const.SCREEN, pygame.mouse.get_pos()) + random_button.draw(const.SCREEN, pygame.mouse.get_pos()) - if okbutton.is_clicked(pygame.mouse.get_pos(),events): - running = True - if randombutton.is_clicked(pygame.mouse.get_pos(), events): + if ok_button.is_clicked(pygame.mouse.get_pos(), events): + in_menu = False + if random_button.is_clicked(pygame.mouse.get_pos(), events): input1.set_texts(user_input=str(random.randint(0, 9))) input2.set_texts(user_input=str(random.randint(0, 9))) pygame.display.flip() clock.tick(const.V_FPS) - toy=int(input1.get_input()) - tox = int(input2.get_input()) + + if running: + to_x = int(input1.get_input()) + to_y = int(input2.get_input()) + action_sequence = bfs.graphsearch( initial_state=bfs.State( row=minefield.agent.position[0], column=minefield.agent.position[1], direction=minefield.agent.direction), - minefield=minefield, tox=tox, toy = toy) - while running: + minefield=minefield, tox=to_x, toy=to_y) + + while running and not in_menu: # FPS control clock.tick(const.V_FPS) @@ -88,15 +111,12 @@ def main(): time.sleep(const.ACTION_INTERVAL) else: - running = False + in_menu = True time.sleep(const.ACTION_INTERVAL) - #'for event in pygame.event.get(): + # 'for event in pygame.event.get(): # if event.type == pygame.QUIT: # running = False - - - if __name__ == "__main__": main()