Merge branch 'master' into a_star

# Conflicts:
#	main.py
This commit is contained in:
s452645 2021-05-09 01:17:09 +02:00
commit 7d16dcc616
15 changed files with 1085 additions and 474 deletions

3
.gitignore vendored
View File

@ -1,5 +1,6 @@
# .idea files # .idea files
.idea .idea*
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/

View File

@ -1,21 +1,19 @@
import pygame
import project_constants as const import project_constants as const
import json_generator as js
import json import json
# Class of our agent, initialization of it # Class of our agent, initialization of it
# movement functions (those defiend by the 'go_' prefix are not meant to actually move our agent, they just return some values # movement functions (those defined by the 'go_' prefix are not meant to actually move our agent, they just return some
# that are later used by another function called 'is_valid_move' (which is defined in Minefield)); # values that are later used by another function called 'is_valid_move' (which is defined in Minefield));
class Agent: class Agent:
def __init__(self, json_path): def __init__(self, json_path):
with open(json_path) as json_data: with open(json_path) as json_data:
data = json.load(json_data) data = json.load(json_data)
self.row, self.column = data['agent_starting_position'].split(",") self.row, self.column = data["agents_initial_state"]["position"].split(",")
self.position = [int(self.row), int(self.column)] self.position = [int(self.row), int(self.column)]
# self.direction = const.Direction() # self.direction = const.Direction()
self.direction = const.Direction.UP self.direction = const.Direction(data["agents_initial_state"]["direction"])
def rotate_left(self): def rotate_left(self):
self.direction = self.direction.previous() self.direction = self.direction.previous()

View File

