135 lines
4.1 KiB
Python
135 lines
4.1 KiB
Python
# libraries
|
|
import time
|
|
from pyglet.gl import * # for blocky textures
|
|
import random
|
|
# other files of this project
|
|
import project_constants as const
|
|
import minefield as mf
|
|
import searching_algorithms.a_star as a_star
|
|
from display_assets import blit_graphics
|
|
from ui.input_box import *
|
|
from ui.button import *
|
|
from ui.text_box import *
|
|
import json_generator as jg
|
|
|
|
|
|
def main():
|
|
pygame.init()
|
|
pygame.display.set_caption(const.V_NAME_OF_WINDOW)
|
|
jes = jg.JsonGenerator()
|
|
print(jes.get_grid())
|
|
|
|
# for blocky textures
|
|
glEnable(GL_TEXTURE_2D)
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
|
|
|
# FPS clock
|
|
clock = pygame.time.Clock()
|
|
|
|
# create an instance of Minefield, pass necessary data
|
|
minefield = mf.Minefield(const.MAP_RANDOM_10x10)
|
|
|
|
# setting flags for program
|
|
running = True
|
|
in_menu = True
|
|
|
|
# counting coordinates for ui components
|
|
ib_width, ib_height = 100, 65
|
|
bt_width, bt_height = 210, 55
|
|
ib_y = 200
|
|
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
|
|
|
|
# creating ui components used in menu
|
|
input1 = InputBox(
|
|
position=(ib1_x, ib_y),
|
|
dimensions=(ib_width, ib_height),
|
|
text="row",
|
|
input_centered=True,
|
|
clear_input_on_click=True
|
|
)
|
|
input2 = InputBox(
|
|
position=(ib2_x, ib_y),
|
|
dimensions=(ib_width, ib_height),
|
|
text="column",
|
|
input_centered=True,
|
|
clear_input_on_click=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")
|
|
|
|
# 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()
|
|
|
|
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)
|
|
|
|
ok_button.draw(const.SCREEN, pygame.mouse.get_pos())
|
|
random_button.draw(const.SCREEN, pygame.mouse.get_pos())
|
|
|
|
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)
|
|
|
|
if running:
|
|
to_x = int(input1.get_input())
|
|
to_y = int(input2.get_input())
|
|
|
|
action_sequence = a_star.graphsearch(
|
|
initial_state=a_star.State(
|
|
row=minefield.agent.position[0],
|
|
column=minefield.agent.position[1],
|
|
direction=minefield.agent.direction),
|
|
minefield=minefield, tox=to_x, toy=to_y)
|
|
|
|
while running and not in_menu:
|
|
|
|
# FPS control
|
|
clock.tick(const.V_FPS)
|
|
|
|
# graphics (from display_assets)
|
|
blit_graphics(minefield)
|
|
|
|
# make the next move from sequence of actions
|
|
if any(action_sequence):
|
|
|
|
action = action_sequence.pop(0)
|
|
|
|
if action == const.Action.ROTATE_LEFT:
|
|
minefield.agent.rotate_left()
|
|
elif action == const.Action.ROTATE_RIGHT:
|
|
minefield.agent.rotate_right()
|
|
elif action == const.Action.GO:
|
|
minefield.agent.go()
|
|
|
|
time.sleep(const.ACTION_INTERVAL)
|
|
|
|
else:
|
|
in_menu = True
|
|
time.sleep(const.ACTION_INTERVAL)
|
|
# 'for event in pygame.event.get():
|
|
# if event.type == pygame.QUIT:
|
|
# running = False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|