我正在尝试实现一个相机功能,它的工作原理是图像移动 - 但是:
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
相关变量在 Controller 类中声明:
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]
相关功能,都在Controller里面
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()
非常感谢您的帮助。:)