如何在pygame中滚动图块地图?

时间:2019-06-21 02:22:26

标签: python pygame 2d-games pytmx

我使用pytmx实现了图块地图,我试图弄清楚如何使用if Keys[pygame.K_somekey]:滚动它(整个地图)。我渲染了一些按钮,以便当您按下播放按钮时,它开始渲染要显示的地图。从那里,我需要实现地图的滚动或移动。

我尝试使用Screen.blit(Map,(MAP_X - 5, MAP_Y))在x或y方向上将其渲染为-5或+5,并针对其他方向进行其他操作,但这似乎不起作用。我也尝试过pygame.scroll,但是那也不起作用。

#=======
# Imports 
#=======

import pygame ,sys , pytmx , time

#=======
# Start Pygame
#=======

pygame.init()

#=======
# Variables 
#=======

#NUMBER VARIABLES
DISPLAY_WIDTH = 1920
DISPLAY_HEIGHT = 1080
WHITE = (255,255,255)
FPS = 120
BUTTON_X = DISPLAY_WIDTH/2 - 64
PB_Y = 400
CB_Y  = 450
EB_Y  = 500
MOUSE_X = 0
MOUSE_Y = 1
OVER = 1
NOT_OVER = 0

#FUNCTION VARIABLES
Screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT),pygame.FULLSCREEN)
Keys = pygame.key.get_pressed()
MOUSE = pygame.mouse.get_pos()

#TRUE/FALSE
Game_Over = False
Pressed = False

#=======
# Pre-Loads 
#=======

#BUTTONS
Config_Button = [pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Test\@Resources\Menu Buttons\Config.png"),
                    pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Test\@Resources\Menu Buttons\Config_Over.png")]
Play_Button = [pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Test\@Resources\Menu Buttons\Play.png"),
                pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Test\@Resources\Menu Buttons\Play_Over.png")]
Exit_Button = [pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Test\@Resources\Menu Buttons\Exit.png"),
                pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Test\@Resources\Menu Buttons\Exit_Over.png")]

#MAP ( TMX )
Map = pytmx.load_pygame(r"C:\Users\Damien Santiago\Desktop\Test\@Resources\Maps\Map.tmx")

#======
# Defined Functions
#======
def Render_Map():
    for layer in Map.visible_layers:
        for x, y, gid, in layer:
            MAP_X = x * Map.tilewidth
            MAP_Y = y * Map.tileheight
            tile = Map.get_tile_image_by_gid(gid)
            Screen.blit(tile, (MAP_X,MAP_Y))

#=======
# Code
#=======


while not Game_Over:

    MOUSE = pygame.mouse.get_pos()

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            sys.exit()
            break 

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
                break
            break

        if event.type == pygame.MOUSEBUTTONDOWN:
            #PLAY BUTTON
            if  892 < MOUSE[MOUSE_X] < 1020 and 480 > MOUSE[MOUSE_Y] > 440:
                Pressed = True
                Render_Map()
                pygame.display.update()

            #SETTINGS/CONFIG BUTTON
            if  892 < MOUSE[0] < 1020 and 530 > MOUSE[1] >500:
                Pressed = True

            #EXIT BUTTON
            if  892 < MOUSE[MOUSE_X] < 1020 and 580 > MOUSE[MOUSE_Y] >550:
                sys.exit()

            break

        if Pressed == False:

            #PLAY BUTTON
            if  892 < MOUSE[MOUSE_X] < 1020 and 480 > MOUSE[MOUSE_Y] > 440:
                Screen.blit(Play_Button[OVER],(BUTTON_X, PB_Y))
                pygame.display.update()

            else:
                Screen.blit(Play_Button[NOT_OVER],(BUTTON_X, PB_Y))
                pygame.display.update()

            #SETTINGS/CONFIG BUTTON
            if  892 < MOUSE[MOUSE_X] < 1020 and 530 > MOUSE[MOUSE_Y] >500:
                Screen.blit(Config_Button[OVER],(BUTTON_X, CB_Y))
                pygame.display.update()

            else:
                Screen.blit(Config_Button[NOT_OVER],(BUTTON_X, CB_Y))
                pygame.display.update()

            #EXIT BUTTON
            if  892 < MOUSE[MOUSE_X] < 1020 and 580 > MOUSE[MOUSE_Y] >550:
                Screen.blit(Exit_Button[OVER],(BUTTON_X, EB_Y))
                pygame.display.update()

            else:
                Screen.blit(Exit_Button[NOT_OVER],(BUTTON_X, EB_Y))
                pygame.display.update()

1 个答案:

答案 0 :(得分:1)

在功能.blit中,地图的所有图块分别Render_Map到屏幕。计算每个图块的位置。
如果要滚动地图,则必须以动态偏移量来移动图块:

def Render_Map(offset_x, offset_y):
    for layer in Map.visible_layers:
        for x, y, gid, in layer:
            MAP_X = x * Map.tilewidth  + offset_x
            MAP_Y = y * Map.tileheight + offset_y
            tile = Map.get_tile_image_by_gid(gid)
            Screen.blit(tile, (MAP_X, MAP_Y))

按下键时更改图块的原点。例如:

pos_x = 0

while not Game_Over:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            sys.exit()
            break 

    # [...]

    Keys = pygame.key.get_pressed()

    if Keys[pygame.K_a]:
        pos_x += 5

    Render_Map(pos_x, 0)
    pygame.display.update()