@ -2,26 +2,25 @@ import pygame
import project_constants as const import project_constants as const
from mine_models.standard_mine import StandardMine
from mine_models.chained_mine import ChainedMine
from mine_models.time_mine import TimeMine
# ================================= # # ================================= #
# === SO THE OLD BLITTING WORKS === # # === SO THE OLD BLITTING WORKS === #
# ================================= # # ================================= #
tile_asset_options = { tile_asset_options = {
"BLUE": const.ASSET_TILE_BLUE, "MUD": const.ASSET_MUD,
"GREEN": const.ASSET_TILE_GREEN, "GRASS": const.ASSET_GRASS,
"ORANGE": const.ASSET_TILE_ORANGE, "CONCRETE": const.ASSET_CONCRETE
"PURPLE": const.ASSET_TILE_PURPLE,
"RED": const.ASSET_TILE_RED,
"WHITE": const.ASSET_TILE_WHITE,
"YELLOW": const.ASSET_TILE_YELLOW
} }
mine_asset_options = { mine_asset_options = {
'A': const.ASSET_MINE_A, "MINE": const.ASSET_MINE,
'B': const.ASSET_MINE_B, "CHAINS": const.ASSET_CHAINS,
'F': const.ASSET_MINE_F, "TIME_MINE": const.ASSET_TIME_MINE,
'K': const.ASSET_MINE_K
} }
@ -30,21 +29,6 @@ mine_asset_options = {
# ====================== # # ====================== #
def test_blits():
display_concrete((2, 3))
display_mud((5, 6))
display_mud((5, 7))
display_grass((2, 1))
display_mine((2, 7))
display_mine((5, 7))
display_concrete((0, 0))
display_chained_mine((1, 2), (2, 3))
display_chained_mine((2, 3))
display_chained_mine((4, 8), (11, 15))
display_time_mine((1, 8), 12)
display_time_mine((2, 8), 34)
def blit_graphics(minefield): def blit_graphics(minefield):
# background grid (fills frame with white, blits grid) # background grid (fills frame with white, blits grid)
const.SCREEN.fill((255, 255, 255)) const.SCREEN.fill((255, 255, 255))
@ -64,7 +48,7 @@ def blit_graphics(minefield):
# draw a tile # draw a tile
const.SCREEN.blit( const.SCREEN.blit(
tile_asset_options.get(tile.color), tile_asset_options.get(tile.terrain_type),
tile_screen_coords tile_screen_coords
) )
@ -74,11 +58,16 @@ def blit_graphics(minefield):
# draw a mine on top if there is one # draw a mine on top if there is one
if tile.mine is not None: if tile.mine is not None:
# current icons don't represent actual types, thus every mine has the same icon (temporary solution) if isinstance(tile.mine, StandardMine):
const.SCREEN.blit(mine_asset_options['A'], tile_screen_coords) const.SCREEN.blit(mine_asset_options["MINE"], tile_screen_coords)
# all the tests in one place if isinstance(tile.mine, ChainedMine):
test_blits() predecessor = tile.mine.predecessor
predecessor_position = predecessor.position if predecessor is not None else None
display_chained_mine(tile.position, predecessor_position)
elif isinstance(tile.mine, TimeMine):
display_time_mine(tile.position, "0" + str(tile.mine.timer))
# sapper # sapper
display_sapper( display_sapper(
@ -264,9 +253,9 @@ def display_time_mine(coords, time):
mine_coords = calculate_screen_position(coords) mine_coords = calculate_screen_position(coords)
number_coords = mine_coords number_coords = mine_coords
if which_digit == const.Digit.ONES: if which_digit == const.Digit.ONES:
number_coords = (mine_coords[0] + 36, mine_coords[1] + 22)
elif which_digit == const.Digit.TENS:
number_coords = (mine_coords[0] + 44, mine_coords[1] + 22) number_coords = (mine_coords[0] + 44, mine_coords[1] + 22)
elif which_digit == const.Digit.TENS:
number_coords = (mine_coords[0] + 36, mine_coords[1] + 22)
const.SCREEN.blit( const.SCREEN.blit(
number_asset, number_asset,

View File

@ -4,12 +4,12 @@ import random
import project_constants as const import project_constants as const
# import tile class # import tile class
import tile as tl from tile import Tile
# import mine models # import mine models
import mine_models.standard_mine as sm from mine_models.standard_mine import StandardMine
import mine_models.time_mine as tm from mine_models.time_mine import TimeMine
import mine_models.chained_mine as cm from mine_models.chained_mine import ChainedMine
class JsonGenerator: class JsonGenerator:
@ -18,7 +18,7 @@ class JsonGenerator:
# constructor that can be used to set agent's initial state # constructor that can be used to set agent's initial state
def __init__(self, agents_initial_position=(0, 0), agents_initial_direction=const.Direction.UP.value): def __init__(self, agents_initial_position=(0, 0), agents_initial_direction=const.Direction.UP.value):
# saving agent's initial state (position & direction) # saving agent's initial state (position & direction)
self.agents_initial_position = agents_initial_position self.agents_initial_position = format_position_to_tuple(agents_initial_position)
self.agents_initial_direction = agents_initial_direction self.agents_initial_direction = agents_initial_direction
# saving data to the grid dictionary # saving data to the grid dictionary
@ -30,7 +30,7 @@ class JsonGenerator:
# sets agent's initial state # sets agent's initial state
def set_agents_initial_state(self, position=(0, 0), direction=const.Direction.UP.value): def set_agents_initial_state(self, position=(0, 0), direction=const.Direction.UP.value):
# setting fields in the instance # setting fields in the instance
self.agents_initial_position = position self.agents_initial_position = format_position_to_tuple(position)
self.agents_initial_direction = direction self.agents_initial_direction = direction
# setting new agent's initial state # setting new agent's initial state
@ -44,6 +44,9 @@ class JsonGenerator:
# clearing grid field # clearing grid field
self.clear_grid() self.clear_grid()
# formatting dimensions to tuple
dimensions = format_position_to_tuple(dimensions)
# getting grid dimensions # getting grid dimensions
num_of_rows, num_of_columns = dimensions num_of_rows, num_of_columns = dimensions
@ -53,7 +56,7 @@ class JsonGenerator:
for j in range(num_of_columns): for j in range(num_of_columns):
# picking random values for tiles # picking random values for tiles
random_tile_color = random.choice(const.STRUCT_TILE_COLORS) random_tile_color = random.choice(const.STRUCT_TILE_TERRAINS)
# adding added tile's indexes to a pool # adding added tile's indexes to a pool
tile_pool.append((i, j)) tile_pool.append((i, j))
@ -129,7 +132,7 @@ class JsonGenerator:
self.grid[format_position_to_str(position)]["mine"] = {} self.grid[format_position_to_str(position)]["mine"] = {}
for key in mine_values.keys(): for key in mine_values.keys():
self.grid[format_position_to_str()]["mine"][key] = mine_values[key] self.grid[format_position_to_str(position)]["mine"][key] = mine_values[key]
# deletes a mine with a given position from the grid field # deletes a mine with a given position from the grid field
def delete_a_tile(self, position): def delete_a_tile(self, position):
@ -227,16 +230,16 @@ def create_a_mine(mine_dict, position):
# if mine's type is "standard" - creating standard mine # if mine's type is "standard" - creating standard mine
if mine_dict["mine_type"] == "standard": if mine_dict["mine_type"] == "standard":
mine = sm.StandardMine(position) mine = StandardMine(position)
# if mine's type is "time" - creating time mine # if mine's type is "time" - creating time mine
elif mine_dict["mine_type"] == "time": elif mine_dict["mine_type"] == "time":
timer_value = mine_dict["timer"] timer_value = mine_dict["timer"]
mine = tm.TimeMine(position, timer_value) mine = TimeMine(position, timer_value)
# if mine's type is "chained" - creating chained mine (no successors assigned yet) # if mine's type is "chained" - creating chained mine (no successors assigned yet)
elif mine_dict["mine_type"] == "chained": elif mine_dict["mine_type"] == "chained":
mine = cm.ChainedMine(position) mine = ChainedMine(position)
return mine return mine
@ -248,10 +251,10 @@ def create_a_tile(tile_dict, position):
# getting tile's parameters # getting tile's parameters
color = tile_dict["color"] color = tile_dict["color"]
mine = create_a_mine(position, tile_dict["mine"]) mine = create_a_mine(tile_dict["mine"], position)
# creating and returning a tile with the parameters set above # creating and returning a tile with the parameters set above
return tl.Tile(position, color, mine) return Tile(position, color, mine)
# returns a list of tuples containing chained mine's position and it's predecessors position # returns a list of tuples containing chained mine's position and it's predecessors position
@ -268,7 +271,8 @@ def get_chained_mine_and_its_predecessor_pairs(minefield_dictionary):
# getting the chained mines and it's predecessors positions # getting the chained mines and it's predecessors positions
this_mines_position = tuple(int(i) for i in key.split(',')) this_mines_position = tuple(int(i) for i in key.split(','))
its_predecessors_position = tuple(int(i) for i in minefield_dictionary[key]["mine"]["predecessor"].split(',')) its_predecessors_position = \
tuple(int(i) for i in minefield_dictionary[key]["mine"]["predecessor"].split(','))
# adding the positions to the list as a tuple # adding the positions to the list as a tuple
predecessors.append((this_mines_position, its_predecessors_position)) predecessors.append((this_mines_position, its_predecessors_position))

136
main.py
View File

@ -1,20 +1,24 @@
# libraries # libraries
import pygame
import time import time
from pyglet.gl import * # for blocky textures from pyglet.gl import * # for blocky textures
import random
# other files of this project # other files of this project
import project_constants as const import project_constants as const
import minefield as mf import minefield as mf
import searching_algorithms.a_star as a_star import searching_algorithms.a_star as a_star
import searching_algorithms.bfs as bfs
from display_assets import blit_graphics 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(): def main():
pygame.init() pygame.init()
pygame.display.set_caption(const.V_NAME_OF_WINDOW) pygame.display.set_caption(const.V_NAME_OF_WINDOW)
jes = jg.JsonGenerator()
print(jes.get_grid())
# for blocky textures # for blocky textures
glEnable(GL_TEXTURE_2D) glEnable(GL_TEXTURE_2D)
@ -26,44 +30,106 @@ def main():
# create an instance of Minefield, pass necessary data # create an instance of Minefield, pass necessary data
minefield = mf.Minefield(const.MAP_RANDOM_10x10) minefield = mf.Minefield(const.MAP_RANDOM_10x10)
# get sequence of actions found by BFS algorithm # setting flags for program
action_sequence = a_star.graphsearch(
initial_state=a_star.State(
row=minefield.agent.position[0],
column=minefield.agent.position[1],
direction=const.Direction.UP),
minefield=minefield)
running = True 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:
# FPS control while running and in_menu:
clock.tick(const.V_FPS) const.SCREEN.fill((255, 255, 255))
events = pygame.event.get()
# 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:
for event in pygame.event.get():
for event in events:
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False 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 = bfs.graphsearch(
initial_state=bfs.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__": if __name__ == "__main__":
main() main()

View File

@ -1,10 +1,10 @@
import json
import agent as ag import agent as ag
import project_constants as const import project_constants as const
import tile as tl import tile as tl
from mine_models import standard_mine as sm from mine_models import standard_mine as sm
from mine_models import time_mine as tm from mine_models import time_mine as tm
from mine_models import chained_mine as cm from mine_models import chained_mine as cm
import json_generator as jg
class Minefield: class Minefield:
@ -14,29 +14,28 @@ class Minefield:
self.agent = ag.Agent(const.MAP_RANDOM_10x10) self.agent = ag.Agent(const.MAP_RANDOM_10x10)
# open JSON with minefield info # open JSON with minefield info
with open(json_path) as json_data: json_gen = jg.JsonGenerator()
data = json.load(json_data) json_gen.load_from_a_file(json_path)
# create matrix of a desired size, fill it with default tile objects # create matrix of a desired size, fill it with default tile objects
self.matrix = [ self.matrix = [
[ [
tl.Tile((row, column)) for column in range(const.V_GRID_HOR_TILES) tl.Tile(
(row, column),
terrain_type=json_gen.get_tile((row, column))["terrain"],
mine=jg.create_a_mine(json_gen.get_mine((row, column)), (row, column))
) for column in range(const.V_GRID_HOR_TILES)
] for row in range(const.V_GRID_VER_TILES) ] for row in range(const.V_GRID_VER_TILES)
] ]
# iterate through tiles, set their colors and add mines # iterate through chained mines, set mine predecessors
for row in range(const.V_GRID_VER_TILES): for pair in jg.get_chained_mine_and_its_predecessor_pairs(json_gen.get_grid()):
for column in range(const.V_GRID_HOR_TILES): successor_position, predecessor_position = pair
successor_row, successor_column = successor_position
predecessor_row, predecessor_column = predecessor_position
# load tile's data from json predecessor = self.matrix[predecessor_row][predecessor_column]
tile_data = data[f"{row},{column}"] self.matrix[successor_row][successor_column].mine.predecessor = predecessor
# if there is a mine, create & assign new Mine object (type recognition included)
if tile_data["mine"] is not None:
mine = self._create_mine(tile_data["mine"], row, column)
self.matrix[row][column].mine = mine
self.matrix[row][column].color = tile_data["color"].upper()
# ================ # # ================ #
# === MOVEMENT === # # === MOVEMENT === #

View File

@ -17,11 +17,11 @@ V_NAME_OF_WINDOW = "MineFusion TM"
DIR_ASSETS = os.path.join("resources", "assets") DIR_ASSETS = os.path.join("resources", "assets")
V_FPS = 60 V_FPS = 60
ACTION_INTERVAL = 1 # interval between two actions in seconds ACTION_INTERVAL = 1 # interval between two actions in seconds
V_TILE_SIZE = 60 V_TILE_SIZE = 60
V_GRID_VER_TILES = 10 # vertical (number of rows) V_GRID_VER_TILES = 10 # vertical (number of rows)
V_GRID_HOR_TILES = 10 # horizontal (number of columns) V_GRID_HOR_TILES = 10 # horizontal (number of columns)
V_SCREEN_PADDING = 10 V_SCREEN_PADDING = 10
V_NUMBER_PADDING = 50 V_NUMBER_PADDING = 50
V_TILE_AREA_HEIGHT = V_TILE_SIZE * V_GRID_VER_TILES V_TILE_AREA_HEIGHT = V_TILE_SIZE * V_GRID_VER_TILES
@ -30,7 +30,7 @@ V_TILE_AREA_WIDTH = V_TILE_SIZE * V_GRID_HOR_TILES
SCREEN = pygame.display.set_mode( SCREEN = pygame.display.set_mode(
( (
V_TILE_AREA_WIDTH + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING, # screen width V_TILE_AREA_WIDTH + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING, # screen width
V_TILE_AREA_HEIGHT + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING # screen height V_TILE_AREA_HEIGHT + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING # screen height
) )
) )
@ -70,6 +70,12 @@ class Coords(Enum):
Y = 1 Y = 1
class Terrain(Enum):
CONCRETE = 1
GRASS = 2
MUD = 4
# =============== # # =============== #
# === STRUCTS === # # === STRUCTS === #
# =============== # # =============== #
@ -79,7 +85,7 @@ class Coords(Enum):
# # USED BY JSON GENERATOR # # USED BY JSON GENERATOR
# used to generate random tile colors # used to generate random tile colors
STRUCT_TILE_COLORS = ["BLUE", "GREEN", "ORANGE", "PURPLE", "RED", "WHITE", "YELLOW"] STRUCT_TILE_TERRAINS = ["CONCRETE", "GRASS", "MUD"]
# used to generate random mines and create not random mines # used to generate random mines and create not random mines
STRUCT_MINE_TYPES = ["standard", "chained", "time"] STRUCT_MINE_TYPES = ["standard", "chained", "time"]
@ -113,13 +119,11 @@ STRUCT_MINE_ATTRIBUTE_TYPES = {
"time": [int] "time": [int]
} }
# ============== # # ============== #
# ==== MAPS ==== # # ==== MAPS ==== #
# ============== # # ============== #
MAP_RANDOM_10x10 = os.path.join("resources", "minefields", "secondmap.json") MAP_RANDOM_10x10 = os.path.join("resources", "minefields", "fourthmap.json")
# ============== # # ============== #
# === ASSETS === # # === ASSETS === #
@ -170,7 +174,6 @@ ASSET_TIME_MINE = pygame.transform.scale(
(V_TILE_SIZE, V_TILE_SIZE) (V_TILE_SIZE, V_TILE_SIZE)
) )
# ==================== # # ==================== #
# === TIME NUMBERS === # # === TIME NUMBERS === #
# ==================== # # ==================== #
@ -225,7 +228,6 @@ ASSET_NUMBER_TIME_9 = pygame.transform.scale(
(6, 16) (6, 16)
) )
# ====================== # # ====================== #
# === CHAINS NUMBERS === # # === CHAINS NUMBERS === #
# ====================== # # ====================== #
@ -310,7 +312,6 @@ ASSET_NUMBER_CHAINS_15 = pygame.transform.scale(
(6, 12) (6, 12)
) )
# ================== # # ================== #
# === OLD ASSETS === # # === OLD ASSETS === #
# ================== # # ================== #

View File

@ -0,0 +1,476 @@
{
"0,0": {
"terrain": "CONCRETE",
"mine": null
},
"0,1": {
"terrain": "CONCRETE",
"mine": null
},
"0,2": {
"terrain": "GRASS",
"mine": null
},
"0,3": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"0,4": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"0,5": {
"terrain": "CONCRETE",
"mine": null
},
"0,6": {
"terrain": "MUD",
"mine": null
},
"0,7": {
"terrain": "GRASS",
"mine": null
},
"0,8": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "chained",
"predecessor": "0,3"
}
},
"0,9": {
"terrain": "GRASS",
"mine": {
"mine_type": "time",
"timer": 20
}
},
"1,0": {
"terrain": "GRASS",
"mine": null
},
"1,1": {
"terrain": "GRASS",
"mine": {
"mine_type": "standard"
}
},
"1,2": {
"terrain": "GRASS",
"mine": null
},
"1,3": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"1,4": {
"terrain": "MUD",
"mine": null
},
"1,5": {
"terrain": "GRASS",
"mine": null
},
"1,6": {
"terrain": "GRASS",
"mine": null
},
"1,7": {
"terrain": "GRASS",
"mine": null
},
"1,8": {
"terrain": "MUD",
"mine": null
},
"1,9": {
"terrain": "MUD",
"mine": null
},
"2,0": {
"terrain": "MUD",
"mine": null
},
"2,1": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "standard"
}
},
"2,2": {
"terrain": "MUD",
"mine": null
},
"2,3": {
"terrain": "MUD",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"2,4": {
"terrain": "GRASS",
"mine": null
},
"2,5": {
"terrain": "MUD",
"mine": {
"mine_type": "standard"
}
},
"2,6": {
"terrain": "CONCRETE",
"mine": null
},
"2,7": {
"terrain": "CONCRETE",
"mine": null
},
"2,8": {
"terrain": "MUD",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"2,9": {
"terrain": "CONCRETE",
"mine": null
},
"3,0": {
"terrain": "CONCRETE",
"mine": null
},
"3,1": {
"terrain": "MUD",
"mine": {
"mine_type": "time",
"timer": 19
}
},
"3,2": {
"terrain": "GRASS",
"mine": null
},
"3,3": {
"terrain": "MUD",
"mine": {
"mine_type": "chained",
"predecessor": "1,3"
}
},
"3,4": {
"terrain": "MUD",
"mine": null
},
"3,5": {
"terrain": "CONCRETE",
"mine": null
},
"3,6": {
"terrain": "MUD",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"3,7": {
"terrain": "MUD",
"mine": null
},
"3,8": {
"terrain": "GRASS",
"mine": null
},
"3,9": {
"terrain": "GRASS",
"mine": null
},
"4,0": {
"terrain": "CONCRETE",
"mine": null
},
"4,1": {
"terrain": "MUD",
"mine": null
},
"4,2": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "standard"
}
},
"4,3": {
"terrain": "CONCRETE",
"mine": null
},
"4,4": {
"terrain": "MUD",
"mine": null
},
"4,5": {
"terrain": "MUD",
"mine": null
},
"4,6": {
"terrain": "CONCRETE",
"mine": null
},
"4,7": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"4,8": {
"terrain": "CONCRETE",
"mine": null
},
"4,9": {
"terrain": "MUD",
"mine": null
},
"5,0": {
"terrain": "CONCRETE",
"mine": null
},
"5,1": {
"terrain": "MUD",
"mine": {
"mine_type": "chained",
"predecessor": "2,3"
}
},
"5,2": {
"terrain": "MUD",
"mine": null
},
"5,3": {
"terrain": "GRASS",
"mine": null
},
"5,4": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "chained",
"predecessor": "2,8"
}
},
"5,5": {
"terrain": "CONCRETE",
"mine": null
},
"5,6": {
"terrain": "MUD",
"mine": null
},
"5,7": {
"terrain": "MUD",
"mine": null
},
"5,8": {
"terrain": "GRASS",
"mine": null
},
"5,9": {
"terrain": "MUD",
"mine": null
},
"6,0": {
"terrain": "GRASS",
"mine": null
},
"6,1": {
"terrain": "CONCRETE",
"mine": null
},
"6,2": {
"terrain": "MUD",
"mine": null
},
"6,3": {
"terrain": "CONCRETE",
"mine": null
},
"6,4": {
"terrain": "CONCRETE",
"mine": null
},
"6,5": {
"terrain": "GRASS",
"mine": {
"mine_type": "standard"
}
},
"6,6": {
"terrain": "GRASS",
"mine": null
},
"6,7": {
"terrain": "CONCRETE",
"mine": null
},
"6,8": {
"terrain": "CONCRETE",
"mine": null
},
"6,9": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "standard"
}
},
"7,0": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "chained",
"predecessor": "4,7"
}
},
"7,1": {
"terrain": "CONCRETE",
"mine": null
},
"7,2": {
"terrain": "GRASS",
"mine": null
},
"7,3": {
"terrain": "MUD",
"mine": null
},
"7,4": {
"terrain": "GRASS",
"mine": null
},
"7,5": {
"terrain": "GRASS",
"mine": {
"mine_type": "standard"
}
},
"7,6": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "time",
"timer": 39
}
},
"7,7": {
"terrain": "MUD",
"mine": null
},
"7,8": {
"terrain": "GRASS",
"mine": null
},
"7,9": {
"terrain": "GRASS",
"mine": null
},
"8,0": {
"terrain": "GRASS",
"mine": null
},
"8,1": {
"terrain": "GRASS",
"mine": {
"mine_type": "time",
"timer": 24
}
},
"8,2": {
"terrain": "GRASS",
"mine": null
},
"8,3": {
"terrain": "CONCRETE",
"mine": {
"mine_type": "standard"
}
},
"8,4": {
"terrain": "CONCRETE",
"mine": null
},
"8,5": {
"terrain": "GRASS",
"mine": null
},
"8,6": {
"terrain": "GRASS",
"mine": null
},
"8,7": {
"terrain": "CONCRETE",
"mine": null
},
"8,8": {
"terrain": "GRASS",
"mine": {
"mine_type": "chained",
"predecessor": "3,6"
}
},
"8,9": {
"terrain": "GRASS",
"mine": null
},
"9,0": {
"terrain": "MUD",
"mine": null
},
"9,1": {
"terrain": "GRASS",
"mine": null
},
"9,2": {
"terrain": "CONCRETE",
"mine": null
},
"9,3": {
"terrain": "CONCRETE",
"mine": null
},
"9,4": {
"terrain": "CONCRETE",
"mine": null
},
"9,5": {
"terrain": "GRASS",
"mine": {
"mine_type": "chained",
"predecessor": "0,4"
}
},
"9,6": {
"terrain": "CONCRETE",
"mine": null
},
"9,7": {
"terrain": "MUD",
"mine": null
},
"9,8": {
"terrain": "MUD",
"mine": null
},
"9,9": {
"terrain": "MUD",
"mine": null
},
"agents_initial_state": {
"direction": 1,
"position": "0,0"
}
}

