|
| 1 | +// 版权 @2023 life 作者。保留所有权利。 |
| 2 | + |
| 3 | +import ( |
| 4 | + "image" |
| 5 | + "image/color" |
| 6 | + "js/canvas" |
| 7 | + "math/rand" |
| 8 | +) |
| 9 | + |
| 10 | +global ( |
| 11 | + width :int = 0 |
| 12 | + height :int = 0 |
| 13 | + |
| 14 | + randSeed :int = 0 |
| 15 | + pausing :bool = false |
| 16 | + |
| 17 | + cells0 :*image.Gray = nil |
| 18 | + cells1 :*image.Gray = nil |
| 19 | + cellsRGBA :*image.RGBA = nil |
| 20 | + |
| 21 | + ctxCanvas :canvas.Context2D |
| 22 | +) |
| 23 | + |
| 24 | +func LifeInit(w, h, s: int) { |
| 25 | + width = w |
| 26 | + height = h |
| 27 | + randSeed = s |
| 28 | + |
| 29 | + cells0 = image.NewGray(w, h) |
| 30 | + cells1 = image.NewGray(w, h) |
| 31 | + cellsRGBA = image.NewRGBA(w, h) |
| 32 | + |
| 33 | + rand.Seed(i64(s)) |
| 34 | + for i := 0; i < width*height; i++ { |
| 35 | + if (rand.Int() % 2) != 0 { |
| 36 | + cells0.Pix[i] = color.Gray(1) |
| 37 | + } else { |
| 38 | + cells0.Pix[i] = color.Gray(0) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +type DIR struct { |
| 44 | + x: int |
| 45 | + y: int |
| 46 | +} |
| 47 | + |
| 48 | +global dirs = [...]DIR{ |
| 49 | + {-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}, |
| 50 | +} |
| 51 | + |
| 52 | +// 生命进化 |
| 53 | +func lifeEvolve { |
| 54 | + for y := 0; y < height; y++ { |
| 55 | + for x := 0; x < width; x++ { |
| 56 | + live_count := 0 |
| 57 | + for i := 0; i < 8; i++ { |
| 58 | + nx := (x + dirs[i].x + width) % width |
| 59 | + ny := (y + dirs[i].y + height) % height |
| 60 | + if cells0.Pix[ny*width+nx] != 0 { |
| 61 | + live_count++ |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if cells0.Pix[y*width+x] != 0 { |
| 66 | + switch live_count { |
| 67 | + case 2, 3: |
| 68 | + cells1.Pix[y*width+x] = color.Gray(1) |
| 69 | + default: |
| 70 | + cells1.Pix[y*width+x] = color.Gray(0) |
| 71 | + } |
| 72 | + } else { |
| 73 | + switch live_count { |
| 74 | + case 3: |
| 75 | + cells1.Pix[y*width+x] = color.Gray(1) |
| 76 | + default: |
| 77 | + cells1.Pix[y*width+x] = color.Gray(0) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // 交换 cells0 和 cells1 |
| 84 | + cells0, cells1 = cells1, cells0 |
| 85 | +} |
| 86 | + |
| 87 | +func LifeStep { |
| 88 | + if cellsRGBA == nil { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + if !pausing { |
| 93 | + lifeEvolve() |
| 94 | + } |
| 95 | + |
| 96 | + for x := 0; x < width; x++ { |
| 97 | + for y := 0; y < height; y++ { |
| 98 | + if cells0.Pix[y*width+x] != 0 { |
| 99 | + cellsRGBA.SetRGBA(x, y, 0xFF0000FF) |
| 100 | + } else { |
| 101 | + cellsRGBA.SetRGBA(x, y, 0xFFFFFFFF) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // 绘制到 Canvas |
| 107 | + ctxCanvas.PutImageData( |
| 108 | + raw(cellsRGBA.Pix), 0, 0, 0, 0, |
| 109 | + f32(width), f32(height), |
| 110 | + ) |
| 111 | +} |
| 112 | + |
| 113 | +func LifePausing { |
| 114 | + println("pausing:", pausing) |
| 115 | + pausing = !pausing |
| 116 | +} |
0 commit comments