Code - R9

關於 Nature of Code
Evolution EcoSystem 的實驗程式。 使用 EbitenLibrary.

O_o

package es

import "r9/sb"

// Evolution EcoSystem

// The World we live in
// Has Bloop and food

const (
	Width  = 800
	Height = 600
)

type World struct {
	Bloops []*Bloop
	Food   *Foods
	Canvas *sb.Canvas
}

func NewWorld(num int) *World {
	//Start with initial food and creature
	food := NewFoods(num)
	blps := make([]*Bloop, 0)
	for i := 0; i < num; i++ {
		l := sb.NewVec2(sb.RandIntn(Width), sb.RandIntn(Height))
		dna := NewDNA(20)
		blp := NewBloop(l, dna)
		blps = append(blps, blp)
	}
	canvas := sb.NewCanvas(Width, Height)
	return &World{blps, food, canvas}
}

// Make a new creature
func (w *World) Born(x, y float64) {
	l := sb.NewVec2(int(x), int(y))
	dna := NewDNA(20)
	w.Bloops = append(w.Bloops, NewBloop(l, dna))
}

// Remove a creature
func (w *World) Remove(index int) {
	oldlen := len(w.Bloops)
	w.Bloops[index] = w.Bloops[oldlen-1]
	w.Bloops[oldlen-1] = nil
	w.Bloops = w.Bloops[:oldlen-1]
}

//Run
func (w *World) Run() {
	w.Canvas.Clear(sb.ColorBlack)
	next := make([]*Bloop, 0)
	//Deal with food
	w.Food.Run(w.Canvas)
	for _, bloop := range w.Bloops {
		bloop.Run(w.Canvas)
		bloop.Eat(w.Food)
		if bloop.IsDead() == false {
			next = append(next, bloop)
		} else {
			w.Food.Add(bloop.Location.Clone())
		}
		child := bloop.Reproduce()
		if child != nil {
			next = append(next, child)
		}
	}
	w.Bloops = next
}

完整的在這裏


See also