Beispiel #1
0
    def render_world(self, camera):
        view = Matrix.lookat(
            camera.x + 19, camera.y, 15,
            camera.x + 19, camera.y, 0,
            0,1,0)

        with self.fbo as frame:
            frame.clear(0,0.03,0.15,1)

            Shader.upload_projection_view(self.projection, view)
            Shader.upload_game(None)
            Shader.upload_light(self.ambient_light, self.cull_lights())

            # parallax background
            pm = Matrix.transform(
                0.75 * camera.x, camera.y * 0.5 - 12, 0,
                (19*4) * self.parallax_rep, 19, 0
            )
            self.shader_hud.bind()
            self.parallax.texture_bind()
            Shader.upload_model(pm)
            self.repquad.draw()

            Shader.upload_projection_view(self.projection, view)

            self.shader.bind()
            self.map.draw()

            # entities
            for obj in self.map.obj:
                obj.draw()
            if not self.is_over:
                self.projectile.draw()
Beispiel #2
0
    def matrix(self):
        """Get the camera matrix to convert object from world coordinate
        to camera coordinate
        """
        if self.__matrix is not None:
            return self.__matrix

        # calculate matrices
        translation = [
            [1, 0, 0, -self.position.x],
            [0, 1, 0, -self.position.y],
            [0, 0, 1, -self.position.z],
            [0, 0, 0, 1]
        ]
        trans_mat = Matrix.new(translation)
        camera_zaxis = self.facing_dir.unit_vector
        camera_yaxis = self.up_dir.unit_vector
        camera_xaxis = camera_yaxis.cross(camera_zaxis).unit_vector
        alignment = [
            [*camera_xaxis.to_list(), 0],
            [*camera_yaxis.to_list(), 0],
            [*camera_zaxis.to_list(), 0],
            [0, 0, 0, 1]
        ]
        alignment_mat = Matrix.new(alignment)
        # now, we have our world coordinate aligned with camera coordinate
        # calculate projection matrix
        proj_mat = self.projection.matrix
        self.__matrix = proj_mat * alignment_mat * trans_mat
        return self.__matrix
Beispiel #3
0
 def __init__(self, angleOfView=60, aspectRatio=1, near=0.1, far=1000):
     super().__init__()
     self.projectionMatrix = Matrix.perspective(angleOfView=angleOfView,
                                                aspectRatio=aspectRatio,
                                                near=near,
                                                far=far)
     self.viewMatrix = Matrix.identity()
Beispiel #4
0
    def model_matrix(self):
        phase = (pygame.time.get_ticks() - self.phase) / 1000.0
        if self.flip: phase *= -1

        offset = Matrix.translate(-0.5, -0.5)
        translate = Matrix.translate(self.pos.x + 0.5, self.pos.y + 0.5)
        scale = Matrix.scale(2.2, 2.2)
        rotate = Matrix.rotatez(phase * 1.0)

        return np.dot(offset, np.dot(rotate, np.dot(scale, translate)))
Beispiel #5
0
 def draw(self, *args, **kwargs):
     Shader.upload_model(Matrix.identity())
     glActiveTexture(GL_TEXTURE1)
     self.normal[0].texture_bind()
     glActiveTexture(GL_TEXTURE0)
     self.texture[0].texture_bind()
     self.vbo.draw(*args, **kwargs)
Beispiel #6
0
def main() -> None:
    print("Podaj pierwszy ciąg:")
    string1 = input()

    print("Podaj drugi ciąg:")
    string2 = input()

    matrix = Matrix(string1, string2)
    print(f"Najdłuższy wspólny podciąg: {matrix.right_bottom}")
    print(f"LCS: {matrix.lcs}")
Beispiel #7
0
 def transform_point(self, point3d):
     pointarr = [*point3d.to_list(), 1.]
     pointmatrix = Matrix.new([[x] for x in pointarr])
     result = self.camera.matrix * pointmatrix
     z = result.array[-1][0]
     x = result.array[0][0]
     y = result.array[1][0]
     try:
         return [x/z, y/z]
     except Exception as e:  # DivisionByZero in fact
         return None
Beispiel #8
0
    def _gen_random_matrix(self, n_rows, n_colums):
        """Generate a n_rows * n_columns matrix with random numbers.
        Arguments:
            n_rows {int} -- The number of rows.
            n_colums {int} -- The number of columns.
        Returns:
            Matrix
        """

        data = [[random() for _ in range(n_colums)] for _ in range(n_rows)]
        return Matrix(data)
