Ejemplo n.º 1
1
func (e *Explosion) Draw(w *gc.Window) {
	w.Overlay(e.Window)
}
Ejemplo n.º 2
0
func renderInitialWindow(w *goncurses.Window) {
	y, _ := w.MaxYX()
	for i := 1; i < y-1; i++ {
		w.MoveAddChar(i, 0, '~')
	}
	w.Move(0, 0)
	w.Refresh()
}
Ejemplo n.º 3
0
func createFileList(stdscr *gc.Window) *gc.Menu {

	// build the menu items
	menu_items := getFiles()
	items := make([]*gc.MenuItem, len(menu_items))
	i := 0
	for key, val := range menu_items {
		items[i], _ = gc.NewItem(key, val)
		// defer items[i].Free()
		i++
	}
	menu, _ := gc.NewMenu(items)
	defer menu.Free()
	rows, cols := stdscr.MaxYX()

	// menuwin, _ := gc.NewWindow(rows, cols/2, 3, 1)
	menuwin, _ := gc.NewWindow(rows, cols/2, 0, 0)
	menuwin.Keypad(true)

	menu.SetWindow(menuwin)
	dwin := menuwin.Derived(29, 50, 3, 1)
	menu.SubWindow(dwin)
	menu.Option(gc.O_SHOWDESC, true)
	menu.Mark(" * ")
	menu.Option(gc.O_ONEVALUE, false)

	// Print centered menu title
	_, x := menuwin.MaxYX()
	title := "My Menu"
	menuwin.Box(0, 0)
	menuwin.ColorOn(1)
	menuwin.MovePrint(1, (x/2)-(len(title)/2), title)
	menuwin.ColorOff(1)
	menuwin.MoveAddChar(2, 0, gc.ACS_LTEE)
	menuwin.HLine(2, 1, gc.ACS_HLINE, x-3)
	menuwin.MoveAddChar(2, x-2, gc.ACS_RTEE)

	gc.NewPanel(menuwin)

	menu.Post()
	//defer menu.UnPost()
	menu.Window().Refresh()
	return menu
}
Ejemplo n.º 4
0
func createPictureView(stdscr *gc.Window) *gc.Window {

	rows, cols := stdscr.MaxYX()
	pic_view, _ := gc.NewWindow(rows, cols/2, 0, cols/2)
	pic_view.Box(0, 0)
	// pic_view.MovePrintf(y+1, x+1, "TEST")
	gc.NewPanel(pic_view)

	y, _ := stdscr.MaxYX()
	stdscr.MovePrint(y-2, 1, "'q' to exit")
	stdscr.Refresh()
	pic_view.Refresh()
	return pic_view

}
Ejemplo n.º 5
0
func handleInput(stdscr *gc.Window, ship *Ship) bool {
	lines, cols := stdscr.MaxYX()
	y, x := ship.YX()
	k := stdscr.GetChar()

	switch byte(k) {
	case 0:
		break
	case 'a':
		x--
		if x < 2 {
			x = 2
		}
	case 'd':
		x++
		if x > cols-3 {
			x = cols - 3
		}
	case 's':
		y++
		if y > lines-4 {
			y = lines - 4
		}
	case 'w':
		y--
		if y < 2 {
			y = 2
		}
	case ' ':
		objects = append(objects, newBullet(y+1, x+4))
		objects = append(objects, newBullet(y+3, x+4))
	default:
		return false
	}
	ship.MoveWindow(y, x)
	return true
}
Ejemplo n.º 6
0
func main() {
	f, err := os.Create("err.log")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	log.SetOutput(f)

	var stdscr *gc.Window
	stdscr, err = gc.Init()
	if err != nil {
		log.Println("Init:", err)
	}
	defer gc.End()

	rand.Seed(time.Now().Unix())
	gc.StartColor()
	gc.Cursor(0)
	gc.Echo(false)
	gc.HalfDelay(1)

	gc.InitPair(1, gc.C_WHITE, gc.C_BLACK)
	gc.InitPair(2, gc.C_YELLOW, gc.C_BLACK)
	gc.InitPair(3, gc.C_MAGENTA, gc.C_BLACK)
	gc.InitPair(4, gc.C_RED, gc.C_BLACK)

	gc.InitPair(5, gc.C_BLUE, gc.C_BLACK)
	gc.InitPair(6, gc.C_GREEN, gc.C_BLACK)

	lines, cols := stdscr.MaxYX()
	pl, pc := lines, cols*3

	ship := newShip(lines/2, 5)
	objects = append(objects, ship)

	field := genStarfield(pl, pc)
	text := stdscr.Duplicate()

	c := time.NewTicker(time.Second / 2)
	c2 := time.NewTicker(time.Second / 16)
	px := 0

loop:
	for {
		text.MovePrintf(0, 0, "Life: [%-5s]", lifeToText(ship.life))
		stdscr.Erase()
		stdscr.Copy(field.Window, 0, px, 0, 0, lines-1, cols-1, true)
		drawObjects(stdscr)
		stdscr.Overlay(text)
		stdscr.Refresh()
		select {
		case <-c.C:
			spawnAsteroid(stdscr.MaxYX())
			if px+cols >= pc {
				break loop
			}
			px++
		case <-c2.C:
			updateObjects(stdscr.MaxYX())
			drawObjects(stdscr)
		default:
			if !handleInput(stdscr, ship) || ship.Expired(-1, -1) {
				break loop
			}
		}
	}
	msg := "Game Over"
	end, err := gc.NewWindow(5, len(msg)+4, (lines/2)-2, (cols-len(msg))/2)
	if err != nil {
		log.Fatal("game over:", err)
	}
	end.MovePrint(2, 2, msg)
	end.Box(gc.ACS_VLINE, gc.ACS_HLINE)
	end.Refresh()
	gc.Nap(2000)
}
Ejemplo n.º 7
0
func (s *Ship) Draw(w *gc.Window) {
	w.Overlay(s.Window)
}
Ejemplo n.º 8
0
func (b *Bullet) Draw(w *gc.Window) {
	w.Overlay(b.Window)
}
Ejemplo n.º 9
0
func (a *Asteroid) Draw(w *gc.Window) {
	w.Overlay(a.Window)
}
Ejemplo n.º 10
0
func update(s *state, stdscr *gc.Window) {
	stdscr.Clear()
	var last_found *section
	var ascii rune
	for i := 0; i < len(s.content); i++ {
		found := s.matches[i]
		if found != nil {
			last_found = found
			// Print the associated key instead of the real letter
			stdscr.AttrOn(gc.A_BOLD)
			for k, v := range s.index {
				if found == v {
					ascii = k
					break
				}
			}
			stdscr.Print(string(ascii))
			stdscr.AttrOff(gc.A_BOLD)
		} else {
			// Print the real letter, in bold if selected
			if last_found != nil && i >= last_found.begin && i <= last_found.end && strings.Contains(s.selected, string(ascii)) {
				stdscr.AttrOn(gc.A_BOLD)
			}
			ch := string(s.content[i])
			//			if (s.content[i] != '\n' ) {
			stdscr.Print(ch)
			//			}
			stdscr.AttrOff(gc.A_BOLD)
		}
	}

	//	row, _ := stdscr.MaxYX()
	//	stdscr.MovePrint(row - 1, 0, "selected = " + s.selected + "] >> ")
	stdscr.Refresh()
}
Ejemplo n.º 11
0
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Fatal(err)
	}
	defer gc.End()

	// Turn off character echo, hide the cursor and disable input buffering
	gc.Echo(false)
	gc.CBreak(true)
	gc.Cursor(0)

	stdscr.Print("Use arrow keys to move the window. Press 'q' to exit")
	stdscr.NoutRefresh()

	// Determine the center of the screen and offset those coordinates by
	// half of the window size we are about to create. These coordinates will
	// be used to move our window around the screen
	rows, cols := stdscr.MaxYX()
	height, width := 5, 10
	y, x := (rows-height)/2, (cols-width)/2

	// Create a new window centered on the screen and enable the use of the
	// keypad on it so the arrow keys are available
	var win *gc.Window
	win, err = gc.NewWindow(height, width, y, x)
	if err != nil {
		log.Fatal(err)
	}
	win.Keypad(true)

