Skip to content

Commit

Permalink
Merge pull request #302 from unklnik/master
Browse files Browse the repository at this point in the history
add more shapes examples
  • Loading branch information
gen2brain authored Nov 6, 2023
2 parents 30493ab + 64b853a commit 20ca90b
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples/shapes/bouncing_ball/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
84 changes: 84 additions & 0 deletions examples/shapes/following_eyes/main.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 20ca90b

Please sign in to comment.