def __init__(self, screen, rows=0, columns=0): super().__init__() self.cards = [] self.screen = screen self.cards_offset = 10 self.card_size = vector2(70, 100) self.selected_cards = [] self.on_exit = None self.score = 0 self.wrong_attempts = 0 # SCORE TEXT self.score_text = Text(Rect(vector2(20, 20), vector2(150, 50)), Game.defaultScoreText + str(self.score)) self.score_text.justify = Text.justify_up self.score_text.align = Text.align_left self.childs.append(self.score_text) # EXIT BUTTON x, y = screen.get_size() exit_button = TextButton(vector2(20, y - 70), vector2(150, 50), "Exit") exit_button.on_click = lambda game=self: game.on_exit_game() self.childs.append(exit_button) # WIN TEXT self.win_text = Text(Rect(vector2(0, 0), vector2(x, y)), "Congratulations!") self.childs.append(self.win_text) if rows != 0 and columns != 0: self.new_game(rows, columns) else: self.is_visible = False
def subarea(self, *rect): """ Return Surface and Rect that represents a subsurface that shares data with this surface. The rect argument is the area of the subsurface. """ try: x, y, w, h = rect[0].x, rect[0].y, rect[0].width, rect[0].height except AttributeError: try: x, y, w, h = rect[0] except ValueError: x, y = rect[0] w, h = rect[1] rect = Rect(x, y, w, h) try: subsurf = self.getSubimage(rect.x, rect.y, rect.width, rect.height) except RasterFormatException: try: clip = self.get_rect().createIntersection(rect) rect = Rect(clip.x, clip.y, clip.width, clip.height) subsurf = self.getSubimage(rect.x, rect.y, rect.width, rect.height) except: #rect outside surface subsurf = None return subsurf, rect
def fill(self, color=(0, 0, 0), rect=None): """ Fill surface with color. """ try: g2d = self._g2d surface_graphics = True except AttributeError: g2d = self.createGraphics() surface_graphics = False color = Color(color) #0.23 g2d.setColor(color) if not rect: rect = Rect(0, 0, self.width, self.height) x, y, w, h = rect.x, rect.y, rect.width, rect.height else: try: x, y, w, h = rect.x, rect.y, rect.width, rect.height except AttributeError: rect = Rect(rect[0], rect[1], rect[2], rect[3]) x, y, w, h = rect.x, rect.y, rect.width, rect.height g2d.fillRect(x, y, w, h) if not surface_graphics: g2d.dispose() return rect
def blit(self, surface, position, area=None): """ Draw given surface on this surface at position. Optional area delimitates the region of given surface to draw. """ try: x, y = position.x, position.y except AttributeError: x, y = position[0], position[1] try: if not area: rect = self.get_rect().createIntersection( Rect(x, y, surface.width, surface.height)) surface_rect = Rect(rect.x, rect.y, rect.width, rect.height) else: surface, surface_rect = surface.subarea(area) except AttributeError: return Rect(0, 0, 0, 0) try: g2d = self._g2d surface_graphics = True except AttributeError: g2d = self.createGraphics() surface_graphics = False try: g2d.drawImage(surface, x, y, None) except TypeError: g2d.drawImage(surface, int(x), int(y), None) if not surface_graphics: g2d.dispose() return surface_rect
def circle(self, surface, color, position, radius, width=0): """ Draw circular shape, and returns bounding Rect. Argument include surface to draw, color, position and radius. Optional width argument of outline, which defaults to 0 for filled shape. """ surface.beginPath() surface.arc(position[0], position[1], radius, 0, 2 * pi, False) if width: surface.setLineWidth(width) if hasattr(color, 'a'): surface.setStrokeStyle(color) else: surface.setStrokeStyle(Color(color)) surface.stroke() else: if hasattr(color, 'a'): surface.setFillStyle(color) else: surface.setFillStyle(Color(color)) surface.fill() if surface._display: return surface._display._surface_rect.clip( Rect(position[0] - radius, position[1] - radius, 2 * radius, 2 * radius)) else: return surface.get_rect().clip( Rect(position[0] - radius, position[1] - radius, 2 * radius, 2 * radius))
def fill(self, color=None, rect=None): """ Fill surface with color. """ if color is None: HTML5Canvas.fill(self) return self.beginPath() if color: if hasattr(color, 'a'): self.setFillStyle(color) else: self.setFillStyle(Color(color)) if not rect: _rect = Rect(0, 0, self.width, self.height) else: if self._display: surface_rect = self._display._surface_rect else: surface_rect = self.get_rect() if hasattr(rect, 'width'): _rect = surface_rect.clip(rect) else: _rect = surface_rect.clip(Rect(rect)) if not _rect.width or not _rect.height: return _rect self.fillRect(_rect.x, _rect.y, _rect.width, _rect.height) else: _rect = Rect(0, 0, self.width, self.height) self.clear() return _rect
def update(self): last = Rect(*(self.pos + self.size)) dx = 0 if keys.get(Keyboard.keycodes['left']): dx -= 2 * params.scale if keys.get(Keyboard.keycodes['right']): dx += 2 * params.scale if keys.get(Keyboard.keycodes['spacebar']) and self.resting: self.dy = 9 * params.scale self.resting = False self.dy = max(-8 * params.scale, self.dy - .5 * params.scale) self.x += dx self.y += self.dy new = Rect(*(self.pos + self.size)) for cell in self.map.layers['objects'].collide(new, 'blocker'): blocker = cell['blocker'] if 'l' in blocker and last.right <= cell.left and new.right > cell.left: new.right = cell.left if 'r' in blocker and last.left >= cell.right and new.left < cell.right: new.left = cell.right if 't' in blocker and last.bottom >= cell.top and new.bottom < cell.top: self.resting = True new.bottom = cell.top self.dy = 0 if 'b' in blocker and last.top <= cell.bottom and new.top > cell.bottom: new.top = cell.bottom self.dy = 0 self.pos = new.bottomleft
def lines(self, surface, color, closed, pointlist, width=1): """ Draw interconnected lines, and returns Rect bound. Argument include surface to draw, color, closed, and pointlist. Optional width argument of line. """ surface.beginPath() surface.moveTo(*pointlist[0]) for point in pointlist[1:]: surface.lineTo(*point) if closed: surface.closePath() surface.setLineWidth(width) if hasattr(color, 'a'): surface.setStrokeStyle(color) else: surface.setStrokeStyle(Color(color)) surface.stroke() xpts = [pt[0] for pt in pointlist] ypts = [pt[1] for pt in pointlist] xmin, xmax = min(xpts), max(xpts) ymin, ymax = min(ypts), max(ypts) if surface._display: return surface._display._surface_rect.clip( Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)) else: return surface.get_rect().clip( Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
def line(self, surface, color, point1, point2, width=1): """ Draw line, and returns bounding Rect. Argument include surface to draw, color, point1, point2. Optional width argument of line. """ surface.beginPath() surface.moveTo(*point1) surface.lineTo(*point2) surface.setLineWidth(width) if hasattr(color, 'a'): surface.setStrokeStyle(color) else: surface.setStrokeStyle(Color(color)) surface.stroke() xpts = [pt[0] for pt in (point1, point2)] ypts = [pt[1] for pt in (point1, point2)] xmin, xmax = min(xpts), max(xpts) ymin, ymax = min(ypts), max(ypts) if surface._display: return surface._display._surface_rect.clip( Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)) else: return surface.get_rect().clip( Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
def polygon(self, surface, color, pointlist, width=0): """ Draw polygon shape, and returns bounding Rect. Argument include surface to draw, color, and pointlist. Optional width argument of outline, which defaults to 0 for filled shape. """ surface.beginPath() surface.moveTo(*pointlist[0]) for point in pointlist[1:]: surface.lineTo(*point) surface.closePath() if width: surface.setLineWidth(width) if hasattr(color, 'a'): surface.setStrokeStyle(color) else: surface.setStrokeStyle(Color(color)) surface.stroke() else: if hasattr(color, 'a'): surface.setFillStyle(color) else: surface.setFillStyle(Color(color)) surface.fill() xpts = [pt[0] for pt in pointlist] ypts = [pt[1] for pt in pointlist] xmin, xmax = min(xpts), max(xpts) ymin, ymax = min(ypts), max(ypts) if surface._display: return surface._display._surface_rect.clip( Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)) else: return surface.get_rect().clip( Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
def test_center(): r = Rect(0, 0, 2, 2) Hb = Hitbox(r) t = CollideTransformable() t.set_hit_box(Hb) assert t.get_hit_box().get_world_rect() == Rect(0, 0, 2, 2) assert t.get_position() == Vector(1, 1)
def dredgeRiverSingle(nature, direction, CX, CY): rivert = [] # This is the start coordinate of the river, perpendicular to the # axis of the river. N/S rivers choose positions on the E/W axis, # and vice-versa axis_size = CX if direction else CY line_pos = random.randint(0, axis_size) #river width, no more than half the map river_size = min(random.randint(3, 120), axis_size // 2) for i in range(0, axis_size): if direction: element = Rect(Point(i, line_pos), Point(i, line_pos + river_size)) else: element = Rect(Point(line_pos, i), Point(line_pos + river_size, i)) element.set_name('RIVER') rivert.append(element) prev = 0 for f in rivert: prev = prev + random.randint(-1, 1) f.move(direction + 2, prev) # in rect, 1=up, 2=right nature.extend(rivert)
def test_08(): # Problem: get_rect gibt immer 0, 0 t1 = TextBox("0123456789") t2 = TextBox("0123456789") p1 = Paragraph([Row([t1, t2, NewlineBox(defaultstyle)])]) assert p1.get_rect(0, 0, 0) == Rect(0, 0.0, 1, 1.0) assert p1.get_rect(10, 0, 0) == Rect(10, 0.0, 11, 1.0)
def map_all_and_work(zone, duty): workers = [] pool = Pool() nw = Rect(Point(0, 0), Point(CITY_SIZE_X / 2, CITY_SIZE_Y / 2)) ne = Rect(Point(CITY_SIZE_X / 2, 0), Point(CITY_SIZE_X, CITY_SIZE_Y / 2)) sw = Rect(Point(0, CITY_SIZE_Y / 2), Point(CITY_SIZE_X / 2, CITY_SIZE_Y)) se = Rect(Point(CITY_SIZE_X / 2, CITY_SIZE_Y / 2), Point(CITY_SIZE_X, CITY_SIZE_Y)) workers.append( pool.apply_async(conflictSolver, [(0, 0), zone[0][0], nw, duty])) workers.append( pool.apply_async(conflictSolver, [(0, 1), zone[0][1], ne, duty])) workers.append( pool.apply_async(conflictSolver, [(1, 0), zone[1][0], sw, duty])) workers.append( pool.apply_async(conflictSolver, [(1, 1), zone[1][1], se, duty])) pool.close() pool.join() zone = [[[], []], [[], []]] scambi = False bimbi_perduti = 0 for w in workers: res = w.get() #print(res) #print("index x:" +str(res[0][0])) #print("index y:" +str(res[0][1])) zone[res[0][0]][res[0][1]].extend(res[1]) YY = res[0][0] XX = res[0][1] if ((YY - 1) >= 0): #top if (len(res[2]) > 0): scambi = True zone[YY - 1][XX].extend(res[2]) else: bimbi_perduti = bimbi_perduti + len(res[2]) if ((XX - 1) >= 0): #left if (len(res[3]) > 0): scambi = True zone[YY][XX - 1].extend(res[3]) else: bimbi_perduti = bimbi_perduti + len(res[3]) if ((XX + 1) <= 1): #right if (len(res[4]) > 0): scambi = True zone[YY][XX + 1].extend(res[4]) else: bimbi_perduti = bimbi_perduti + len(res[4]) if ((YY + 1) <= 1): #bottom if (len(res[5]) > 0): scambi = True zone[YY + 1][XX].extend(res[5]) else: bimbi_perduti = bimbi_perduti + len(res[5]) #print('abbiamo perso n '+str(bimbi_perduti)) return (zone, scambi)
def test_intersect_of_non_overlapping_rects(self): r1 = Rect(10, 20, 20, 30) r2 = Rect(10, 30, 20, 40) # abuts the bottom of r1 without intersecting r3 = Rect(20, 20, 30, 40) # abuts the right of r1 without intersecting self.assertIsNone(r1.intersect(r2)) self.assertIsNone(r2.intersect(r1)) self.assertIsNone(r1.intersect(r3)) self.assertIsNone(r3.intersect(r1))
def test_rect_center(): R = Rect(0, 0, 2, 2) tr = R.center() assert R == Rect(-1, -1, 2, 2) assert tr == Vector(-1, -1) R2 = Rect(-2, -2, 2, 2) tr2 = R2.center() assert R2 == Rect(-1, -1, 2, 2) assert tr2 == Vector(1, 1)
def loadimage(self): self.image_rect = Rect(self.image.size) self.w = self.image_rect.w self.h = self.image_rect.h self.region_rect = Rect((0, 0), (self.w, self.h)) self.edited_img = self.image.copy().convert('RGB') self.displayimage()
def test_adjacent(): recs = [ Rect(0, 0, 1, 1), Rect(0, 1, 1, 1), Rect(1, 0, 1, 1), ] recs.sort() result = split_rectangles(recs) result.sort()
def loadimage(self): ''' loads the image file from getFile() into the GUI, placing the original image on the left half and editable version on the right ''' self.image_rect = Rect(self.image.size) self.w = self.image_rect.w self.h = self.image_rect.h self.region_rect = Rect((0, 0), (self.w, self.h)) self.edited_img = self.image.copy().convert('RGB') self.displayimage() self.display_edited()
def loadimage(self): self.image_rect = Rect(self.image.size) self.w = self.image_rect.w self.h = self.image_rect.h self.region_rect = Rect((0, 0), (self.w, self.h)) self.edited_img = self.image.copy().convert('RGB') self.draw_handle = ImageDraw.Draw(self.edited_img) # print(type(self.edited_img)) self.displayimage() self.display_edited()
def test_no_intersection(): recs = [ Rect(0, 0, 1, 1), Rect(2, 2, 1, 1), Rect(3, 3, 1, 1), ] recs.sort() result = split_rectangles(recs) result.sort() assert not any(a != b for a, b in zip(recs, result))
def draw_dash_bar(self, player, pos, direction): assert (direction == "left" or direction == "right") if direction == "left": self.fill_rect( Rect( pos.x, pos.y, player.ticks_until_dash_ability / player.max_dash_ticks * 300, 10), (0, 0, 0)) elif direction == "right": dash_length = player.ticks_until_dash_ability /\ player.max_dash_ticks * 300 self.fill_rect(Rect(pos.x - dash_length, pos.y, dash_length, 10), (0, 0, 0))
def display_sampler(self, frame): #print(frame.shape) frame_h = frame.shape[0] frame_w = frame.shape[1] rect_size = 10 self.rect1 = Rect(frame_w / 2, frame_h / 2, rect_size, rect_size) self.rect2 = Rect(frame_w / 2, frame_h / 3, rect_size, rect_size) cv2.rectangle(frame, self.rect1.minXY, self.rect1.maxXY, (0, 255, 0), 2) cv2.rectangle(frame, self.rect2.minXY, self.rect2.maxXY, (255, 0, 0), 2)
def aggro_check(self, size): aggro_range = self.map.tile_width * size offset = (self.map.tile_width * (size / 2)) collided = [] for player in self.player_characters: rect = Rect(player.x - offset, player.y - offset, aggro_range, aggro_range) collided += [ e for e in self.entities if rect.intersect(Rect(*e.pos + e.size)) and e.data.type == 'enemy' ] return collided
def on_system_paint(self, ev): bitmap = self.bitmap painter = Painter(self.bitmap) border = self.border_width() width = self.window_width() height = self.window_height() caption_color = SteelBlue if self.active() else LightSteelBlue border_color = 0xaaffffff & caption_color if self.attr() & WND_TRANSPARENT: painter.clear() else: painter.fill_rect(self.margin_left(), self.margin_top(), self.width(), self.height(), White) if self.attr() & WND_FRAME: rc = Rect(0, 0, self.window_width() - 1, self.window_height() - 1) max_alpha = 40 if self.active() else 20 dalpha = max_alpha / DEFAULT_BORDER_WIDTH for i in xrange(DEFAULT_BORDER_WIDTH - 1): painter.set_pen_color(i * dalpha << 24) painter.draw_rect(rc) rc.adjust(1, 1, -1, -1) painter.set_pen_color(caption_color) painter.draw_rect(rc) if self.attr() & WND_CAPTION: # fill caption caption_color &= 0xddffffff caption_rc = Rect(border, border, width - 2 * border, self.caption_height()) painter.fill_rect(rc, caption_color) # draw title painter.set_pen_color(White) rc = caption_rc.translated(10, 0) painter.draw_text(rc, Painter.AlignLeft | Painter.AlignVCenter, self.title_) # draw close/maximize/minimize painter.save() painter.set_pen_color(DarkWhite) painter.set_render_hint(Painter.Antialiasing) # close painter.set_pen_width(2) rc = Rect(self.close_rect()) painter.draw_line(rc.top_left(), rc.bottom_right()) painter.draw_line(rc.top_right(), rc.bottom_left()) # maximize painter.draw_rect(Rect(self.maximize_rect())) # minimize rc = Rect(self.minimize_rect()) painter.draw_line(rc.bottom_left(), rc.bottom_right()) painter.restore()
def test_is_in_camera(): c = Camera() c.set_position(Vector(0, 0)) c.set_dimension(Vector(10, 5)) r = Rect(0, 0, 1, 1) assert c.is_in_camera(r) r = Rect(-10, -5, 2, 2) assert not (c.is_in_camera(r)) r = Rect(10, 5, 2, 2) assert c.is_in_camera(r) r = Rect(5.001, 9, 2, 2) assert not (c.is_in_camera(r)) c.set_dimension(Vector(6, 15)) assert c.is_in_camera(r)
def test_copy(): r = Rect(-1, -1, 2, 2) Hb = Hitbox(r) t = CollideTransformable() t.set_hit_box(Hb) t.set_collide(True) t2 = t.copy() t.set_collide(False) assert t2.get_collide() Hb2 = t2.get_hit_box() assert Hb2.get_ctrbl() == t2 t2.translate(Vector(2, 0)) assert Hb2.get_world_rect() == Rect(1, -1, 2, 2) assert Hb.get_world_rect() == Rect(-1, -1, 2, 2)
def test_hitbox4(): T1 = CollideTransformable() T2 = CollideTransformable() R1 = Rect(-1, -1, 2, 2) R2 = Rect(-1, -1, 2, 2) Hb1 = Hitbox(R1) Hb2 = Hitbox(R2) Hb1.link(T1) Hb2.link(T2) T1.translate(Vector(1, 1)) T2.translate(Vector(3, 1)) T2.set_speed(Vector(-1, 0)) v = Hb2.remove_collide(Hb1) T2.translate(v) assert not (Hb1.collide(Hb2))
def draw_bitmap(self, *args): painter = self.painter if isinstance(args[0], int): x, y, bitmap = args rc = self.rc dst_rc = Rect(x + rc.left(), y + rc.top(), bitmap.width(), bitmap.height()) dst_rc = dst_rc.intersected(rc) src_rc = Rect(dst_rc) src_rc.translate(-rc.left() - x, -rc.top() - y) src_rc = src_rc.intersected(bitmap.rect()) painter.drawImage(dst_rc.rc, bitmap.im, src_rc.rc) else: dst_rc, bitmap, src_rc = args painter.drawImage(dst_rc.rc, bitmap.im, src_rc.rc)
def test_node_extends_point(self): dimensions = Rect(0, 0, 12, 12) start = Point(x=3, y=4) goal = Point(x=8, y=11) barriers = [] NavNode.board = Board(dimensions, start, goal, barriers) position1 = Point(x=8, y=9) node1 = NavNode(position1, 0) position2 = Point(x=8, y=10) node2 = NavNode(position2, 1, None, node1) node3 = NavNode(position1, 2, None, node2) # test equals() self.assertTrue(node1.__eq__(node1)) self.assertFalse(node1.__eq__(node2)) self.assertTrue(node1.__eq__(node3)) # test as_tuple() self.assertEquals(node1.position.as_tuple(), (8, 9)) # test is_solution() goal_node = NavNode(goal, 3, None, node3) self.assertTrue(goal_node.is_solution()) # test get_ancestors() ancestors = goal_node.get_ancestors() self.assertTrue(ancestors[0].__eq__(node3)) self.assertTrue(ancestors[1].__eq__(node2)) self.assertTrue(ancestors[2].__eq__(node1))