Example #1
0
class canvasTest(Scene):
    def __init__(self, *args):
        super(canvasTest, self).__init__(*args)
        # self.can = canvas(color(0, 0, 0), vec2(800, 600))
        # self.brush = brush(self.can, (255, 255, 255, 0.5), 1)
        # self.painter = Painter(self.can.surface())
        self.blend = pygame.Surface((300, 300)).convert()
        self.blend.fill((255, 255, 255))
        self.blend.set_alpha(80)
        self.sceneCanvas = pygame.Surface((self.width, self.height))
        self.sceneCanvas.fill((255, 255, 255))
        self.sceneCanvas.set_alpha(100)
        self.__E_FPS = TextElement(pygame.Rect(self.width - 80, 0, 80, 20),
                                   'FPS:', gl_Font, 18, (0, 255, 0), 1)

    def draw(self):
        # self.can.fill(color(0, 0, 0))
        # self.brush.drawLine((0, 0), (700, 500))
        # self.painter.Lines([point2(), point2(500, 700)], (255, 255, 255, 100), 1, 0)
        #
        # self.__E_FPS.draw(self.can.surface())
        # self.screen.blit(self.can.surface(), (0, 0))
        self.screen.blit(self.blend, (0, 0))
        self.screen.blit(self.sceneCanvas, (0, 0))
        self.__E_FPS.draw(self.screen)

    def doClockEvent(self, NowClock):
        fps = 'FPS:' + str(self.FPS)
        self.__E_FPS.setText(fps)
Example #2
0
class testSpriteScene(Scene):
    def __init__(self, screen, config, startClock):
        super(testSpriteScene, self).__init__(screen, config, startClock)
        self.img = pygame.Surface((5, 5)).convert()
        self.img.fill((255, 255, 255))
        self.sprintsGroup1 = SpriteGroup(RectangleRange(0, 0, 800, 600))
        self.__E_FPS = TextElement(pygame.Rect(self.width - 80, 0, 80, 20), 'FPS:', gl_Font, 18, (0, 255, 0), 1)
        for i in range(0, 1000):
            x, y = random.randint(0, 800), random.randint(0, 600)
            self.sprintsGroup1.add(CubeSprite(self.img, pygame.Rect(x, y, 5, 5)))

    def draw(self):
        _dict = self.sprintsGroup1.getCollideDict()
        for w in _dict.keys():
            w.highLight = True
        self.sprintsGroup1.draw(self.screen)
        self.sprintsGroup1.update()
        self.__E_FPS.draw(self.screen)

    def doClockEvent(self, NowClock):
        fps = 'FPS:' + str(self.FPS)
        self.__E_FPS.setText(fps)
Example #3
0
class AstartTest(Scene):
    def __init__(self, screen, config, time):
        super(AstartTest, self).__init__(screen, config, time)
        self.cols, self.rows = 100, 100
        self.grid = []
        self.openSet, self.closeSet = [], []
        self.start, self.end = None, None
        self.w, self.h = screen.get_width() / self.cols, screen.get_height(
        ) / self.rows
        self.path = []
        self.done = False
        self.noSolution = False
        self.points = []
        self.__E_TEXT = TextElement(
            pygame.Rect(centeredXPos(self.width, 220), 260, 220, 60), '',
            gl_Font, 50, (255, 255, 0), 1)

        for i in range(0, self.cols):
            self.grid.append([])

        for i in range(0, self.cols):
            for j in range(0, self.rows):
                self.grid[i].append(Spot(i, j))

        for i in range(0, self.cols):
            for j in range(0, self.rows):
                self.grid[i][j].addNeighbors(self.cols, self.rows, self.grid)

        self.start = self.grid[0][0]
        self.end = self.grid[self.cols - 1][self.rows - 1]
        self.start.wall = False
        self.end.wall = False
        self.openSet.append(self.start)

    def draw(self):
        current = None
        if len(self.openSet) > 0:
            winner = 0
            for i in range(0, len(self.openSet)):
                if self.openSet[i].f < self.openSet[winner].f:
                    winner = i

            current = self.openSet[winner]

            if current == self.end:
                self.done = True

            if not self.done:
                self.openSet.remove(current)
                self.closeSet.append(current)

                neighbors = current.neighbors
                for i in range(0, len(neighbors)):
                    neighbor = neighbors[i]
                    if neighbor not in self.closeSet and not neighbor.wall:
                        tempG = current.g + 1
                        newPath = False
                        if neighbor in self.openSet:
                            if tempG < neighbor.g:
                                neighbor.g = tempG
                                newPath = True
                        else:
                            neighbor.g = tempG
                            newPath = True
                            self.openSet.append(neighbor)
                        if newPath:
                            neighbor.h = heuristic(neighbor, self.end)
                            neighbor.f = neighbor.g + neighbor.h
                            neighbor.previous = current
        else:
            self.done = True
            self.__E_TEXT.setText('寻路失败')
            self.noSolution = True

        # self.screen.fill((255, 255, 255))

        for i in range(0, self.cols):
            for j in range(0, self.rows):
                self.grid[i][j].show(self.screen, (255, 255, 255), self.w,
                                     self.h)

        for i in range(0, len(self.openSet)):
            self.openSet[i].show(self.screen, (0, 255, 0), self.w, self.h)

        for i in range(0, len(self.closeSet)):
            self.closeSet[i].show(self.screen, (255, 0, 0), self.w, self.h)

        if not self.done:
            self.path.clear()
            temp = current
            self.path.append(temp)
            while temp is not None and temp.previous:
                self.path.append(temp.previous)
                temp = temp.previous
        elif self.done and not self.noSolution:
            self.path.append(self.end)
            self.__E_TEXT.setText('寻路成功')

        for i in range(0, len(self.path)):
            e = self.path[i]
            if e is not None:
                e.show(self.screen, (0, 0, 255), self.w, self.h)

        self.__E_TEXT.draw(self.screen)
