-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_game_play_test.go
66 lines (59 loc) · 1.29 KB
/
example_game_play_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package doric_test
import (
"log"
"math/rand"
"time"
"github.com/svera/doric"
)
func Example() {
cfg := doric.Config{
NumberTilesForNextLevel: 10,
InitialSpeed: 0.5,
SpeedIncrement: 0.25,
MaxSpeed: 13,
}
command := make(chan int)
well := doric.NewWell(doric.StandardWidth, doric.StandardHeight)
factory := func(n int) [3]int {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
return [3]int{
r.Intn(n) + 1,
r.Intn(n) + 1,
r.Intn(n) + 1,
}
}
// Start the game and return game events in the events channel
events, err := doric.Play(well, factory, cfg, command)
if err != nil {
log.Fatalf("%s\n", err.Error())
}
defer func() {
close(command)
// events channel will be closed when game is over
}()
// Update game every 16 ms ~ 60 hz
tick := time.Tick(16 * time.Millisecond)
// Game loop
for {
// Listen for game events and act accordingly
select {
case ev, open := <-events:
if !open {
// game over, do whatever
break
}
switch ev.(type) {
case doric.EventScored:
// Do whatever
case doric.EventUpdated:
// Do whatever
case doric.EventRenewed:
// Do whatever
}
case <-tick:
// Update screen, send commands to game through the
// command channel, etc.
}
}
}