| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package main |
| 22 |
|
| 23 |
import ( |
| 24 |
"fmt" |
| 25 |
"math/rand" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/gdamore/tcell/v3" |
| 29 |
) |
| 30 |
|
| 31 |
func main() { |
| 32 |
screen, err := tcell.NewScreen() |
| 33 |
if err != nil { |
| 34 |
panic(err) |
| 35 |
} |
| 36 |
if err := screen.Init(); err != nil { |
| 37 |
panic(err) |
| 38 |
} |
| 39 |
|
| 40 |
var frames int |
| 41 |
|
| 42 |
type cell struct { |
| 43 |
c string |
| 44 |
style tcell.Style |
| 45 |
} |
| 46 |
|
| 47 |
width, height := screen.Size() |
| 48 |
glyphs := []string{"@", "#", "&", "*", "=", "%", "Z", "A"} |
| 49 |
attrs := []tcell.AttrMask{tcell.AttrBold, tcell.AttrReverse, tcell.AttrItalic, tcell.AttrNone} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
var patterns [][][]*cell |
| 54 |
for i := 0; i < 100; i++ { |
| 55 |
pattern := make([][]*cell, height) |
| 56 |
for h := 0; h < height; h++ { |
| 57 |
row := make([]*cell, width) |
| 58 |
for w := 0; w < width; w++ { |
| 59 |
rF := int32(rand.Int() % 256) |
| 60 |
gF := int32(rand.Int() % 256) |
| 61 |
bF := int32(rand.Int() % 256) |
| 62 |
rB := int32(rand.Int() % 256) |
| 63 |
gB := int32(rand.Int() % 256) |
| 64 |
bB := int32(rand.Int() % 256) |
| 65 |
|
| 66 |
row[w] = &cell{ |
| 67 |
c: glyphs[rand.Int()%len(glyphs)], |
| 68 |
style: tcell.StyleDefault. |
| 69 |
Attributes(attrs[rand.Int()%len(attrs)]). |
| 70 |
Background(tcell.NewRGBColor(rB, gB, bB)). |
| 71 |
Foreground(tcell.NewRGBColor(rF, gF, bF)), |
| 72 |
} |
| 73 |
} |
| 74 |
pattern[h] = row |
| 75 |
} |
| 76 |
patterns = append(patterns, pattern) |
| 77 |
} |
| 78 |
|
| 79 |
startTime := time.Now() |
| 80 |
loop: |
| 81 |
for { |
| 82 |
select { |
| 83 |
case event := <-screen.EventQ(): |
| 84 |
if event, ok := event.(*tcell.EventKey); ok { |
| 85 |
if event.Key() == tcell.KeyCtrlC || event.Key() == tcell.KeyESC { |
| 86 |
break loop |
| 87 |
} |
| 88 |
} |
| 89 |
default: |
| 90 |
} |
| 91 |
pattern := patterns[frames%len(patterns)] |
| 92 |
for h := 0; h < height; h++ { |
| 93 |
for w := 0; w < width; w++ { |
| 94 |
c := pattern[h][w] |
| 95 |
screen.Put(w, h, c.c, c.style) |
| 96 |
} |
| 97 |
} |
| 98 |
screen.Show() |
| 99 |
frames++ |
| 100 |
} |
| 101 |
duration := time.Since(startTime) |
| 102 |
screen.Fini() |
| 103 |
fps := int(float64(frames) / duration.Seconds()) |
| 104 |
fmt.Println("FPS:", fps) |
| 105 |
} |