我试图通过在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()
答案 0 :(得分:2)
最简单的调试方法是在代码中放几个print语句,然后查看x,y,x2,y2变量。
我注意到以下问题:
每次有新行时,都会计算y值。但是,垂直线只连接两条水平线,它们本身并不是真正的迷宫线。所以你需要做两件事之一:
一个。在每个偶数行的末尾往下走,向下拉墙
湾在每个奇数行的末尾往下走,向上画墙(我在下面做过)
最后,我不太清楚你的" +"应该是。您目前使用它们来表示垂直线,水平线,角,线端以及交叉点。您需要为这些情况找出不同的ASCII符号,并替换您的" +"通过适当的符号然后调整您的代码以绘制所需的任何内容。否则,当您应该绘制垂直线时,最终会绘制水平线,反之亦然。
这是一个有点固定的代码版本。它仍然没有正确处理" +",但是我已经把它绘制成红色的有问题的行,所以应该很容易找出正确的行为。
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()