-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
196 lines (175 loc) · 2.91 KB
/
game.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"os"
"time"
)
type Game struct {
done *chan bool
ticker *time.Ticker
field Field
logs []string
level int
score int
}
func NewGame(done *chan bool) *Game {
game := Game{
done: done,
ticker: time.NewTicker(getSpeed(0)),
level: 0,
score: 0,
field: Field{},
}
game.field.Spawn()
game.field.Spawn()
go game.update()
go game.inputs()
return &game
}
func (g *Game) Draw() {
left := g.field.Display()
right := []string{
"# preview",
}
right = append(right, g.field.Preview()...)
right = append(right,
"",
"level: "+colored(5, fmt.Sprint(g.level)),
"score: "+colored(5, fmt.Sprint(g.score)),
)
// add logs
right = append(right, "", "# logs")
for _, l := range g.logs {
right = append(right, "- "+l)
}
for i, line := range left {
val := " " + line
if i < len(right) {
val += " " + right[i]
}
fmt.Println(val)
}
}
func (g *Game) Redraw() {
clear_lines(H + 2)
g.Draw()
}
func (g *Game) update() {
for {
select {
case <-g.ticker.C:
g.moveDown()
}
}
}
func (g *Game) log(msg string) {
if len(g.logs) >= 10 {
g.logs = append(g.logs[1:], msg)
} else {
g.logs = append(g.logs, msg)
}
}
func (g *Game) scoreLines(count int) {
switch count {
case 1:
g.score += 40
case 2:
g.score += 100
case 3:
g.score += 300
case 4:
g.score += 1200
}
g.log(fmt.Sprintf("%d lines dropped", count))
next := getLevel(g.score)
if next != g.level {
g.level = next
g.ticker.Reset(getSpeed(next))
g.log("level up")
}
}
func getLevel(score int) int {
return score / 200
}
func getSpeed(level int) time.Duration {
if level > 100 {
return 100 * time.Millisecond
}
p := float64(100-level) / 100
val := 900*p + 100
return time.Duration(val) * time.Millisecond
}
// ESC [
// 27 91 __
// - up 65
// - down 66
// - right 67
// - left 68
func (g *Game) inputs() {
var b []byte = make([]byte, 3)
for {
os.Stdin.Read(b)
if b[0] == 27 && b[1] == 91 {
switch b[2] {
case 65:
g.rotate()
case 66:
g.moveDown()
case 67:
g.moveRight()
case 68:
g.moveLeft()
}
}
}
}
func (g *Game) rotate() {
if g.field.Collision(0, 0, 1) {
return
}
g.field.Curr.R = nextRotation(g.field.Curr.R, 1)
g.Redraw()
}
func (g *Game) moveDown() {
if g.field.Collision(0, 1, 0) {
// place block
g.log("place block")
g.field.Place()
lines := g.field.DropLines()
if lines > 0 {
g.scoreLines(lines)
}
// Spawn next block
g.field.Spawn()
g.Redraw()
if g.field.Collision(0, 1, 0) {
g.End()
}
} else {
// move
g.log("move down")
g.field.Curr.Y += 1
g.Redraw()
}
}
func (g *Game) moveLeft() {
if g.field.Collision(-1, 0, 0) {
return
}
g.log("move left")
g.field.Curr.X -= 1
g.Redraw()
}
func (g *Game) moveRight() {
if g.field.Collision(1, 0, 0) {
return
}
g.log("move right")
g.field.Curr.X += 1
g.Redraw()
}
func (g *Game) End() {
// end game
fmt.Println("gg")
*g.done <- true
}