我在网上找到了一个可以生成迷宫的算法,我想用它做一个游戏。该算法在用于生成迷宫的单元的边界周围绘制线条。
我想把这些线条改成矩形,以便在游戏中增加碰撞,但是我一直在这样做。
下面是这些行的代码:
Top Line: pygame.draw.line(window, COLOR, (self.x, self.y), ((self.x + cellWidth), self.y), 2)
Right Line: pygame.draw.line(window, COLOR, ((self.x + cellWidth), self.y), ((self.x + cellWidth), (self.y + cellWidth)), 2)
Left Line: pygame.draw.line(window, COLOR, ((self.x + cellWidth), (self.y + cellWidth)),(self.x, (self.y + cellWidth)), 2)
Bottom Line: pygame.draw.line(window, COLOR, (self.x, (self.y + cellWidth)), (self.x, self.y), 2)

这是我将它们变成矩形的尝试:
Top Line: pygame.Rect((self.x, self.y), (cellWidth, 2))
Right Line: pygame.Rect(((self.x + cellWidth), self.y), (2, cellWidth))
Left Line: pygame.Rect((self.x, cellWidth + self.y), (2, cellWidth))
Bottom Line: pygame.Rect((self.x, self.y + cellWidth), (cellWidth, 2))

我只希望矩形与第一张图中的线条相同,但是计算错误,矩形绘制在错误的位置,这意味着没有办法完成迷宫。
发布于 2020-09-30 01:22:44
2对应于线条的粗细,但在矩形中它是高度。您的任务需要使线条稍粗一点,并得到一个矩形。
wallWidth = 0.01建议的代码
Top Line: pygame.Rect((self.x, self.y), (cellWidth, wallWidth))
Right Line: pygame.Rect(((self.x + cellWidth), self.y), (wallWidth, cellWidth))
Left Line: pygame.Rect((self.x, cellWidth + self.y), (wallWidth, cellWidth))
Bottom Line: pygame.Rect((self.x, self.y + cellWidth), (cellWidth, wallWidth))https://stackoverflow.com/questions/64124480
复制相似问题