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 __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 load_room(self, num_of_room): # загрузка комнаты на экран self.base = [] level = self.rooms[num_of_room].structure() empty = Image.open('Sprites/ground/idle/00.png') wall = Image.open('Sprites/wall/idle/00.png') background = Image.new('RGB', (len(level[0]) * TILE, len(level) * TILE), (255, 255, 255)) # собираем из маленьких изображений пустых клетов и стен # одно большое изображение поля чтобы потом отображать только его for i in range(len(level)): for k in range(len(level[i])): if level[i][k] == 'W': self.base.append(Wall((k, i))) background.paste(wall, (k * TILE, i * TILE)) else: self.base.append(Empty((k, i))) background.paste(empty, (k * TILE, i * TILE)) self.background = pygame.image.fromstring(background.tobytes(), background.size, background.mode) self.top_panel = Panel(self.player, 0) # создаем верхнюю self.bottom_panel = Panel(None, 550) # и нижнюю панели self.buttons = [ # создаем кнопки Button('game/panel/exit', (550, 10), 'menu'), Button('game/panel/inventory', (450, 10), 'inventory'), Button('game/panel/save', (500, 10), 'save'), ]
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 __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, parent): super().__init__(parent) self.bg = Sprite( 0, 0, image=pygame.image.load("assets/misc/mainMenu.png").convert(), size=c.sizes["win"]) pygame.mixer.music.load("assets/audio/Intro.mp3") pygame.mixer.music.play(-1) self.play_button = Button( c.pads["play"], pygame.image.load("assets/misc/start.png").convert_alpha(), pygame.image.load( "assets/misc/startHighlight.png").convert_alpha()) self.exit = Button( c.pads["exit"], pygame.image.load("assets/misc/potQuit.png").convert(), pygame.image.load("assets/misc/potQuitHighlight.png").convert())
def __init__(self): super(MainMenu, self).__init__() self.title = Line((game.screen_size[0] / 2, game.screen_size[1] / 4), "Houd de knop ingedrukt", game.get_font('title')) self.cursor = Cursor() self.start_button = Button( (game.screen_size[0] / 2, game.screen_size[1] / 2), self.start_button_pressed) game.global_state_changed.subscribe(self.global_state_changed)
def set_corners_points(self): corner = Button(DIM_CORNER, (0,0), C.BLUE) corner.MARGE_WIDTH = 3 self.CTOPLEFT = corner self.CTOPLEFT.set_pos(self.TOPLEFT, center=True) corner = Button(DIM_CORNER, (0,0), C.BLUE) corner.MARGE_WIDTH = 3 self.CTOPRIGHT = corner self.CTOPRIGHT.set_pos(self.TOPRIGHT, center=True) corner = Button(DIM_CORNER, (0,0), C.BLUE) corner.MARGE_WIDTH = 3 self.CBOTTOMRIGHT = corner self.CBOTTOMRIGHT.set_pos(self.BOTTOMRIGHT, center=True) corner = Button(DIM_CORNER, (0,0), C.BLUE) corner.MARGE_WIDTH = 3 self.CBOTTOMLEFT = corner self.CBOTTOMLEFT.set_pos(self.BOTTOMLEFT, center=True) self.corners_points = [self.CTOPLEFT, self.CTOPRIGHT, self.CBOTTOMLEFT, self.CBOTTOMRIGHT]
def main(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.win_width, ai_settings.win_height)) pygame.display.set_caption("PyPong_v01") pygame.display.set_icon(pygame.image.load("images/ball.png")) player1 = Racket(screen, 1) player2 = Racket(screen, 2) g_ball = Ball(screen) sb = Scoreboard(screen) bplay1 = Button(screen, '1 Player', 'active', 0) bplay2 = Button(screen, '2 Players', 'noactive', 1) bplayOnlineServer = Button(screen, 'Create Online Game', 'noactive', 2) bplayOnlineClient = Button(screen, 'Connect Online Game', 'noactive', 3) bquit = Button(screen, 'Exit', 'noactive', 4) button_box = [bplay1, bplay2, bquit, bplayOnlineServer, bplayOnlineClient] music = pygame.mixer.music.load('music/Sound.mp3') pygame.mixer.music.play(-1, 0.0) ai = Pong_ai(screen, ai_settings) sock = False conn = False while True: if ai_settings.game_active == 'fase_menu': game_functions.menu_controll(ai_settings, screen, button_box) if ai_settings.game_active == 'game_fase' or ai_settings.game_active == 'fase_end': if ai_settings.server_on == True and ai_settings.yes_online != 1: sock, conn = game_functions.create_server(ai_settings) if ai_settings.client_on == True and ai_settings.yes_online != 2: sock = game_functions.create_client(ai_settings) game_functions.check_events(ai_settings, screen, player1, player2, g_ball, sb, ai, sock, conn) game_functions.check_collide(screen, player1, player2, g_ball) game_functions.up_score(screen, player1, player2, g_ball, sb) game_functions.update_screen(ai_settings, screen, player1, player2, g_ball, sb)
def __init__(self, settings): super().__init__(settings) self.content = Text({ 'position': (self.position[0] + 20, self.position[1] + 15), 'text': '', 'hidden': settings.get('hidden', False) }) self.base = Button({ 'position': self.position, 'path': 'line_edit', 'function': 'line_edit', 'args': '["' + settings['name'] + '"]', }) self.name = settings['name']
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 add_button(*args, **kwargs): b = Button(*args, **kwargs) interface_components.add(b)
class Editor: # tools button_text_box = Button(DIM_TOOLS, (0, POS_TY), C.XLIGHT_GREY,'TextBox') button_button = Button(DIM_TOOLS, (0, POS_TY+80), C.XLIGHT_GREY,'Button') button_input_text = Button(DIM_TOOLS, (0, POS_TY+160), C.XLIGHT_GREY,'InputText') button_cadre = Button(DIM_TOOLS, (0, POS_TY+240), C.XLIGHT_GREY,'Cadre') button_done = Button(DIM_BDONE, (2800, 1400), C.LIGHT_BLUE,'Done',font=Font.f(30)) drag_surf = None objs = [] state = 'running' # set attr here for on_resize method window = None TOP_LEFT = [0,0] TOP_RIGHT = [0,0] BOTTOM_LEFT = [0,0] BOTTOM_RIGHT = [0,0] current_selected = None @classmethod 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]] ) @staticmethod def on_resize(factor): if Editor.window: dim = Editor.window.dim Editor.TOP_LEFT = Interface.dim.scale([POS_WIN[0], POS_WIN[1]]) Editor.TOP_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1]]) Editor.BOTTOM_LEFT = Interface.dim.scale([POS_WIN[0], POS_WIN[1] + dim[1]]) Editor.BOTTOM_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1] + dim[1]] ) @classmethod def create_obj(cls, pos, objtype): gui_obj = GuiObj(pos, C.WHITE, objtype) cls.objs.append(gui_obj) cls.current_selected = gui_obj Settings.set_obj(gui_obj) @classmethod def check_deselect(cls): if cls.current_selected: if cls.cadre.on_it() and not cls.current_selected.as_change_dim: if not cls.current_selected.changing_dim and not cls.current_selected.on_it(): return True return False @classmethod def check_done(cls): for gobj in cls.objs: if not gobj.name: return return True @classmethod def str_gobj(cls, gobj): ''' Name, objtype, dim, pos, text, color, font ''' string = gobj.name + '\n' string += gobj.objtype + '\n' string += f'{gobj.dim[0]} {gobj.dim[1]}\n' pos = gobj.get_real_pos() string += f'{pos[0]} {pos[1]}\n' string += gobj.input_text.content + '\n' string += str(gobj.color) + '\n' # can be either str or list string += str(gobj.input_text.font['size']) return string @classmethod def create_savefile(cls): # create file with open(cls.window.name+'.pygui','w') as file: sep = '\n'+SEPARATOR+'\n' # first write name, dim of window global_info = f'{cls.window.name} {cls.window.dim[0]} {cls.window.dim[1]}' file.write(global_info) file.write(sep) for gobj in cls.objs: file.write(cls.str_gobj(gobj)) file.write(sep) @classmethod def display(cls): cls.cadre.display() cls.button_text_box.display() cls.button_button.display() cls.button_input_text.display() cls.button_cadre.display() cls.button_done.display() if cls.drag_surf: cls.drag_surf.run() for gui_obj in cls.objs: gui_obj.display() @classmethod def react_events(cls, events, pressed): pos = pygame.mouse.get_pos() Settings.run(events, pressed) # first check for changing dim for gobj in cls.objs: gobj.react_events(events, pressed) # check if click anywhere -> deselect gui obj for event in events: if event.type == pygame.MOUSEBUTTONUP: if cls.check_deselect(): cls.current_selected.selected = False cls.current_selected = None Settings.deselect() # then check for new selected for gobj in cls.objs: if gobj.pushed(events): if not cls.current_selected: cls.current_selected = gobj gobj.selected = True Settings.set_obj(gobj) # check for gui obj to remove if pressed[pygame.K_DELETE]: if cls.current_selected: cls.objs.remove(cls.current_selected) cls.current_selected = None Settings.deselect() # check for deselect by enter if pressed[pygame.K_RETURN]: if cls.current_selected: cls.current_selected.selected = False cls.current_selected = None Settings.deselect() # last check for new gui obj if cls.drag_surf: for event in events: if event.type == pygame.MOUSEBUTTONUP: # set new obj on window cls.create_obj(pos,cls.drag_surf.objtype) # reset drag_surf cls.drag_surf = None if cls.button_text_box.pushed(events): cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'TextBox') elif cls.button_button.pushed(events): cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'Button') elif cls.button_input_text.pushed(events): cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'InputText') elif cls.button_cadre.pushed(events): cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'Cadre') # end edition if cls.button_done.pushed(events): cls.create_savefile() cls.state = 'done'
for row, new_row in zip(window.window, new_window.window): for cell, new_cell in zip(row, new_row): neighbours = cell.get_neighbours(window) alive = count_alive_neighbours(neighbours) if cell.state == 0 and alive == 3: new_cell.state = 1 elif cell.state == 1 and alive in [2, 3]: new_cell.state = 1 elif cell.state == 1: new_cell.state = 0 return new_window generation = 1 window = Window(500, 500) gen_button = Button(510, 40, 180, 40) active_cells_button = Button(510, 100, 180, 40) start_button = Button(550, 160, 100, 40, True) stop_button = Button(550, 210, 100, 40, True) save_button = Button(550, 270, 100, 40, True) read_button = Button(550, 320, 100, 40, True) def draw_interface(screen, window, generation): gen_button.draw(screen, 'Generation: {}'.format(generation)) active_cells_button.draw( screen, 'Active cells: {}'.format(window.count_active_cells())) start_button.draw(screen, ' START') stop_button.draw(screen, ' STOP') save_button.draw(screen, ' SAVE') read_button.draw(screen, ' READ')