Add drag; Add window collision; Add quit key

This commit is contained in:
Logen Kain 2018-01-17 17:36:21 -07:00
parent c7bb73158e
commit 32b32a88bf

93
main.go
View File

@ -71,6 +71,12 @@ func run() {
playerX := win.Bounds().Max.X / 2
playerY := win.Bounds().Max.Y / 8
var playerTopSpeed float64 = 350
var playerAcceleration float64 = 0.15 * playerTopSpeed
var playerCurrentSpeed float64 = 0
var playerDrag float64 = 1
var playerXVelocity float64 = 0
var playerYVelocity float64 = 0
var myVec = pixel.Vec{playerX, playerY}
last := time.Now()
@ -97,9 +103,35 @@ func run() {
angle -= 3 * dt
}
if win.Pressed(pixelgl.KeyEscape) {
win.SetClosed(true)
}
if win.Pressed(pixelgl.KeyUp) {
playerX += math.Cos(angle-(270*math.Pi)/180) * 300 * dt
playerY += math.Sin(angle-(270*math.Pi)/180) * 300 * dt
if playerCurrentSpeed < playerTopSpeed {
playerCurrentSpeed += playerAcceleration
}
playerXVelocity +=
math.Cos(angle-(270*math.Pi)/180) * playerCurrentSpeed * dt
playerYVelocity +=
math.Sin(angle-(270*math.Pi)/180) * playerCurrentSpeed * dt
if playerXVelocity > playerTopSpeed {
playerXVelocity = playerTopSpeed
}
if playerXVelocity < playerTopSpeed*-1 {
playerXVelocity = playerTopSpeed * -1
}
if playerYVelocity > playerTopSpeed {
playerYVelocity = playerTopSpeed
}
if playerYVelocity < playerTopSpeed*-1 {
playerYVelocity = playerTopSpeed * -1
}
}
if win.Pressed(pixelgl.KeyDown) {
@ -123,13 +155,66 @@ func run() {
angle = angle + (360*math.Pi)/180
}
if playerCurrentSpeed > 0 {
playerCurrentSpeed -= playerDrag * dt
}
if playerCurrentSpeed < 0 {
playerCurrentSpeed = 0
}
if playerX >= win.Bounds().Max.X-10 {
playerXVelocity = -25
}
if playerX <= win.Bounds().Min.X+10 {
playerXVelocity = 25
}
if playerY >= win.Bounds().Max.Y-10 {
playerYVelocity = -25
}
if playerY <= win.Bounds().Min.Y+10 {
playerYVelocity = 25
}
playerX += playerXVelocity * dt
playerY += playerYVelocity * dt
if playerYVelocity > 0 {
playerYVelocity -= playerDrag
if playerYVelocity < 0 {
playerYVelocity = 0
}
}
if playerYVelocity < 0 {
playerYVelocity += playerDrag
if playerYVelocity > 0 {
playerYVelocity = 0
}
}
if playerXVelocity > 0 {
playerXVelocity -= playerDrag
if playerXVelocity < 0 {
playerXVelocity = 0
}
}
if playerXVelocity < 0 {
playerXVelocity += playerDrag
if playerXVelocity > 0 {
playerXVelocity = 0
}
}
win.Update()
frames++
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d | Degrees: %f %f", cfgMain.Title,
frames, angleInDegrees, angle))
win.SetTitle(fmt.Sprintf("%s | FPS: %d | playerYSpeed: %f", cfgMain.Title,
frames, playerYVelocity))
frames = 0
default:
}