1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
import random import pygame
pygame.init() size = width, height = 1024, 768 screen = pygame.display.set_mode(size) pygame.display.set_caption('火苗999℃')
diamond_color_size = 8 COLOR_RED, COLOR_BLUE, COLOR_GREEN, COLOR_YELLOW, COLOR_BLACK, COLOR_GREY, COLOR_GOLDEN, COLOR_NO_DIAMOND = list(range( diamond_color_size)) COLOR = { COLOR_RED: (255, 0, 0), COLOR_BLUE: (0, 0, 255), COLOR_GREEN: (0, 255, 0), COLOR_YELLOW: (255, 255, 0), COLOR_BLACK: (0, 0, 0), COLOR_GREY: (250, 240, 230), COLOR_GOLDEN : (255,215,0), COLOR_NO_DIAMOND: (160, 160, 160), }
DIAMOND_LENGTH = 10
DIAMOND_SIZE = (DIAMOND_LENGTH, DIAMOND_LENGTH)
DIAMOND=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND.fill(COLOR[COLOR_BLUE])
DIAMOND_GREEN=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_GREEN.fill(COLOR[COLOR_GREEN])
DIAMOND_RED=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_RED.fill(COLOR[COLOR_RED])
DIAMOND_YELLOW=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_YELLOW.fill(COLOR[COLOR_YELLOW])
DIAMOND_GRAY=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_GRAY.fill(COLOR[COLOR_GREY])
DIAMOND_BLACK=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_BLACK.fill(COLOR[COLOR_BLACK])
DIAMOND_BLUE=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_BLUE.fill(COLOR[COLOR_BLUE])
DIAMOND_O=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_O.fill((255,128,0)) DIAMOND_G=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_G.fill((0,102,51)) DIAMOND_R=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_R.fill((102,0,0))
use_font = pygame.font.Font("FONT.TTF", 16)
background=pygame.surface.Surface(size).convert() background.fill(COLOR[COLOR_NO_DIAMOND])
mini_font = pygame.font.Font("FONT.TTF", 8) DIAMOND_U=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_U.fill(COLOR[COLOR_GREEN]) surface_u = mini_font.render("U", True, COLOR[COLOR_BLACK], COLOR[COLOR_GREEN]) DIAMOND_U.blit(surface_u, (0, 0))
DIAMOND_D=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_D.fill(COLOR[COLOR_GREEN]) surface_d = mini_font.render("D", True, COLOR[COLOR_BLACK], COLOR[COLOR_GREEN]) DIAMOND_D.blit(surface_d, (0, 0))
DIAMOND_UD=pygame.surface.Surface(DIAMOND_SIZE).convert() DIAMOND_UD.fill(COLOR[COLOR_GREEN]) surface_ud = mini_font.render("UD", True, COLOR[COLOR_BLACK], COLOR[COLOR_GREEN]) DIAMOND_UD.blit(surface_ud, (0, 0))
clock = pygame.time.Clock()
NOWALL = 0 WALL = 1 STAIRS_U = 2 STAIRS_D = 4 STAIRS_UD = 6
def depth_maze_demo(levels, rows, cols): if 0 == rows % 2: rows+=1 if 0 == cols % 2: cols+=1 wall=[[[ WALL for i in range(cols)]for i in range(rows)]for i in range(levels)] l=0 r=1 c=1 history = [(l, r, c)] while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if history: if wall[l][r][c] == WALL: wall[l][r][c] = NOWALL check = [] if c > 1 and wall[l][r][c-2] == WALL: check.append('L') if r > 1 and wall[l][r-2][c] == WALL: check.append('F') if c < cols-2 and wall[l][r][c+2] == WALL: check.append('R') if r < rows-2 and wall[l][r+2][c] == WALL: check.append('B') if l < levels-1 and wall[l+1][r][c] == WALL: check.append('U') if l > 0 and wall[l-1][r][c] == WALL: check.append('D') if len(check): history.append((l, r, c)) move_direction = random.choice(check) if move_direction == 'L': wall[l][r][c-1] = NOWALL wall[l][r][c-2] = NOWALL c=c-2 if move_direction == 'F': wall[l][r-1][c] = NOWALL wall[l][r-2][c] = NOWALL r=r-2 if move_direction == 'R': wall[l][r][c+1] = NOWALL wall[l][r][c+2] = NOWALL c=c+2 if move_direction == 'B': wall[l][r+1][c] = NOWALL wall[l][r+2][c] = NOWALL r=r+2 if move_direction == 'U': if wall[l][r][c] == STAIRS_D: wall[l][r][c] = STAIRS_UD else: wall[l][r][c] = STAIRS_U if wall[l+1][r][c] == STAIRS_U: wall[l+1][r][c] = STAIRS_UD else: wall[l+1][r][c] = STAIRS_D l=l+1 if move_direction == 'D': if wall[l][r][c] == STAIRS_U: wall[l][r][c] = STAIRS_UD else: wall[l][r][c] = STAIRS_D if wall[l-1][r][c] == STAIRS_D: wall[l-1][r][c] = STAIRS_UD else: wall[l-1][r][c] = STAIRS_U l=l-1 else: l, r, c = history.pop() screen.blit(background, (0, 0)) for z in range(levels): for x in range(cols): for y in range(rows): a = z % 2 b = z // 2 px, py = 1 + x * DIAMOND_SIZE[0] + a * (cols*DIAMOND_LENGTH+10), 1 + y * DIAMOND_SIZE[1] + b*(rows*DIAMOND_LENGTH+10) s = (z)%3 if 0 == s: color_c = DIAMOND_G color_u = DIAMOND_O color_d = DIAMOND_R color_ud = DIAMOND_BLUE elif 1 == s: color_c = DIAMOND_O color_u = DIAMOND_R color_d = DIAMOND_G color_ud = DIAMOND_BLUE else: color_c = DIAMOND_R color_u = DIAMOND_G color_d = DIAMOND_O color_ud = DIAMOND_BLUE if NOWALL == wall[z][y][x]: screen.blit(color_c, (px, py)) elif WALL == wall[z][y][x]: screen.blit(DIAMOND_GRAY, (px, py)) elif STAIRS_U == wall[z][y][x]: screen.blit(color_u, (px, py)) elif STAIRS_D == wall[z][y][x]: screen.blit(color_d, (px, py)) elif STAIRS_UD == wall[z][y][x]: screen.blit(color_ud, (px, py)) else: screen.blit(DIAMOND_YELLOW, (px, py)) if history: a = l % 2 b = l // 2 px, py = 1 + c * DIAMOND_SIZE[0] + a * (cols*DIAMOND_LENGTH+10), 1 + r * DIAMOND_SIZE[1] + b*(rows*DIAMOND_LENGTH+10) screen.blit(DIAMOND_RED, (px, py)) if not history: score_surface = use_font.render("生成完成!", True, COLOR[COLOR_BLACK], COLOR[COLOR_GREY]) screen.blit(score_surface, (20, ((levels+ 1) // 2 )*(rows*DIAMOND_LENGTH+10))) time_passed = clock.tick(30)
pygame.display.update() return
if __name__ == "__main__": '''main''' depth_maze_demo(3, 21, 31)
|