func genStarfield(pl, pc int) *gc.Pad { pad, err := gc.NewPad(pl, pc) if err != nil { log.Fatal(err) } stars := int(float64(pc*pl) * density) planets := int(float64(pc*pl) * planet_density) for i := 0; i < stars; i++ { y, x := rand.Intn(pl), rand.Intn(pc) c := int16(rand.Intn(4) + 1) pad.AttrOn(gc.A_BOLD | gc.ColorPair(c)) pad.MovePrint(y, x, ".") pad.AttrOff(gc.A_BOLD | gc.ColorPair(c)) } for i := 0; i < planets; i++ { y, x := rand.Intn(pl), rand.Intn(pc) c := int16(rand.Intn(2) + 5) pad.ColorOn(c) if i%2 == 0 { pad.MoveAddChar(y, x, 'O') } pad.MoveAddChar(y, x, 'o') pad.ColorOff(c) } return pad }
func (h *SmallHouse) GetColor() gc.Char { if !h.Dead { return gc.ColorPair(3) } else { return gc.ColorPair(6) | gc.A_BOLD } }
func (v *CursesView) printState(s model.State, showShips bool, inverted bool) { if v.colored { col := int16(s + 1) if !showShips && s == model.Ship { col = int16(model.Sea + 1) } if inverted { // number of colors in colorMap col += 6 } v.win.AttrOn(cur.ColorPair(col)) defer v.win.AttrOff(cur.ColorPair(col)) } switch s { case model.Sea: v.win.Print(".") case model.Ship: if showShips { v.win.Print("#") } else { v.win.Print(".") } case model.Shot: v.win.Print("*") case model.Injured, model.Killed: v.win.Print("X") default: panic(fmt.Errorf("Unhandled state: %v", s)) } }
func InitNcurseUi() (ui *NcurseUi) { ui = &NcurseUi{} var err error ui.contents, err = goncurses.Init() HandleErr(err) y, x := ui.contents.MaxYX() ui.initColors() ui.contents.Resize(y-1, x-MENU_WIDTH) ui.contents.MoveWindow(1, MENU_WIDTH) ui.contents.Keypad(true) ui.titleBar, err = goncurses.NewWindow(1, x, 0, 0) HandleErr(err) ui.menuBar, err = goncurses.NewWindow(y-1, MENU_WIDTH, 1, 0) HandleErr(err) ui.menuBar.Border(' ', '|', ' ', ' ', ' ', '|', ' ', ' ') ui.titleBar.ColorOn(10) ui.titleBar.SetBackground(goncurses.Char('.') | goncurses.ColorPair(10)) return }
//sets up the initial configuration of curses. Keeps code in main cleaner. func configCurses(stdscr *gc.Window) { if !gc.HasColors() { log.Fatal("Example requires a colour capable terminal") } if err := gc.StartColor(); err != nil { log.Fatal("Starting Colors failed:", err) } gc.Echo(false) if err := gc.InitPair(1, gc.C_RED, gc.C_BLACK); err != nil { log.Fatal("InitPair failed: ", err) } gc.InitPair(2, gc.C_BLUE, gc.C_BLACK) gc.InitPair(3, gc.C_GREEN, gc.C_BLACK) gc.InitPair(4, gc.C_YELLOW, gc.C_BLACK) gc.InitPair(5, gc.C_CYAN, gc.C_BLACK) gc.InitPair(6, gc.C_MAGENTA, gc.C_WHITE) gc.InitPair(7, gc.C_MAGENTA, gc.C_BLACK) //set background color to black stdscr.SetBackground(gc.Char(' ') | gc.ColorPair(0)) stdscr.Keypad(true) gc.Cursor(1) gc.CBreak(true) stdscr.Clear() }
func newBullet(y, x int) *Bullet { w, err := gc.NewWindow(1, 1, y, x) if err != nil { log.Println("newBullet:", err) } w.AttrOn(gc.A_BOLD | gc.ColorPair(4)) w.Print("-") return &Bullet{w, true} }
func main() { stdscr, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() // HasColors can be used to determine whether the current terminal // has the capability of using colours. You could then chose whether or // not to use some other mechanism, like using A_REVERSE, instead if !gc.HasColors() { log.Fatal("Example requires a colour capable terminal") } // Must be called after Init but before using any colour related functions if err := gc.StartColor(); err != nil { log.Fatal(err) } gc.Echo(false) // Initialize a colour pair. Should only fail if an improper pair value // is given if err := gc.InitPair(1, gc.C_RED, gc.C_WHITE); err != nil { log.Fatal("InitPair failed: ", err) } gc.InitPair(2, gc.C_BLACK, gc.C_CYAN) stdscr.Println("Type any key to proceed and again to exit") // An example of trying to set an invalid color pair err = gc.InitPair(-1, gc.C_BLACK, gc.C_CYAN) stdscr.Println("An intentional error:", err) stdscr.Keypad(true) stdscr.MovePrint(12, 30, "Hello, World!!!") stdscr.Refresh() stdscr.GetChar() // Note that background doesn't just accept colours but will fill // any blank positions with the supplied character too. Note that newly // added text with spaces in it will have the blanks converted to the fill // character, if given stdscr.SetBackground(gc.Char('*') | gc.ColorPair(2)) // ColorOn/Off is a shortcut to calling AttrOn/Off(gc.ColorPair(pair)) stdscr.ColorOn(1) stdscr.MovePrint(13, 30, "Hello, World in Color!!!") stdscr.ColorOff(1) stdscr.Refresh() stdscr.GetChar() }
func (ui *NcurseUi) PrintSubmissions(submissions []*geddit.Submission) (last string) { freeMenu(ui.menu) ui.titleBar.Printf("Frontpage") ui.titleBar.Refresh() ui.contents.Clear() menuItems := make([]*goncurses.MenuItem, len(submissions)) menuMap = make(map[int]*menuItem) for i, sub := range submissions { //fmt.Sprintf("%d: Title: %s\n Author: %s, Subreddit: %s\n Votes: %d\n\n", i, sub.Title, sub.Author, sub.Subreddit, sub.Score) menuMap[i] = &menuItem{ id: sub.FullID, } item, err := goncurses.NewItem(fmt.Sprintf("%d", i), "") HandleErr(err) ui.contents.ColorOn(1) ui.Println(sub.Title) if sub.Score > 0 { ui.contents.ColorOn(3) } else { ui.contents.ColorOn(2) } ui.Println(fmt.Sprintf("[%5d]", sub.Score)) ui.contents.ColorOn(4) ui.Println(sub.Subreddit + " - " + sub.Author) ui.contents.ColorOn(1) menuItems[i] = item last = sub.FullID } if len(menuItems) > 0 { var err error ui.menu, err = goncurses.NewMenu(menuItems) HandleErr(err) ui.menu.SetSpacing(0, 3, 0) ui.menu.SetBackground(goncurses.ColorPair(20)) ui.menu.SetWindow(ui.menuBar) ui.menu.Post() } ui.menuBar.Refresh() ui.contents.Refresh() return }
func createWindow(h, w, y, x, bgColor int, borders bool) func() { win, err := gc.NewWindow(h, w, y, x) if err != nil { log.Fatal(err) } if bgColor != 0 { win.SetBackground(gc.Char(' ') | gc.ColorPair(int16(bgColor))) } if borders { win.Box(gc.ACS_VLINE, gc.ACS_HLINE) } win.NoutRefresh() return func() { win.Erase() win.SetBackground(gc.Char(' ')) win.NoutRefresh() win.Delete() } }
func main() { stdscr, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() gc.StartColor() gc.Raw(true) gc.Echo(false) gc.Cursor(0) stdscr.Keypad(true) gc.InitPair(1, gc.C_RED, gc.C_BLACK) gc.InitPair(2, gc.C_GREEN, gc.C_BLACK) gc.InitPair(3, gc.C_MAGENTA, gc.C_BLACK) // build the menu items menu_items := []string{ "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", "Exit"} items := make([]*gc.MenuItem, len(menu_items)) for i, val := range menu_items { items[i], _ = gc.NewItem(val, "") defer items[i].Free() if i == 2 || i == 4 { items[i].Selectable(false) } } // create the menu menu, _ := gc.NewMenu(items) defer menu.Free() y, _ := stdscr.MaxYX() stdscr.MovePrint(y-3, 0, "Use up/down arrows to move; 'q' to exit") stdscr.Refresh() menu.SetForeground(gc.ColorPair(1) | gc.A_REVERSE) menu.SetBackground(gc.ColorPair(2) | gc.A_BOLD) menu.Grey(gc.ColorPair(3) | gc.A_BOLD) menu.Post() defer menu.UnPost() for { gc.Update() ch := stdscr.GetChar() switch ch { case ' ': menu.Driver(gc.REQ_TOGGLE) case 'q': return case gc.KEY_RETURN: stdscr.Move(20, 0) stdscr.ClearToEOL() stdscr.Printf("Item selected is: %s", menu.Current(nil).Name()) menu.PositionCursor() default: menu.Driver(gc.DriverActions[ch]) } } }
func main() { stdscr, _ := gc.Init() defer gc.End() gc.Echo(false) gc.CBreak(true) gc.StartColor() stdscr.Keypad(true) gc.InitPair(1, gc.C_WHITE, gc.C_BLUE) gc.InitPair(2, gc.C_YELLOW, gc.C_BLUE) fields := make([]*gc.Field, 2) fields[0], _ = gc.NewField(1, 10, 4, 18, 0, 0) defer fields[0].Free() fields[0].SetForeground(gc.ColorPair(1)) fields[0].SetBackground(gc.ColorPair(2) | gc.A_UNDERLINE | gc.A_BOLD) fields[0].SetOptionsOff(gc.FO_AUTOSKIP) fields[1], _ = gc.NewField(1, 10, 6, 18, 0, 0) defer fields[1].Free() fields[1].SetForeground(gc.ColorPair(1)) fields[1].SetBackground(gc.A_UNDERLINE) fields[1].SetOptionsOff(gc.FO_AUTOSKIP) fields[1].SetPad('*') form, _ := gc.NewForm(fields) form.Post() defer form.UnPost() defer form.Free() stdscr.Refresh() fields[0].SetBuffer("Buffer 0") stdscr.AttrOn(gc.ColorPair(2) | gc.A_BOLD) stdscr.MovePrint(4, 10, "Value 1:") stdscr.AttrOff(gc.ColorPair(2) | gc.A_BOLD) stdscr.MovePrint(6, 10, "Value 2:") stdscr.Refresh() form.Driver(gc.REQ_FIRST_FIELD) ch := stdscr.GetChar() for ch != 'q' { switch ch { case gc.KEY_DOWN, gc.KEY_TAB: form.Driver(gc.REQ_NEXT_FIELD) form.Driver(gc.REQ_END_LINE) case gc.KEY_UP: form.Driver(gc.REQ_PREV_FIELD) form.Driver(gc.REQ_END_LINE) case gc.KEY_BACKSPACE: form.Driver(gc.REQ_CLR_FIELD) default: form.Driver(ch) } ch = stdscr.GetChar() } stdscr.MovePrint(20, 0, fields[1].Buffer()) stdscr.GetChar() }
func main() { stdscr, err := gc.Init() if err != nil { log.Fatal("init", err) } defer gc.End() if err := gc.StartColor(); err != nil { log.Fatal(err) } lines, cols := stdscr.MaxYX() gc.Raw(true) // turn on raw "uncooked" input gc.Echo(false) // turn echoing of typed characters off gc.Cursor(0) // hide cursor stdscr.Timeout(50) stdscr.Keypad(true) // allow keypad input gc.InitPair(1, gc.C_YELLOW, gc.C_BLACK) gc.InitPair(2, gc.C_RED, gc.C_BLACK) gc.InitPair(3, gc.C_GREEN, gc.C_BLACK) gc.InitPair(4, gc.C_BLUE, gc.C_BLACK) gc.InitPair(5, gc.C_WHITE, gc.C_BLACK) gc.InitPair(6, gc.C_BLACK, gc.C_BLACK) gc.InitPair(7, gc.C_MAGENTA, gc.C_BLACK) stdscr.SetBackground(gc.ColorPair(3)) rand.Seed(time.Now().UTC().UnixNano()) state := State{Money: 150} var build *Thing things := make([]Thing, 0, 20) var toUpdate int64 = 0 lastTimestamp := int64(time.Nanosecond) * time.Now().UTC().UnixNano() / int64(time.Millisecond) frames := make(map[string][][]string) count := rand.Intn(15) + 25 for i := 0; i < count; i++ { for { var kind Kind var pop int r := rand.Intn(2) switch r { case 0: kind = &SmallHouse{Frames: &frames} pop = 10 case 1: kind = &House{Frames: &frames} pop = 40 } house := &Thing{2 + rand.Intn(cols-8), 2 + rand.Intn(lines-8), kind} overlap := false for _, thing := range things { if house.Overlap(&thing) { overlap = true break } } if !overlap { things = append(things, *house) state.Population += pop break } } } var overlap bool var price float32 showInfo := true for { now := int64(time.Nanosecond) * time.Now().UTC().UnixNano() / int64(time.Millisecond) delta := now - lastTimestamp lastTimestamp = now toUpdate -= delta c := stdscr.GetChar() switch c { case 'q': break case 'a': if build != nil && build.X > 0 { build.X-- } case 'd': if build != nil && build.X+build.GetSize().X < cols { build.X++ } case 'w': if build != nil && build.Y > 0 { build.Y-- } case 's': if build != nil && build.Y+build.GetSize().Y < lines { build.Y++ } case ' ': if build != nil && !overlap && state.Money >= price { things = append(things, *build) state.Money -= price build = nil } case '1': if state.Money >= 100 { build = &Thing{cols / 2, lines / 2, &Shop{Frames: &frames}} price = 100 } case '2': if state.Money >= 600 { build = &Thing{cols / 2, lines / 2, &Factory{Frames: &frames}} price = 600 } case '3': if state.Money >= 1100 { build = &Thing{cols / 2, lines / 2, &BigFactory{Frames: &frames}} price = 1100 } case '4': if state.Money >= 2000 { build = &Thing{cols / 2, lines / 2, &Office{Frames: &frames}} price = 2000 } } if c == 'q' { break } stdscr.Erase() if state.Population == 0 { stdscr.AttrOn(gc.ColorPair(3) | gc.A_BOLD) stdscr.MovePrint(lines/2, cols/2-4, "You won!") continue } if showInfo && c != 0 { showInfo = false } if showInfo { info := ` __ __ _____ _ _ _ | \/ | / ____| | | | | | | \ / |_ __ | | __ ___ | | __| | |__ ___ _ __ __ _ | |\/| | '__| | | |_ |/ _ \| |/ _ | '_ \ / _ \ '__/ _ | | | | | |_ | |__| | (_) | | (_| | |_) | __/ | | (_| | |_| |_|_(_) \_____|\___/|_|\__,_|_.__/ \___|_| \__, | __/ | |___/ You are Mr. Goldberg, a famous and well respected businessman. You're looking to expand your business to a new city. It looks promising, but has one serious problem - too many people living in it, getting in the way! Maybe if you build some factories near their homes they will eventually move out and stop bothering you? Instructions: * earn money to build stuff * pollution will cause people to move out * you win when city population reaches 0! * 1/2/3/4 to choose building, WASD to move, SPACE to build it, Q to quit [ any key to start ] ` stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD) for i, s := range strings.Split(info, "\n") { stdscr.MovePrint(lines/2+i-16, cols/2-30, strings.Trim(s, "\t")) } continue } overlap = false if toUpdate <= 0 { state.MoneyDelta = 0 state.PollutionDelta = 0 } for _, thing := range things { stdscr.AttrOn(thing.GetColor()) thing.Draw(stdscr) stdscr.AttrOff(thing.GetColor()) if toUpdate <= 0 { thing.Update(&state) } if build != nil && build.Overlap(&thing) { overlap = true } } if build != nil { if overlap { stdscr.AttrOn(gc.ColorPair(2)) } else { stdscr.AttrOn(gc.ColorPair(7)) } build.Draw(stdscr) } if toUpdate <= 0 { toUpdate = 1000 } stdscr.AttrOn(gc.ColorPair(1) | gc.A_BOLD) stdscr.MovePrintf(1, 1, "Cash: $%.2f (+$%.2f/s)", state.Money, state.MoneyDelta) stdscr.AttrOff(gc.ColorPair(1) | gc.A_BOLD) stdscr.AttrOn(gc.ColorPair(3) | gc.A_BOLD) stdscr.MovePrintf(2, 1, "Population: %d", state.Population) stdscr.AttrOff(gc.ColorPair(3) | gc.A_BOLD) stdscr.AttrOn(gc.ColorPair(2) | gc.A_BOLD) stdscr.MovePrintf(3, 1, "Pollution: %d (+%d/s)", state.Pollution, state.PollutionDelta) stdscr.AttrOff(gc.ColorPair(2) | gc.A_BOLD) if state.Money >= 100 { stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD) } else { stdscr.AttrOn(gc.ColorPair(6) | gc.A_BOLD) } stdscr.MovePrint(1, cols-50, "1: Shop ($100) [2$/s]") if state.Money >= 600 { stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD) } else { stdscr.AttrOn(gc.ColorPair(6) | gc.A_BOLD) } stdscr.MovePrint(3, cols-50, "2: Factory ($600) [5$/s, 1 pollution/s]") if state.Money >= 1100 { stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD) } else { stdscr.AttrOn(gc.ColorPair(6) | gc.A_BOLD) } stdscr.MovePrint(5, cols-50, "3: Big factory ($1100) [10$/s, 3 pollution/s]") stdscr.AttrOff(gc.A_BOLD) if state.Money >= 2000 { stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD) } else { stdscr.AttrOn(gc.ColorPair(6) | gc.A_BOLD) } stdscr.MovePrint(7, cols-50, "4: Office ($2000) [+20% money generated]") stdscr.AttrOff(gc.A_BOLD) stdscr.Refresh() } stdscr.GetChar() }
func (h *Office) GetColor() gc.Char { return gc.ColorPair(7) | gc.A_BOLD }
func (h *Shop) GetColor() gc.Char { return gc.ColorPair(7) | gc.A_BOLD }
func (h *BigFactory) GetColor() gc.Char { return gc.ColorPair(7) | gc.A_BOLD }