From 32b32a88bf0668968c1d988f8086a12b59b35e00 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Wed, 17 Jan 2018 17:36:21 -0700 Subject: [PATCH] Add drag; Add window collision; Add quit key --- main.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index f68f1d5..f7fc137 100644 --- a/main.go +++ b/main.go @@ -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: }