Example #1
0
from maze_run.load_tiles import load_tiles, get_tile_rect
from maze_run.load_tiles import SIZE
from maze_run.generate_maze import create_maze
from maze_run.util import debug_print


def parse_grid(data):
    """Parses the string representation into a nested list"""
    return [list(row) for row in data.strip().split("\n")]


def draw_grid(data, tile_img, tiles):
    """Returns an image of a tile-based grid"""
    debug_print("drawing level", data)
    xsize = len(data[0]) * SIZE
    ysize = len(data) * SIZE
    img = Surface((xsize, ysize))
    for y, row in enumerate(data):
        for x, char in enumerate(row):
            rect = get_tile_rect(x, y)
            img.blit(tile_img, rect, tiles[char])
    return img


if __name__ == '__main__':
    tile_img, tiles = load_tiles()
    level = create_maze(12, 7)
    level = parse_grid(level)
    maze = draw_grid(level, tile_img, tiles)
    image.save(maze, 'maze.png')
Example #2
0

def move(level, direction):
    """Handles moves on the level"""
    oldx, oldy = get_player_pos(level)
    newx = oldx + direction[0]
    newy = oldy + direction[1]
    if level[newy][newx] == 'x':
        sys.exit(0)
    if level[newy][newx] == 'o':
        cratex = newx + direction[0]
        cratey = newy + direction[1]
        if level[cratey][cratex] in '. ':
            level[cratey][cratex] = 'o'
            level[newy][newx] = ' '
    if level[newy][newx] in '. ':
        level[oldy][oldx] = ' '
        level[newy][newx] = '*'


if __name__ == '__main__':
    tile_img, tiles = load_tiles()
    maze = create_maze(12, 7)
    maze = parse_grid(maze)
    maze[1][1] = '*'
    for i in range(100):
        direction = random.choice([LEFT, RIGHT, UP, DOWN])
        move(maze, direction)
    img = draw_grid(maze, tile_img, tiles)
    image.save(img, 'moved.png')
Example #3
0

def move(level, direction):
    """Handle move on the level."""
    oldx, oldy = get_player_pos(level)
    newx = oldx + direction[0]
    newy = oldy + direction[1]
    if level[newy][newx] == 'x':
        sys.exit(0)
    if level[newy][newx] == 'o':
        cratex = newx + direction[0]
        cratey = newy + direction[1]
        if level[cratey][cratex] in '. ':
            level[cratey][cratex] = 'o'
            level[newy][newx] = ' '
    if level[newy][newx] in '. ':
        level[oldy][oldx] = ' '
        level[newy][newx] = '*'


if __name__ == '__main__':
    tile_img, tiles = load_tiles()
    maze = create_maze(12, 7)
    maze = parse_grid(maze)
    maze[1][1] = '*'
    for i in range(100):
        direction = random.choice([LEFT, RIGHT, UP, DOWN])
        move(maze, direction)
    img = draw_grid(maze, tile_img, tiles)
    image.save(img, 'moved.png')
from maze_run.load_tiles import load_tiles
from maze_run.generate_maze import create_maze
from maze_run.event_loop import event_loop
from maze_run.draw_maze import draw_grid, parse_grid
from maze_run.moves import move, LEFT, RIGHT, UP, DOWN
from pygame import Rect
import pygame

# initialize display
pygame.init()
pygame.display.set_mode((800, 600))
display = pygame.display.get_surface()

# prepare the maze
maze = parse_grid(create_maze(12, 7))
maze[1][1] = '*'
maze[5][10] = 'x'

# draw the graphics
tile_img, tiles = load_tiles()
img = draw_grid(maze, tile_img, tiles)
display.blit(img, Rect((0, 0, 384, 224)), Rect((0, 0, 384, 224)))
pygame.display.update()

# start the game
DIRECTIONS = {276: LEFT, 275: RIGHT, 273: UP, 274: DOWN}


def handle_key(key):
    """Handles key events in the game"""
    direction = DIRECTIONS.get(key)