Example #4
0
class RobotRunScene(Scene):
    def __init__(self, screen, config, startClock):
        super(RobotRunScene, self).__init__(screen, config, startClock)
        self.w, self.h = self.screen.get_width(), self.screen.get_height()
        self.__As_MAP = AStarArea(Rectangle(0, 0, 800, 600), 80, 60)
        self.caption = 'Actor与A*结合测试场景'
        self.__containers = []
        for i in range(0, 6):
            x = int(random.randint(100, self.w - 150) / 10) * 10
            y = int(random.randint(100, self.h - 150) / 10) * 10
            w = int(random.randint(50, 150) / 10) * 10
            h = int(random.randint(50, 150) / 10) * 10
            self.__containers.append(Container(Rectangle(x, y, w, h)))
            _x, _y = int(x / self.__As_MAP.unit_w), int(y / self.__As_MAP.unit_h)
            _w, _h = int(w / self.__As_MAP.unit_w), int(h / self.__As_MAP.unit_h)
            self.__As_MAP.addObstacleArea(_x, _w, _y, _h)

        self.__A_Robot = RobotActor(Rectangle(0, 0, 100, 100))
        self.__E_Msg = TextElement((600, 0, 200, 60), 'Robotlocal:(0, 0)\nMouselocal:(0, 0)', gl_Font, 12,
                                   (255, 255, 255), 1)
        self.__E_Msg2 = TextElement((600, 40, 200, 600), 'Astart:\n', gl_Font, 10, (255, 255, 255), 1)
        self.__pathList = []
        self.__normalLis = []
        self.__build_As_Map()
        self.__i = 0
        self.__end = (0, 0)

    def __build_As_Map(self):
        self.__As_MAP.setStart((0, 0))

    def __updateMap(self, point):
        h, w = self.__As_MAP.unit_h, self.__As_MAP.unit_w
        pos = self.__A_Robot.getLocal()
        x = int(pos.x / w)
        y = int(pos.y / h)
        try:
            self.__As_MAP.setStart((x, y))
        except CoordinateException as e:
            return e.pos
        x = int(point[0] / w)
        y = int(point[1] / h)
        try:
            self.__As_MAP.setEnd((x, y))
        except CoordinateException as e:
            return e.pos

    def __normalList(self):
        self.__normalLis.clear()
        h, w = self.__As_MAP.unit_h, self.__As_MAP.unit_w
        for p in self.__pathList:
            self.__normalLis.append(point2(p[0] * w, p[1] * h))
        if len(self.__normalLis) > 1:
            self.__normalLis.pop(0)
        self.__A_Robot.setPathList(self.__normalLis)

    def draw(self):
        # for e in self.__As_MAP.getObstaclesList():
        #     Painter(self.screen).Rect(Rectangle(e[0] * 10, e[1] * 10, 10, 10), (255, 255, 255), 0)
        for container in self.__containers:
            container.draw(self.screen)
        self.__A_Robot.draw(self.screen)
        self.__E_Msg.draw(self.screen)
        self.__E_Msg2.draw(self.screen)

    def doClockEvent(self, NowClock):
        if self.__i < len(self.__normalLis):
            if self.__A_Robot.update(self.__normalLis[self.__i], 5, self.__normalLis[-1]):
                self.__i += 1
        p = self.__A_Robot.area.barycenter()
        p2 = self.__end
        lis_str = 'A-start:\n'
        for _p in self.__normalLis:
            lis_str += str(_p) + '\n'
        self.__E_Msg.setText('Robotlocal:({}, {})\nMouselocal:({}, {})\n'.format(p.x, p.y, p2[0], p2[1]))
        self.__E_Msg2.setText(lis_str)

    def doMouseButtonDownEvent(self, Button):
        if Button == 1:
            self.__end = self.mousePos
            pos = self.__updateMap(self.__end)
            if pos:
                return
            self.__pathList = self.__As_MAP.run()
            self.__normalList()
            self.__As_MAP.refresh()
            self.__i = 0