minor changes in main; code refactoring

This commit is contained in:
JakubR 2021-05-05 13:48:48 +02:00
parent 0d2955f90a
commit d8f1101bc6

66
main.py
View File

@ -13,8 +13,9 @@ from display_assets import blit_graphics
from ui.input_box import * from ui.input_box import *
from ui.button import * from ui.button import *
from ui.text_box import * from ui.text_box import *
def main():
def main():
pygame.init() pygame.init()
pygame.display.set_caption(const.V_NAME_OF_WINDOW) pygame.display.set_caption(const.V_NAME_OF_WINDOW)
@ -28,44 +29,66 @@ def main():
# create an instance of Minefield, pass necessary data # create an instance of Minefield, pass necessary data
minefield = mf.Minefield(const.MAP_RANDOM_10x10) minefield = mf.Minefield(const.MAP_RANDOM_10x10)
# get sequence of actions found by BFS algorithm # setting flags for program
running = True
in_menu = True
# counting coordinates for ui components
running = False 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 # basic font for user typed
input1=InputBox(position=(200,200),dimensions=(140,32),label="x") input1 = InputBox(position=(ib1_x, ib_y), dimensions=(ib_width, ib_height), label="x", input_centered=True)
input2 = InputBox(position=(200, 250), dimensions=(140, 32),label="y") input2 = InputBox(position=(ib2_x, ib_y), dimensions=(ib_width, ib_height), label="y", input_centered=True)
randombutton = Button(position=(200,350),dimensions=(140,32),text="random") random_button = Button(position=(bt_x, bt1_y), dimensions=(bt_width, bt_height), text="random")
okbutton = Button(position=(200,300),dimensions=(140,32),text="ok") ok_button = Button(position=(bt_x, bt2_y), dimensions=(bt_width, bt_height), text="ok")
while True:
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)) const.SCREEN.fill((255, 255, 255))
events = pygame.event.get() events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
input1.run(const.SCREEN, pygame.mouse.get_pos(), events) input1.run(const.SCREEN, pygame.mouse.get_pos(), events)
input2.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()) ok_button.draw(const.SCREEN, pygame.mouse.get_pos())
randombutton.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): if ok_button.is_clicked(pygame.mouse.get_pos(), events):
running = True in_menu = False
if randombutton.is_clicked(pygame.mouse.get_pos(), events): if random_button.is_clicked(pygame.mouse.get_pos(), events):
input1.set_texts(user_input=str(random.randint(0, 9))) input1.set_texts(user_input=str(random.randint(0, 9)))
input2.set_texts(user_input=str(random.randint(0, 9))) input2.set_texts(user_input=str(random.randint(0, 9)))
pygame.display.flip() pygame.display.flip()
clock.tick(const.V_FPS) 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( action_sequence = bfs.graphsearch(
initial_state=bfs.State( initial_state=bfs.State(
row=minefield.agent.position[0], row=minefield.agent.position[0],
column=minefield.agent.position[1], column=minefield.agent.position[1],
direction=minefield.agent.direction), direction=minefield.agent.direction),
minefield=minefield, tox=tox, toy = toy) minefield=minefield, tox=to_x, toy=to_y)
while running:
while running and not in_menu:
# FPS control # FPS control
clock.tick(const.V_FPS) clock.tick(const.V_FPS)
@ -88,15 +111,12 @@ def main():
time.sleep(const.ACTION_INTERVAL) time.sleep(const.ACTION_INTERVAL)
else: else:
running = False in_menu = True
time.sleep(const.ACTION_INTERVAL) time.sleep(const.ACTION_INTERVAL)
# 'for event in pygame.event.get(): # 'for event in pygame.event.get():
# if event.type == pygame.QUIT: # if event.type == pygame.QUIT:
# running = False # running = False
if __name__ == "__main__": if __name__ == "__main__":
main() main()