-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
174 lines (156 loc) · 4.12 KB
/
main.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
package main
import (
"math/rand"
"flag"
"time"
sprite "github.com/pdevine/go-asciisprite"
tm "github.com/pdevine/go-asciisprite/termbox"
)
var allSprites sprite.SpriteGroup
var allBlocks []*TetrominoBlock
var background *Well
var Width int
var Height int
var CurrentLevel *LevelText
var TotalLines int
var activeTetromino *Tetromino
var nextScore int
var nextTetromino *Tetromino
var linesText *LinesText
var scoreText *ScoreText
var src rand.Source
const (
title = iota
levelselect
play
paused
gameover
cathedral
)
var gamemode = title
func main() {
// XXX - hack to make this work inside of a Docker container
time.Sleep(1000 * time.Millisecond)
var seed int64
debug := flag.Bool("debug", false, "")
flag.Int64Var(&seed, "seed", -1, "")
flag.Parse()
err := tm.Init()
if err != nil {
panic(err)
}
defer tm.Close()
Width, Height = tm.Size()
event_queue := make(chan tm.Event)
go func() {
for {
event_queue <- tm.PollEvent()
}
}()
if seed == -1 {
seed = time.Now().Unix()
}
src = rand.NewSource(seed)
background = NewWell()
background.TimeOut = 20
linesText = NewLinesText(0)
linesText.Y = background.Y - 2
linesText.X = background.X + 8
scoreText = NewScoreText()
helpText := NewHelpText()
t := NewTitle()
tstr := NewTitleString()
allSprites.Sprites = append(allSprites.Sprites, t)
allSprites.Sprites = append(allSprites.Sprites, tstr)
selector := NewSelector()
mainloop:
for {
start := time.Now()
tm.Clear(tm.ColorDefault, tm.ColorDefault)
select {
case ev := <-event_queue:
if ev.Type == tm.EventKey {
if ev.Key == tm.KeyEsc {
break mainloop
}
if gamemode == title {
gamemode = levelselect
allSprites.Sprites = append(allSprites.Sprites, selector)
} else if ev.Key == tm.KeyEnter {
if gamemode == levelselect {
allSprites.RemoveAll()
NewStats()
CurrentLevel = NewLevelText(selector.GetVal())
activeTetromino = getRandTetromino(src, background)
activeTetromino.PlaceInWell()
nextTetromino = getRandTetromino(src, background)
nextTetromino.X = 45
allSprites.Sprites = append(allSprites.Sprites, helpText)
allSprites.Sprites = append(allSprites.Sprites, linesText)
allSprites.Sprites = append(allSprites.Sprites, scoreText)
allSprites.Sprites = append(allSprites.Sprites, CurrentLevel)
allSprites.Sprites = append(allSprites.Sprites, activeTetromino)
allSprites.Sprites = append(allSprites.Sprites, nextTetromino)
gamemode = play
}
} else if ev.Key == tm.KeySpace {
if gamemode == levelselect {
selector.NextCostume()
if selector.currentLevel < 10 {
selector.SetLevel(selector.currentLevel + 10)
} else {
selector.SetLevel(selector.currentLevel - 10)
}
} else if gamemode == play {
activeTetromino.RotateClockwise()
}
} else if ev.Key == tm.KeyArrowLeft {
if gamemode == levelselect {
selector.MoveLeft()
} else if gamemode == play {
activeTetromino.MoveLeft()
}
} else if ev.Key == tm.KeyArrowRight {
if gamemode == levelselect {
selector.MoveRight()
} else if gamemode == play {
activeTetromino.MoveRight()
}
} else if ev.Key == tm.KeyArrowUp {
if gamemode == levelselect {
selector.MoveUp()
} else if gamemode == play {
activeTetromino.Drop()
}
} else if ev.Key == tm.KeyArrowDown {
if gamemode == levelselect {
selector.MoveDown()
} else if gamemode == play {
activeTetromino.Timer += levelFPG[CurrentLevel.Val] / 2
nextScore += 4
}
} else if ev.Ch == 'p' || ev.Ch == 'P' {
if gamemode == paused {
gamemode = play
} else if gamemode == play {
gamemode = paused
}
} else if *debug && ev.Ch == '$' {
scoreText.AddVal(10000)
}
} else if ev.Type == tm.EventResize {
Width = ev.Width
Height = ev.Height
}
default:
allSprites.Update()
if gamemode == play || gamemode == gameover || gamemode == paused {
background.Update()
background.Render()
}
allSprites.Render()
elapsed := time.Since(start)
time.Sleep(time.Second/60 - elapsed)
}
}
}