def __init__(self): self.r = 0 self.tw = None # create a new window self.win = Gtk.Window(Gdk.WindowType.TOPLEVEL) self.win.maximize() self.win.set_title( _("CardSort") + ": " + _("click to rotate; drag to swap")) self.win.connect("delete-event", lambda w, e: Gtk.main_quit()) menu = Gtk.Menu() menu_items = Gtk.MenuItem(_("2x2")) menu.append(menu_items) menu_items.connect("activate", self._grid2x2_cb) menu_items.show() menu_items = Gtk.MenuItem(_("2x3")) menu.append(menu_items) menu_items.connect("activate", self._grid2x3_cb) menu_items.show() menu_items = Gtk.MenuItem(_("3x2")) menu.append(menu_items) menu_items.connect("activate", self._grid3x2_cb) menu_items.show() menu_items = Gtk.MenuItem(_("3x3")) menu.append(menu_items) menu_items.connect("activate", self._grid3x3_cb) menu_items.show() """ menu_items = Gtk.MenuItem(_("Solve it")) menu.append(menu_items) menu_items.connect("activate", self._solve_cb) menu_items.show() """ root_menu = Gtk.MenuItem("Tools") root_menu.show() root_menu.set_submenu(menu) # A vbox to put a menu and the canvas in: vbox = Gtk.VBox(False, 0) self.win.add(vbox) vbox.show() menu_bar = Gtk.MenuBar() vbox.pack_start(menu_bar, False, False, 2) menu_bar.show() canvas = Gtk.DrawingArea() vbox.pack_end(canvas, True, True, 3) canvas.show() menu_bar.append(root_menu) self.win.show_all() # Start the activity self.game = Game(canvas, os.path.join(os.path.abspath('.'), 'images')) self.game.win = self.win self.game.test = self.game.grid.test2x2 self.game.grid.reset2x2(self.game)
def __init__(self, handle): """ Initialize the toolbars and the game board """ super(PukllanapacActivity, self).__init__(handle) self._play_level = 0 self._play_mode = 0 self._setup_toolbars(_have_toolbox) # Create a canvas canvas = gtk.DrawingArea() canvas.set_size_request(gtk.gdk.screen_width(), \ gtk.gdk.screen_height()) self.set_canvas(canvas) canvas.show() self.show_all() self.win = Game(canvas, os.path.join(activity.get_bundle_path(), 'images'), self) # Restore game state from Journal or start new game if 'play_level' in self.metadata: self.change_play_level_cb( play_level=int(self.metadata['play_level'])) if 'play_mode' in self.metadata: self.change_play_mode_cb(play_mode=int(self.metadata['play_mode'])) grid = [] for i in range(24): if 'card' + str(i) in self.metadata: grid.append(int(self.metadata['card' + str(i)])) self.win.grid.restore_grid(grid, self.win.mode) for i in range(24): if 'rotate' + str(i) in self.metadata: self.win.grid.card_table[grid[i]].set_orientation( int(self.metadata['rotate' + str(i)])) self.win.mask(self._play_level)
def __init__(self, handle): """ Initialize the toolbars and the game board """ super(PukllanapacActivity,self).__init__(handle) self._play_level = 0 self._play_mode = 0 self._setup_toolbars(_have_toolbox) # Create a canvas canvas = gtk.DrawingArea() canvas.set_size_request(gtk.gdk.screen_width(), \ gtk.gdk.screen_height()) self.set_canvas(canvas) canvas.show() self.show_all() self.win = Game(canvas, os.path.join(activity.get_bundle_path(), 'images'), self) # Restore game state from Journal or start new game if 'play_level' in self.metadata: self.change_play_level_cb(play_level=int( self.metadata['play_level'])) if 'play_mode' in self.metadata: self.change_play_mode_cb(play_mode=int( self.metadata['play_mode'])) grid = [] for i in range(24): if 'card' + str(i) in self.metadata: grid.append(int(self.metadata['card' + str(i)])) self.win.grid.restore_grid(grid, self.win.mode) for i in range(24): if 'rotate' + str(i) in self.metadata: self.win.grid.card_table[grid[i]].set_orientation( int(self.metadata['rotate' + str(i)])) self.win.mask(self._play_level)
if __name__ == '__main__': # Create session s = session() # Set Content-Type header s.headers["Content-Type"] = "application/json" server = "https://server---5.repl.co/{}" data = {} data_json = json.dumps(data) # Authenticate endpoint = "auth" r = s.get(server.format(endpoint)) myid = r.json()["userid"] print("myid={}".format(myid)) manager = Manager() global_list = manager.dict() # Create a child process to listen for incoming updates freeze_support() p = Process(target=get_updates, args=( s, myid, global_list, )).start() g = Game(myid, s) g.loop() p.terminate()
class PukllanapacActivity(activity.Activity): """ Sliding puzzle game """ def __init__(self, handle): """ Initialize the toolbars and the game board """ super(PukllanapacActivity, self).__init__(handle) self._play_level = 0 self._play_mode = 0 self._setup_toolbars(_have_toolbox) # Create a canvas canvas = gtk.DrawingArea() canvas.set_size_request(gtk.gdk.screen_width(), \ gtk.gdk.screen_height()) self.set_canvas(canvas) canvas.show() self.show_all() self.win = Game(canvas, os.path.join(activity.get_bundle_path(), 'images'), self) # Restore game state from Journal or start new game if 'play_level' in self.metadata: self.change_play_level_cb( play_level=int(self.metadata['play_level'])) if 'play_mode' in self.metadata: self.change_play_mode_cb(play_mode=int(self.metadata['play_mode'])) grid = [] for i in range(24): if 'card' + str(i) in self.metadata: grid.append(int(self.metadata['card' + str(i)])) self.win.grid.restore_grid(grid, self.win.mode) for i in range(24): if 'rotate' + str(i) in self.metadata: self.win.grid.card_table[grid[i]].set_orientation( int(self.metadata['rotate' + str(i)])) self.win.mask(self._play_level) def write_file(self, file_path): """ Write the grid status to the Journal """ self.metadata['play_mode'] = self._play_mode self.metadata['play_level'] = self._play_level for i in range(24): self.metadata['card' + str(i)] = str(self.win.grid.grid[i]) self.metadata['rotate' + str(i)] = str( self.win.grid.card_table[self.win.grid.grid[i]].orientation) def _setup_toolbars(self, have_toolbox): """ Setup the toolbars.. """ if have_toolbox: toolbox = ToolbarBox() # Activity toolbar activity_button = ActivityToolbarButton(self) toolbox.toolbar.insert(activity_button, 0) activity_button.show() self.set_toolbar_box(toolbox) toolbox.show() toolbar = toolbox.toolbar else: # Use pre-0.86 toolbar design games_toolbar = gtk.Toolbar() toolbox = activity.ActivityToolbox(self) self.set_toolbox(toolbox) toolbox.add_toolbar(_('Game'), games_toolbar) toolbox.show() toolbox.set_current_toolbar(1) toolbar = games_toolbar # Add the buttons and labels to the toolbars self.level_button = button_factory(LEVEL_ICONS[self._play_level], toolbar, self.change_play_level_cb, tooltip=_('Set difficulty level.')) mode = self._play_mode mode += 1 if mode == len(GAME_ICONS): mode = 0 self.game_buttons = [] for i in range(len(GAME_ICONS)): if i == 0: self.game_buttons.append( radio_factory(GAME_ICONS[0], toolbar, self.change_play_mode_cb, cb_arg=0, tooltip=_('Select game.'), group=None)) else: self.game_buttons.append( radio_factory(GAME_ICONS[i], toolbar, self.change_play_mode_cb, cb_arg=i, tooltip=_('Select game.'), group=self.game_buttons[0])) self.game_buttons[mode].set_active(True) separator_factory(toolbar, False, True) self.status_label = label_factory(toolbar, _("drag to swap")) if _have_toolbox: separator_factory(toolbox.toolbar, True, False) stop_button = StopButton(self) stop_button.props.accelerator = '<Ctrl>q' toolbox.toolbar.insert(stop_button, -1) stop_button.show() def change_play_level_cb(self, button=None, play_level=None): """ Cycle between levels """ if self._play_mode > 0: return if play_level is None: self._play_level += 1 if self._play_level == len(LEVEL_ICONS): self._play_level = 0 else: self._play_level = play_level self.level_button.set_icon(LEVEL_ICONS[self._play_level]) self.win.grid.reset(GAME_ICONS[self._play_mode]) self.win.mask(self._play_level) def change_play_mode_cb(self, button=None, play_mode=None): """ Cycle between game modes """ if play_mode is None: self._play_mode += 1 if self._play_mode == len(GAME_ICONS): self._play_mode = 0 else: self._play_mode = play_mode mode = self._play_mode mode += 1 if mode == len(GAME_ICONS): mode = 0 if hasattr(self, 'win'): self.win.mode = GAME_ICONS[self._play_mode] self.win.grid.initialize_cards(self.win.sprites, self.win.path, self.win.card_dim, self.win.scale, GAME_ICONS[self._play_mode]) if self._play_mode > 0: self._play_level = len(LEVEL_ICONS) - 1 self.level_button.set_icon(LEVEL_ICONS[self._play_level]) self.win.mask(self._play_level) self.win.grid.reset(GAME_ICONS[self._play_mode])
def __init__(self): self.r = 0 self.tw = None # create a new window self.win = Gtk.Window(Gdk.WindowType.TOPLEVEL) self.win.maximize() self.win.set_title(_("CardSort") + ": " + _("click to rotate; drag to swap")) self.win.connect("delete_event", lambda w,e: gtk.main_quit()) menu = Gtk.Menu() menu_items = Gtk.MenuItem(_("2x2")) menu.append(menu_items) menu_items.connect("activate", self._grid2x2_cb) menu_items.show() menu_items = Gtk.MenuItem(_("2x3")) menu.append(menu_items) menu_items.connect("activate", self._grid2x3_cb) menu_items.show() menu_items = Gtk.MenuItem(_("3x2")) menu.append(menu_items) menu_items.connect("activate", self._grid3x2_cb) menu_items.show() menu_items = Gtk.MenuItem(_("3x3")) menu.append(menu_items) menu_items.connect("activate", self._grid3x3_cb) menu_items.show() """ menu_items = gtk.MenuItem(_("Solve it")) menu.append(menu_items) menu_items.connect("activate", self._solve_cb) menu_items.show() """ root_menu = Gtk.MenuItem("Tools") root_menu.show() root_menu.set_submenu(menu) # A vbox to put a menu and the canvas in: vbox = Gtk.VBox(False, 0) self.win.add(vbox) vbox.show() menu_bar = Gtk.MenuBar() vbox.pack_start(menu_bar, False, False, 2) menu_bar.show() canvas = Gtk.DrawingArea() vbox.pack_end(canvas, True, True, 3) canvas.show() menu_bar.append(root_menu) self.win.show_all() # Start the activity self.game = Game(canvas, os.path.join(os.path.abspath('.'), 'images')) self.game.win = self.win self.game.test = self.game.grid.test2x2 self.game.grid.reset2x2(self.game)
class CardSortMain: def __init__(self): self.r = 0 self.tw = None # create a new window self.win = Gtk.Window(Gdk.WindowType.TOPLEVEL) self.win.maximize() self.win.set_title(_("CardSort") + ": " + _("click to rotate; drag to swap")) self.win.connect("delete_event", lambda w,e: gtk.main_quit()) menu = Gtk.Menu() menu_items = Gtk.MenuItem(_("2x2")) menu.append(menu_items) menu_items.connect("activate", self._grid2x2_cb) menu_items.show() menu_items = Gtk.MenuItem(_("2x3")) menu.append(menu_items) menu_items.connect("activate", self._grid2x3_cb) menu_items.show() menu_items = Gtk.MenuItem(_("3x2")) menu.append(menu_items) menu_items.connect("activate", self._grid3x2_cb) menu_items.show() menu_items = Gtk.MenuItem(_("3x3")) menu.append(menu_items) menu_items.connect("activate", self._grid3x3_cb) menu_items.show() """ menu_items = gtk.MenuItem(_("Solve it")) menu.append(menu_items) menu_items.connect("activate", self._solve_cb) menu_items.show() """ root_menu = Gtk.MenuItem("Tools") root_menu.show() root_menu.set_submenu(menu) # A vbox to put a menu and the canvas in: vbox = Gtk.VBox(False, 0) self.win.add(vbox) vbox.show() menu_bar = Gtk.MenuBar() vbox.pack_start(menu_bar, False, False, 2) menu_bar.show() canvas = Gtk.DrawingArea() vbox.pack_end(canvas, True, True, 3) canvas.show() menu_bar.append(root_menu) self.win.show_all() # Start the activity self.game = Game(canvas, os.path.join(os.path.abspath('.'), 'images')) self.game.win = self.win self.game.test = self.game.grid.test2x2 self.game.grid.reset2x2(self.game) def set_title(self, title): self.win.set_title(title) # # Grid resize callbacks # def _grid2x2_cb(self, button): self.game.test = self.game.grid.test2x2 self.game.grid.reset2x2(self.game) def _grid3x2_cb(self, button): self.game.test = self.game.grid.test3x2 self.game.grid.reset3x2(self.game) def _grid2x3_cb(self, button): self.game.test = self.game.grid.test2x3 self.game.grid.reset2x3(self.game) def _grid3x3_cb(self, button): self.game.test = self.game.grid.test3x3 self.game.grid.reset3x3(self.game) def _solve_cb(self, widget): self.show_all() self.set_grid([0,1,2,3,4,5,6,7,8]) self.set_orientations([0,0,0,0,0,0,0,0,0]) self.rotation_sets = get_rotation_sets() counter = 0 a = [0,1,2,3,4,5,6,7,8] for i in Permutation(a): if self.test(i) is True: return True counter += 1 if (counter/1000)*1000 == counter: print str(counter) + ": " + str(self.game.grid.grid) print "no solution found :(" return True def test(self,g): self.game.grid.grid = g for o in range(64): for r in range(9): self.game.grid.card_table[self.game.grid.grid.index(r)]\ .set_orientation(self.rotation_sets[o][r],False) if self.game.test() is True: print _("You solved the puzzle.") self.game.grid.print_grid() self.game.grid.print_orientations() self.game.win.set_title(_("CardSort") + ": " + \ _("You solved the puzzle.")) self.game.grid.reset3x3(self.game) self.game.grid.set_grid(g) for r in range(9): self.game.grid.card_table[self.game.grid.grid.index(r)]\ .set_orientation(self.rotation_sets[o][r],True) self.game.grid.print_grid() self.game.grid.print_orientations() self.game.sprites.redraw_sprites() return True return False
def __init__(self, handle): super(CardSortActivity,self).__init__(handle) toolbar_box = ToolbarBox() activity_button = ActivityToolbarButton(self) toolbar_box.toolbar.insert(activity_button, 0) activity_button.show() self.grid2x2 = radio_factory('2x2on', toolbar_box.toolbar, self._grid2x2_cb, tooltip=_('2x2'), group = None) self.grid3x2 = radio_factory('3x2on', toolbar_box.toolbar, self._grid3x2_cb, tooltip=_('3x2'), group = self.grid2x2) self.grid2x3 = radio_factory('2x3on', toolbar_box.toolbar, self._grid2x3_cb, tooltip=_('2x3'), group = self.grid2x2) self.grid3x3 = radio_factory('3x3on', toolbar_box.toolbar, self._grid3x3_cb, tooltip=_('3x3'), group = self.grid2x2) separator_factory(toolbar_box.toolbar, visible=False) self.results_label = label_factory(toolbar_box.toolbar, _("click to rotate; drag to swap"), width=300) separator_factory(toolbar_box.toolbar, expand=True, visible=False) # The ever-present Stop Button stop_button = StopButton(self) stop_button.props.accelerator = '<Ctrl>Q' toolbar_box.toolbar.insert(stop_button, -1) stop_button.show() self.set_toolbar_box(toolbar_box) toolbar_box.show() # Create a canvas canvas = Gtk.DrawingArea() canvas.set_size_request(Gdk.Screen.width(), Gdk.Screen.height()) self.set_canvas(canvas) canvas.show() self.show_all() # Initialize the canvas self.game = Game(canvas, os.path.join( activity.get_bundle_path(), 'images'), self) # Read the mode from the Journal try: if self.metadata['grid'] == '2x2': self.show_grid2x2() elif self.metadata['grid'] == '3x2': self.show_grid3x2() elif self.metadata['grid'] == '2x3': self.show_grid2x3() elif self.metadata['grid'] == '3x3': self.show_grid3x3() except: self.metadata['grid'] = "2x2" self.show_grid2x2()
class PukllanapacActivity(activity.Activity): """ Sliding puzzle game """ def __init__(self, handle): """ Initialize the toolbars and the game board """ super(PukllanapacActivity,self).__init__(handle) self._play_level = 0 self._play_mode = 0 self._setup_toolbars(_have_toolbox) # Create a canvas canvas = gtk.DrawingArea() canvas.set_size_request(gtk.gdk.screen_width(), \ gtk.gdk.screen_height()) self.set_canvas(canvas) canvas.show() self.show_all() self.win = Game(canvas, os.path.join(activity.get_bundle_path(), 'images'), self) # Restore game state from Journal or start new game if 'play_level' in self.metadata: self.change_play_level_cb(play_level=int( self.metadata['play_level'])) if 'play_mode' in self.metadata: self.change_play_mode_cb(play_mode=int( self.metadata['play_mode'])) grid = [] for i in range(24): if 'card' + str(i) in self.metadata: grid.append(int(self.metadata['card' + str(i)])) self.win.grid.restore_grid(grid, self.win.mode) for i in range(24): if 'rotate' + str(i) in self.metadata: self.win.grid.card_table[grid[i]].set_orientation( int(self.metadata['rotate' + str(i)])) self.win.mask(self._play_level) def write_file(self, file_path): """ Write the grid status to the Journal """ self.metadata['play_mode'] = self._play_mode self.metadata['play_level'] = self._play_level for i in range(24): self.metadata['card' + str(i)] = str(self.win.grid.grid[i]) self.metadata['rotate' + str(i)] = str( self.win.grid.card_table[self.win.grid.grid[i]].orientation) def _setup_toolbars(self, have_toolbox): """ Setup the toolbars.. """ if have_toolbox: toolbox = ToolbarBox() # Activity toolbar activity_button = ActivityToolbarButton(self) toolbox.toolbar.insert(activity_button, 0) activity_button.show() self.set_toolbar_box(toolbox) toolbox.show() toolbar = toolbox.toolbar else: # Use pre-0.86 toolbar design games_toolbar = gtk.Toolbar() toolbox = activity.ActivityToolbox(self) self.set_toolbox(toolbox) toolbox.add_toolbar(_('Game'), games_toolbar) toolbox.show() toolbox.set_current_toolbar(1) toolbar = games_toolbar # Add the buttons and labels to the toolbars self.level_button = button_factory( LEVEL_ICONS[self._play_level], toolbar, self.change_play_level_cb, tooltip=_('Set difficulty level.')) mode = self._play_mode mode += 1 if mode == len(GAME_ICONS): mode = 0 self.game_buttons = [] for i in range(len(GAME_ICONS)): if i==0: self.game_buttons.append(radio_factory( GAME_ICONS[0], toolbar, self.change_play_mode_cb, cb_arg=0, tooltip=_('Select game.'), group=None)) else: self.game_buttons.append(radio_factory( GAME_ICONS[i], toolbar, self.change_play_mode_cb, cb_arg=i, tooltip=_('Select game.'), group=self.game_buttons[0])) self.game_buttons[mode].set_active(True) separator_factory(toolbar, False, True) self.status_label = label_factory(toolbar, _("drag to swap")) if _have_toolbox: separator_factory(toolbox.toolbar, True, False) stop_button = StopButton(self) stop_button.props.accelerator = '<Ctrl>q' toolbox.toolbar.insert(stop_button, -1) stop_button.show() def change_play_level_cb(self, button=None, play_level=None): """ Cycle between levels """ if self._play_mode > 0: return if play_level is None: self._play_level += 1 if self._play_level == len(LEVEL_ICONS): self._play_level = 0 else: self._play_level = play_level self.level_button.set_icon(LEVEL_ICONS[self._play_level]) self.win.grid.reset(GAME_ICONS[self._play_mode]) self.win.mask(self._play_level) def change_play_mode_cb(self, button=None, play_mode=None): """ Cycle between game modes """ if play_mode is None: self._play_mode += 1 if self._play_mode == len(GAME_ICONS): self._play_mode = 0 else: self._play_mode = play_mode mode = self._play_mode mode += 1 if mode == len(GAME_ICONS): mode = 0 if hasattr(self, 'win'): self.win.mode = GAME_ICONS[self._play_mode] self.win.grid.initialize_cards(self.win.sprites, self.win.path, self.win.card_dim, self.win.scale, GAME_ICONS[self._play_mode]) if self._play_mode > 0: self._play_level = len(LEVEL_ICONS) - 1 self.level_button.set_icon(LEVEL_ICONS[self._play_level]) self.win.mask(self._play_level) self.win.grid.reset(GAME_ICONS[self._play_mode])