Python/Pygame如何创建用于查看世界地图的摄影机(自上而下,无玩家角色)

2021-12-01 11:18:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现一个摄像头功能,它的工作原理是图像移动-但是:

1)如果我没有移动相机,后续图像(如新菜单)将在创建位置创建。因此,如果我将相机向右移动30个像素,那么在(0,0)处创建的后续图像现在将在(30,0)处创建。 这也适用于与按钮的交互;在上面的示例中,要使用在(0,0)处创建的按钮,我需要将鼠标保持在(0,0)上,即使按钮现在显示在(30,0)处

因此,如果我移动我的相机,我会得到像https://i.imgur.com/FsydpSj.png这样的结果(看看当我的鼠标在上面时我是如何选择一个新游戏的?我的鼠标在哪里,就是我移动相机之前的按钮在哪里)或https://i.imgur.com/wAr9pFJ.png(游戏记得我在上一个菜单中移动了相机,现在在我移动相机之前,在原始菜单所在的位置创建下一个菜单

2)如果我创建了一个大于屏幕宽度或屏幕高度的图像,然后移动相机,我实际上看不到图像的其余部分。相反,我会看到如下内容:https://i.imgur.com/e0qgUW1.png

因此,游戏没有显示地图的其余部分,只是移动了一个快照(大小为屏幕宽度乘以屏幕高度):https://i.imgur.com/pYMsR8B.png-但是如果我放大菜单,你可以看到地图实际上应该大得多,我显然想在移动相机时看到地图的其余部分:https://i.imgur.com/Y3WmcBa.png

相关变量在类控制器中声明:

    windowWidth = 800 #1792
    windowHeight = 600 #896
    windowResize = False
    cameraactive = False
    camera = pygame.display.set_mode((windowWidth, windowHeight))
    screen = pygame.Surface((windowWidth, windowHeight))
    mouse = pygame.mouse.get_pos()
    camerax = 0
    cameray = 0
    sprites = pygame.sprite.Group()
# All the other sprite.Group()s, such as spritesciv, are created just like this one
    spriteslist = [sprites , spritesciv , tiles , cities , texttiles , textcities , textcitiesselected , buttonscitiesselected , textbuttonscitiesselected , buttons , buttonsselectciv , buttonsrandomciv , textinputs]
    buttonslist = [tiles , cities , buttonscitiesselected , buttons , buttonsselectciv , buttonsrandomciv , textinputs]

控制器内的所有相关功能

    def on_event(self):
        keys = pygame.key.get_pressed()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self._running = False
            if keys[K_ESCAPE]:
                self._running = False
            if event.type == MOUSEMOTION:
                Controller.mouse = pygame.mouse.get_pos()
            if event.type == pygame.VIDEORESIZE: #Controller.windowResize
                screensize = event.size
                Controller.windowWidth = event.w
                Controller.windowHeight = event.h
                screen = pygame.display.set_mode(screensize,RESIZABLE)
            if self.cameraactive:
                if Controller.mouse[0]<50:
                    self.camerax += 8
                if (self.windowWidth - Controller.mouse[0])<50:
                    self.camerax -= 8
                if Controller.mouse[1]<50:
                    self.cameray += 8
                if (self.windowHeight - Controller.mouse[1])<50:
                    self.cameray -= 8
            if keys[K_SPACE] and event.type == pygame.KEYUP:
                self.nextturn()
            if keys[K_TAB] and event.type == pygame.KEYUP:
                i = Controller.civilisationsactive.index(Controller.civilisation)
                if i < (len(Controller.civilisationsactive)-1):
                    Controller.civilisation = Controller.civilisationsactive[i + 1]
                else:
                    Controller.civilisation = Controller.civilisationsactive[0]
            for i in Controller.buttonslist:
                for button in i:
                    button._create_event(event)
    def empty_draw(self):
        for i in Controller.spriteslist:
            i.empty()
    def on_draw(self):
        self.screen.fill((255, 255, 255))
        for i in Controller.spriteslist:
            i.draw(self.screen)
        self.camera.blit(self.screen, (self.camerax, self.cameray))
        pygame.display.flip()
        pygame.display.update()

非常感谢您的帮助。:)