-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ Modularised most of everything, integrating dearch methods
- Loading branch information
1 parent
457b95f
commit 66fddb1
Showing
8 changed files
with
227 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,136 +1,111 @@ | ||
# /usr/bin/env python3 | ||
|
||
|
||
import pygame | ||
import numpy as np | ||
|
||
import pygame as pg | ||
from .constants import SCR_SIZE, BG_COLOR, RED, WHITE, NODE_RADIUS, SEARCH_RATE | ||
|
||
|
||
from .Search import Search | ||
from .Graph import Graph | ||
|
||
|
||
class Game: | ||
def __init__(self, search_method: Search, num_nodes: int): | ||
pygame.init() | ||
pg.init() | ||
|
||
# main attributes of the game | ||
self.search_method = search_method | ||
self.Graph = Graph(num_nodes) | ||
|
||
# pygame initialization | ||
self.screen = pygame.display.set_mode(SCR_SIZE) | ||
pygame.display.set_caption(f"Search Method: {search_method.name}") | ||
# pg initialization | ||
self.screen = pg.display.set_mode(SCR_SIZE) | ||
self.graph_surf = pg.Surface(self.screen.get_size(), pg.SRCALPHA) | ||
pg.display.set_caption(f"Search Method: {search_method.name}") | ||
|
||
# more helper attributes | ||
self.font = pg.font.Font(None, 36) | ||
self.bg_surf = pg.Surface(self.screen.get_size()) | ||
self.bg_surf.fill(BG_COLOR) | ||
|
||
# control flow related attributes | ||
self.start_search = False | ||
self.running = True | ||
|
||
print("🐼 searchViz has been initialized! 🎉") | ||
|
||
def handle_events(self): | ||
for event in pg.event.get(): | ||
if event.type == pg.QUIT: | ||
self.running = False | ||
pg.quit() | ||
print("\n\t🐼 Bye from searchViz 🔥") | ||
exit() | ||
if event.type == pg.MOUSEBUTTONDOWN: | ||
if event.button == 1: # Left mouse button | ||
click_x, click_y = event.pos | ||
pg.draw.circle( | ||
self.graph_surf, | ||
RED, | ||
(click_x, click_y), | ||
radius=5 * NODE_RADIUS, | ||
) | ||
|
||
def run(self): | ||
# Handle spacebar key press | ||
if event.type == pg.KEYDOWN: | ||
if event.key == pg.K_SPACE: | ||
self.start_search = not self.start_search | ||
|
||
return None | ||
|
||
def draw_graph(self): | ||
# unpacking frequently used variables | ||
graph_surf = self.graph_surf | ||
num_nodes = self.Graph.num_nodes | ||
nodes = self.Graph.nodes | ||
colors = self.Graph.colors | ||
radius = self.Graph.radius | ||
edges = self.Graph.edges | ||
|
||
open = int(np.random.randint(0, num_nodes, 1)[0]) | ||
closed = [] | ||
|
||
screen = self.screen | ||
|
||
step = 1 | ||
start_search = False | ||
font = pygame.font.Font(None, 36) | ||
last_step_update_time = pygame.time.get_ticks() | ||
|
||
# Precompute some values outside the loop | ||
bg_surface = pygame.Surface(screen.get_size()) | ||
bg_surface.fill(BG_COLOR) | ||
|
||
graph_surface = pygame.Surface(screen.get_size(), pygame.SRCALPHA) | ||
# need to use this when traversing | ||
# edge_colors = self.Graph.edge_color | ||
|
||
# Draw nodes and their edges | ||
for i in range(num_nodes): | ||
# Unique (uni-directional) edges | ||
for j in range(i, num_nodes): | ||
if edges[i, j]: | ||
pygame.draw.line(graph_surface, WHITE, nodes[i], nodes[j]) | ||
pg.draw.line(graph_surf, WHITE, nodes[i], nodes[j]) | ||
|
||
node = nodes[i] | ||
pygame.draw.circle( | ||
graph_surface, | ||
pg.draw.circle( | ||
graph_surf, | ||
color=tuple(colors[i]), | ||
center=node, | ||
radius=radius[i], | ||
) | ||
|
||
running = True | ||
while running: | ||
current_time = pygame.time.get_ticks() | ||
# Clear the screen once at the beginning of the frame | ||
# graph_surface.blit(bg_surface, (0, 0)) | ||
|
||
screen.blit(bg_surface, (0, 0)) | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
running = False | ||
if event.type == pygame.MOUSEBUTTONDOWN: | ||
if event.button == 1: # Left mouse button | ||
click_x, click_y = event.pos | ||
|
||
pygame.draw.circle( | ||
graph_surface, | ||
RED, | ||
(click_x, click_y), | ||
radius=5 * NODE_RADIUS, | ||
) | ||
# Handle spacebar key press | ||
if event.type == pygame.KEYDOWN: | ||
if event.key == pygame.K_SPACE: | ||
start_search = not start_search | ||
|
||
screen.blit(graph_surface, (0, 0)) | ||
|
||
# Update game logic here | ||
if start_search: | ||
# Calculate the time elapsed since the last step update | ||
time_elapsed = current_time - last_step_update_time | ||
|
||
# Check if 5 seconds (5000 milliseconds) have passed | ||
if time_elapsed >= SEARCH_RATE * 1000: | ||
# Increment the step and update the last step update time | ||
def run(self): | ||
last_time = pg.time.get_ticks() | ||
step = 0 | ||
self.draw_graph() | ||
while self.running: | ||
cur_time = pg.time.get_ticks() | ||
|
||
self.screen.blit(self.bg_surf, (0, 0)) | ||
self.handle_events() | ||
self.screen.blit(self.graph_surf, (0, 0)) | ||
|
||
_txt = self.font.render(f"searchViz: {step}", 1, (255, 255, 255)) | ||
self.screen.blit(_txt, (10, 10)) | ||
|
||
pg.display.flip() | ||
|
||
# Time control | ||
if self.start_search: | ||
_delta = cur_time - last_time | ||
if _delta >= SEARCH_RATE * 1000: | ||
step += 1 | ||
last_step_update_time = current_time | ||
# draw node | ||
|
||
node = nodes[open] | ||
pygame.draw.circle( | ||
graph_surface, | ||
color=RED, | ||
center=nodes[open], | ||
radius=2 * radius[open], | ||
) | ||
last_time = cur_time | ||
# APPLY SEARCH HERE | ||
|
||
flag = False | ||
for i in range(open, num_nodes): | ||
connected = edges[open, i] | ||
if connected: | ||
closed.append(i) | ||
open = i | ||
flag = True | ||
break | ||
|
||
if not flag: | ||
for i in range(0, open): | ||
connected = edges[i, open] | ||
if connected and (i not in closed): | ||
closed.append(i) | ||
open = i | ||
flag = True | ||
break | ||
if not flag: | ||
open = int(np.random.randint(0, num_nodes, 1)[0]) | ||
|
||
step_text = font.render(f"Step: {step}", True, (255, 255, 255)) | ||
screen.blit(step_text, (10, 10)) # Adjust the position as needed | ||
|
||
pygame.display.flip() | ||
|
||
pygame.quit() | ||
pg.quit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.