Python 3 Pygame绘图迷宫

时间:2016-06-13 01:58:05

标签: pygame python-3.4

我试图通过在python中使用pygame绘制线条来创建这个迷宫轮廓,但格式搞砸了,我不确定发生了什么。我认为这可能是我创建的用于根据for循环中哪些字符处于活动状态来绘制线条的逻辑问题。

import pygame
pygame.init()
global black, white
white = (255,255,255)
black = (0,0,0)
display_width = 1200
display_height = 800


gameDisplay = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock()


def draw_line(surface,color,start_pos,end_pos,width):
    pygame.draw.line(surface,color,start_pos,end_pos,width)

def game_loop():
    maze = "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+n\
            |        |     |        |           |     |     |n\
            +  +--+  +  +  +--+  +  +  +  +--+--+  +  +--+  +n\
            |  |     |  |        |  |  |           |  |     |n\
            +  +--+  +  +  +--+--+  +  +--+--+--+--+  +  +  +n\
            |     |     |  |     |  |  |        |     |  |  |n\
            +--+  +--+--+--+  +  +--+  +  +--+--+--+--+  +  +n\
            |     |           |        |                 |  |n\
            +  +  +  +--+--+--+--+--+--+--+--+--+--+--+  +--+n\
            |  |  |  |        |                       |     |n\
            +  +--+  +  +--+  +  +--+--+--+--+--+--+  +  +  +n\
            |        |  |        |        |        |  |  |  |n\
            +  +--+--+  +--+--+--+  +--+  +  +  +--+  +  +  +n\
            |        |           |  |     |  |        |  |  |n\
            +--+--+  +--+--+--+  +  +  +--+  +--+--+--+--+  +n\
            |                    |  |                       |n\
            +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+"

    gameExit = False

    WALL_LENGTH = 20
    x=y=x2=y2=START=5


    gameDisplay.fill(white)

    for i in maze:
        if i == "+" or i == "-":
            x2+=WALL_LENGTH
            draw_line(gameDisplay,black,(x,y),(x2,y2),5)
            x+=WALL_LENGTH
        elif i == "n":
            y+=WALL_LENGTH*2
            y2=y
            x=START
            x2=START
        elif i == "|":
            y2+=WALL_LENGTH*2
            draw_line(gameDisplay,black,(x,y),(x2,y2),5)
            x+=WALL_LENGTH
            x2=x
            y2=y
        elif i == " ":
            x+=WALL_LENGTH
            x2=x





    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT: gameExit = True


        pygame.display.update()
        clock.tick(30)


game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:2)

最简单的调试方法是在代码中放几个print语句,然后查看x,y,x2,y2变量。

我注意到以下问题:

  1. 一行末尾的反斜杠告诉python字符串在下一行的开头继续。这意味着你在那里缩进迷宫的所有空格都被计算在内。当代码看到第一个" |"或" +",x计数器已经在245,将整行向右移动。删除每个迷宫线开头的空白以解决此问题。
  2. 每次有新行时,都会计算y值。但是,垂直线只连接两条水平线,它们本身并不是真正的迷宫线。所以你需要做两件事之一:

    一个。在每个偶数行的末尾往下走,向下拉墙

    湾在每个奇数行的末尾往下走,向上画墙(我在下面做过)

  3. 最后,我不太清楚你的" +"应该是。您目前使用它们来表示垂直线,水平线,角,线端以及交叉点。您需要为这些情况找出不同的ASCII符号,并替换您的" +"通过适当的符号然后调整您的代码以绘制所需的任何内容。否则,当您应该绘制垂直线时,最终会绘制水平线,反之亦然。

  4. 这是一个有点固定的代码版本。它仍然没有正确处理" +",但是我已经把它绘制成红色的有问题的行,所以应该很容易找出正确的行为。

    import pygame
    pygame.init()
    global black, white
    white = (255,255,255)
    black = (0,0,0)
    red = (255,0,0)
    display_width = 1200
    display_height = 800
    
    gameDisplay = pygame.display.set_mode((display_width,display_height))
    clock = pygame.time.Clock()
    
    def draw_line(surface,color,start_pos,end_pos,width):
        pygame.draw.line(surface,color,start_pos,end_pos,width)
    
    def game_loop():
        maze = "\
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+n\
    |        |     |        |           |     |     |n\
    +  +--+  +  +  +--+  +  +  +  +--+--+  +  +--+  +n\
    |  |     |  |        |  |  |           |  |     |n\
    +  +--+  +  +  +--+--+  +  +--+--+--+--+  +  +  +n\
    |     |     |  |     |  |  |        |     |  |  |n\
    +--+  +--+--+--+  +  +--+  +  +--+--+--+--+  +  +n\
    |     |           |        |                 |  |n\
    +  +  +  +--+--+--+--+--+--+--+--+--+--+--+  +--+n\
    |  |  |  |        |                       |     |n\
    +  +--+  +  +--+  +  +--+--+--+--+--+--+  +  +  +n\
    |        |  |        |        |        |  |  |  |n\
    +  +--+--+  +--+--+--+  +--+  +  +  +--+  +  +  +n\
    |        |           |  |     |  |        |  |  |n\
    +--+--+  +--+--+--+  +  +  +--+  +--+--+--+--+  +n\
    |                    |  |                       |n\
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+"
    
        gameExit = False
    
        WALL_LENGTH = 20
        x=y=x2=y2=START=5
    
        gameDisplay.fill(white)
        linecount = 0
        for i in maze:
            if i == "-":
                x2+=WALL_LENGTH
                draw_line(gameDisplay,black,(x,y),(x2,y2),5)
                x+=WALL_LENGTH
            elif i == "+":
                x2+=WALL_LENGTH
                draw_line(gameDisplay,red,(x,y),(x2,y2),5)
                x+=WALL_LENGTH
            elif i == "n":
                linecount+=1
                if(linecount % 2 == 0):
                    y+=WALL_LENGTH*2
                    y2=y
                x=START
                x2=START
            elif i == "|":
                y2-=WALL_LENGTH*2
                draw_line(gameDisplay,black,(x,y),(x2,y2),5)
                x+=WALL_LENGTH
                x2=x
                y2=y
            elif i == " ":
                x+=WALL_LENGTH
                x2=x
    
        while not gameExit:
    
            for event in pygame.event.get():
                if event.type == pygame.QUIT: gameExit = True
    
            pygame.display.update()
            clock.tick(30)
    
    game_loop()
    pygame.quit()
    quit()
    

    当前迷宫输出的屏幕截图: Screenshot of the current maze output