vendredi 25 mars 2016

Maze generation error with python/pygame

So I'm desperately trying to create this code for my final project. I am supposed to create something based off of an algorithm and make it presentable to class. I thought it wouldn't be that difficult but it seems that I was sorely wrong. I was following a code that seemed like it could be useful but it ended up leaving me with more questions than answers or results. Here is is:

import pygame
from pygame.locals import *
import random

class Maze:
  def __init__(self, mazeLayer):
    self.mazeArray = []
    self.state = 'create'
    self.mLayer = mazeLayer
    self.mLayer.fill((0, 0, 0, 0))
    for y in xrange(60): # 80 wide + 60 tall
        pygame.draw.line(self.mLayer, (0,0,0,255), (0, y*8), (640, y*8))
        for x in xrange(80):
            self.mazeArray.append(0)
            if ( y == 0 ):
                pygame.draw.line(self.mLayer, (0, 0, 0, 255), (x*8, 0), (x*8, 480))
        self.totalCells = 4800 # 80 * 60
        self.currentCell = random.randint(0, self.totalCells-1)
        self.visitedCells = 1
        self.cellStack = []
        self.compass = [(-1,0),(0,1),(1,0),(0,-1)]

    def update(self):
        if self.state == 'create':
            if self.visitedCells >= self.totalCells:
                self.currentCell = 0
                self.cellStack = []
                self.state = 'solve'
                return
            moved = False
            while (moved == False):
                x = self.currentCell % 80
                y = self.currentCell / 80
                neighbors = []
                for i in xrange(4):
                    nx = x + self.compass[i] [0]
                    ny = y + self.compass[i] [1]
                    if ((nx >= 0) and (ny >= 0) and (nx < 80) and (ny < 60)):
                        if (self.mazeArray[(ny*80+nx)] & 0x000F) == 0:
                            nidx = ny*80+nx
                            neighbors.append((nidx,1<<i))
                if len(neighbors) > 0:
                    idx = random.randint(0,len(neighbors)-1)
                    nidx,direction = neighbors[idx]
                    dx = x*8
                    dy = y*8
                    if direction & 1:
                        self.mazeArray[nidx] |= (4)
                        pygame.draw.line(self.mLayer, (0,0,0,0), (dx,dy+1),(dx,dy+7))
                    elif direction & 2:
                        self.mazeArray[nidx] |= (8)
                        pygame.draw.line(self.mLayer, (0,0,0,0), (dx+1,dy+8),(dx+7,dy+8))
                    elif direction & 4:
                        self.mazeArray[nidx] |= (1)
                        pygame.draw.line(self.mLayer, (0,0,0,0), (dx+8,dy+1),(dx+8,dy+7))
                    elif direction & 8:
                        self.mazeArray[nidx] |= (2)
                        pygame.draw.line(self.mLayer, (0,0,0,0), (dx+1,dy),(dx+7,dy))
                    self.cellStack.append(self.currentCell)
                    self.currentCell = nidx
                    self.visitedCells = self.visitedCells + 1
                    moved = True
                else:
                    self.currentCell = self.cellStack.pop()

    def draw(self, screen):
        screen.blit(self.mLayer, (0, 0))

def main():
pygame.init()
screen=pygame.display.set_mode((700, 700))
pygame.display.set_caption('Run...')

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,255))

newMaze = Maze(mazeLayer)

screen.blit(background, (0,0))
pygame.display.flip()
clock = pygame.time.Clock()

while 1:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == QUIT:
            return
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                return
    newMaze.update()
    screen.blit(background, (0,0))
    pygame.display.flip()

if __name__ == '__main__': main()

I wanted to create a maze that randomly generated each time it was started anew. There would be about 7 levels, each being different. The player is supposed to be able to maneuver through the maze and reach the end which will take them to the next level. An AI (the minotaur of the maze) is supposed to chase the player through the maze until they make it to the final level and escape. I also wanted to implement areas where a player could slip in and "hide" from the minotaur whom would just wander aimlessly until the player made a break for it.

Any help you guys could give me would be great. This is due by the end of the semester but I want to try and knock it out ASAP.

Ah, and I am using a macbook air, Python 3 and Pygame.

Thanks for your time.




Aucun commentaire:

Enregistrer un commentaire