diff --git a/ai.py b/ai.py index d027efa..ab15258 100644 --- a/ai.py +++ b/ai.py @@ -1,6 +1,6 @@ from random import choice, seed from copy import deepcopy -from time import time +from time import time, sleep from config import * from global_items import bodies, handle, plants, data_for_smart_body @@ -8,10 +8,20 @@ from crosses import delete_all_crosses from window_functions import update_progress, show_progress_bar, delete_progress_bar from tips import show_tip +from global_items import window_commands +import global_items @handle def shape_ai_guess(): - select_ai_survivor().shape = SQUARE + (sqaure := select_ai_survivor()).shape = SQUARE + if window_commands['hold-evolution-start-back']: + for k in range(9): + global_items.canvas.itemconfig(sqaure.image_reference, fill='black' if k % 2 == 0 else 'white') + sleep(0.1) + global_items.canvas.update() + global_items.canvas.itemconfig(sqaure.image_reference, fill='#%02x%02x%02x' % sqaure.species) + sleep(0.1) + global_items.canvas.update() def select_ai_survivor() -> object: show_tip('Working on the prediction of the AI...') diff --git a/draw_erase.py b/draw_erase.py index 7642ef9..297b895 100644 --- a/draw_erase.py +++ b/draw_erase.py @@ -149,57 +149,40 @@ def update_vision_distance_circles(): for body in bodies: draw_one_vision_distance_circle(body) -def prepare_draw_handle(): - global privious_window_command - privious_window_command = None - # Writing properties of bodies over them - -def update_body_properties(): - global privious_window_command - new_window_command = window_commands['to-show-selected-property'] - if evolution_status.description in (USER_SELECTING_BODY, ON_PAUSE): - if new_window_command == privious_window_command: - return - privious_window_command = new_window_command +def display_property(body: object,text: str): + center_x, center_y = body.x+CANVAS_BORDER, body.y+CANVAS_BORDER + global_items.canvas.create_text( + center_x, center_y-HALF_BODY_SIZE, + text=text, + tags='property', + anchor=S + ) + +def handle_body_properties(): erase_body_properties() erase_circles() for body in bodies: - match new_window_command: + match window_commands['to-show-selected-property']: case '"Newly born" if newly born': - text = 'Newly born' if body.current_lifetime < NEWLY_BORN_PERIOD and body.generation_n != 0 else '' + display_property(body=body, text='Newly born' if body.current_lifetime < NEWLY_BORN_PERIOD and body.generation_n != 0 else '') case 'Current energy': - text = round(body.energy) + display_property(body=body, texgt=round(body.energy)) case 'Speed': - text = round(body.speed*RATIO) + display_property(body=body, text=round(body.speed*RATIO)) case 'Procreation threshold': - text = round(body.procreation_threshold) + display_property(body=body, text=round(body.procreation_threshold)) case 'Food preference': - text = body.food_preference + display_property(body=body, text=body.food_preference) case 'Generation number': - text = body.generation_n + display_property(body=body, text=body.generation_n) case 'Amount of bodies with this species': - text = len(tuple(filter(lambda body_: body_.species == body.species, bodies))) + display_property(body=body, text=len(tuple(filter(lambda body_: body_.species == body.species, bodies)))) case 'ID of the species': - text = body.species_id + display_property(body=body, text = body.species_id) case 'Vision distance': update_vision_distance_circles() return - - center_x, center_y = body.x+CANVAS_BORDER, body.y+CANVAS_BORDER - global_items.canvas.create_text( - center_x, center_y-HALF_BODY_SIZE, - text=text, - tags='property', - anchor=S - ) def erase_body_properties(): - global_items.canvas.delete('property') - -def handle_body_properties(): - if window_commands['to-show-selected-property'] == 'Nothing': - erase_body_properties() - erase_circles() - else: - update_body_properties() \ No newline at end of file + global_items.canvas.delete('property') \ No newline at end of file diff --git a/evolution_functions.py b/evolution_functions.py index d48b64c..15fafe5 100644 --- a/evolution_functions.py +++ b/evolution_functions.py @@ -6,7 +6,7 @@ from plants import create_plant from crosses import delete_old_cross from bodies_functions import progenitor_properties -from draw_erase import prepare_draw_handle, handle_body_properties, update_arrows, update_crosses, update_plants, update_bodies +from draw_erase import handle_body_properties, update_arrows, update_crosses, update_plants, update_bodies from global_items import data_for_smart_body, handle, window_commands, bodies, evolution_status from tips import prepare_info_handle, info_handle, erase_information, show_tip, show_evolution_number from window_functions import handle_checkbuttons, disable_checkbuttons_checkmarks @@ -61,7 +61,6 @@ def one_evolution_step(): evolution_status.description = ON_PAUSE show_tip('Put your cursor on a body.') prepare_info_handle() - prepare_draw_handle() while window_commands['run/pause'] == PAUSE: handle_body_properties() info_handle() diff --git a/tips.py b/tips.py index e9e4548..1e22c64 100644 --- a/tips.py +++ b/tips.py @@ -1,7 +1,7 @@ -from global_items import handle, bodies, evolution_status +from global_items import handle, bodies, evolution_status, window from math import dist from config import * -from tkinter import NE, NW, SE, SW, TclError +from tkinter import GROOVE, LEFT, NE, NW, SE, SW, Label from time import time import global_items @@ -34,7 +34,11 @@ def mouse_clicked_on_body(body: object): show_tip_for_body(body) def erase_information(): - global_items.canvas.delete('information') + global information + try: + information.destroy() + except NameError: + return GAP = 3 DELAY = 0.3 @@ -72,19 +76,11 @@ def select_body(): hovered_start = time() selected_body = None -def raise_information_window(): - try: - global_items.canvas.tag_raise('information', 'circle') - except TclError: - pass - -@handle def info_handle(): '''Handling the mouse hovering upon bodies and displaying all of the needed info.''' global selected_body, body_info_shown_for select_body() show_tips() - raise_information_window() global_items.canvas.update() if selected_body is None: body_info_shown_for = None @@ -105,16 +101,16 @@ def show_tips(): show_tip_for_body(selected_body) def show_info(): - global selected_body, information_text, border + global selected_body, information # Making some corrections to the x and the y of the information window because clicks on the body are considered clicks on the window whenever it overlaps the body, therefore the clicks are not registered if selected_body.x >= HALF_EVOLUTION_FIELD_SIZE['width'] and selected_body.y >= HALF_EVOLUTION_FIELD_SIZE['height']: - corner, dx, dy = SE, -HALF_BODY_SIZE, -HALF_BODY_SIZE + corner, dx, dy = SE, 0, 0 elif selected_body.x < HALF_EVOLUTION_FIELD_SIZE['width'] and selected_body.y > HALF_EVOLUTION_FIELD_SIZE['height']: - corner, dx, dy = SW, HALF_BODY_SIZE, -HALF_BODY_SIZE + corner, dx, dy = SW, HALF_BODY_SIZE, 0 elif selected_body.x <= HALF_EVOLUTION_FIELD_SIZE['width'] and selected_body.y <= HALF_EVOLUTION_FIELD_SIZE['height']: corner, dx, dy = NW, HALF_BODY_SIZE, HALF_BODY_SIZE else: - corner, dx, dy = NE, -HALF_BODY_SIZE, HALF_BODY_SIZE + corner, dx, dy = NE, 0, HALF_BODY_SIZE information_tuple = ( f"Energy: {round(selected_body.energy)}", @@ -127,20 +123,17 @@ def show_info(): ) # Creating the information box - information_text = global_items.canvas.create_text( - selected_body.x + CANVAS_BORDER + dx, - selected_body.y + CANVAS_BORDER + dy, - text='\n'.join(information_tuple), - tags='information', - anchor=corner + information = Label( + master=window, + text='\n'.join(information_tuple), + padx=3, pady=3, + justify=LEFT, + relief=GROOVE, + bg='white' ) - estimated_info_box_size = global_items.canvas.bbox(information_text) - border = global_items.canvas.create_rectangle( - (estimated_info_box_size[0]-GAP, - estimated_info_box_size[1]-GAP, - estimated_info_box_size[2]+GAP, - estimated_info_box_size[3]+GAP), - tags='information', fill='white') - - global_items.canvas.tag_raise(information_text, border) \ No newline at end of file + information.place( + x=selected_body.x + dx, + y=selected_body.y + dy, + anchor=corner + ) \ No newline at end of file diff --git a/window_functions.py b/window_functions.py index 7c6224d..5cfc978 100644 --- a/window_functions.py +++ b/window_functions.py @@ -3,7 +3,7 @@ from tkinter.ttk import Progressbar from config import * -from draw_erase import change_shape, handle_body_properties, prepare_draw_handle +from draw_erase import change_shape, handle_body_properties from global_items import handle, window_commands, bodies, evolution_status, window, checkbuttons_for_triangle from tips import info_handle, mouse_clicked_on_body, prepare_info_handle @@ -68,7 +68,6 @@ def selected() -> bool: # A separate function is required for the decorator to w info_handle() return window_commands['run/pause'] == RUN prepare_info_handle() - prepare_draw_handle() while not selected(): continue @@ -129,5 +128,6 @@ def delete_progress_bar(): global canvas_window try: global_items.canvas.delete(canvas_window) + global_items.canvas.update() except NameError: # NameError is thrown if the progress bar does not exist yet pass