aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go28
1 files changed, 19 insertions, 9 deletions
diff --git a/main.go b/main.go
index c212e5b..6bc8ce0 100644
--- a/main.go
+++ b/main.go
@@ -22,17 +22,23 @@ func GetKey() []byte {
}
func main() {
- rand.Seed(time.Now().UnixNano())
- var f tris.Field
- var floor, topout, harddrop bool
+ rand.Seed(time.Now().UnixNano()) // to start with truly random pieces
+ var f tris.Field // the playing field (10x20)
+ var floor, topout, harddrop bool // state machine variables to check special situations
+
+ // define outside of game loop to avoid accidental resets of position
+ // x, y and rot are the values calculated to feed to Move
+ // which then checks collisions and does the wall kicks
var x, y int
var rot tris.Rotation
- var key []byte
+
+ // init variable for pressed key - MUST be initialized 3 long to avoid crash, GetKey() does this
+ key := GetKey()
+
fmt.Print("\033[2J") // Clear screen
b := tris.NewBag()
b, p := b.Pick()
t := time.Tick(time.Second)
- key = GetKey()
for {
x, y, rot = p.X, p.Y, p.Rot
if !harddrop {
@@ -76,19 +82,23 @@ func main() {
y = p.Y + 1
}
}
- p, floor, topout = p.Move(f, rot, x, y)
+ p, floor, topout = p.Move(f, rot, x, y) // Check if the piece can move, then do it and communicate back for housekeeping
+
+ // Give some time before actually locking in to enable tucks
+ // This code runs when the time is over
if floor && p.Lock.Add(tris.LockDelay).Before(time.Now()) {
harddrop = false
var l int
f = f.Add(p)
- l, f = f.Lines()
+ l, f = f.Lines() // count and remove full lines
if l > 0 {
- fmt.Println(l, " lines removed!")
+ fmt.Println(l, "lines removed!")
time.Sleep(time.Second)
}
fmt.Print("\033[2J") // Clear screen
- b, p = b.Pick()
+ b, p = b.Pick() // ... and pick a new piece from the bag
}
+ // when not touching a piece or floor below yet, reset lock delay
if !floor {
p.Lock = time.Now()
}