对象中的列表被覆盖

时间:2019-07-12 09:15:42

标签: python-3.x

经过一些工作,我完成了迷宫生成器的第一个算法,现在我试图使其在Pygame中可视化。我首先让算法生成一个迷宫,然后对其进行可视化表示。

在这里,我遇到了多个问题,但我认为它们都与同一件事相关,那就是迷宫的第一个单元以某种方式被覆盖。因此,我得到的完全不是迷宫,而是到处都是几行。

我尝试将清除墙壁的方法分开,但这似乎也不起作用。我查看了是否在第一个单元格上调用了removewall方法,并说这是对的,但是在某种程度上,该单元格的值被覆盖了。

代码:

import pygame
import random

WIDTH = 300
HEIGHT = 300
CellSize = 30
Rows = int(WIDTH/30)
Columns = int(HEIGHT/30)
current = None
grid = []
visited = []
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))


class Cell:
    def __init__(self, r, c):
        self.r = r
        self.c = c
        self.x = CellSize * c
        self.y = CellSize * r
        self.sides = [True, True, True, True]  # Top, Bottom, Left, Right
        self.visited = False
        self.neighbours = []
        self.NTop = None
        self.NBottom = None
        self.NRight = None
        self.NLeft = None
        self.NTopIndex = None
        self.NBottomIndex = None
        self.NRightIndex = None
        self.NLeftIndex = None
        self.nr = None
        self.nc = None
        self.random = None
        self.neighbour = None

    def index(self, nr, nc):
        self.nr = nr
        self.nc = nc
        if self.nr < 0 or self.nc < 0 or self.nr > Rows-1 or self.nc > Columns-1:
            return -1

        return self.nr + self.nc * Columns

    def neighbour_check(self):
        # Get neighbour positions in Grid
        self.NTopIndex = self.index(self.r, self.c - 1)
        self.NBottomIndex = self.index(self.r, self.c + 1)
        self.NRightIndex = self.index(self.r + 1, self.c)
        self.NLeftIndex = self.index(self.r - 1, self.c)

        # Look if they are truly neighbours and then append to neighbour list
        if self.NTopIndex >= 0:
            self.NTop = grid[self.NTopIndex]
            if not self.NTop.visited:
                self.neighbours.append(self.NTop)
        if self.NBottomIndex >= 0:
            self.NBottom = grid[self.NBottomIndex]
            if not self.NBottom.visited:
                self.neighbours.append(self.NBottom)
        if self.NRightIndex >= 0:
            self.NRight = grid[self.NRightIndex]
            if not self.NRight.visited:
                self.neighbours.append(self.NRight)
        if self.NLeftIndex >= 0:
            self.NLeft = grid[self.NLeftIndex]
            if not self.NLeft.visited:
                self.neighbours.append(self.NLeft)

        # Choose random neighbour
        if len(self.neighbours) > 0:
            self.random = random.randint(0, len(self.neighbours) - 1)
            self.neighbour = self.neighbours[self.random]
            # Remove the wall between self and neighbour
            if self.neighbour == self.NTop:
                if self == grid[0]:
                    print('TOP')
                self.sides[0] = False
                self.NTop.sides[1] = False
            elif self.neighbour == self.NBottom:
                if self == grid[0]:
                    print('BOTTOM')
                self.sides[1] = False
                self.NBottom.sides[0] = False
            elif self.neighbour == self.NLeft:
                if self == grid[0]:
                    print('LEFT')
                self.sides[2] = False
                self.NLeft.sides[3] = False
            elif self.neighbour == self.NRight:
                if self == grid[0]:
                    print('RIGHT')
                self.sides[3] = False
                self.NRight.sides[2] = False
            else:
                print('SIDES ERROR')
            return self.neighbours[self.random]
        else:
            return -1

    def draw(self):
        global DISPLAY, CellSize
        # Top
        if self.sides[0]:
            pygame.draw.line(DISPLAY, (0, 0, 0), (self.x, self.y), (self.x + CellSize, self.y))
        # Bottom
        if self.sides[1]:
            pygame.draw.line(DISPLAY, (0, 0, 0), (self.x, self.y + CellSize), (self.x + CellSize, self.y + CellSize))
        # Left
        if self.sides[2]:
            pygame.draw.line(DISPLAY, (0, 0, 0), (self.x, self.y), (self.x, self.y + CellSize))
        # Right
        if self.sides[3]:
            pygame.draw.line(DISPLAY, (0, 0, 0), (self.x + CellSize, self.y), (self.x + CellSize, self.y + CellSize))


class Maze:

    def __init__(self):
        global current
        self.next = None
        self.running = True
        self.DISPLAY = None
        self.display_running = True

    def init_cells(self):
        # Make grid and make cell 0 the begin of the algorithm
        global current
        for i in range(0, Columns):
            for j in range(0, Rows):
                cell = Cell(j, i)
                grid.append(cell)
        current = grid[0]

    def init_maze(self):
        global current, visited
        print(grid[0].sides)
        # Start Algorithm
        while self.running:
            # Check if the current cell is visited, if not make it visited and choose new neighbour
            if not current.visited:
                current.visited = True
                visited.append(current)
            self.next = current.neighbour_check()
            if not self.next == -1:
                # If it finds a neighbour then make it the new current cell
                # self.next.visited = True
                current = self.next
            elif self.next == -1 and len(visited) > 0:
                # If it doesn't then look trough the path and backtrack trough it to find a possible neighbour
                if len(visited) > 1:
                    del visited[-1]
                    current = visited[-1]
                # If the last cell of the visited list is Cell 0 then remove it
                elif len(visited) <= 1:
                    del visited[-1]
            elif len(visited) <= 0:
                # Stop the Algorithm
                self.running = False
                print('Done')

    def draw(self):
        DISPLAY.fill((255, 255, 255))
        # Get the maze made by the algorithm and draw it on the screen
        for i in range(0, len(grid)):
            grid[i].draw()
        pygame.display.update()
        while self.display_running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.display_running = False


maze = Maze()
maze.init_cells()
maze.init_maze()
maze.draw()

出于调试目的,我在其中放入了一些打印方法。

并且仍然是编程的初学者,我知道它可能更简洁,或者方法的某些命名可能会更好。

我想发生的是,在def init_maze中迷宫的蓝图被写出,而在def draw中迷宫的蓝图被画在屏幕上。

0 个答案:

没有答案