class CreatePy: state = 'wait' def __init__(self, dim, pos): self.cadre = Cadre(dim, pos, C.WHITE) self.text_create = TextBox(DIM_INPNAME, (pos[0] + 40, pos[1] + 100), C.WHITE, 'Create a python file?', font=Font.f(30)) self.button_yes = Button(DIM_BDONE, (pos[0] + 40, pos[1] + 180), C.LIGHT_GREEN, 'Yes', font=Font.f(30)) self.button_no = Button(DIM_BDONE, (pos[0] + 200, pos[1] + 180), C.LIGHT_RED, 'No', font=Font.f(30)) def display(self): self.cadre.display() self.button_yes.display() self.button_no.display() self.text_create.display() def react_events(self, events, pressed): if self.button_yes.pushed(events): self.state = 'yes' elif self.button_no.pushed(events): self.state = 'no'
def __init__(self, pos): self.dim = (800, 600) self.pos = list(pos) self.cadre = Cadre(self.dim, pos, C.WHITE, set_transparent=True) self.text_newwindow = TextBox((self.dim[0], 80), pos, C.WHITE, 'New Window') self.text_dim = TextBox((400, 60), (pos[0] + 200, pos[1] + 150), C.WHITE, "Enter window dimension", font=Font.f(30)) self.input_x = InputText(DIM_INPDIM, (pos[0] + 210, pos[1] + 250), C.WHITE, text='1000', limit=4) self.input_y = InputText(DIM_INPDIM, (pos[0] + 410, pos[1] + 250), C.WHITE, text='1000', limit=4) self.text_name = TextBox((150, 60), (pos[0] + 100, pos[1] + 350), C.WHITE, 'File name:', font=Font.f(30)) self.input_name = InputText(DIM_INPNAME, (pos[0] + 300, pos[1] + 350), C.WHITE) self.button_done = Button(DIM_BDONE, (pos[0] + 650, pos[1] + 500), C.LIGHT_BLUE, 'Done', font=Font.f(30))
def __init__(self, dim, pos): dim_cr = (round(dim[0] * 5 / 9), dim[1]) dim_bar = (round(dim[0] * 1 / 18), dim[1]) dim_ptr = (round(dim[0] / 90), round(dim[0] / 90)) dim_view = (round(dim[0] * 5 / 18), dim[1]) pos_ptr = (round(pos[0] + dim_cr[0] / 2), round(pos[1] + dim_cr[1] / 2)) pos_bar = (pos[0] + dim_cr[0] + dim_bar[0], pos[1]) pos_view = (pos[0] + dim_cr[0] + 3 * dim_bar[0], pos[1]) self.cadre = Cadre(dim, pos, set_transparent=True) # create color range img self.arr_color = get_color_range([255, 0, 0]) self.color_range = Button(dim_cr, pos, surface=self.arr_color, highlight=False) self.pointer = Form(dim_ptr, pos_ptr, C.LIGHT_GREY) self.bar_pointer = Form((dim_bar[0], dim_ptr[0]), pos_bar, C.LIGHT_GREY) self.arr_bar = create_color_bar(150) self.color_bar = Button(dim_bar, pos_bar, surface=self.arr_bar, highlight=False) self.color_view = Form(dim_view, pos_view) self.range_active = False self.bar_active = False self.chosen_color = None
class NewWindow: def __init__(self, pos): self.dim = (800, 600) self.pos = list(pos) self.cadre = Cadre(self.dim, pos, C.WHITE, set_transparent=True) self.text_newwindow = TextBox((self.dim[0], 80), pos, C.WHITE, 'New Window') self.text_dim = TextBox((400, 60), (pos[0] + 200, pos[1] + 150), C.WHITE, "Enter window dimension", font=Font.f(30)) self.input_x = InputText(DIM_INPDIM, (pos[0] + 210, pos[1] + 250), C.WHITE, text='1000', limit=4) self.input_y = InputText(DIM_INPDIM, (pos[0] + 410, pos[1] + 250), C.WHITE, text='1000', limit=4) self.text_name = TextBox((150, 60), (pos[0] + 100, pos[1] + 350), C.WHITE, 'File name:', font=Font.f(30)) self.input_name = InputText(DIM_INPNAME, (pos[0] + 300, pos[1] + 350), C.WHITE) self.button_done = Button(DIM_BDONE, (pos[0] + 650, pos[1] + 500), C.LIGHT_BLUE, 'Done', font=Font.f(30)) def react_events(self, events, pressed): self.input_x.run(events, pressed) self.input_y.run(events, pressed) self.input_name.run(events, pressed) def create_windows(self): try: x = int(self.input_x.content) y = int(self.input_y.content) if x > 3000 or y > 1600: print('Wrong coord') return name = self.input_name.content except: print('Wrong coord') return new_win = Window(name, (x, y)) return new_win def display(self): self.cadre.display() self.text_newwindow.display() self.text_dim.display() self.input_x.display() self.input_y.display() self.text_name.display() self.input_name.display() self.button_done.display()
class Menu: default_text_turn = "Turn white" default_text_poss_moves = "Possible moves: 20" def __init__(self, ChessGame): self.ChessGame = ChessGame self.cadre = Cadre((600, 1600), POS_MENU) self.text_turn = TextBox((400, 80), (POS_MENU[0] + 50, POS_MENU[1] + 50), text="Turn white", font=Font.f(50), centered=False) self.text_poss_moves = TextBox((600, 60), (POS_MENU[0] + 50, POS_MENU[1] + 210), text="Possible moves: 20", font=Font.f(50), centered=False) self.text_end = TextBox((450, 120), (POS_MENU[0] + 50, POS_MENU[1] + 360), font=Font.f(50)) self.button_start = Button((450, 100), (POS_MENU[0] + 50, POS_MENU[1] + 590), C.LIGHT_GREEN, text="Start new game", font=Font.f(50)) self.state = 'start' def display(self): self.cadre.display() self.text_turn.display() self.text_poss_moves.display() if self.state == 'start': self.text_end.display() self.button_start.display() def react_events(self, events, pressed): if self.state == 'start': if self.button_start.pushed(events): self.state = 'run' self.ChessGame.set_players() # re-add pieces Interface.add_resizable_objs( self.ChessGame.players['white'].pieces) Interface.add_resizable_objs( self.ChessGame.players['black'].pieces) def __call__(self, turn, poss_moves): '''Call in ChessGame.check_end_game''' self.text_turn.set_text(f'Turn: {turn}') self.text_poss_moves.set_text(f'Possible moves: {poss_moves}') def end_game(self, winner): self.text_end.set_text(f'Winner {winner}') self.text_turn.set_text(self.default_text_turn) self.text_poss_moves.set_text(self.default_text_poss_moves) self.state = 'start'
def __init__(self, dim, pos): self.cadre = Cadre(dim, pos, C.WHITE) self.text_create = TextBox(DIM_INPNAME, (pos[0] + 40, pos[1] + 100), C.WHITE, 'Create a python file?', font=Font.f(30)) self.button_yes = Button(DIM_BDONE, (pos[0] + 40, pos[1] + 180), C.LIGHT_GREEN, 'Yes', font=Font.f(30)) self.button_no = Button(DIM_BDONE, (pos[0] + 200, pos[1] + 180), C.LIGHT_RED, 'No', font=Font.f(30))
def set_window(cls, window): cls.window = window # don't rescale window.dim to have the original dimension stored (for on_resize) dim = window.dim cls.cadre = Cadre(dim, POS_WIN, C.WHITE) cls.TOP_LEFT = Interface.dim.scale([POS_WIN[0], POS_WIN[1]]) cls.TOP_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1]]) cls.BOTTOM_LEFT = Interface.dim.scale([POS_WIN[0], POS_WIN[1] + dim[1]]) cls.BOTTOM_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1] + dim[1]] )
def __init__(self, ChessGame): self.ChessGame = ChessGame self.cadre = Cadre((600, 1600), POS_MENU) self.text_turn = TextBox((400, 80), (POS_MENU[0] + 50, POS_MENU[1] + 50), text="Turn white", font=Font.f(50), centered=False) self.text_poss_moves = TextBox((600, 60), (POS_MENU[0] + 50, POS_MENU[1] + 210), text="Possible moves: 20", font=Font.f(50), centered=False) self.text_end = TextBox((450, 120), (POS_MENU[0] + 50, POS_MENU[1] + 360), font=Font.f(50)) self.button_start = Button((450, 100), (POS_MENU[0] + 50, POS_MENU[1] + 590), C.LIGHT_GREEN, text="Start new game", font=Font.f(50)) self.state = 'start'
class ColorRange: ''' Graphical interface to choose colors Advise: choose a dimension with a 9/5 ration Selected color stored in: chosen_color ''' def __init__(self, dim, pos): dim_cr = (round(dim[0] * 5 / 9), dim[1]) dim_bar = (round(dim[0] * 1 / 18), dim[1]) dim_ptr = (round(dim[0] / 90), round(dim[0] / 90)) dim_view = (round(dim[0] * 5 / 18), dim[1]) pos_ptr = (round(pos[0] + dim_cr[0] / 2), round(pos[1] + dim_cr[1] / 2)) pos_bar = (pos[0] + dim_cr[0] + dim_bar[0], pos[1]) pos_view = (pos[0] + dim_cr[0] + 3 * dim_bar[0], pos[1]) self.cadre = Cadre(dim, pos, set_transparent=True) # create color range img self.arr_color = get_color_range([255, 0, 0]) self.color_range = Button(dim_cr, pos, surface=self.arr_color, highlight=False) self.pointer = Form(dim_ptr, pos_ptr, C.LIGHT_GREY) self.bar_pointer = Form((dim_bar[0], dim_ptr[0]), pos_bar, C.LIGHT_GREY) self.arr_bar = create_color_bar(150) self.color_bar = Button(dim_bar, pos_bar, surface=self.arr_bar, highlight=False) self.color_view = Form(dim_view, pos_view) self.range_active = False self.bar_active = False self.chosen_color = None def react_events(self, events, pressed): if self.color_range.pushed(events): self.range_active = not self.range_active # activate/deactivate color selection if self.color_bar.pushed(events): self.bar_active = not self.bar_active # update pointer position if self.range_active: mouse_pos = Interface.mouse_pos # check that pointer is still on surf if self.color_range.on_it(): self.pointer.set_pos(mouse_pos, center=True) else: self.range_active = False # update bar pointer position if self.bar_active: mouse_pos = Interface.mouse_pos # check that pointer is still on surf if self.color_bar.on_it(): self.bar_pointer.set_pos( (self.bar_pointer.pos[0], mouse_pos[1])) else: self.bar_active = False # set last color range self.set_color_range() def set_color_range(self): '''Create a new color range based on the current bar color''' # first get base_color y = self.bar_pointer.pos[1] - self.cadre.pos[1] y = int(y * 6 * 256 / self.cadre.dim[1]) base_color = self.arr_bar[0, y, :] # create a new color range self.arr_color = get_color_range(base_color) self.color_range.set_surf(surface=self.arr_color) @set_color_range_deco def check_set_color_range(self): '''Use a delayer to avoid freaking out the cpu for no reason (creating the color range is quite heavy)''' if self.bar_active: self.set_color_range() return True def set_view_color(self): ''' Get the selected color in color range, get the correct pixel of arr_color and set color_view's color ''' # get color x = self.pointer.pos[0] - self.cadre.pos[0] x = int(x * 256 / self.color_range.dim[0]) y = self.pointer.pos[1] - self.cadre.pos[1] y = int(y * 256 / self.color_range.dim[1]) if x < 0: x = 0 if y < 0: y = 0 color = self.arr_color[x, y, :] # set new color self.color_view.set_color(color) # store color self.chosen_color = list(color) def display(self): # update colors self.set_view_color() self.check_set_color_range() # display everything self.color_range.display() self.color_bar.display() self.pointer.display() self.bar_pointer.display() self.color_view.display() self.cadre.display()