func init() { if err := termbox.Init(); err != nil { panic(err) } termbox.SetCursor(0, 0) termbox.HideCursor() }
func (this *Game) Run() { err := tb.Init() if err != nil { panic(err) } defer tb.Close() tb.HideCursor() this.Draw() loop: for { switch ev := tb.PollEvent(); ev.Type { case tb.EventKey: switch ev.Key { case tb.KeyEsc: break loop } switch ev.Ch { case 'h': this.Player.Move(-1, 0) case 'j': this.Player.Move(0, 1) case 'k': this.Player.Move(0, -1) case 'l': this.Player.Move(1, 0) } } this.Draw() } }
func main() { termbox.Init() termbox.HideCursor() var p Player p = *NewPlayer() p.Draw() termbox.Flush() <-time.After(2000 * time.Millisecond) termbox.Close() os.Exit(0) }
func main() { if err := tb.Init(); err != nil { panic(err) } else { defer tb.Close() tb.HideCursor() } x, y := tb.Size() drawBoundingBox(0, x-1, 0, y-1) drawTypedBoundingBox(tb.ColorBlack, tb.ColorWhite)(x/3, 2*x/3, y/3, 2*y/3) fillInBox(x/3+1, 2*x/3-1, y/3+1, 2*y/3-1, tb.ColorBlack, tb.ColorWhite) tb.Flush() time.Sleep(3 * time.Second) }
func main() { rand.Seed(time.Now().UnixNano()) dim := dimensions{10, 10} ships := canonicalBattleship() attackField := makeScatteredField(dim, ships, adversary) attackOrigin := coord{0, 0} defendField := makeScatteredField(dim, ships, human) defendOrigin := coord{30, 0} printOrigin := coord{0, 30} termbox.Init() defer termbox.Close() termbox.HideCursor() termbox.Clear(termbox.ColorBlack, termbox.ColorBlack) buildLabels(attackField, attackOrigin) buildLabels(defendField, defendOrigin) // game loop var msg_us, msg_them string for attackField.shipsLeft() && defendField.shipsLeft() { buildInnerField(attackField, attackOrigin) buildInnerField(defendField, defendOrigin) termbox.Flush() msg_us = move(&attackField) termbox.Flush() msg_them = counterMove(&defendField) tbprint(printOrigin, msg_us, termbox.ColorWhite, termbox.ColorBlack) tbprint(printOrigin.down(1), msg_them, termbox.ColorWhite, termbox.ColorBlack) termbox.Flush() } if defendField.shipsLeft() { fmt.Println("You've won! Congratulations.") } else if attackField.shipsLeft() { fmt.Println("You've lost!") } }
/* ProcessEvent processes all events come from the control parent. If a control processes an event it should return true. If the method returns false it means that the control do not want or cannot process the event and the caller sends the event to the control parent */ func (e *EditField) ProcessEvent(event Event) bool { if !e.Active() || !e.Enabled() { return false } if event.Type == EventActivate && event.X == 0 { term.HideCursor() } if event.Type == EventKey && event.Key != term.KeyTab { if e.onKeyPress != nil { res := e.onKeyPress(event.Key) if res { return true } } switch event.Key { case term.KeyEnter: return false case term.KeySpace: e.insertRune(' ') return true case term.KeyBackspace: e.backspace() return true case term.KeyDelete: e.del() return true case term.KeyArrowLeft: e.charLeft() return true case term.KeyHome: e.home() return true case term.KeyEnd: e.end() return true case term.KeyCtrlR: if !e.readonly { e.Clear() } return true case term.KeyArrowRight: e.charRight() return true case term.KeyCtrlC: clipboard.WriteAll(e.Title()) return true case term.KeyCtrlV: if !e.readonly { s, _ := clipboard.ReadAll() e.SetTitle(s) e.end() } return true default: if event.Ch != 0 { e.insertRune(event.Ch) return true } } return false } return false }
func hideCursor() { // Probably a bug: Cursor must be set to valid position before hiding setCursor(PointZero) termbox.HideCursor() }