Beispiel #9
0
 def matrix(self):
     if self.__matrix is not None:
         return self.__matrix
     d = self.plane_distance
     projection = [
         [d, 0, 0, 0],
         [0, d, 0, 0],
         [0, 0, d, 0],
         # we want w coordinate of projected point to be the z coordinate
         [0, 0, 1, 0],
     ]
     self.__matrix = Matrix.new(projection)
     return self.__matrix
Beispiel #10
0
    def __init__(self, height, properties, **kwargs):
        Item.__init__(self, height=height, properties=properties, **kwargs)

        flip = 1
        offset = 0
        if 'Flip' in properties:
            flip = -1
            offset = 5

        self.mat = Matrix.transform(self.pos.x + offset, self.pos.y - height * (1.0/8) + 1, 0, 5 * flip, 5, 1)

        self.loaded = self.sprite
        self.unloaded = image.Sprite(diffuse=Catapult.other)
        self.broken = image.Sprite(diffuse=Catapult.broken)
Beispiel #11
0
    def response_from_server(self, method: int):
        """
        Ask the server to solve a sudoku with a specific algorithm

        Parameters
        ----------
        method : int
            The algorithm to execute to solve the sudoku
        """
        self.flag = False
        self.response = ""
        if self.socket != None:

            message = "SOLVE " + str(method) + " " + self.sudoku_matrix
            try:
                self.socket.send(message.encode("utf-8"))
                response = self.socket.recv(1024).decode("utf-8")
                if response != "":
                    response_split = response.split(" ")
                    if response_split[1] != "-1":
                        server = response_split[0]
                        position = int(response_split[1])
                        number = response_split[2]

                        print("Before built matrix")
                        Matrix.print_matrix(Matrix.build_matrix(self.sudoku_matrix))

                        matriz_list_temp = Matrix.to_list(self.sudoku_matrix)
                        matriz_list_temp[position] = number
                        self.sudoku_matrix = Matrix.to_str(matriz_list_temp)

                        print(response)
                        self.response = response
                        print("After built matrix")
                        Matrix.print_matrix(Matrix.build_matrix(self.sudoku_matrix))
                    else:
                        self.response = response
            except socket.timeout as ex:
                print("Error: Connection timeout. A response cannot be found for method: " + str(method) + " =>", ex)

        else:
            print("Error: A valid connection doesn't created, a response can't be waited")

        self.flag = True
        self.thread = None
