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

2024-03-29 11:48:02 发布

您现在位置: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()

非常感谢您的帮助。:)


Tags: https图像selfcomeventifpng菜单
1条回答
网友
1楼 · 发布于 2024-03-29 11:48:02

您可以使用完整地图创建曲面,并在屏幕上复制此地图的一部分

screen.blit(fullmap, (0,0), camera)

但我会用不同的名字:

screen(最终window)-使用set_mode((800,600))创建的曲面

fullmap-使用Surface((2000, 1000))创建的曲面以绘制所有元素

camera-Rect,而不是Surface相机的位置/偏移


最小工作代码

我使用箭头来改变cameraRect)的位置(x,y),并检查这个矩形是否没有留下完整的映射

稍后,我使用这个矩形camera将地图的一部分复制到屏幕上

我还添加了“图标”,它是单独闪烁的-总是在同一个地方-所以它永远不会随着箭头移动

import pygame
import random # to draw random rectangles on map

#  - constants  -

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
COLORS = (RED, GREEN, BLUE)

TILE_SIZE = 50

#  - main  -

pygame.init()

# - window -
screen = pygame.display.set_mode((800,600))
screen_rect = screen.get_rect()

# - map - (with random rectangles)
fullmap = pygame.Surface((2000, 1000))
#for y in range(TILE_SIZE, 1000-TILE_SIZE, TILE_SIZE):
#    for x in range(TILE_SIZE, 2000-TILE_SIZE, TILE_SIZE):
for y in range(0, 1000, TILE_SIZE):
    for x in range(0, 2000, TILE_SIZE):
        pygame.draw.rect(fullmap, random.choice(COLORS), (x, y, TILE_SIZE, TILE_SIZE))
fullmap_rect = fullmap.get_rect()

# - icon(s) which not move -
icon = pygame.Surface((100, 100))
icon.fill((255,255,255))
icon_rect = icon.get_rect()
icon_rect.right = screen_rect.right - 10
icon_rect.bottom = screen_rect.bottom - 10

# - camera -
# `camera` is not `Surface` but only `Rect` with value/offset which I uses to cut map
camera = screen.get_rect()

#  - loop  -                         
running = True
while running:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    # - updates (without draws) -

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        camera.x -= 5
        if camera.left < fullmap_rect.left:
            camera.left = fullmap_rect.left
    if keys[pygame.K_RIGHT]:
        camera.x += 5
        if camera.right > fullmap_rect.right:
            camera.right = fullmap_rect.right
    if keys[pygame.K_UP]:
        camera.y -= 5
        if camera.top < fullmap_rect.top:
            camera.top = fullmap_rect.top
    if keys[pygame.K_DOWN]:
        camera.y += 5
        if camera.bottom > fullmap_rect.bottom:
            camera.bottom = fullmap_rect.bottom

    # - draws (without updates) -

    # moving map - using `camera` to copy part of `fullmap` to `screen`
    screen.blit(fullmap, (0,0), camera)

    # static icon(s)
    screen.blit(icon, icon_rect)

    pygame.display.update()

#  - end  -    
pygame.quit()

相关问题 更多 >