Ver código fonte

Updated starfield to handle unsorted map.

maps are not sorted in go.  I sometimes have last_pos.X > pos.X.
Steve Thielemann 3 anos atrás
pai
commit
d1ede6342e
2 arquivos alterados com 19 adições e 5 exclusões
  1. 2 1
      space-ace.go
  2. 17 4
      starfield.go

+ 2 - 1
space-ace.go

@@ -299,7 +299,7 @@ func panel_demo(d *door.Door) {
 	p.Lines = append(p.Lines, door.Line{Text: fmt.Sprintf(fmtStr, "Welcome to golang!"), DefaultColor: lineColor})
 
 	d.Write(door.Clrscr)
-	d.Write(p.Output())
+	d.Write(p.Output() + door.CRNL)
 }
 
 func createTable(db *sql.DB) {
@@ -362,6 +362,7 @@ func main() {
 
 	for choice >= 0 {
 		d.Write(door.Clrscr)
+		s.Display(&d)
 		choice = mainmenu.Choose(&d)
 
 		if choice < 0 {

+ 17 - 4
starfield.go

@@ -67,20 +67,33 @@ func (s *StarField) Display(d *door.Door) {
 	var i int = 0
 	var last_pos StarPos
 
+	// maps in go are not sorted
 	for pos, _ := range s.Sky {
 		use_goto := true
 
 		if i != 0 {
 			if pos.Y == last_pos.Y {
 				dx := pos.X - last_pos.X
-				if dx <= 0 {
+				// Sometimes pos.X < last_pos.X
+				if dx == 0 {
 					use_goto = false
 				} else {
-					if dx < 5 {
-						d.Write(strings.Repeat(" ", dx))
+					if dx < 0 {
+						// handle negative
+						// fmt.Printf("from %#v to %#v\n", last_pos, pos)
+						dx = -dx
+						if dx < 5 {
+							d.Write(strings.Repeat("\x08", dx))
+						} else {
+							d.Write(fmt.Sprintf("\x1b[%dD", dx))
+						}
 						use_goto = false
 					} else {
-						d.Write(fmt.Sprintf("\x1b[%dC", dx))
+						if dx < 5 {
+							d.Write(strings.Repeat(" ", dx))
+						} else {
+							d.Write(fmt.Sprintf("\x1b[%dC", dx))
+						}
 						use_goto = false
 					}
 				}