View File

@ -1,456 +1,456 @@
{ {
"0,0": { "0,0": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"0,1": { "0,1": {
"color": "WHITE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"0,2": { "0,2": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"0,3": { "0,3": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"0,4": { "0,4": {
"color": "WHITE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"0,5": { "0,5": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"0,6": { "0,6": {
"color": "RED", "terrain": "MUD",
"mine": null "mine": null
}, },
"0,7": { "0,7": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"0,8": { "0,8": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"0,9": { "0,9": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": { "mine": {
"mine_type": "time", "mine_type": "time",
"timer": 28 "timer": 28
} }
}, },
"1,0": { "1,0": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"1,1": { "1,1": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"1,2": { "1,2": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"1,3": { "1,3": {
"color": "WHITE", "terrain": "GRASS",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": "8,9" "predecessor": "8,9"
} }
}, },
"1,4": { "1,4": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"1,5": { "1,5": {
"color": "RED", "terrain": "MUD",
"mine": null "mine": null
}, },
"1,6": { "1,6": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"1,7": { "1,7": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"1,8": { "1,8": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"1,9": { "1,9": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"2,0": { "2,0": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"2,1": { "2,1": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": { "mine": {
"mine_type": "time", "mine_type": "time",
"timer": 27 "timer": 27
} }
}, },
"2,2": { "2,2": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"2,3": { "2,3": {
"color": "RED", "terrain": "MUD",
"mine": null "mine": null
}, },
"2,4": { "2,4": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"2,5": { "2,5": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"2,6": { "2,6": {
"color": "BLUE", "terrain": "GRASS",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": "7,2" "predecessor": "7,2"
} }
}, },
"2,7": { "2,7": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"2,8": { "2,8": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"2,9": { "2,9": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"3,0": { "3,0": {
"color": "WHITE", "terrain": "GRASS",
"mine": { "mine": {
"mine_type": "time", "mine_type": "time",
"timer": 27 "timer": 27
} }
}, },
"3,1": { "3,1": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"3,2": { "3,2": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"3,3": { "3,3": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"3,4": { "3,4": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": "8,8" "predecessor": "8,8"
} }
}, },
"3,5": { "3,5": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"3,6": { "3,6": {
"color": "RED", "terrain": "MUD",
"mine": null "mine": null
}, },
"3,7": { "3,7": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"3,8": { "3,8": {
"color": "WHITE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"3,9": { "3,9": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"4,0": { "4,0": {
"color": "GREEN", "terrain": "MUD",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": null "predecessor": null
} }
}, },
"4,1": { "4,1": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": { "mine": {
"mine_type": "standard" "mine_type": "standard"
} }
}, },
"4,2": { "4,2": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"4,3": { "4,3": {
"color": "RED", "terrain": "MUD",
"mine": null "mine": null
}, },
"4,4": { "4,4": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"4,5": { "4,5": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"4,6": { "4,6": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"4,7": { "4,7": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"4,8": { "4,8": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"4,9": { "4,9": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"5,0": { "5,0": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"5,1": { "5,1": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"5,2": { "5,2": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"5,3": { "5,3": {
"color": "YELLOW", "terrain": "GRASS",
"mine": { "mine": {
"mine_type": "time", "mine_type": "time",
"timer": 25 "timer": 25
} }
}, },
"5,4": { "5,4": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"5,5": { "5,5": {
"color": "RED", "terrain": "MUD",
"mine": null "mine": null
}, },
"5,6": { "5,6": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"5,7": { "5,7": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"5,8": { "5,8": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"5,9": { "5,9": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"6,0": { "6,0": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": "6,1" "predecessor": "6,1"
} }
}, },
"6,1": { "6,1": {
"color": "BLUE", "terrain": "GRASS",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": "3,4" "predecessor": "3,4"
} }
}, },
"6,2": { "6,2": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"6,3": { "6,3": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"6,4": { "6,4": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"6,5": { "6,5": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"6,6": { "6,6": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"6,7": { "6,7": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"6,8": { "6,8": {
"color": "WHITE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"6,9": { "6,9": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"7,0": { "7,0": {
"color": "YELLOW", "terrain": "GRASS",
"mine": { "mine": {
"mine_type": "time", "mine_type": "time",
"timer": 30 "timer": 30
} }
}, },
"7,1": { "7,1": {
"color": "WHITE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"7,2": { "7,2": {
"color": "WHITE", "terrain": "GRASS",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": "7,3" "predecessor": "7,3"
} }
}, },
"7,3": { "7,3": {
"color": "GREEN", "terrain": "MUD",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": null "predecessor": null
} }
}, },
"7,4": { "7,4": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"7,5": { "7,5": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": { "mine": {
"mine_type": "standard" "mine_type": "standard"
} }
}, },
"7,6": { "7,6": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"7,7": { "7,7": {
"color": "BLUE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"7,8": { "7,8": {
"color": "WHITE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"7,9": { "7,9": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"8,0": { "8,0": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"8,1": { "8,1": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"8,2": { "8,2": {
"color": "WHITE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"8,3": { "8,3": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"8,4": { "8,4": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"8,5": { "8,5": {
"color": "YELLOW", "terrain": "GRASS",
"mine": null "mine": null
}, },
"8,6": { "8,6": {
"color": "WHITE", "terrain": "GRASS",
"mine": { "mine": {
"mine_type": "standard" "mine_type": "standard"
} }
}, },
"8,7": { "8,7": {
"color": "GREEN", "terrain": "MUD",
"mine": null "mine": null
}, },
"8,8": { "8,8": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": null "predecessor": null
} }
}, },
"8,9": { "8,9": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": null "predecessor": null
} }
}, },
"9,0": { "9,0": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"9,1": { "9,1": {
"color": "BLUE", "terrain": "GRASS",
"mine": { "mine": {
"mine_type": "chained", "mine_type": "chained",
"predecessor": "4,0" "predecessor": "4,0"
} }
}, },
"9,2": { "9,2": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"9,3": { "9,3": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"9,4": { "9,4": {
"color": "WHITE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"9,5": { "9,5": {
"color": "PURPLE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"9,6": { "9,6": {
"color": "WHITE", "terrain": "GRASS",
"mine": null "mine": null
}, },
"9,7": { "9,7": {
"color": "RED", "terrain": "MUD",
"mine": null "mine": null
}, },
"9,8": { "9,8": {
"color": "ORANGE", "terrain": "CONCRETE",
"mine": null "mine": null
}, },
"9,9": { "9,9": {
"color": "RED", "terrain": "MUD",
"mine": null "mine": null
}, },
"agents_initial_state": { "agents_initial_state": {

View File

@ -47,13 +47,15 @@ def get_successors(state: State, minefield: Minefield):
return successors return successors
def graphsearch(initial_state: State, minefield: Minefield, fringe: List[Node] = None, explored: List[Node] = None): def graphsearch(initial_state: State, minefield: Minefield, fringe: List[Node] = None, explored: List[Node] = None, tox: int = None,toy: int = None):
# fringe and explored initialization # fringe and explored initialization
global GOAL
if fringe is None: if fringe is None:
fringe = list() fringe = list()
if explored is None: if explored is None:
explored = list() explored = list()
if tox is not None and toy is not None:
GOAL = (tox, toy)
explored_states = set() explored_states = set()
fringe_states = set() fringe_states = set()

25
tile.py
View File

@ -1,7 +1,24 @@
class Tile: from project_constants import Terrain
def __init__(self, position, color=None, mine=None):
self.position = position
self.color = color
# Assume_cost function assumes colour as an argument (colour that is already given to a tile) and depending
# on what it is returns value of Terrein Enum
# It is used in Tile.cost (giving the value to the tile)
def assume_cost(terrain_type):
if terrain_type == "CONCRETE":
return Terrain.CONCRETE
elif terrain_type == "GRASS":
return Terrain.GRASS
elif terrain_type == "MUD":
return Terrain.MUD
class Tile:
def __init__(self, position, terrain_type=None, mine=None):
self.position = position
self.terrain_type = terrain_type
self.cost = assume_cost(terrain_type)
# mine is an instance of Mine class # mine is an instance of Mine class
self.mine = mine self.mine = mine

View File

@ -0,0 +1,6 @@
def _return_itself(fun):
def wrapper(*args, **kwargs):
fun(*args, **kwargs)
return wrapper

View File

@ -1,8 +1,14 @@
import pygame import pygame
from math import floor
# importing an auxiliary functions
from ui.auxiliary_decorator import _return_itself
from ui.text_box import get_fixed_rgb
# importing parent class
from ui.text_box import TextBox
class Button: class Button(TextBox):
# constructor that can be used to set parameters of Button instance # constructor that can be used to set parameters of Button instance
def __init__( def __init__(
self, self,
@ -17,90 +23,82 @@ class Button:
is_active=True, is_active=True,
fit_text=True, fit_text=True,
outline=True, outline=True,
irregular_outline=False,
outline_additional_pixel=False,
outline_hover_inclusive=True,
outline_thickness=None, outline_thickness=None,
outline_color=(50, 50, 50), outline_color=(50, 50, 50),
box_transform_hover=1.16, box_transform_hover=1.16,
box_transform_inactive=0.9, box_transform_inactive=0.9,
font_transform_hover=1.24, font_transform_hover=1.24,
font_transform_inactive=0.85 font_transform_inactive=0.85,
outline_transform_hover=1,
outline_transform_inactive=0.9
): ):
# extracting and setting values # calling base class constructor
smaller_dimension = min(dimensions) super().__init__(
width, height = dimensions position=position,
dimensions=dimensions,
# counting default outline thickness text=text,
default_outline_thickness = (smaller_dimension + (width + height) / 4) / 50 box_color=box_color,
font=font,
font_color=font_color,
font_size=font_size,
is_visible=is_visible,
fit_text=fit_text,
outline=outline,
irregular_outline=irregular_outline,
outline_additional_pixel=outline_additional_pixel,
outline_hover_inclusive=outline_hover_inclusive,
outline_thickness=outline_thickness,
outline_color=outline_color
)
# setting attributes # setting attributes
self.position = position
self.dimensions = dimensions
self.text = text
self.box_color = _get_proper_rgb(box_color)
self.font_color = _get_proper_rgb(font_color)
self.font_size = _return_value_or_default(font_size, floor(smaller_dimension / 2))
self.font = pygame.font.SysFont(font, self.font_size)
self.is_visible = is_visible
self.is_active = is_active self.is_active = is_active
self.fit_text = fit_text
self.outline = outline
self.outline_thickness = _return_value_or_default(outline_thickness, default_outline_thickness)
self.outline_color = _get_proper_rgb(outline_color)
self.box_transform_hover = box_transform_hover self.box_transform_hover = box_transform_hover
self.box_transform_inactive = box_transform_inactive self.box_transform_inactive = box_transform_inactive
self.font_transform_hover = font_transform_hover self.font_transform_hover = font_transform_hover
self.font_transform_inactive = font_transform_inactive self.font_transform_inactive = font_transform_inactive
self.outline_transform_hover = outline_transform_hover
# rendering text to get it's width and height self.outline_transform_inactive = outline_transform_inactive
rendered_text = self.font.render(text, True, (0, 0, 0))
# if text is out of bounds and fit_text=True - resizing text to fit the box
if self.fit_text and rendered_text.get_width() > self.dimensions[0]:
self.font_size = floor(self.font_size / (rendered_text.get_width() / width + 0.1))
self.font = pygame.font.SysFont(font, self.font_size)
# counting colors on: hover, inactive # counting colors on: hover, inactive
self.box_hover_color = _get_proper_rgb(tuple(_transform(self.box_color, self.box_transform_hover))) self.box_hover_color = get_fixed_rgb(_transform_rgb(self.box_color, self.box_transform_hover))
self.box_inactive_color = _get_proper_rgb(tuple(_transform(self.box_color, self.box_transform_inactive))) self.box_inactive_color = get_fixed_rgb(_transform_rgb(self.box_color, self.box_transform_inactive))
self.font_hover_color = _get_proper_rgb(tuple(_transform(self.font_color, self.font_transform_hover))) self.font_hover_color = get_fixed_rgb(_transform_rgb(self.font_color, self.font_transform_hover))
self.font_inactive_color = _get_proper_rgb(tuple(_transform(self.font_color, self.font_transform_inactive))) self.font_inactive_color = get_fixed_rgb(_transform_rgb(self.font_color, self.font_transform_inactive))
self.outline_hover_color = get_fixed_rgb(_transform_rgb(self.outline_color, self.outline_transform_hover))
self.outline_inactive_color = get_fixed_rgb(_transform_rgb(self.outline_color, self.outline_transform_inactive))
# draws the Button instance # draws the Button instance
def draw(self, window, mouse_position): @_return_itself
# if is_visible=True - drawing Button def draw(self, window, mouse_position, *args, **kwargs):
if self.is_visible: # saving attribute values
# extracting and setting values from attributes box_color_attribute_value = self.box_color
box_x, box_y = self.position text_color_attribute_value = self.font_color
width, height = self.dimensions outline_attribute_value = self.outline_color
# if outline=True - drawing an outline # temporarily changing box and font color if necessary
if self.outline: # is active and mouse is over
padding = self.outline_thickness if self.is_active and self.is_over(mouse_position):
outline_coordinates = (box_x - padding, box_y - padding, width + 2 * padding, height + 2 * padding) self.box_color = self.box_hover_color
pygame.draw.rect(window, self.outline_color, outline_coordinates) self.font_color = self.font_hover_color
self.outline_color = self.outline_hover_color
# setting values accordingly to InputBox'es state # is not active
# is active and mouse is not over else:
if not self.is_over(mouse_position) and self.is_active: self.box_color = self.box_inactive_color
pygame.draw.rect(window, self.box_color, (box_x, box_y, width, height)) self.font_color = self.font_inactive_color
text_color = self.font_color self.outline_color = self.outline_inactive_color
# is active and mouse is over # calling the draw function of base class
elif self.is_active: super().draw(window)
pygame.draw.rect(window, self.box_hover_color, (box_x, box_y, width, height))
text_color = self.font_hover_color
# is not active # resetting colors to original values
else: self.box_color = box_color_attribute_value
pygame.draw.rect(window, self.box_inactive_color, (box_x, box_y, width, height)) self.font_color = text_color_attribute_value
text_color = self.font_inactive_color self.outline_color = outline_attribute_value
# rendering text and counting its coordinates
rendered_text = self.font.render(self.text, True, text_color)
rendered_text_x = box_x + width / 2 - rendered_text.get_width() / 2
rendered_text_y = box_y + height / 2 - rendered_text.get_height() / 2
# drawing the text on the coordinates
window.blit(rendered_text, (rendered_text_x, rendered_text_y))
# returns True if the Button is clicked, or False otherwise # returns True if the Button is clicked, or False otherwise
def is_clicked(self, mouse_position, events): def is_clicked(self, mouse_position, events):
@ -114,49 +112,6 @@ class Button:
return False return False
# checks if a position is over the InputBox and returns True or False
def is_over(self, position):
mouse_x, mouse_y = position
button_x, button_y = self.position
width, height = self.dimensions
return button_x <= mouse_x <= (button_x + width) and button_y <= mouse_y <= (button_y + height)
# returns text value
def get_text(self):
return self.text
# sets chosen coordinates
def set_coordinates(self, position=None, dimensions=None, outline_thickness=None):
if position is not None:
self.position = position
if dimensions is not None:
self.dimensions = dimensions
if outline_thickness is not None:
self.outline_thickness = outline_thickness
# sets text attribute
def set_text(self, text):
self.text = text
# sets chosen font attributes
def set_font(self, font=None, font_size=None):
if font_size is not None:
self.font_size = font_size
# rendering text to get it's width and height
rendered_text = self.font.render(self.text, True, (0, 0, 0))
# if text is out of bounds and fit_text=True - resizing text to fit the box
if self.fit_text and rendered_text.get_width() > self.dimensions[0]:
self.font_size = floor(self.font_size / (rendered_text.get_width() / self.dimensions[0] + 0.1))
self.font = pygame.font.SysFont(font, self.font_size)
if font is not None:
self.font = pygame.font.SysFont(font, self.font_size)
# sets chosen color attributes # sets chosen color attributes
def set_colors( def set_colors(
self, self,
@ -168,14 +123,7 @@ class Button:
font_transform_hover=None, font_transform_hover=None,
font_transform_inactive=None font_transform_inactive=None
): ):
if box_color is not None: super().set_colors(box_color=box_color, font_color=font_color, outline_color=outline_color)
self.box_color = _get_proper_rgb(box_color)
if font_color is not None:
self.font_color = _get_proper_rgb(font_color)
if outline_color is not None:
self.outline_color = _get_proper_rgb(outline_color)
if box_transform_hover is not None: if box_transform_hover is not None:
self.box_transform_hover = box_transform_hover self.box_transform_hover = box_transform_hover
@ -189,33 +137,44 @@ class Button:
if font_transform_inactive is not None: if font_transform_inactive is not None:
self.font_transform_inactive = font_transform_inactive self.font_transform_inactive = font_transform_inactive
self.box_hover_color = _get_proper_rgb(tuple(_transform(self.box_color, self.box_transform_hover))) self.box_hover_color = get_fixed_rgb(tuple(_transform_rgb(self.box_color, self.box_transform_hover)))
self.box_inactive_color = _get_proper_rgb(tuple(_transform(self.box_color, self.box_transform_inactive))) self.box_inactive_color = get_fixed_rgb(tuple(_transform_rgb(self.box_color, self.box_transform_inactive)))
self.font_hover_color = _get_proper_rgb(tuple(_transform(self.font_color, self.font_transform_hover))) self.font_hover_color = get_fixed_rgb(tuple(_transform_rgb(self.font_color, self.font_transform_hover)))
self.font_inactive_color = _get_proper_rgb(tuple(_transform(self.font_color, self.font_transform_inactive))) self.font_inactive_color = get_fixed_rgb(tuple(_transform_rgb(self.font_color, self.font_transform_inactive)))
# sets chosen flags # sets chosen flags
def set_flags(self, is_visible=None, is_active=None, outline=None): def set_flags(
if is_visible is not None: self,
self.is_visible = is_visible is_visible=None,
is_active=None,
outline=None,
irregular_outline=None,
outline_additional_pixel=None,
outline_hover_inclusive=None
):
super().set_flags(
is_visible=is_visible,
outline=outline,
irregular_outline=irregular_outline,
outline_additional_pixel=outline_additional_pixel,
outline_hover_inclusive=outline_hover_inclusive
)
if is_active is not None: if is_active is not None:
self.is_active = is_active self.is_active = is_active
if outline is not None:
self.outline = outline
def _transform_rgb(iterable, transform_value):
if isinstance(transform_value, int) or isinstance(transform_value, float):
return tuple([x * transform_value for x in iterable])
def _get_proper_rgb(x): elif isinstance(transform_value, tuple) or isinstance(transform_value, list):
r, g, b = x if len(transform_value) == 3 and all([isinstance(x, int) or isinstance(x, float) for x in transform_value]):
r, g, b = max(0, min(255, floor(r))), max(0, min(255, floor(g))), max(0, min(255, floor(b))) rgb_list = [x for x in iterable]
return tuple((r, g, b)) for i in range(3):
rgb_list[i] += transform_value[i]
return tuple(rgb_list)
def _transform(iterable, transform_value): return [0, 0, 0]
return [x * transform_value for x in iterable]
def _return_value_or_default(value, default):
return default if value is None else value

View File

@ -1,24 +1,33 @@
import pygame import pygame
from math import floor from math import floor
from ui.auxiliary_decorator import _return_itself
class InputBox: from ui.button import Button
from ui.button import get_fixed_rgb
from ui.button import _transform_rgb
from ui.text_box import _return_value_or_default
class InputBox(Button):
backspace_tick = 0 backspace_tick = 0
backspace_deleted_streak = 0 backspace_deleted_streak = 0
backspace_breakpoint = 6 backspace_breakpoint = 6
deleting_speeds = [12, 4] deleting_speeds = [12, 4]
was_enter_hit = False
# constructor that can be used to set parameters of InputBox instance # constructor that can be used to set parameters of InputBox instance
def __init__( def __init__(
self, self,
position, position,
dimensions, dimensions,
text="",
input_box_position=None, input_box_position=None,
input_box_dimensions=None, input_box_dimensions=None,
label="",
user_input="", user_input="",
box_color=(195, 195, 195), box_color=(195, 195, 195),
input_box_color=(225, 245, 245), input_box_color=(225, 245, 245),
writing_highlight_color=(150, 255, 255),
inner_box_color=(205, 205, 205), inner_box_color=(205, 205, 205),
bottom_strip_color=(180, 180, 180), bottom_strip_color=(180, 180, 180),
font=pygame.font.get_default_font(), font=pygame.font.get_default_font(),
@ -28,13 +37,46 @@ class InputBox:
is_active=True, is_active=True,
input_centered=False, input_centered=False,
fit_text=True, fit_text=True,
clear_input_on_click=False,
outline=True, outline=True,
irregular_outline=False,
outline_additional_pixel=False,
outline_hover_inclusive=True,
outline_thickness=None, outline_thickness=None,
outline_color=(50, 50, 50), outline_color=(50, 50, 50),
box_transform_hover=1,
box_transform_inactive=1,
font_transform_hover=1,
font_transform_inactive=1,
outline_transform_hover=1,
outline_transform_inactive=0.9,
input_box_transform_hover=1.07, input_box_transform_hover=1.07,
input_box_transform_selected=2, input_box_transform_selected=2,
input_box_transform_inactive=0.9 input_box_transform_inactive=0.9
): ):
super().__init__(
position=position,
dimensions=dimensions,
text=text,
box_color=box_color,
font_color=font_color,
is_visible=is_visible,
is_active=is_active,
fit_text=fit_text,
outline=outline,
irregular_outline=irregular_outline,
outline_additional_pixel=outline_additional_pixel,
outline_hover_inclusive=outline_hover_inclusive,
outline_thickness=outline_thickness,
outline_color=outline_color,
box_transform_hover=box_transform_hover,
box_transform_inactive=box_transform_inactive,
font_transform_hover=font_transform_hover,
font_transform_inactive=font_transform_inactive,
outline_transform_hover=outline_transform_hover,
outline_transform_inactive=outline_transform_inactive
)
# extracting and setting values # extracting and setting values
smaller_dimension = min(dimensions) smaller_dimension = min(dimensions)
box_x, box_y = position box_x, box_y = position
@ -49,59 +91,59 @@ class InputBox:
default_input_box_position = (box_x + width / 12, box_y + height / 2) default_input_box_position = (box_x + width / 12, box_y + height / 2)
default_input_box_dimensions = (width - width / 6, 3 * height / 10) default_input_box_dimensions = (width - width / 6, 3 * height / 10)
# counting default outline thickness
default_outline_thickness = (smaller_dimension + (width + height) / 4) / 50
# setting attributes # setting attributes
self.position = position
self.dimensions = dimensions
self.input_box_position = _return_value_or_default(input_box_position, default_input_box_position) self.input_box_position = _return_value_or_default(input_box_position, default_input_box_position)
self.input_box_dimensions = _return_value_or_default(input_box_dimensions, default_input_box_dimensions) self.input_box_dimensions = _return_value_or_default(input_box_dimensions, default_input_box_dimensions)
self.label = label
self.user_input = user_input self.user_input = user_input
self.box_color = _get_proper_rgb(box_color) self.ib_color = get_fixed_rgb(input_box_color)
self.ib_color = _get_proper_rgb(input_box_color) self.typing_highlight_color = get_fixed_rgb(writing_highlight_color)
self.inner_box_color = _get_proper_rgb(inner_box_color) self.inner_box_color = get_fixed_rgb(inner_box_color)
self.bottom_strip_color = _get_proper_rgb(bottom_strip_color) self.bottom_strip_color = get_fixed_rgb(bottom_strip_color)
self.font_color = _get_proper_rgb(font_color) self.clear_input_on_click = clear_input_on_click
self.font_size = _return_value_or_default(font_size, floor(smaller_dimension / 2.8)) self.font_size = _return_value_or_default(font_size, floor(smaller_dimension / 2.8))
self.font = pygame.font.SysFont(font, self.font_size) self.font = pygame.font.SysFont(font, self.font_size)
self.input_font_size = max(15, floor(self.input_box_dimensions[1] - 2)) self.input_font_size = max(15, floor(self.input_box_dimensions[1] - 2))
self.input_font = pygame.font.SysFont(font, self.input_font_size) self.input_font = pygame.font.SysFont(font, self.input_font_size)
self.is_visible = is_visible
self.is_active = is_active
self.is_selected = False self.is_selected = False
self.is_active = is_active
self.input_centered = input_centered self.input_centered = input_centered
self.fit_text = fit_text
self.outline = outline
self.outline_thickness = _return_value_or_default(outline_thickness, default_outline_thickness)
self.outline_color = outline_color
self.ib_transform_hover = input_box_transform_hover self.ib_transform_hover = input_box_transform_hover
self.ib_transform_selected = input_box_transform_selected self.ib_transform_selected = input_box_transform_selected
self.ib_transform_inactive = input_box_transform_inactive self.ib_transform_inactive = input_box_transform_inactive
self.backspace_pressed_down = False self.backspace_pressed_down = False
# rendering label to get it's width and height # rendering text to get it's width and height
rendered_label = self.font.render(label, True, (0, 0, 0)) rendered_text = self.font.render(text, True, (0, 0, 0))
# if text is out of bounds and fit_text=True - resizing text to fit the box # if text is out of bounds and fit_text=True - resizing text to fit the box
if self.fit_text and rendered_label.get_width() > self.input_box_dimensions[0]: if self.fit_text and rendered_text.get_width() > self.input_box_dimensions[0]:
self.font_size = floor(self.font_size / (rendered_label.get_width() / width + 0.1)) self.font_size = floor(self.font_size / (rendered_text.get_width() / width + 0.1))
self.font = pygame.font.SysFont(font, self.font_size) self.font = pygame.font.SysFont(font, self.font_size)
# counting colors on: hover, inactive, selected # counting colors on: hover, inactive, selected
self.ib_hover_color = _get_proper_rgb(tuple(_transform(self.ib_color, self.ib_transform_hover))) self.ib_hover_color = get_fixed_rgb(tuple(_transform_rgb(self.ib_color, self.ib_transform_hover)))
self.ib_inactive_color = _get_proper_rgb(tuple(_transform(self.ib_color, self.ib_transform_inactive))) self.ib_inactive_color = get_fixed_rgb(tuple(_transform_rgb(self.ib_color, self.ib_transform_inactive)))
self.ib_selected_color = _get_proper_rgb(tuple(_transform(self.ib_color, self.ib_transform_selected))) self.ib_selected_color = get_fixed_rgb(tuple(_transform_rgb(self.ib_color, self.ib_transform_selected)))
# draws, updates and selects on mouse click the InputBox instance # draws, updates and selects on mouse click the InputBox instance
def run(self, window, mouse_position, events): def run(self, window, mouse_position, events):
self.select_on_click(mouse_position, events) self.select_on_click(mouse_position, events)
self.clear_user_input_on_click(mouse_position, events)
self.draw(window, mouse_position) self.draw(window, mouse_position)
self.update(events) self.update(events)
@_return_itself
# draws an InputBox instance (doesn't run interactions [for everything use run(self, ...) method]) # draws an InputBox instance (doesn't run interactions [for everything use run(self, ...) method])
def draw(self, window, mouse_position): def draw(self, window, mouse_position, *args, **kwargs):
text_attribute_value = self.text
self.text = ""
super().draw(window, mouse_position, *args, **kwargs)
# resetting original attribute values
self.text = text_attribute_value
# if is_visible=True drawing the InputBox # if is_visible=True drawing the InputBox
if self.is_visible: if self.is_visible:
# extracting and setting values from attributes # extracting and setting values from attributes
@ -110,12 +152,6 @@ class InputBox:
input_box_x, input_box_y = self.input_box_position input_box_x, input_box_y = self.input_box_position
input_width, input_height = self.input_box_dimensions input_width, input_height = self.input_box_dimensions
# if outline=True - drawing an outline
if self.outline:
padding = self.outline_thickness
outline_coordinates = (box_x - padding, box_y - padding, width + 2 * padding, height + 2 * padding)
pygame.draw.rect(window, self.outline_color, outline_coordinates)
# setting "transparent" background color # setting "transparent" background color
text_background_color = self.ib_color text_background_color = self.ib_color
@ -123,7 +159,7 @@ class InputBox:
# is selected # is selected
if self.is_selected: if self.is_selected:
input_box_color = self.ib_selected_color input_box_color = self.ib_selected_color
text_background_color = (150, 255, 255) text_background_color = self.typing_highlight_color
# is active and mouse is not over # is active and mouse is not over
elif not self.is_over(mouse_position) and self.is_active: elif not self.is_over(mouse_position) and self.is_active:
@ -137,9 +173,6 @@ class InputBox:
else: else:
input_box_color = self.ib_inactive_color input_box_color = self.ib_inactive_color
# drawing outer box
pygame.draw.rect(window, self.box_color, (box_x, box_y, width, height))
# drawing inner (upper) box # drawing inner (upper) box
pygame.draw.rect(window, pygame.draw.rect(window,
self.inner_box_color, self.inner_box_color,
@ -152,13 +185,13 @@ class InputBox:
# drawing bottom strip # drawing bottom strip
pygame.draw.rect(window, self.bottom_strip_color, (box_x, box_y + 9 * height / 10, width, 1 * height / 10)) pygame.draw.rect(window, self.bottom_strip_color, (box_x, box_y + 9 * height / 10, width, 1 * height / 10))
# rendering label and counting its coordinates # rendering text and counting its coordinates
rendered_label = self.font.render(self.label, True, self.font_color) rendered_text = self.font.render(self.text, True, self.font_color)
rendered_label_x = box_x + width / 2 - rendered_label.get_width() / 2 rendered_text_x = box_x + width / 2 - rendered_text.get_width() / 2
rendered_label_y = box_y + height / 4 - rendered_label.get_height() / 2 rendered_text_y = box_y + height / 4 - rendered_text.get_height() / 2
# drawing the label on the coordinates # drawing the text on the coordinates
window.blit(rendered_label, (rendered_label_x, rendered_label_y)) window.blit(rendered_text, (rendered_text_x, rendered_text_y))
# rendering input # rendering input
rendered_input_text = self.input_font.render(self.user_input, True, self.font_color, text_background_color) rendered_input_text = self.input_font.render(self.user_input, True, self.font_color, text_background_color)
@ -177,6 +210,9 @@ class InputBox:
# updates InputBox'es attributes if user types something in it # updates InputBox'es attributes if user types something in it
def update(self, events): def update(self, events):
# resetting attribute value
self.was_enter_hit = False
# if the InputBox is selected - updating the input text # if the InputBox is selected - updating the input text
if self.is_selected: if self.is_selected:
self._delete_characters_if_backspace_pressed() self._delete_characters_if_backspace_pressed()
@ -194,6 +230,7 @@ class InputBox:
elif event.key == pygame.K_RETURN: elif event.key == pygame.K_RETURN:
self.is_selected = False self.is_selected = False
self.was_enter_hit = True
# if text isn't too long - adding a character # if text isn't too long - adding a character
elif rendered_input.get_width() + 10 < self.input_box_dimensions[0]: elif rendered_input.get_width() + 10 < self.input_box_dimensions[0]:
@ -210,28 +247,42 @@ class InputBox:
# if is active set is_selected attribute accordingly # if is active set is_selected attribute accordingly
if self.is_active: if self.is_active:
for event in events: for event in events:
# if mouse is clicked set as selected if mouse is over, or set as not selected otherwise
if event.type == pygame.MOUSEBUTTONUP: if event.type == pygame.MOUSEBUTTONUP:
self.set_is_selected(self.is_over(mouse_position)) self.set_is_selected(self.is_over(mouse_position))
# checks if a position is over the InputBox and returns True or False # clears user's input on click
def is_over(self, position): def clear_user_input_on_click(self, mouse_position, events):
mouse_x, mouse_y = position if self.is_active and self.clear_input_on_click:
button_x, button_y = self.position for event in events:
width, height = self.dimensions
return button_x <= mouse_x <= (button_x + width) and button_y <= mouse_y <= (button_y + height) if event.type == pygame.MOUSEBUTTONUP and self.is_over(mouse_position):
self.clear_input()
# returns InputBox'es label # returns True if user input is empty, or False otherwise
def get_label(self): def is_input_empty(self):
return self.label return not any(self.user_input)
# clears user's input
def clear_input(self):
self.user_input = ""
# returns True if enter key is hit, or False otherwise
def is_enter_hit(self, events):
if self.is_active and self.is_selected:
for event in events:
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
return True
return self.was_enter_hit
# returns InputBox'es user's input # returns InputBox'es user's input
def get_input(self): def get_input(self):
return self.user_input return self.user_input
# sets chosen coordinates # sets chosen coordinates
def set_space( def set_coordinates(
self, self,
position=None, position=None,
dimensions=None, dimensions=None,
@ -239,11 +290,7 @@ class InputBox:
input_box_dimensions=None, input_box_dimensions=None,
outline_thickness=None outline_thickness=None
): ):
if position is not None: super().set_coordinates(position=position, dimensions=dimensions, outline_thickness=outline_thickness)
self.position = position
if dimensions is not None:
self.dimensions = dimensions
if input_box_position is not None: if input_box_position is not None:
self.input_box_position = input_box_position self.input_box_position = input_box_position
@ -251,13 +298,9 @@ class InputBox:
if input_box_dimensions is not None: if input_box_dimensions is not None:
self.input_box_dimensions = input_box_dimensions self.input_box_dimensions = input_box_dimensions
if outline_thickness is not None:
self.outline_thickness = outline_thickness
# sets chosen text attributes # sets chosen text attributes
def set_texts(self, label=None, user_input=None): def set_texts(self, text=None, user_input=None):
if label is not None: super().set_text(text=text)
self.label = label
if user_input is not None: if user_input is not None:
self.user_input = user_input self.user_input = user_input
@ -268,15 +311,14 @@ class InputBox:
self.font_size = font_size self.font_size = font_size
# rendering text to get it's width and height # rendering text to get it's width and height
rendered_text = self.font.render(self.user_input, True, (0, 0, 0)) rendered_text = self.font.render(self.text, True, (0, 0, 0))
# if text is out of bounds and fit_text=True - resizing text to fit the box # if text is out of bounds and fit_text=True - resizing text to fit the box
if self.fit_text and rendered_text.get_width() > self.dimensions[0]: if self.fit_text and rendered_text.get_width() > self.input_box_dimensions[0]:
self.font_size = floor(self.font_size / (rendered_text.get_width() / self.dimensions[0] + 0.1)) self.font_size = floor(self.font_size / (rendered_text.get_width() / self.dimensions[0] + 0.1))
self.font = pygame.font.SysFont(font, self.font_size) self.font = pygame.font.SysFont(font, self.font_size)
if font is not None: super().set_font(font=font)
self.font = pygame.font.SysFont(font, self.font_size)
# sets chosen color attributes # sets chosen color attributes
def set_colors( def set_colors(
@ -284,30 +326,39 @@ class InputBox:
box_color=None, box_color=None,
input_box_color=None, input_box_color=None,
inner_box_color=None, inner_box_color=None,
writing_highlight_color=None,
bottom_strip_color=None, bottom_strip_color=None,
font_color=None, font_color=None,
outline_color=None, outline_color=None,
box_transform_hover=None,
box_transform_inactive=None,
font_transform_hover=None,
font_transform_inactive=None,
input_box_transform_hover=None, input_box_transform_hover=None,
input_box_transform_selected=None, input_box_transform_selected=None,
input_box_transform_inactive=None input_box_transform_inactive=None,
): ):
if box_color is not None: super().set_colors(
self.box_color = _get_proper_rgb(box_color) box_color=box_color,
font_color=font_color,
outline_color=outline_color,
box_transform_hover=box_transform_hover,
box_transform_inactive=box_transform_inactive,
font_transform_hover=font_transform_hover,
font_transform_inactive=font_transform_inactive
)
if input_box_color is not None: if input_box_color is not None:
self.ib_color = _get_proper_rgb(input_box_color) self.ib_color = get_fixed_rgb(input_box_color)
if writing_highlight_color is not None:
self.typing_highlight_color = writing_highlight_color
if inner_box_color is not None: if inner_box_color is not None:
self.inner_box_color = _get_proper_rgb(inner_box_color) self.inner_box_color = get_fixed_rgb(inner_box_color)
if bottom_strip_color is not None: if bottom_strip_color is not None:
self.bottom_strip_color = _get_proper_rgb(bottom_strip_color) self.bottom_strip_color = get_fixed_rgb(bottom_strip_color)
if font_color is not None:
self.font_color = _get_proper_rgb(font_color)
if outline_color is not None:
self.outline_color = _get_proper_rgb(outline_color)
if input_box_transform_hover is not None: if input_box_transform_hover is not None:
self.ib_transform_hover = input_box_transform_hover self.ib_transform_hover = input_box_transform_hover
@ -318,14 +369,29 @@ class InputBox:
if input_box_transform_inactive is not None: if input_box_transform_inactive is not None:
self.ib_transform_inactive = input_box_transform_inactive self.ib_transform_inactive = input_box_transform_inactive
self.ib_hover_color = _get_proper_rgb(tuple(_transform(self.ib_color, self.ib_transform_hover))) self.ib_hover_color = get_fixed_rgb(tuple(_transform_rgb(self.ib_color, self.ib_transform_hover)))
self.ib_inactive_color = _get_proper_rgb(tuple(_transform(self.ib_color, self.ib_transform_inactive))) self.ib_inactive_color = get_fixed_rgb(tuple(_transform_rgb(self.ib_color, self.ib_transform_inactive)))
self.ib_selected_color = _get_proper_rgb(tuple(_transform(self.ib_color, self.ib_transform_selected))) self.ib_selected_color = get_fixed_rgb(tuple(_transform_rgb(self.ib_color, self.ib_transform_selected)))
# sets chosen flag attributes # sets chosen flag attributes
def set_flags(self, is_visible=None, is_active=None, input_centered=None, fit_text=None, outline=None): def set_flags(
if is_visible is not None: self,
self.is_visible = is_visible is_visible=None,
is_active=None,
input_centered=None,
fit_text=None,
outline=None,
irregular_outline=None,
outline_additional_pixel=None,
outline_hover_inclusive=None
):
super().set_flags(
is_visible=is_visible,
outline=outline,
irregular_outline=None,
outline_additional_pixel=None,
outline_hover_inclusive=None
)
if is_active is not None: if is_active is not None:
self.is_active = is_active self.is_active = is_active
@ -336,9 +402,6 @@ class InputBox:
if fit_text is not None: if fit_text is not None:
self.fit_text = fit_text self.fit_text = fit_text
if outline is not None:
self.outline = outline
# sets is_selected with a given value # sets is_selected with a given value
def set_is_selected(self, is_selected): def set_is_selected(self, is_selected):
self.is_selected = is_selected self.is_selected = is_selected
@ -360,18 +423,3 @@ class InputBox:
elif self.backspace_pressed_down: elif self.backspace_pressed_down:
self.backspace_tick += 1 self.backspace_tick += 1
def _get_proper_rgb(x):
r, g, b = x
r, g, b = max(0, min(255, floor(r))), max(0, min(255, floor(g))), max(0, min(255, floor(b)))
return tuple((r, g, b))
def _transform(iterable, transform_value):
return [x * transform_value for x in iterable]
def _return_value_or_default(value, default):
return default if value is None else value

View File

@ -16,6 +16,9 @@ class TextBox:
is_visible=True, is_visible=True,
fit_text=True, fit_text=True,
outline=True, outline=True,
irregular_outline=False,
outline_additional_pixel=False,
outline_hover_inclusive=True,
outline_thickness=None, outline_thickness=None,
outline_color=(50, 50, 50) outline_color=(50, 50, 50)
): ):
@ -24,21 +27,24 @@ class TextBox:
width, height = dimensions width, height = dimensions
# counting default outline thickness # counting default outline thickness
default_outline_thickness = (smaller_dimension + (width + height) / 4) / 50 default_outline_thickness = max(1, floor((2 * smaller_dimension + width + height) / 200))
# setting attributes # setting attributes
self.position = position self.position = position
self.dimensions = dimensions self.dimensions = dimensions
self.text = text self.text = text
self.box_color = _get_proper_rgb(box_color) self.box_color = get_fixed_rgb(box_color)
self.font_color = _get_proper_rgb(font_color) self.font_color = get_fixed_rgb(font_color)
self.font_size = _return_value_or_default(font_size, floor(smaller_dimension / 2)) self.font_size = _return_value_or_default(font_size, floor(smaller_dimension / 2))
self.font = pygame.font.SysFont(font, self.font_size) self.font = pygame.font.SysFont(font, self.font_size)
self.is_visible = is_visible self.is_visible = is_visible
self.fit_text = fit_text self.fit_text = fit_text
self.outline = outline self.outline = outline
self.irregular_outline = irregular_outline
self.outline_additional_pixel = outline_additional_pixel
self.outline_hover_inclusive = outline_hover_inclusive
self.outline_thickness = _return_value_or_default(outline_thickness, default_outline_thickness) self.outline_thickness = _return_value_or_default(outline_thickness, default_outline_thickness)
self.outline_color = _get_proper_rgb(outline_color) self.outline_color = get_fixed_rgb(outline_color)
# rendering text to get it's width and height # rendering text to get it's width and height
rendered_text = self.font.render(text, True, (0, 0, 0)) rendered_text = self.font.render(text, True, (0, 0, 0))
@ -49,7 +55,7 @@ class TextBox:
self.font = pygame.font.SysFont(font, self.font_size) self.font = pygame.font.SysFont(font, self.font_size)
# draws the TextBox # draws the TextBox
def draw(self, window): def draw(self, window, *args, **kwargs):
# if is_visible=True drawing the TextBox # if is_visible=True drawing the TextBox
if self.is_visible: if self.is_visible:
# extracting and setting values from attributes # extracting and setting values from attributes
@ -58,8 +64,9 @@ class TextBox:
# if outline=True - drawing an outline # if outline=True - drawing an outline
if self.outline: if self.outline:
padding = self.outline_thickness padding = self.outline_thickness - self.irregular_outline / 2 + self.outline_additional_pixel
outline_coordinates = (box_x - padding, box_y - padding, width + 2 * padding, height + 2 * padding) outline_coordinates = (box_x - padding, box_y - padding, width + 2 * padding, height + 2 * padding)
pygame.draw.rect(window, self.outline_color, outline_coordinates) pygame.draw.rect(window, self.outline_color, outline_coordinates)
# drawing the box # drawing the box
@ -73,6 +80,27 @@ class TextBox:
# drawing the box on the coordinates # drawing the box on the coordinates
window.blit(rendered_text, (rendered_text_x, rendered_text_y)) window.blit(rendered_text, (rendered_text_x, rendered_text_y))
# checks if a position is over the InputBox and returns True or False
def is_over(self, position):
mouse_x, mouse_y = position
if not self.outline_hover_inclusive:
button_x, button_y = self.position
width, height = self.dimensions
else:
padding = self.outline_thickness
button_x, button_y = self.position
width, height = self.dimensions
button_x, button_y = button_x - padding, button_y - padding
width, height = width + 2 * padding, height + 2 * padding
return button_x <= mouse_x <= (button_x + width) and button_y <= mouse_y <= (button_y + height)
# returns text value
def get_text(self):
return self.text
# sets chosen coordinates # sets chosen coordinates
def set_coordinates(self, position=None, dimensions=None, outline_thickness=None): def set_coordinates(self, position=None, dimensions=None, outline_thickness=None):
if position is not None: if position is not None:
@ -86,7 +114,8 @@ class TextBox:
# sets the TextBox's text # sets the TextBox's text
def set_text(self, text): def set_text(self, text):
self.text = text if text is not None:
self.text = text
# sets chosen font attributes # sets chosen font attributes
def set_font(self, font=None, font_size=None): def set_font(self, font=None, font_size=None):
@ -107,25 +136,41 @@ class TextBox:
# sets chosen color attributes # sets chosen color attributes
def set_colors(self, box_color=None, font_color=None, outline_color=None): def set_colors(self, box_color=None, font_color=None, outline_color=None):
if box_color is not None: if box_color is not None:
self.box_color = _get_proper_rgb(box_color) self.box_color = get_fixed_rgb(box_color)
if font_color is not None: if font_color is not None:
self.font_color = _get_proper_rgb(font_color) self.font_color = get_fixed_rgb(font_color)
if outline_color is not None: if outline_color is not None:
self.outline_color = outline_color self.outline_color = outline_color
# sets chosen flags # sets chosen flags
def set_flags(self, is_visible=None, outline=None): def set_flags(
self,
is_visible=None,
outline=None,
irregular_outline=None,
outline_additional_pixel=None,
outline_hover_inclusive=None
):
if is_visible is not None: if is_visible is not None:
self.is_visible = is_visible self.is_visible = is_visible
if outline is not None: if outline is not None:
self.outline = outline self.outline = outline
if irregular_outline is not None:
self.irregular_outline = irregular_outline
if outline_additional_pixel is not None:
self.outline_additional_pixel = outline_additional_pixel
if outline_hover_inclusive is not None:
self.outline_hover_inclusive = outline_hover_inclusive
# returns values that are not out of range # returns values that are not out of range
def _get_proper_rgb(x): def get_fixed_rgb(x):
r, g, b = x r, g, b = x
r, g, b = max(0, min(255, floor(r))), max(0, min(255, floor(g))), max(0, min(255, floor(b))) r, g, b = max(0, min(255, floor(r))), max(0, min(255, floor(g))), max(0, min(255, floor(b)))