diff options
author | Joop Kiefte <ikojba@gmail.com> | 2020-09-12 16:37:57 +0200 |
---|---|---|
committer | Joop Kiefte <ikojba@gmail.com> | 2020-09-12 16:37:57 +0200 |
commit | 38293b6d5df68eff2794576bb64f82c2715429b6 (patch) | |
tree | c6d839a2a055be9b4045fa8acc3d426ae74580fa /main.go | |
parent | bd80439d9620f02b124b274cd79b8c8d30b69016 (diff) |
Add more comments
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 28 |
1 files changed, 19 insertions, 9 deletions
@@ -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() } |