add a guide for making scenes

This commit introduces a way to work with scenes by introducing a scene
type and a function to move between it.
This commit is contained in:
Jason Wangsadinata 2018-07-19 06:58:33 +07:00
parent 7cff3ce3ae
commit 72e07a2c06
2 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,8 @@
# Scene
Created by [Jason Wangsadinata][jwangsadinata].
This is just a simple example on how to create scenes in [Pixel][pixel].
[jwangsadinata]: https://github.com/jwangsadinata
[pixel]: https://github.com/faiface/pixel

View File

@ -0,0 +1,60 @@
package main
import (
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
type scene int
const (
start scene = iota
menu
game
credits
end
)
func (s *scene) nextScene() {
*s = (*s + 1) % 5
}
func run() {
cfg := pixelgl.WindowConfig{
Title: "Click On The Screen!",
Bounds: pixel.R(0, 0, 1024, 768),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
s := start
for !win.Closed() {
switch s {
case start:
win.Clear(colornames.Lavender)
case menu:
win.Clear(colornames.Turquoise)
case game:
win.Clear(colornames.Lightyellow)
case credits:
win.Clear(colornames.Lightpink)
case end:
win.Clear(colornames.Sandybrown)
}
if win.JustPressed(pixelgl.MouseButtonLeft) {
s.nextScene()
}
win.Update()
}
}
func main() {
pixelgl.Run(run)
}