diff --git a/examples/shapes/bouncing_ball/main.go b/examples/shapes/bouncing_ball/main.go new file mode 100644 index 0000000..ed7ddc5 --- /dev/null +++ b/examples/shapes/bouncing_ball/main.go @@ -0,0 +1,63 @@ +package main + +import ( + rl "github.com/gen2brain/raylib-go/raylib" +) + +func main() { + screenWidth := int32(1280) + screenHeight := int32(720) + + rl.SetConfigFlags(rl.FlagMsaa4xHint) + + rl.InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball") + + ballPos := rl.NewVector2(float32(screenWidth)/2, float32(screenHeight)/2) + ballSpeed := rl.NewVector2(5, 4) + ballRadius := 20 + + pause := false + frames := 0 + + rl.SetTargetFPS(60) + + rl.SetMousePosition(0, 0) + + for !rl.WindowShouldClose() { + + if rl.IsKeyPressed(rl.KeySpace) { + pause = !pause + } + + if !pause { + ballPos.X += ballSpeed.X + ballPos.Y += ballSpeed.Y + if ballPos.X >= float32(screenWidth)-float32(ballRadius) || ballPos.X <= float32(ballRadius) { + ballSpeed.X *= -1 + } + if ballPos.Y >= float32(screenHeight)-float32(ballRadius) || ballPos.Y <= float32(ballRadius) { + ballSpeed.Y *= -1 + } + } else { + frames++ + } + + rl.BeginDrawing() + rl.ClearBackground(rl.RayWhite) + + rl.DrawText("SPACE key to pause", 10, 10, 20, rl.Black) + + rl.DrawCircleV(ballPos, float32(ballRadius), rl.Red) + + if pause && (frames/30)%2 == 0 { + rl.DrawText("PAUSED", 10, screenHeight-40, 30, rl.Black) + } + + rl.DrawFPS(screenWidth-100, 10) + + rl.EndDrawing() + + } + + rl.CloseWindow() +} diff --git a/examples/shapes/following_eyes/main.go b/examples/shapes/following_eyes/main.go new file mode 100644 index 0000000..437ce9c --- /dev/null +++ b/examples/shapes/following_eyes/main.go @@ -0,0 +1,84 @@ +package main + +import ( + "math" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +func main() { + screenWidth := int32(1280) + screenHeight := int32(720) + + rl.SetConfigFlags(rl.FlagMsaa4xHint) + + rl.InitWindow(screenWidth, screenHeight, "raylib [shapes] example - following eyes") + + scleraLpos := rl.NewVector2(float32(screenWidth/2)-100, float32(screenHeight/2)) + scleraRpos := rl.NewVector2(float32(screenWidth/2)+100, float32(screenHeight/2)) + scleraRad := 80 + + irisLpos := rl.NewVector2(float32(screenWidth/2)-100, float32(screenHeight/2)) + irisRpos := rl.NewVector2(float32(screenWidth/2)+100, float32(screenHeight/2)) + irisRad := 24 + + angle := float32(0) + dx, dy, dxx, dyy := float32(0), float32(0), float32(0), float32(0) + + rl.SetTargetFPS(60) + + rl.SetMousePosition(0, 0) + + for !rl.WindowShouldClose() { + + irisLpos = rl.GetMousePosition() + irisRpos = rl.GetMousePosition() + + if !rl.CheckCollisionPointCircle(irisLpos, scleraLpos, float32(scleraRad-20)) { + + dx = irisLpos.X - scleraLpos.X + dy = irisLpos.Y - scleraLpos.Y + + angle = float32(math.Atan2(float64(dy), float64(dx))) + + dxx = (float32(scleraRad-irisRad) * float32(math.Cos(float64(angle)))) + dyy = (float32(scleraRad-irisRad) * float32(math.Sin(float64(angle)))) + + irisLpos.X = scleraLpos.X + dxx + irisLpos.Y = scleraLpos.Y + dyy + + } + + if !rl.CheckCollisionPointCircle(irisRpos, scleraRpos, float32(scleraRad)-20) { + dx = irisRpos.X - scleraRpos.X + dy = irisRpos.Y - scleraRpos.Y + + angle = float32(math.Atan2(float64(dy), float64(dx))) + + dxx = (float32(scleraRad-irisRad) * float32(math.Cos(float64(angle)))) + dyy = (float32(scleraRad-irisRad) * float32(math.Sin(float64(angle)))) + + irisRpos.X = scleraRpos.X + dxx + irisRpos.Y = scleraRpos.Y + dyy + + } + + rl.BeginDrawing() + rl.ClearBackground(rl.RayWhite) + + rl.DrawCircleV(scleraLpos, float32(scleraRad), rl.LightGray) + rl.DrawCircleV(irisLpos, float32(irisRad), rl.Red) + rl.DrawCircleV(irisLpos, 10, rl.Black) + + rl.DrawCircleV(scleraRpos, float32(scleraRad), rl.LightGray) + rl.DrawCircleV(irisRpos, float32(irisRad), rl.Orange) + rl.DrawCircleV(irisRpos, 10, rl.Black) + + rl.DrawFPS(screenWidth-100, 10) + + rl.EndDrawing() + + } + + rl.CloseWindow() +}