Created
August 16, 2015 16:18
-
-
Save bradfitz/8d2ca2dac07395c64e25 to your computer and use it in GitHub Desktop.
nope gophers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"image" | |
"image/color" | |
"image/png" | |
"log" | |
"math/rand" | |
"net" | |
"os" | |
"time" | |
gcolor "github.com/bthomson/go-color" | |
) | |
const ( | |
width = 160 | |
height = 96 | |
) | |
var magic = []byte{0} | |
type Gopher struct { | |
x, y int | |
xs int | |
flipped bool | |
} | |
func randomGopher() Gopher { | |
return Gopher{ | |
x: rand.Intn(width), | |
y: rand.Intn(30) - 15, | |
xs: rand.Intn(13) + 2, | |
flipped: rand.Intn(2) == 0, | |
} | |
} | |
func loadPNG(file string) *image.NRGBA { | |
f, err := os.Open(file) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer f.Close() | |
im, err := png.Decode(f) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return im.(*image.NRGBA) | |
} | |
func main() { | |
gopher := loadPNG("/Users/bradfitz/Downloads/gopher.png") | |
yup := loadPNG("/Users/bradfitz/Downloads/yup.png") | |
screen := image.NewRGBA(image.Rectangle{Max: image.Point{X: width, Y: height}}) | |
ua, err := net.ResolveUDPAddr("udp", "151.217.8.152:6073") | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("Addr: %#v", ua) | |
c, err := net.Dial("udp", "151.217.8.152:6073") | |
if err != nil { | |
log.Fatal(err) | |
} | |
uc := c.(*net.UDPConn) | |
uc.SetWriteBuffer(width*height*3 + 1) | |
var gophers []Gopher | |
for i := 0; i < 10; i++ { | |
gophers = append(gophers, randomGopher()) | |
} | |
frame := 0 | |
for { | |
frame++ | |
for y := 0; y < height; y++ { | |
h := float64((y+5*frame)%height) / float64(height) | |
rgb := gcolor.HSL{H: h, S: 1, L: 0.5}.ToRGB() | |
for x := 0; x < width; x++ { | |
screen.SetRGBA(x, y, color.RGBA{ | |
/* R: byte(rand.Intn(256)), | |
G: byte(rand.Intn(256)), | |
B: byte(rand.Intn(256)), | |
*/ | |
R: byte(rgb.R * 255), | |
G: byte(rgb.G * 255), | |
B: byte(rgb.B * 255), | |
}) | |
} | |
} | |
for i := range gophers { | |
g := &gophers[i] | |
for y := 0; y < height; y++ { | |
gopherWidth := gopher.Bounds().Max.X | |
yp := y + g.y | |
if yp < 0 || yp >= height { | |
continue | |
} | |
for x := 0; x < gopherWidth; x++ { | |
tx := x | |
if g.flipped { | |
tx = gopherWidth - x | |
} | |
c := gopher.NRGBAAt(tx, y) | |
if c.R != 0 || c.G != 0 || c.B != 0 { | |
screen.SetRGBA((x+g.x)%width, yp, color.RGBA{ | |
R: c.R, | |
G: c.G, | |
B: c.B, | |
}) | |
} | |
} | |
} | |
g.x += g.xs | |
g.flipped = !g.flipped | |
} | |
yupOn := (frame % 2) < 1 | |
if yupOn { | |
for y := 0; y < height; y++ { | |
for x := 0; x < width; x++ { | |
c := yup.NRGBAAt(x, y) | |
if c.A != 0 { | |
screen.SetRGBA(x, y, color.RGBA{ | |
R: c.R, | |
G: c.G, | |
B: c.B, | |
}) | |
} | |
} | |
} | |
} | |
var buf bytes.Buffer | |
buf.Write(magic) | |
for y := 0; y < height; y++ { | |
for x := 0; x < width; x++ { | |
c := screen.RGBAAt(x, y) | |
buf.WriteByte(c.R) | |
buf.WriteByte(c.G) | |
buf.WriteByte(c.B) | |
} | |
} | |
_, err := uc.Write(buf.Bytes()) | |
log.Printf("Write: %v", err) | |
time.Sleep(500 * time.Millisecond) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment