Skip to content

Commit

Permalink
Fixed a bug with the information windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kul-sudo committed Jun 9, 2022
1 parent b47afbb commit 15f617f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 73 deletions.
14 changes: 12 additions & 2 deletions ai.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
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
from evolution_functions import memory_things
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...')
Expand Down
57 changes: 20 additions & 37 deletions draw_erase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
global_items.canvas.delete('property')
3 changes: 1 addition & 2 deletions evolution_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
53 changes: 23 additions & 30 deletions tips.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)}",
Expand All @@ -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)
information.place(
x=selected_body.x + dx,
y=selected_body.y + dy,
anchor=corner
)
4 changes: 2 additions & 2 deletions window_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 15f617f

Please sign in to comment.