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'
class PromMenu: def __init__(self, player, pos): if player.color == 'white': queen_img = white_queen bishop_img = white_bishop rock_img = white_rock knight_img = white_knight else: queen_img = black_queen bishop_img = black_bishop rock_img = black_rock knight_img = black_knight self.button_queen = Button(DIM_PBUTTON, (pos[0] + 50, pos[1]), surface=queen_img, surf_font_color=C.LIGHT_GREY) self.button_bishop = Button(DIM_PBUTTON, (pos[0] + 300, pos[1]), surface=bishop_img, surf_font_color=C.LIGHT_GREY) self.button_rock = Button(DIM_PBUTTON, (pos[0] + 50, pos[1] + 250), surface=rock_img, surf_font_color=C.LIGHT_GREY) self.button_knight = Button(DIM_PBUTTON, (pos[0] + 300, pos[1] + 250), surface=knight_img, surf_font_color=C.LIGHT_GREY) self.state = 'wait' def react_events(self, events, pressed): if self.button_knight.pushed(events): self.state = 'done' self.piece_name = 'knight' if self.button_queen.pushed(events): self.state = 'done' self.piece_name = 'queen' if self.button_rock.pushed(events): self.state = 'done' self.piece_name = 'rock' if self.button_bishop.pushed(events): self.state = 'done' self.piece_name = 'bishop' def display(self): self.button_queen.display() self.button_bishop.display() self.button_rock.display() self.button_knight.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'
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()