def draw(): """Displays the maze on the screen""" img = draw_grid(maze, tile_img, tiles) display.blit(img, Rect((0, 0, 384, 224)), Rect((0, 0, 384, 224))) pygame.display.update() def handle_key(key): """Handles key events in the game""" move(maze, DIRECTIONS.get(key)) draw() if __name__ == '__main__': # initialize display pygame.init() pygame.display.set_mode((800, 600)) display = pygame.display.get_surface() tile_img, tiles = load_tiles() # prepare the maze maze = parse_grid(create_maze(12, 7)) maze[1][1] = '*' maze[5][10] = 'x' # start the game draw() event_loop(handle_key)
#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')
from pygame import image, Rect, Surface from load_tiles import load_tiles, get_tile_rect, SIZE from generate_maze import create_maze from moves import move import random def parse_grid(data): """Parsuje reprezentacje tekstową do postaci zagnieżdżonej listy""" return [list(row) for row in data.strip().split("\n")] def draw_grid(data, tile_img, tiles): """Zwraca obraz siatki złożonej z płytek""" xs = len(data[0]) * SIZE ys = len(data) * SIZE img = Surface((xs, ys)) for y, row in enumerate(data): for x, char in enumerate(row): img.blit(tile_img, get_tile_rect(x, y), tiles[char]) return img 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, 'maze.png')
from load_tiles import load_tiles, get_tile_rect from load_tiles import SIZE from generate_maze import create_maze from 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')
def get_player_pos(level, player_char='*'): """Returns a (x, y) tuple of player char on the level""" for y, row in enumerate(level): for x, char in enumerate(row): if char == player_char: return x, y 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] != '#': 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')