Beispiel #12
0
    def render(self):
        glClearColor(1,0,1,1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

        camera = self.camera.copy()
        if self.projectile:
            camera.x = min(max(self.projectile.pos.x - 19, 0), self.camera_max)
            self.follow_cam = camera
        elif self.follow_cam:
            camera = self.follow_cam

        self.render_hud(camera)
        self.render_world(camera)

        mat = Matrix.scale(self.size.x, self.size.y)
        Shader.upload_projection_view(self.ortho, Matrix.identity())
        Shader.upload_model(mat)

        self.fbo.bind_texture()
        self.post.bind()
        self.quad.draw()

        y = self.size.x * (160./800)

        # hud background
        pm = Matrix.transform(
            0, y, 0,
            self.size.x, -y, 1
        )
        self.shader_hud.bind()
        self.hudbg.texture_bind()
        Shader.upload_model(pm)
        self.quad.draw()

        # ui
        pm = Matrix.transform(
            0, self.hud_ui.height, 0,
            self.hud_ui.width, -self.hud_ui.height, 1
        )
        self.hud_ui.draw()
        self.shader_hud.bind()
        Shader.upload_model(pm)
        self.quad.draw()

        # messagebox
        mat = Matrix.translate(self.size.x / 2 - self.hud_msgbox.width / 2, self.size.y - self.hud_msgbox.height)
        Shader.upload_model(mat)
        self.hud_msgbox.draw()

        # scrollbar
        mat = Matrix.translate(0, y-22)
        Shader.upload_model(mat)
        self.scrollbar.draw()

        pygame.display.flip()
Beispiel #13
0
    def _items_mul_ratings(self, items, ratings):
        """Multiply a dense matrix(item matrix) with sparse matrix (rating matrix).
        The result(users) is a k * m matrix, m stands for number of user_ids.
        Arguments:
            items {Matrix} -- k * n matrix, n stands for number of item_ids.
            ratings {dict} -- The items ratings by users.
            {user_id: {item_id: rating}}
        Returns:
            Matrix -- User matrix.
        """
        def f(items_row, user_id):
            item_ids = iter(ratings[user_id].keys())
            scores = iter(ratings[user_id].values())
            col_nos = map(lambda x: self.item_ids_dict[x], item_ids)
            _items_row = map(lambda x: items_row[x], col_nos)
            return sum(a * b for a, b in zip(_items_row, scores))

        ret = [[f(items_row, user_id) for user_id in self.user_ids]
               for items_row in items.data]
        return Matrix(ret)
Beispiel #14
0
    def init(self, size, fullscreen=False):
        flags = OPENGL|DOUBLEBUF
        if fullscreen:
            flags |= FULLSCREEN

        pygame.display.set_mode(size.xy, flags)
        pygame.display.set_caption('Ancient Earth')

        i = pygame.display.Info()
        self.size = Vector2i(i.current_w, i.current_h)

        glMatrixMode(GL_MODELVIEW)
        glEnable(GL_TEXTURE_2D)
        glDisable(GL_CULL_FACE)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)

        image.setup()

        self.stage = 1
        self.projection = Matrix.perspective(75, self.size, 0.1, 100)
        self.ortho = Matrix.ortho(self.size)

        v = np.array([
                0,0,0, 0,0,
                1,0,0, 1,0,
                1,1,0, 1,1,
                0,1,0, 0,1,
                ], np.float32)
        i = np.array([0,1,2,3], np.uint32)
        self.quad = VBO(GL_QUADS, v, i)

        # parallax
        self.parallax_rep = 10
        v = np.array([
                0,0,0, 0,1,
                1,0,0, self.parallax_rep, 1,
                1,1,0, self.parallax_rep, 0,
                0,1,0, 0,0,
                ], np.float32)
        i = np.array([0,1,2,3], np.uint32)
        self.repquad = VBO(GL_QUADS, v, i)
        self.parallax = Image('texture/sky.png', wrap=GL_REPEAT)
        self.hudbg = Image('texture/hud_bottom.png')

        self.fbo = FBO(self.size, format=GL_RGB8, depth=True)

        self.shader = Shader.load('default')
        self.shader_hud = Shader.load('hud')
        self.post = Shader.load('post')
        self.windmax = 1.0
        self.random_wind()

        self.ambient_light = (1.0, 1.0, 1.0)

        fontsize = 16 + int(self.res_hack() * 14)

        self.map = Map('map.json')
        self.clock = pygame.time.Clock()
        self.hud_msgbox = HUD(Vector2i(500,100), 'msgbox')
        self.hud_ui = HUD(Vector2i(self.size.x, self.size.x * (160./800)), 'ui')
        self.scrollbar = HUD(Vector2i(self.size.x,28), 'scrollbar')
        self.font = self.hud_msgbox.create_font(size=fontsize)
        self.font_ui = self.hud_ui.create_font(size=fontsize, font='Comic Sans MS')
        self.camera_max = self.map.width - 38
        self.catapults = [self.map.get_named_item('Catapult 1'), self.map.get_named_item('Catapult 2')]

        self.reset()

        with self.hud_msgbox:
            self.hud_msgbox.clear((0,1,1,1))
Beispiel #15
0
 def model_matrix(self):
     return Matrix.translate(self.pos.x, self.pos.y)
Beispiel #16
0
 def rotate(self, x, y, z, localCoord=False):
     matrix = Matrix.rotateX(x) @ Matrix.rotateY(y) @ Matrix.rotateZ(z)
     self.applyMatrix(matrix, localCoord)
Beispiel #17
0
 def scale(self, x, y, z, localCoord=False):
     matrix = Matrix.scale(x, y, z)
     self.applyMatrix(matrix, localCoord)
Beispiel #18
0
 def translate(self, x, y, z, localCoord=False):
     matrix = Matrix.translate(x, y, z)
     self.applyMatrix(matrix, localCoord)
    def handle(self, *args, **options):

        m = Matrix()
        m.pantsMatrixToCsv()

        self.stdout.write(self.style.SUCCESS(f"All tops are added in matrix!"))
Beispiel #20
0
 def __init__(self):
     self.transform = Matrix.identity()
     self.parent = None
     self.children = []