def delete_level(self, x, y): r = Rect(self.delete_level_label.x, self.delete_level_label.y, 130, 20) if r.contains(x, y): status = True self.yes_label.visible = status self.no_label.visible = status self.delete_is_press = status if self.delete_is_press: yes = Rect(self.yes_label.x, self.yes_label.y, 15, 20) no = Rect(self.no_label.x, self.no_label.y, 15, 20) status = False if yes.contains(x, y): path = 'levelfile/level' + str(self.save_as) + '.txt' if os.path.exists(path): os.remove(path) self.save_as = self.get_all_level()[-1] self.yes_label.visible = status self.no_label.visible = status self.delete_is_press = status self.reset_blocks() self.reset_level_select() elif no.contains(x, y): self.delete_is_press = status self.yes_label.visible = status self.no_label.visible = status
def create_page_select(self): '''创建关卡选择的页面选择器''' levels = self.get_all_level() count_pre_pages = 10 pages = len(levels) // count_pre_pages + 1 self.page_count = pages last_page = Label('上一页') next_page = Label('下一页') y = 100 lx = 320 - 54 nx = 320 + 10 for i in range(pages): # 从中间开始放页码 if i < pages / 2: x = 320 - (pages / 2 - i) * 20 lx = 320 - pages / 2 * 20 - 64 else: x = 320 + (i - pages / 2) * 20 nx = 320 + pages / 2 * 20 + 10 label = Label(str(i + 1), position=(x, y)) last_page.position = (lx, y) next_page.position = (nx, y) r = Rect(label.x, label.y, 10, 16) self.page_select.append([r, label]) self.add(label) lr = Rect(last_page.x, last_page.y, 50, 20) nr = Rect(next_page.x, next_page.y, 50, 20) self.page_select.append([lr, last_page]) self.page_select.append([nr, next_page]) self.add(last_page) self.add(next_page)
def __init__(self, title, choices, callback, explanation=None, width=400): super(ChoiceLayer, self).__init__() self.callback = callback self.add(ColorLayer(20, 20, 20, 150), z=-2) w, h = director.get_window_size() y = h - 256 but = Label(title, color=(0, 0, 0, 255), position=(w // 2, y), anchor_x='center', anchor_y='top', font_size=24) x1 = w // 2 - but.element.content_width // 2 x2 = w // 2 + but.element.content_width // 2 y2 = y self.add(but) y -= but.element.content_height + 10 if explanation: but = Label(explanation, multiline=True, color=(0, 0, 0, 255), position=(w // 2, y), width=width, anchor_x='center', anchor_y='top', font_size=14, align='center', font_name='Prototype') self.add(but) x1 = min(w // 2 - width // 2, x1) x2 = max(w // 2 + width // 2, x2) y -= but.element.content_height + 10 y -= 32 self.choice_buts = [] for choice in choices: but = Label(choice, color=(0, 0, 0, 255), position=(w // 2, y), anchor_x='center', anchor_y='bottom', font_size=20, font_name='Prototype') self.add(but, z=1) self.choice_buts.append(but) x = w // 2 - but.element.content_width // 2 x1 = min(x, x1) x2 = max(w // 2 + but.element.content_width // 2, x2) but.rect = Rect(x, y, but.element.content_width, but.element.content_height) y1 = y y -= but.element.content_height self.patch_dimensions = (x1, y1, x2 - x1, y2 - y1) self.ninepatch = NinePatch(pyglet.resource.image('border-9p.png'))
def get_AABB(self): '''Returns a local-coordinates Axis aligned Bounding Box Returns a cocos.rect.Rect instance. ''' v = self._vertex_list.vertices x = v[0], v[2], v[4], v[6] y = v[1], v[3], v[5], v[7] return Rect(min(x), min(y), max(x) - min(x), max(y) - min(y))
def get_AABB(self): """ Returns: :class:`cocos.rect.Rect`: Local-coordinates Axis Aligned Bounding Box. """ v = self._vertex_list.vertices x = v[0], v[2], v[4], v[6] y = v[1], v[3], v[5], v[7] return Rect(min(x), min(y), max(x) - min(x), max(y) - min(y))
def get_planets_as_rects(all_planets): ''' returns (x1, y1, x2, y2) ''' rects = [] for planet in all_planets: rects.append( Rect(planet.x - planet.radius, planet.y - planet.radius, planet.radius * 2, planet.radius * 2)) return rects
def setSelectedCellPosition(self, col, row): if col < 0 or row < 0 or col >= Board.numCols or row >= Board.numRows: return prev_cell = self.getSelectedCell() if prev_cell is not None: prev_cell.show_action_indicator(show=False) prev_cell.show_range_to_display(show=False) self.sel_cell_pos = col, row new_cell = self.getSelectedCell() if new_cell is not None: new_cell.show_action_indicator() new_cell.show_range_to_display() cell_unit = self.getUnitAtCell(col, row) if cell_unit == self.getTurnUnit(): Interface.UI.updateTargetUnitStats(None) else: Interface.UI.updateTargetUnitStats( cell_unit, is_friendly=self.isFriendlyUnit(self.getTurnPlayer(), cell_unit)) # only refocus if getting too close to edge of display (within 3 Tiles of each side) window_size = director.get_window_size() view_width = window_size[0] - Board.BOARD.TILE_SIZE * 6 view_height = window_size[1] - Board.BOARD.TILE_SIZE * 6 view_bottom_left = self.scroller.screen_to_world( Board.BOARD.TILE_SIZE * 3, Board.BOARD.TILE_SIZE * 3) view_rect = Rect(view_bottom_left[0], view_bottom_left[1], view_width, view_height) cell_screen_pos = Board.board_to_layer(col, row) cell_rect = Rect(cell_screen_pos[0], cell_screen_pos[1], Board.BOARD.TILE_SIZE, Board.BOARD.TILE_SIZE) if not cell_rect.intersects(view_rect): self.scroller.set_focus( cell_screen_pos[0] + Board.BOARD.TILE_SIZE // 2, cell_screen_pos[1] + Board.BOARD.TILE_SIZE // 2)
def create_grid(self): b = Block() ewidth = self.editor_right - self.editor_left left = int(self.editor_left + ewidth % b.sprite.width / 2) right = self.editor_right top = self.editor_top bottom = self.editor_bottom for x in range(left, right, b.sprite.width): for y in range(bottom, top, b.sprite.height): self.recttmp.append(Rect(x, y, b.sprite.width, b.sprite.height))
def on_mouse_release(self, x, y, buttons, modifiers): print('$global', x, y) from cocos.rect import Rect aabb2 = self.explosion2.get_AABB() global_2_bl = self.explosion.point_to_world(aabb2.bottomleft) global_2_tr = self.explosion.point_to_world(aabb2.topright) rect2 = Rect(*global_2_bl, *(global_2_tr - global_2_bl)) print('%local_rect2', self.explosion2.get_rect()) print('%aabb2', self.explosion2.get_AABB()) print('%rect2', rect2) print('Contains:', rect2.contains(x, y)) print()
def create_level_select(self): '''创建关卡选择器''' levels = self.get_all_level() count_pre_pages = 10 for i in range(0, len(levels)): page = i // count_pre_pages if i % 10 < 5: offect_y = 200 else: offect_y = 150 # 每一排5个选择 所以用 数量 i 除以 5 取余 offect_x = 640 * page + 40 + (i % 5) * 120 label = Label('第' + str(levels[i]) + '关', position=(offect_x, offect_y)) self.add(label) # 40,20 宽度和高度是试验出来的 r = Rect(label.x, label.y, 40, 20) self.level_select.append([r, label]) self.level_select_position.append((label.x, label.y))
def get_rect(self): '''Get a cocos.rect.Rect for this sprite. Note that this rect's position is most likely NOT the same as the Sprite's position - in fact by default the rect's center is the Sprite's position. If you move the rect around and wish to reflect this change in the Sprite, you will probably have to do something like (again with the default image anchor in the center):: rect = sprite.get_rect() rect.midbottom = (0, 100) sprite.position = rect.center Returns a cocos.rect.Rect instance. ''' x, y = self.position x -= self.image_anchor_x y -= self.image_anchor_y return Rect(x, y, self.width, self.height)
def __init__(self, viewport_h): self.viewport_height = viewport_h viewport = Rect(0, self.viewport_height, director.window.width, director.window.height - self.viewport_height) #director.window ScrollingManager.__init__(self, viewport) self.config = Config() self.gallery = Gallery() self.is_event_handler = True self.sprite_list = {} # scrolling self.inscroll = False self.scroll_direction = (0, 0) self.marked_tile = (-1, -1) self.combatants = {} self.valid_squares = {} self.move_path = [] #self.fire_path = [] self.spawn_unit = "na" self.bracket = Sprite(self.gallery.content["trn"]["bracket"]) self.bracket.image_anchor = 0, 0 self.bracket.scale = 1 self.bracket.x = -1000 self.bracket.y = -1000 self.bracket_sel = Sprite(self.gallery.content["trn"]["bracket_sel"]) self.bracket_sel.image_anchor = 0, 0 self.bracket_sel.scale = 1 self.bracket_sel.x = -1000 self.bracket_sel.y = -1000 self.adjacent_tiles = OrderedDict() self.adjacent_tiles["N"] = (0, 1) self.adjacent_tiles["E"] = (1, 0) self.adjacent_tiles["S"] = (0, -1) self.adjacent_tiles["W"] = (-1, 0) self.adjacent_tiles["B"] = (1, 1) self.adjacent_tiles["C"] = (1, -1) self.adjacent_tiles["D"] = (-1, -1) self.adjacent_tiles["A"] = (-1, 1)
def __init__(self, viewport_h): """ initializer """ self.viewport_height = viewport_h viewport = Rect(0, self.viewport_height, director.window.width, director.window.height - self.viewport_height) #director.window ScrollingManager.__init__(self, viewport) self.config = Config() self.gallery = Gallery() self.is_event_handler = True # scrolling self.inscroll = False self.scroll_direction = () if len(self.config.loaded_objects) > 0: self.POI = self.config.loaded_objects["POI"] else: self.POI = {} self.transarctica_actor = TransarcticaActor(self) self.transarctica = director.core.query_mover("Transarctica") self.vutrain = [] self.vutrain_actor = [] for id in range(self.config.vutrain_count): self.vutrain_actor.append(id) self.vutrain_actor[id] = VUTrainActor(self, id) self.vutrain.append(id) self.vutrain[id] = director.core.query_mover("VUTrain" + str(id)) self.roamer = [] self.roamer_actor = [] for id in range(self.config.roamer_count): self.roamer_actor.append(id) self.roamer_actor[id] = RoamerActor(self, id) self.roamer.append(id) self.roamer[id] = director.core.query_mover("Roamer" + str(id))
def add_button(): y = 758 x = 10 while 1: indent, but, label = yield y -= but.image.height but.position = (x + indent, y) if isinstance(but.info, str): name = but.info else: name = but.info.name self.add(but, z=1, name=name) lx = x + indent + but.image.width + 10 ly = y + 12 if '\n' in label: ly += 12 if label.count('\n') > 1: y -= 24 * (label.count('\n') - 1) l = Label(label, multiline=True, width=400, position=(lx, ly), color=(255,255,255,255), font_name='Prototype') l.rect = Rect(l.x, l.y, l.element.content_width, l.element.content_height) self.add(l) but.label_ob = l
def test_clippedBy__disjoint(): r1 = Rect(0, 0, 10, 10) r2 = Rect(20, 20, 10, 10) assert r1.clippedBy(r2)
def new_level(self, x, y): r = Rect(self.new_level_label.x, self.new_level_label.y, 70, 20) if r.contains(x, y): self.create_new_level()
def get_rect(self): ppos = self.cshape.center r = self.cshape.r # FIXME: Use top, bottom, left, right return Rect(ppos.x - r, ppos.y - r, r * 2, r * 2)
def has_point(self, x, y): px, py = self.position px -= self.width // 2 py -= self.height // 2 r = Rect(px, py, self.width, self.height) return r.contains(x, y)
def on_mouse_press(self, x, y, button, modifiers): if Rect(*self.patch_dimensions).contains(x, y): self.callback(self.parent) self.callback = None self.kill() return True
def draw(self): x, y, w, h = self.ninepatch.draw_around( self.label.element.x, self.label.element.y, self.label.element.content_width, self.label.element.content_height) self.rect = Rect(x, y, w, h)
def test_clippedBy__encompassing(): r1 = Rect(0, 0, 10, 10) r2 = Rect(0, 0, 10, 10) assert not r1.clippedBy(r2)
def test_clippedBy__partial_overlappinng(): r1 = Rect(0, 0, 10, 10) r2 = Rect(5, 5, 10, 10) assert r1.clippedBy(r2)