前提・実現したいこと
ProcessingのPythonモードで、迷路を作成しています。
rキーを押した際に、ランダムで迷路の壁が生成される仕組みになっています。
現状ではrキーを何回か押さなければなりませんが、一度押しただけで迷路内のマス(x、y)のうち、x、yがともに偶数であるマスがすべて壁になるまで壁を生成するようにしたいです。
while文の中身のヒントを教えてください。
該当のソースコード
Processing(Python)
1board_x = 0 2board_y = 0 3road_w = 0 4road_map = [] 5 6piece_x = 0 7piece_y = 0 8is_playing = False 9is_goal = False 10piece_size = 0 11play_time = 0 12 13dir_x = [1, 0, -1, 0] 14dir_y = [0, 1, 0, -1] 15 16def make_board(x, y, w): 17 global board_x, board_y 18 global road_w 19 global road_map 20 21 board_x = x+4 22 board_y = y+4 23 road_w = w 24 road_map = [[0 for i in range(board_y)] for j in range(board_x)] 25 26def init_maze(): 27 for x in range(0, board_x, 1): 28 for y in range(0, board_y, 1): 29 road_map[x][y] = 1 30 for x in range(3, board_x-3, 1): 31 for y in range(3, board_y-3, 1): 32 road_map[x][y] = 0 33 34 global piece_x, piece_y 35 global is_playing 36 global is_goal 37 global piece_size 38 global play_time 39 40 road_map[2][3] = 2 41 road_map[board_x-3][board_y-4] = 3 42 43 piece_x = 2 44 piece_y = 3 45 is_playing = False 46 is_goal = False 47 piece_size = 0.7 * road_w 48 play_time = 0 49 50def draw_maze(): 51 noStroke() 52 background(100) 53 54 for x in range(2, board_x-2, 1): 55 for y in range(2, board_y-2, 1): 56 if road_map[x][y] == 0: 57 fill(100, 0, 0) 58 elif road_map[x][y] == 1: 59 fill(0, 200, 0) 60 elif road_map[x][y] == 2: 61 fill(200, 200, 0) 62 elif road_map[x][y] == 3: 63 fill(200, 0, 200) 64 65 rect(road_w*x, road_w*y, road_w, road_w) 66 67def setup(): 68 size(800, 600) 69 make_board(13, 9, 46) 70 init_maze() 71 72def draw(): 73 draw_maze() 74 75 draw_piece() 76 77 if is_playing or is_goal : 78 draw_info() 79 80 check_finish() 81 82def keyPressed(): 83 global piece_x, piece_y 84 global is_playing 85 86 if key == 'a': 87 generate_maze_up_down() 88 elif key == 'k': 89 is_playing = True 90 elif key == 'i': 91 init_maze() 92 93 if is_playing : 94 if keyCode == UP and piece_y > 0 and road_map[piece_x][piece_y-1] != 1: 95 piece_y -= 1 96 if keyCode == RIGHT and piece_x < board_x-1 and road_map[piece_x+1][piece_y] != 1: 97 piece_x += 1 98 if keyCode == DOWN and piece_y < board_y-1 and road_map[piece_x][piece_y+1] != 1: 99 piece_y += 1 100 if keyCode == LEFT and piece_x > 0 and road_map[piece_x-1][piece_y] != 1: 101 piece_x -= 1 102 103 elif key == 'r': 104 generate_maze_random() 105 106def generate_maze_up_down(): 107 for x in range(4, board_x-3, 4): 108 for y in range(3, board_y-4): 109 road_map[x][y] = 1 110 for x in range(6, board_x-3, 4): 111 for y in range(board_y-4, 3, -1): 112 road_map[x][y] = 1 113 114def draw_piece(): 115 fill(0, 200, 0) 116 ellipse((piece_x+0.5)*road_w, (piece_y+0.5)*road_w, piece_size, piece_size) 117 118def draw_info(): 119 global play_time 120 if is_playing : 121 play_time += 1 122 123 textSize(30) 124 fill(255, 255, 0) 125 text("Time=" + str(play_time), 20, 30) 126 127def check_finish(): 128 global is_playing, is_goal 129 if road_map[piece_x][piece_y] == 3: 130 is_playing = False 131 is_goal = True 132 133def generate_maze_random(): 134 while road_map[x][y] != 1: 135 for x in range(2, board_x-2, 2): 136 for y in range(2, board_y-2, 2): 137 if road_map[x][y] == 1: 138 r = int(random(0, 3)) 139 dx = dir_x[r] 140 dy = dir_y[r] 141 if road_map[x+dx*2][y+dy*2] == 0: 142 road_map[x+dx][y+dy] = 1 143 road_map[x+dx*2][y+dy*2] =1
あなたの回答
tips
プレビュー