main:
	for {
		// Clear the section of screen where the box is currently located so
		// that it is blanked by calling Erase on the window and refreshing it
		// so that the chances are sent to the virtual screen but not actually
		// output to the terminal
		win.Erase()
		win.NoutRefresh()
		// Move the window to it's new location (if any) and redraw it
		win.MoveWindow(y, x)
		win.Box(0, 0)
		win.NoutRefresh()
		// Update will flush only the characters which have changed between the
		// physical screen and the virtual screen, minimizing the number of
		// characters which must be sent
		gc.Update()

		// In order for the window to display correctly, we must call GetChar()
		// on it rather than stdscr
		switch win.GetChar() {
		case 'q':
			break main
		case gc.KEY_LEFT:
			if x > 0 {
				x--
			}
		case gc.KEY_RIGHT:
			if x < cols-width {
				x++
			}
		case gc.KEY_UP:
			if y > 1 {
				y--
			}
		case gc.KEY_DOWN:
			if y < rows-height {
				y++
			}
		}
	}
	win.Delete()
}
Ejemplo n.º 12
0
func printmenu(w *gc.Window, menu []string, active int) {
	y, x := 2, 2
	w.Box(0, 0)
	for i, s := range menu {
		if i == active {
			w.AttrOn(gc.A_REVERSE)
			w.MovePrint(y+i, x, s)
			w.AttrOff(gc.A_REVERSE)
		} else {
			w.MovePrint(y+i, x, s)
		}
	}
	w.Refresh()
}