-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththrees_gui.py
More file actions
123 lines (96 loc) · 4.23 KB
/
threes_gui.py
File metadata and controls
123 lines (96 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pygame
import os
import subprocess
import sys
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
BLUE = (70, 130, 180)
GREEN = (50, 205, 50)
RED = (255, 69, 0)
pygame.init()
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 450
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Threes GUI")
TITLE_FONT = pygame.font.Font(None, 60)
SUBTITLE_FONT = pygame.font.Font(None, 40)
BUTTON_FONT = pygame.font.Font(None, 30)
# Obtener la ruta del directorio actual
script_directory = os.path.dirname(os.path.abspath(__file__)).replace('\\', '\\\\')
# Estado para animaciones instantáneas (por defecto OFF)
instant_animation = False
# Ejecutar scripts
def run_script(script_name, fast=False, agent=True):
script_path = os.path.join(script_directory, script_name)
if fast:
subprocess.run(["python", script_path, "5000", "100"])
else:
subprocess.run(["python", script_path, "60", "20"])
if agent:
# Abrir movements.txt con la aplicación por defecto del sistema
movements_path = os.path.join(script_directory, "movements.txt")
if sys.platform.startswith('win'): # Windows
os.startfile(movements_path)
elif sys.platform.startswith('darwin'): # macOS
subprocess.run(['open', movements_path])
else: # Linux
subprocess.run(['xdg-open', movements_path])
def play_game(fast=False):
game_path = os.path.join(script_directory, "threes.py")
run_script(game_path, fast=fast, agent=False)
def run_agent_bfs(fast=False):
bfs_path = os.path.join(script_directory, "agent_bfs.py")
run_script(bfs_path, fast=fast)
def run_agent_astar(fast=False):
astar_path = os.path.join(script_directory, "agent_astar.py")
run_script(astar_path, fast=fast)
# Crear botones
def draw_button(text, x, y, width, height, action=None, selected=False):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
color = GRAY if x + width > mouse[0] > x and y + height > mouse[1] > y else WHITE
pygame.draw.rect(screen, color, (x, y, width, height))
if selected:
pygame.draw.rect(screen, GREEN, (x, y, width, height), 2)
if click[0] == 1 and action is not None and color == GRAY:
action()
text_surface = BUTTON_FONT.render(text, True, BLACK)
screen.blit(text_surface, (x + (width - text_surface.get_width()) // 2, y + (height - text_surface.get_height()) // 2))
# Cambiar el estado de las animaciones instantáneas
def set_instant_animation(state):
global instant_animation
instant_animation = state
# Función principal de la GUI
def main_menu():
running = True
while running:
screen.fill(WHITE)
# Títulos
title_text = TITLE_FONT.render("Threes", True, BLUE)
subtitle_text = SUBTITLE_FONT.render("Selecciona una opción", True, BLACK)
screen.blit(title_text, ((SCREEN_WIDTH - title_text.get_width()) // 2, 20))
screen.blit(subtitle_text, ((SCREEN_WIDTH - subtitle_text.get_width()) // 2, 80))
# Jugar desde el teclado
play_title = BUTTON_FONT.render("Jugar desde teclado", True, BLACK)
screen.blit(play_title, (50, 140))
draw_button("Jugar", 350, 135, 100, 40, lambda: play_game(instant_animation))
# Resolver con un agente
agent_title = BUTTON_FONT.render("Resolver con un agente", True, BLACK)
screen.blit(agent_title, (50, 200))
draw_button("BFS", 350, 195, 100, 40, lambda: run_agent_bfs(instant_animation))
draw_button("A*", 350, 245, 100, 40, lambda: run_agent_astar(instant_animation))
# Animaciones instantáneas
animation_title = BUTTON_FONT.render("Animaciones instantáneas", True, BLACK)
screen.blit(animation_title, (50, 320))
draw_button("ON", 350, 312, 100, 40, lambda: set_instant_animation(True), selected=instant_animation)
draw_button("OFF", 350, 357, 100, 40, lambda: set_instant_animation(False), selected=not instant_animation)
# Actualizar la pantalla
pygame.display.update()
# Eventos de salida
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if __name__ == "__main__":
main_menu()