zmodyfukuj kod o błędy: go:73:46: insect.Speed2 undefined (type Insect has no field or…
- User
zmodyfukuj kod o błędy:
go:73:46: insect.Speed2 undefined (type Insect has no field or method Speed2) go:74:46: insect.Speed2 undefined (type Insect has no field or method Speed2)
package main
import ( "fmt" "math/rand" "strings" "time" )
// Struktura reprezentujaca pozycje owada na ekranie type Position struct { X, Y int }
// Struktura reprezentujaca owada type Insect struct { ID int Position Position Speed int IsAlive bool PixelColor string }
// Stale reprezentujace wymiary ekranu const ( ScreenWidth = 800 ScreenHeight = 600 NumPerLine = 10 )
func main() { rand.Seed(time.Now().UnixNano())
// Tworzenie armii 10 owadow armySize := 10 army := createArmy(armySize)
// Symulacja ruchu i pozerania owadow for { // Aktualizacja pozycji owadow (ruch) moveInsects(army)
// Pozeranie pikseli na ekranie przez zywe owady consumePixels(army)
// Wyswietlanie aktualnego stanu armii owadow printArmy(army)
// Opoznienie symulacji time.Sleep(500 * time.Millisecond) } }
// Funkcja tworzaca armie owadow o podanej liczbie func createArmy(size int) []Insect { army := make([]Insect, size) for i := 0; i < size; i++ { army[i] = Insect{ ID: i + 1, Position: Position{rand.Intn(ScreenWidth), rand.Intn(ScreenHeight)}, Speed: rand.Intn(5) + 1, IsAlive: true, PixelColor: "black", } } return army }
// Funkcja aktualizujaca pozycje owadow func moveInsects(army []Insect) { for i, insect := range army { if insect.IsAlive { newX := insect.Position.X + rand.Intn(insect.Speed2+1) - insect.Speed newY := insect.Position.Y + rand.Intn(insect.Speed2+1) - insect.Speed army[i].Position = Position{clamp(newX, 0, ScreenWidth), clamp(newY, 0, ScreenHeight)} } } }
// Funkcja pozerajaca piksele na ekranie przez owady func consumePixels(army []Insect) { for x := 0; x < ScreenWidth; x++ { for y := 0; y < ScreenHeight; y++ { for i, insect := range army { if insect.IsAlive && insect.Position.X == x && insect.Position.Y == y { army[i].PixelColor = "white" // Owad pozera piksel } } } } }
// Funkcja wyswietlajaca aktualny stan armii owadow func printArmy(army []Insect) { count := 0 var output string for _, insect := range army { if count >= NumPerLine { count = 0 fmt.Println(output) output = "" } output += fmt.Sprintf("Insect ID: %d, Position: (%d, %d), IsAlive: %v, PixelColor: %s\r", insect.ID, insect.Position.X, insect.Position.Y, insect.IsAlive, insect.PixelColor) count++ } fmt.Println(output) // Dodanie ostatniego wiersza fmt.Println(strings.Repeat("\r", NumPerLine-len(army)%NumPerLine)) // Zamiana brakujacych wierszy na "\r"
}
// Funkcja pomocnicza do ograniczania wartosci do zadanego zakresu (min, max) func clamp(value, min, max int) int { if value < min { return min } if value > max { return max } return value }
- Assistant