summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go160
1 files changed, 160 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..181e9e5
--- /dev/null
+++ b/main.go
@@ -0,0 +1,160 @@
+package main
+
+import (
+ // "git.kiefte.eu/clitris/tris"
+ "image/color"
+ "gioui.org/app"
+ "log"
+ "os"
+ "gioui.org/unit"
+ "gioui.org/io/system"
+ "gioui.org/layout"
+ "gioui.org/op"
+ "gioui.org/text"
+ "gioui.org/widget/material"
+
+ "gioui.org/font/gofont"
+// "math/rand"
+)
+
+func main() {
+ go func() {
+ w := app.NewWindow(app.Title("Gioco - another block stacking game"), app.Size(unit.Dp(500), unit.Dp(500)))
+ if err := loop(w); err != nil {
+ log.Fatal(err)
+ }
+ os.Exit(0)
+ }()
+
+ app.Main()
+}
+
+func loop(w *app.Window) error {
+ th := material.NewTheme(gofont.Collection())
+ /*
+ rand.Seed(time.Now().UnixNano()) // to start with truly random pieces
+ f := tris.NewField(20, 10) // 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 level, linescleared, score, dropfrom int
+
+ fmt.Print("\033[2J") // Clear screen
+ b := tris.NewBag()
+ b, p := b.Pick()
+ lev := time.NewTicker(time.Second)
+ level = 1
+ for {
+ x, y, rot = p.X, p.Y, p.Rot
+ level = linescleared/10 + 1
+ slevel := fmt.Sprintf("level %d", level)
+ sscore := fmt.Sprintf("score %d", score)
+ slines := fmt.Sprintf("lines %d", linescleared)
+ if !harddrop {
+ ppos(0, 0, "Hold (c)")
+ npos(3, 0, tris.HoldBox)
+ fpos(0, 10, f.Add(p))
+ var next tris.Field
+ b, next = b.Next(5)
+ npos(0, 34, next)
+ ppos(1, 42, sscore)
+ ppos(3, 42, slevel)
+ ppos(5, 42, slines)
+ key = GetKey(t)
+ }
+ switch key[0] {
+ case 27: // Escape, read the arrow key pressed
+ switch key[2] {
+ case 65: // Up
+ rot = (p.Rot + 1) % 4
+ case 66: // Down
+ y = p.Y + 1
+ score += 1
+ case 67: // Right
+ x = p.X + 1
+ case 68: // Left
+ x = p.X - 1
+ default:
+ ppos(22, 0, "...escape, escape!")
+ return
+ }
+ case 'x':
+ rot = (p.Rot + 1) % 4
+ case 'z':
+ rot = (p.Rot + 3) % 4
+ case 'c':
+ b, p = b.Swap(p)
+ continue
+ case ' ':
+ if !harddrop {
+ dropfrom = p.Y
+ }
+ harddrop = true
+ case 'q':
+ ppos(22, 0, "...that was exciting!")
+ return
+ case 'Q':
+ ppos(22, 0, "...never let an engineer pick the name of your software?")
+ return
+ }
+ select {
+ case <-lev.C:
+ y = p.Y + 1
+ lev.Reset(time.Second * 100 / (100 * time.Duration(level)))
+ default:
+ if harddrop {
+ y = p.Y + 1
+ }
+ }
+ 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()) {
+ if harddrop {
+ score += 2 * (p.Y - dropfrom)
+ harddrop = false
+ }
+ var l int
+ f = f.Add(p)
+ l, f = f.Lines() // count and remove full lines
+ if l > 0 {
+ linescleared += l
+ score += 40 * level * l * l
+ }
+ fmt.Print("\033[2J") // Clear screen
+ 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()
+ }
+
+ if topout {
+ ppos(4, 15, " GAME OVER ")
+ return
+ }
+ }
+ */
+ var ops op.Ops
+ for {
+ e := <-w.Events()
+ switch e := e.(type) {
+ case system.DestroyEvent:
+ return e.Err
+ case system.FrameEvent:
+ gtx := layout.NewContext(&ops, e)
+ l := material.H1(th, "Hello, Gio")
+ maroon := color.RGBA{127, 0, 0, 255}
+ l.Color = maroon
+ l.Alignment = text.Middle
+ l.Layout(gtx)
+ e.Frame(gtx.Ops)
+ }
+ }
+}