summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoop Kiefte <ikojba@gmail.com>2020-11-13 06:13:20 +0200
committerJoop Kiefte <ikojba@gmail.com>2020-11-13 06:13:20 +0200
commit26f65958fb10e82c6574848bbbd276e3e0be860e (patch)
tree535cb78cec731d2c11c670429b6005a1be16f4e3
parentc6a0a549b12cdb6318cbe9db1a8f3be8fba7f5a9 (diff)
working game loop, next: graphics
-rw-r--r--go.mod5
-rw-r--r--go.sum5
-rw-r--r--main.go185
3 files changed, 106 insertions, 89 deletions
diff --git a/go.mod b/go.mod
index 4c2a9cf..15216a5 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,7 @@ module git.kiefte.eu/lapingvino/gioco
go 1.15
-require gioui.org v0.0.0-20201112145235-3c739323cb4f
+require (
+ gioui.org v0.0.0-20201112145235-3c739323cb4f
+ git.kiefte.eu/lapingvino/clitris v1.1.1
+)
diff --git a/go.sum b/go.sum
index d2bb660..24e75fb 100644
--- a/go.sum
+++ b/go.sum
@@ -1,8 +1,11 @@
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20201112145235-3c739323cb4f h1:O1WtE27z2GqOJjE0Dl6k2qqts63tqoSwwoWyE7K3RLM=
gioui.org v0.0.0-20201112145235-3c739323cb4f/go.mod h1:Y+uS7hHMvku1Q+ooaoq6fYD5B2LGoT8JtFgvmYmRzTw=
+git.kiefte.eu/lapingvino/clitris v1.1.1 h1:Zeg9lWkYn2gO6W73oSSyGKs2NxepKRVV4+b6axXKG5I=
+git.kiefte.eu/lapingvino/clitris v1.1.1/go.mod h1:yIbR0s0L3njEyIGQJk6NL+RvQrsWeNmc1yc+dHcNPk8=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
+github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCvdOWbLqTHhJiYV414CURZJba6L8qA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -22,6 +25,8 @@ golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 h1:1/DFK4b7JH8DmkqhUk48onnSfrPzImPoVxuomtbT2nk=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200918174421-af09f7315aff h1:1CPUrky56AcgSpxz/KfgzQWzfG09u5YOL8MvPYBlrL8=
+golang.org/x/sys v0.0.0-20200918174421-af09f7315aff/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
diff --git a/main.go b/main.go
index 181e9e5..58b57f4 100644
--- a/main.go
+++ b/main.go
@@ -1,20 +1,22 @@
package main
import (
- // "git.kiefte.eu/clitris/tris"
- "image/color"
+ "git.kiefte.eu/lapingvino/clitris/tris"
+ // "image/color"
+ "fmt"
"gioui.org/app"
- "log"
- "os"
- "gioui.org/unit"
+ "gioui.org/font/gofont"
+ "gioui.org/io/key"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
- "gioui.org/text"
+ "gioui.org/unit"
+ "log"
+ "os"
+ "time"
+ // "gioui.org/text"
"gioui.org/widget/material"
-
- "gioui.org/font/gofont"
-// "math/rand"
+ "math/rand"
)
func main() {
@@ -31,9 +33,8 @@ func 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)
+ 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
@@ -44,72 +45,98 @@ func loop(w *app.Window) error {
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
+ var debug string
+ var ops op.Ops
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 e := <-w.Events():
+ switch e := e.(type) {
+ case system.DestroyEvent:
+ return e.Err
+ case key.Event:
+ if e.State == key.Press {
+ switch e.Name {
+ case key.NameUpArrow: // Up
+ debug = "up"
+ rot = (p.Rot + 1) % 4
+ case key.NameDownArrow: // Down
+ debug = "down"
+ y = p.Y + 1
+ score += 1
+ case key.NameRightArrow: // Right
+ debug = "right"
+ x = p.X + 1
+ case key.NameLeftArrow: // Left
+ debug = "left"
+ x = p.X - 1
+ case key.NameEscape, "q", "Q":
+ os.Exit(0)
+ 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 system.FrameEvent:
+ gtx := layout.NewContext(&ops, e)
+ level = linescleared/10 + 1
+ slevel := fmt.Sprintf("level %d", level)
+ sscore := fmt.Sprintf("score %d", score)
+ slines := fmt.Sprintf("lines %d", linescleared)
+ /*
+ 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)
+ */
+ inset := layout.UniformInset(unit.Dp(10))
+ inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
+ return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
+ layout.Rigid(func(gtx layout.Context) layout.Dimensions {
+ return material.H5(th, sscore).Layout(gtx)
+ }),
+ layout.Rigid(func(gtx layout.Context) layout.Dimensions {
+ return material.H5(th, slevel).Layout(gtx)
+ }),
+ layout.Rigid(func(gtx layout.Context) layout.Dimensions {
+ return material.H5(th, slines).Layout(gtx)
+ }),
+ layout.Rigid(func(gtx layout.Context) layout.Dimensions {
+ return material.H5(th, debug).Layout(gtx)
+ }),
+ layout.Rigid(func(gtx layout.Context) layout.Dimensions {
+ return material.H5(th, f.Add(p).String()).Layout(gtx)
+ }),
+ )
+ })
+ op.InvalidateOp{}.Add(gtx.Ops)
+ e.Frame(gtx.Ops)
+ }
case <-lev.C:
y = p.Y + 1
lev.Reset(time.Second * 100 / (100 * time.Duration(level)))
- default:
- if harddrop {
- y = p.Y + 1
- }
+ }
+ debug = "move"
+ 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
@@ -127,8 +154,7 @@ func loop(w *app.Window) error {
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
+ 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 {
@@ -136,25 +162,8 @@ func loop(w *app.Window) error {
}
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)
+ return fmt.Errorf("GAME OVER")
}
}
+ return nil
}