def get_laser_points( self, original_image: np.ndarray, image: np.ndarray, extreme_points: ExtremePoints, ) -> Tuple[Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], ]: """ Given the interesting region of an image, containing the wall and desk planes and the object, return the laser points in the three separate regions: one for the wall plane, one for the desk plane, one of the object. """ height, width = image.shape[:2] ymin_wall = extreme_points.wall.top_left.y ymax_wall = extreme_points.wall.bottom_right.y ymin_desk = extreme_points.desk.top_left.y xmin = extreme_points.desk.top_left.x laser_desk = self.get_laser_points_in_region( image=image, region=Rectangle( top_left=Point(0, ymin_desk - ymin_wall), bottom_right=Point(width, height), ), ) if laser_desk is not None: laser_wall = self.get_laser_points_in_region( image=image, region=Rectangle( top_left=Point(0, 0), bottom_right=Point(width, ymax_wall - ymin_wall), ), ) if laser_wall is not None: laser_obj = self.get_laser_points_in_region( image=image, region=Rectangle( top_left=Point(0, ymax_wall - ymin_wall), bottom_right=Point(width, ymin_desk - ymin_wall), ), is_obj=True, ) if laser_obj is not None: laser_desk = self.offset_points(points=laser_desk, offset=Point( xmin, ymin_desk)) laser_wall = self.offset_points(points=laser_wall, offset=Point( xmin, ymin_wall)) laser_obj = self.remove_obj_outliers(laser_obj) if laser_obj is not None: laser_obj = self.offset_points(points=laser_obj, offset=Point( xmin, ymax_wall)) obj_colors = self.get_colors(original_image, laser_obj) return laser_wall, laser_desk, laser_obj, obj_colors return None, None, None, None
def __init__(self, points, color, *groups): super().__init__() self.dirty = 2 self._layer = constants.LAYER_GROUND self.add(*groups) self.points = points self.origin = Point(min([p.x for p in points]), min([p.y for p in points])) self.width = max([p.x for p in points]) - self.origin.x self.height = max([p.y for p in points]) - self.origin.y self.color = color self.image = PyGameSurface(( self.width, self.height, )) self.image.set_colorkey(colors.BLACK) self.rect = Rect(self.origin.x, self.origin.y, self.width, self.height) self.update() # Make sure we don't try to make a mask from a 1-D image. if self.width * self.height == 0: raise ValueError() self.mask = mask.from_surface(self.image)
def __init__(self, point, funds=0, *groups): super().__init__() self.funds = funds self.points = [point] self._layer = constants.LAYER_BALL self.add(*groups) self._velocity = math.Vector2(0, 0) self.logical_position = Point(point.x - self.RADIUS, point.y - self.RADIUS) self.rect.x = self.logical_position.x self.rect.y = self.logical_position.y # This is a special collision mask we use for checks against surfaces. # It's a little bit smaller that then the sprite-based collision mask # to account for the fact that the ball is, in theory, spherical. self.surface_mask = create_rectangular_mask(self.rect.width, self.rect.height, 5, 5, 10, 10) self.previous_positions = [] self.previous_velocities = []
def __init__(self, point1, point2, width=5, *groups): super().__init__() self.dirty = 2 self._layer = constants.LAYER_WALL self.add(*groups) self.points = [point1, point2] self.width = width # This sprite's origin on the application's screen. # Necessary to draw the sprite correctly, as well as to calculate # collision rectangles correctly. self.origin = Point(min(point1.x, point2.x), min(point1.y, point2.y)) self.point1 = point1 - self.origin self.point2 = point2 - self.origin v = math.Vector2(*(point2 - point1).as_2d_tuple()) self.reflect_vector = math.Vector2(-1 * v.y, v.x) self.image = Surface(( abs(point2.x - point1.x) + self.width, abs(point2.y - point1.y) + self.width )) self.image.set_colorkey(colors.BLACK) self.rect = self.image.get_rect() self.rect.x = self.origin.x self.rect.y = self.origin.y self.update() self.mask = mask.from_surface(self.image)
def __init__(self, name='untitled', par=3, origin=Point(50, 50), width=1080, height=900, ball=None, **labeled_groups): self.score = -1 self.name = name self.par = par self.origin = origin self.width = width self.height = height self.image = Surface((width, height)) self.rect = pygame.Rect(*origin.as_2d_tuple(), width, height) self.groups = {'all': LayeredDirty()} for label, group in labeled_groups.items(): self.groups[label] = group self.groups['all'].add(*group.sprites()) self.ball = GolfBall(ball, 0, self.groups['all']) if ball else None
def get_extreme_points(self, wall_corners: np.ndarray, desk_corners: np.ndarray) -> ExtremePoints: """ Given the corners of the rectangles on the wall and on the desk, return the coordinates for a tight bounding box of the area between the two rectangles. """ ymin_wall = int(np.min(wall_corners[:, :, 1])) ymax_wall = int(np.max(wall_corners[:, :, 1])) ymin_desk = int(np.min(desk_corners[:, :, 1])) ymax_desk = int(np.max(desk_corners[:, :, 1])) xmin = int(np.min(wall_corners[:, :, 0])) xmax = int(np.max(wall_corners[:, :, 0])) return ExtremePoints( wall=Rectangle(top_left=Point(xmin, ymin_wall), bottom_right=Point(xmax, ymax_wall)), desk=Rectangle(top_left=Point(xmin, ymin_desk), bottom_right=Point(xmax, ymax_desk)), )
def save(self): tmpl = """ from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole( {hole.name!r}, par={hole.par}, origin={origin!r}, ball={ball_pos!r}, noncollidibles=LayeredDirty(), collidibles=LayeredDirty( {collidibles} ) ) """ ball = None for s in self.hole.groups['all'].sprites(): if isinstance(s, GolfBall): ball = s break self.hole.groups['all'].remove(ball) with open('new_hole.py', 'w') as outfile: outfile.write( tmpl.format( hole=self.hole, origin=self.hole.origin, ball_pos=Point( int(ball.logical_position.x), int(ball.logical_position.y), ), collidibles=(",\n" + 8 * ' ').join([ repr(s) for s in self.hole.groups['collidibles'].sprites() if not isinstance(s, GolfBall) ]))) pygame.quit() raise Quit()
def _mouse_pos(self): """ Return the current position of the mouse cursor relative to the canvas, snapped to a 10-pixel grid, and snapped to the x or y axis. """ where = Point(*map(round_, pygame.mouse.get_pos())) - self.hole.origin if self.should_snap_to_x(*where.as_2d_tuple()): where.y = self.points[-1].y elif self.should_snap_to_y(*where.as_2d_tuple()): where.x = self.points[-1].x return where
def _draw_pointer(self, ball, surface): """ Draw a line that visualizes strike direction and power. """ mouse_pos = Point(*mouse.get_pos()) mouse_vec = math.Vector2(mouse_pos.x - ball.center.x - self.origin.x, mouse_pos.y - ball.center.y - self.origin.y) max_speed = constants.MAX_SPEED * constants.STRIKE_SCALE_FACTOR if mouse_vec.length_squared() > max_speed**2: mouse_vec.scale_to_length(max_speed) draw.line(surface, (0, 0, 255), (ball.center.x, ball.center.y), (int(ball.center.x - mouse_vec.x), int(ball.center.y - mouse_vec.y)), 3) power = int(mouse_vec.length() / max_speed * 100) font = pygame.font.Font(None, 20) surface.blit( font.render("{power!s}%".format(power=power), True, colors.WHITE), (ball.center.x + 10, ball.center.y + 10))
class ShapeDefinitions: Shapes = [((255, 0, 0), (Point(1, 0), Point(1, 1), Point(1, 2), Point(1, 3))), ((0, 255, 0), (Point(1, 0), Point(0, 1), Point(1, 1), Point(2, 1))), ((0, 0, 255), (Point(1, 0), Point(0, 1)))]
def get_node_coordinate(self, point): return Point((self.x + point.x) * 20, (self.y + point.y) * 20)
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole( 'Hole #6', par=3, origin=Point(150, 100, 0), ball=Point(200, 630, 0), noncollidibles=LayeredDirty( Text(Point(88, 92), 'Par 3', font.Font(None, 30), colors.WHITE)), collidibles=LayeredDirty( Water([ Point(150, 150, 0), Point(150, 550, 0), Point(550, 550, 0), Point(550, 150, 0) ]), Green([ Point(150, 550, 0), Point(550, 550, 0), Point(550, 700, 0), Point(150, 700, 0) ]), Green([ Point(150, 150, 0), Point(150, 50, 0),
def center(self): return Point(int(self.logical_position.x + self.RADIUS), int(self.logical_position.y + self.RADIUS))
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole('Hole #12', par=5, origin=Point(80, 30, 0), ball=Point(440, 125, 0), noncollidibles=LayeredDirty( Text(Point(530, 15), 'Par 5', font.Font(None, 30), colors.WHITE), ), collidibles=LayeredDirty( Green([ Point(50, 50, 0), Point(500, 50, 0), Point(500, 200, 0), Point(100, 200, 0), Point(100, 450, 0), Point(50, 450, 0) ]), Slope([ Point(100, 200, 0), Point(200, 200, 0), Point(200, 450, 0), Point(100, 450, 0) ], Color(200, 0, 0, 255), Vector2(0.3, 0.0)), Sand([
def __repr__(self): return "Pin({p!r})".format(p=Point(self.rect.x, self.rect.y))
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole( 'Hole #7', par=4, origin=Point(50, 50, 0), ball=Point(255, 630, 0), noncollidibles=LayeredDirty( Text(Point(805, 70), 'Par 4', font.Font(None, 30), colors.WHITE)), collidibles=LayeredDirty( Green([ Point(50, 500, 0), Point(450, 500, 0), Point(450, 700, 0), Point(50, 700, 0) ]), Slope([ Point(200, 500, 0), Point(300, 500, 0), Point(300, 325, 0), Point(200, 325, 0) ], Color(125, 0, 0, 255), Vector2(0.0, -0.1)), Rough([ Point(200, 325, 0), Point(200, 200, 0),
def center_on(self, point): self.set_logical_pos( Point(point.x - self.RADIUS, point.y - self.RADIUS))
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole( 'Hole #8', par=2, origin=Point(50, 50, 0), ball=Point(145, 520, 0), noncollidibles=LayeredDirty( Text(Point(708, 20), 'Par 2', font.Font(None, 30), colors.WHITE), Text(Point(925, 310), 'Is this lava?', font.Font(None, 24), colors.WHITE) ), collidibles=LayeredDirty( Green([Point(50, 50, 0), Point(50, 580, 0), Point(250, 580, 0), Point(250, 200, 0), Point(650, 200, 0), Point(650, 50, 0)]), Rough([Point(650, 50, 0), Point(800, 50, 0), Point(800, 200, 0), Point(650, 200, 0)]), Lava([Point(250, 580, 0), Point(250, 200, 0), Point(800, 200, 0), Point(800, 50, 0), Point(900, 50, 0), Point(900, 580, 0)]), Pin(Point(730, 125, 0)), Money(Point(225, 175)), Wall(Point(50, 50, 0), Point(900, 50, 0), 5), Wall(Point(900, 50, 0), Point(900, 580, 0), 5), Wall(Point(900, 580, 0), Point(50, 580, 0), 5), Wall(Point(50, 580, 0), Point(50, 50, 0), 5) ) )
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole( 'Hole #11', par=3, origin=Point(75, 50, 0), ball=Point(180, 680, 0), noncollidibles=LayeredDirty( Text(Point(900, 75), 'Par 3', font.Font(None, 30), colors.WHITE), ), collidibles=LayeredDirty( Green([Point(130, 620, 0), Point(230, 620, 0), Point(230, 730, 0), Point(130, 730, 0)]), Rough([Point(230, 730, 0), Point(230, 680, 0), Point(350, 680, 0), Point(350, 730, 0)]), Slope([Point(230, 620, 0), Point(230, 540, 0), Point(350, 540, 0), Point(350, 620, 0)], Color(200, 0, 0, 255), Vector2(0.15, 0.4)), Green([Point(230, 620, 0), Point(230, 680, 0), Point(350, 680, 0), Point(350, 620, 0)]), Rough([Point(350, 460, 0), Point(470, 460, 0), Point(470, 730, 0), Point(350, 730, 0)]), Green([Point(470, 620, 0), Point(710, 620, 0), Point(710, 680, 0), Point(590, 680, 0), Point(590, 730, 0), Point(470, 730, 0)]), Green([Point(650, 620, 0), Point(650, 310, 0), Point(790, 310, 0), Point(790, 620, 0)]), Sand([Point(790, 620, 0), Point(790, 560, 0), Point(880, 560, 0), Point(880, 620, 0)]), Water([Point(790, 560, 0), Point(790, 450, 0), Point(880, 450, 0), Point(880, 560, 0)]), Sand([Point(790, 450, 0), Point(790, 310, 0), Point(880, 310, 0), Point(880, 450, 0)]), Slope([Point(470, 510, 0), Point(470, 390, 0), Point(650, 390, 0), Point(650, 510, 0)], Color(200, 0, 0, 255), Vector2(-0.2, -0.15)), Green([Point(470, 460, 0), Point(130, 460, 0), Point(130, 310, 0), Point(470, 310, 0)]),
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole( 'Hole #3', par=3, origin=Point(50, 50, 0), ball=Point(253, 555, 0), noncollidibles=LayeredDirty( Text(Point(850, 183), 'Par 3', font.Font(None, 30), colors.WHITE) ), collidibles=LayeredDirty( Green([Point(180, 140, 0), Point(830, 140, 0), Point(830, 240, 0), Point(310, 240, 0), Point(310, 590, 0), Point(180, 590, 0)]), Rough([Point(310, 240, 0), Point(380, 240, 0), Point(380, 590, 0), Point(310, 590, 0)]), Sand([Point(380, 240, 0), Point(830, 240, 0), Point(830, 310, 0), Point(380, 310, 0)]), Pin(Point(780, 190, 0)), Money(Point(773, 265)), Wall(Point(180, 140, 0), Point(830, 140, 0), 5), Wall(Point(830, 140, 0), Point(830, 310, 0), 5), Wall(Point(830, 310, 0), Point(380, 310, 0), 5), Wall(Point(380, 310, 0), Point(380, 590, 0), 5), Wall(Point(380, 590, 0), Point(180, 590, 0), 5), Wall(Point(180, 590, 0), Point(180, 140, 0), 5) )
def center(self): return Point(self.rect.x + self.rect.width // 2, self.rect.y + self.rect.height // 2)
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole( 'Hole #9', par=3, origin=Point(150, 50, 0), ball=Point(505, 450, 0), noncollidibles=LayeredDirty( Text(Point(590, 143), 'Par 3', font.Font(None, 30), colors.WHITE), ), collidibles=LayeredDirty( Green([Point(450, 100, 0), Point(560, 100, 0), Point(560, 200, 0), Point(450, 200, 0)]), Lava([Point(450, 200, 0), Point(440, 200, 0), Point(440, 90, 0), Point(450, 90, 0)]), Lava([Point(450, 100, 0), Point(450, 90, 0), Point(570, 90, 0), Point(570, 100, 0)]), Lava([Point(560, 100, 0), Point(560, 210, 0), Point(570, 210, 0), Point(570, 100, 0)]), Lava([Point(560, 210, 0), Point(440, 210, 0), Point(440, 200, 0), Point(560, 200, 0)]), Green([Point(440, 210, 0), Point(440, 510, 0), Point(570, 510, 0), Point(570, 210, 0)]), Slope([Point(570, 510, 0), Point(570, 360, 0), Point(620, 360, 0), Point(620, 510, 0)], Color(200, 0, 0, 255), Vector2(0.33, 0.0)), Green([Point(620, 360, 0), Point(750, 510, 0), Point(620, 510, 0)]), Slope([Point(620, 510, 0), Point(620, 560, 0), Point(750, 560, 0), Point(750, 510, 0)], Color(200, 0, 0, 255), Vector2(0.0, 0.4)), Green([Point(620, 560, 0), Point(620, 700, 0), Point(750, 560, 0)]), Slope([Point(620, 560, 0), Point(570, 560, 0), Point(570, 700, 0), Point(620, 700, 0)], Color(200, 0, 0, 255), Vector2(-0.5, 0.0)), Green([Point(340, 560, 0), Point(340, 620, 0), Point(570, 620, 0), Point(570, 560, 0)]),
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole('Hole #10', par=2, origin=Point(50, 75, 0), ball=Point(485, 690, 0), noncollidibles=LayeredDirty( Text(Point(810, 340), 'Par 2', font.Font(None, 30), colors.WHITE), ), collidibles=LayeredDirty( Rough([ Point(300, 430, 0), Point(340, 430, 0), Point(340, 470, 0), Point(300, 470, 0) ]), Rough([ Point(630, 430, 0), Point(670, 430, 0), Point(670, 470, 0), Point(630, 470, 0) ]), Slope([ Point(300, 430, 0), Point(260, 390, 0),
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole('Hole #2', par=2, origin=Point(50, 50, 0), ball=Point(390, 575, 0), noncollidibles=LayeredDirty( Text(Point(670, 140), 'Par 2', font.Font(None, 30), colors.WHITE)), collidibles=LayeredDirty( Green([ Point(440, 210, 0), Point(540, 210, 0), Point(540, 510, 0), Point(640, 510, 0), Point(640, 630, 0), Point(340, 630, 0), Point(340, 510, 0), Point(440, 510, 0) ]), Rough([ Point(440, 210, 0), Point(340, 210, 0), Point(340, 90, 0), Point(640, 90, 0),
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole('Hole #1', par=1, origin=Point(50, 50, 0), ball=Point(535, 545, 0), noncollidibles=LayeredDirty( Text(Point(515, 160), 'Par 1', font.Font(None, 30), colors.WHITE), ), collidibles=LayeredDirty( Green([ Point(360, 190, 0), Point(700, 190, 0), Point(700, 600, 0), Point(360, 600, 0) ]), Pin(Point(535, 260, 0)), Money(Point(529, 215)), Wall(Point(360, 190, 0), Point(700, 190, 0), 5), Wall(Point(700, 190, 0), Point(700, 600, 0), 5), Wall(Point(700, 600, 0), Point(360, 600, 0), 5), Wall(Point(360, 600, 0), Point(360, 190, 0), 5)))
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole( 'Hole #4', par=4, origin=Point(50, 100, 0), ball=Point(312, 580, 0), noncollidibles=LayeredDirty( Text(Point(900, 268), 'Par 4', font.Font(None, 30), colors.WHITE), ), collidibles=LayeredDirty( Green([Point(250, 600, 0), Point(250, 150, 0), Point(370, 150, 0), Point(370, 250, 0), Point(770, 250, 0), Point(770, 150, 0), Point(870, 150, 0), Point(870, 400, 0), Point(770, 400, 0), Point(770, 300, 0), Point(370, 300, 0), Point(370, 600, 0)]), Slope([Point(250, 150, 0), Point(250, 90, 0), Point(370, 90, 0), Point(370, 150, 0)], Color(100, 0, 0, 255), Vector2(0.0, -0.7)), Sand([Point(250, 90, 0), Point(250, 50, 0), Point(370, 50, 0), Point(370, 90, 0)]), Slope([Point(410, 250, 0), Point(410, 200, 0), Point(770, 200, 0), Point(770, 250, 0)], Color(100, 0, 0, 255), Vector2(-0.33, -0.6)), Slope([Point(410, 300, 0), Point(410, 360, 0), Point(770, 360, 0), Point(770, 300, 0)], Color(100, 0, 0, 255), Vector2(-0.33, 0.6)), Rough([Point(410, 200, 0), Point(410, 150, 0), Point(770, 150, 0), Point(770, 200, 0)]), Rough([Point(770, 400, 0), Point(410, 400, 0), Point(410, 360, 0), Point(770, 360, 0)]), Pin(Point(840, 275, 0)), Money(Point(305, 200)), Wall(Point(250, 50, 0), Point(370, 50, 0), 5), Wall(Point(370, 50, 0), Point(370, 250, 0), 5), Wall(Point(370, 250, 0), Point(410, 250, 0), 5),
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole('Hole #5', par=4, origin=Point(50, 100, 0), ball=Point(490, 550, 0), noncollidibles=LayeredDirty( Text(Point(670, 138), 'Par 4', font.Font(None, 30), colors.WHITE), ), collidibles=LayeredDirty( Green([ Point(440, 100, 0), Point(540, 100, 0), Point(540, 200, 0), Point(440, 200, 0) ]), Slope([ Point(440, 100, 0), Point(380, 50, 0), Point(600, 50, 0), Point(540, 100, 0) ], Color(100, 0, 0, 255), Vector2(0.0, -0.4)), Slope([ Point(600, 50, 0), Point(540, 100, 0),
from pygame import Color, font from pygame.math import Vector2 from pygame.sprite import LayeredDirty from src.models import Hole as BaseHole from src.sprites import * from src.utils import colors, Point Hole = BaseHole( 'Hole #13', par=3, origin=Point(100, 0, 0), ball=Point(495, 315, 0), noncollidibles=LayeredDirty( Text(Point(775, 605), 'Par 3', font.Font(None, 30), colors.WHITE), ), collidibles=LayeredDirty( Green([ Point(440, 260, 0), Point(540, 260, 0), Point(540, 360, 0), Point(440, 360, 0) ]), Slope([ Point(440, 360, 0), Point(510, 360, 0), Point(510, 410, 0), Point(470, 410, 0), Point(470, 460, 0), Point(440, 460, 0) ], Color(200, 0, 0, 255), Vector2(0.5, 0.5)), Slope([