popraw kod o błąd: go:46:62: insect.Speed2 undefined (type Insect has no field or method…
- User
popraw kod o błąd: go:46:62: insect.Speed2 undefined (type Insect has no field or method Speed2) go:47:62: insect.Speed2 undefined (type Insect has no field or method Speed2)
package main
import ( "fmt" "math/rand" "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 }
func main() { rand.Seed(time.Now().UnixNano())
// Tworzenie armii 10 owadow armySize := 10 army := make([]Insect, armySize) // Inicjalizacja owadow for i := 0; i < armySize; i++ { army[i] = Insect{ ID: i + 1, Position: Position{rand.Intn(800), rand.Intn(600)}, // Zakladamy rozmiar ekranu 800x600 Speed: rand.Intn(5) + 1, // Predkosc owada (1-5 pikseli na jedno zaktualizowane polozenie) IsAlive: true, PixelColor: "black", } } // Symulacja ruchu i pozerania owadow for { // Aktualizacja pozycji owadow (ruch) 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, 800), clamp(newY, 0, 600)} // Zakladamy rozmiar ekranu 800x600 } } // Pozeranie pikseli na ekranie przez zywe owady for x := 0; x < 800; x++ { for y := 0; y < 600; y++ { for i, insect := range army { if insect.IsAlive && insect.Position.X == x && insect.Position.Y == y { // Owad pozera piksel army[i].PixelColor = "white" } } } } // Wyswietlanie aktualnego stanu armii owadow for _, insect := range army { fmt.Printf("Insect ID: %d, Position: (%d, %d), IsAlive: %v, PixelColor: %s\n", insect.ID, insect.Position.X, insect.Position.Y, insect.IsAlive, insect.PixelColor) } // Opoznienie symulacji time.Sleep(500 * time.Millisecond) }}
// 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
package main
import ( "fmt" "math/rand" "time" )
// Struktura reprezentujaca pozycje owada na ekranie type Position struct { X, Y int }