Skip to content

Instantly share code, notes, and snippets.

@faiface
Created May 4, 2017 14:44
Show Gist options
  • Save faiface/bd015d95df485be1b0ae8e7dd5adfad6 to your computer and use it in GitHub Desktop.
Save faiface/bd015d95df485be1b0ae8e7dd5adfad6 to your computer and use it in GitHub Desktop.
Replace "Pacifico.ttf" with an actual TTF file path
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"time"
_ "image/jpeg"
_ "image/png"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"github.com/golang/freetype/truetype"
"golang.org/x/image/colornames"
"golang.org/x/image/font"
)
func loadTTF(path string, options *truetype.Options) (font.Face, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
ttf, err := truetype.Parse(b)
if err != nil {
return nil, err
}
return truetype.NewFace(ttf, options), nil
}
func testBig() {
cfg := pixelgl.WindowConfig{
Title: "Pixel Rocks!",
Bounds: pixel.R(0, 0, 1024, 768),
Resizable: true,
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
face, err := loadTTF("Pacifico.ttf", &truetype.Options{
Size: 72,
})
if err != nil {
panic(err)
}
txt := text.New(face, text.ASCII)
txt.Orig = pixel.Y(win.Bounds().H())
txt.Color(colornames.Black)
txt.LineHeight(1.5 * txt.Atlas().LineHeight())
frames := 0
second := time.Tick(time.Second)
for !win.Closed() {
if win.JustPressed(pixelgl.KeySpace) {
txt.Clear()
txt.Dot = txt.Orig
for txt.Dot.Y() > -22 {
for txt.Dot.X() < win.Bounds().W() {
fmt.Fprint(txt, string(text.ASCII[rand.Intn(len(text.ASCII))]))
}
fmt.Fprintln(txt)
}
}
win.Clear(colornames.White)
txt.Draw(win)
win.Update()
frames++
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
default:
}
}
}
func main() {
pixelgl.Run(testBig)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment