Skip to content

Commit

Permalink
V1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lbarcl committed Dec 3, 2023
1 parent 8f3f753 commit b6f3701
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 55 deletions.
Binary file added audio/Hit.wav
Binary file not shown.
Binary file modified audio/swallow.wav
Binary file not shown.
28 changes: 26 additions & 2 deletions calculations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"math"
"math/rand"

rl "github.com/gen2brain/raylib-go/raylib"
Expand Down Expand Up @@ -40,7 +41,7 @@ l:
break
}

return GridToViewport(X, Y)
return rl.Vector2{X: float32(X), Y: float32(Y)}
}

func CheckCollisions(snakeParts []rl.Vector2, fruit rl.Vector2) string {
Expand All @@ -54,9 +55,32 @@ func CheckCollisions(snakeParts []rl.Vector2, fruit rl.Vector2) string {
return "fruit"
}

if snakeParts[0].X < 0 || snakeParts[0].X > gridWidth || snakeParts[0].Y < 0 || snakeParts[0].Y > gridHeight {
if snakeParts[0].X < 0 || int32(snakeParts[0].X) > gridWidth || snakeParts[0].Y < 0 || int32(snakeParts[0].Y) > gridHeight {
return "wall"
}

return ""
}

func DirectionToRotation(direction rl.Vector2) float32 {
var rotation float32 = 0.0

if direction.Y == -1 {
rotation = 0
} else if direction.Y == 1 {
rotation = 180
} else if direction.X == -1 {
rotation = 270
} else {
rotation = 90
}

return rotation
}

func VectorToAngle(v rl.Vector2) float32 {
rad := float32(math.Atan2(float64(v.Y), float64(v.X)) * (180 / math.Pi))
angleDeg := rad * (180 / math.Pi)

return angleDeg
}
105 changes: 79 additions & 26 deletions draw.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,98 @@
package main

import rl "github.com/gen2brain/raylib-go/raylib"
import (
"strconv"

rl "github.com/gen2brain/raylib-go/raylib"
)

func DrawGrid() {
for x := 0; x < gridWidth; x++ {
for y := 0; y < gridHeight; y++ {
var x int32 = 0
for x < gridWidth {
var y int32 = 0
for y < gridHeight {
if (x%2 == 0 && y%2 != 0) || (x%2 != 0 && y%2 == 0) {
rl.DrawRectangle(int32(x*gridBoxSize), int32(y*gridBoxSize), gridBoxSize, gridBoxSize, rl.DarkGreen)
rl.DrawRectangle(int32(x*gridBoxSize), int32(y*gridBoxSize), gridBoxSize, gridBoxSize, settings.BlackSquareColor)
} else {
rl.DrawRectangle(int32(x*gridBoxSize), int32(y*gridBoxSize), gridBoxSize, gridBoxSize, rl.Green)
rl.DrawRectangle(int32(x*gridBoxSize), int32(y*gridBoxSize), gridBoxSize, gridBoxSize, settings.LightSquareColor)
}
if settings.DevMode {
rl.DrawText(strconv.Itoa(int(x))+", "+strconv.Itoa(int(y)), int32(x*gridBoxSize), int32(y*gridBoxSize), 10, rl.White)
}
y++
}
x++
}
}

func DrawSnake(parts []rl.Vector2, headDirection rl.Vector2) {
var rotation float32

if headDirection.Y == -1 {
rotation = 0
} else if headDirection.Y == 1 {
rotation = 180
} else if headDirection.X == -1 {
rotation = 270
} else {
rotation = 90
}

for i := range parts {
for i := 0; i < len(parts); i++ {
vpCords := GridToViewport(int32(parts[i].X), int32(parts[i].Y))
if i == 0 {
rl.DrawTextureEx(textures["Head.png"], vpCords, rotation, 1, rl.White)
} else if i != len(parts)-1 {
var dX [2]int = [2]int{int(parts[i-1].X - parts[i].X), int(parts[i].X - parts[i+1].X)}
var dY [2]int = [2]int{int(parts[i-1].Y - parts[i].Y), int(parts[i].Y - parts[i+1].Y)}

if dX[0] == dX[1] && dY == dY {
rl.DrawTextureEx(textures["Mid.png"], vpCords, rotation, 1, rl.White)
} else {
if i == 0 {
drawTexture("Head.png", DirectionToRotation(headDirection), vpCords)
} else if i == len(parts)-1 {
tailDirection := rl.Vector2Subtract(parts[i-1], parts[i])
drawTexture("Tail.png", DirectionToRotation(tailDirection), vpCords)
} else {
dNext := rl.Vector2Subtract(parts[i], parts[i+1])
dbefore := rl.Vector2Subtract(parts[i-1], parts[i])

if dNext.X == dbefore.X && dNext.Y == dbefore.Y {
// Straight segment
directionToNext := rl.Vector2Subtract(parts[i], parts[i+1])
drawTexture("Mid.png", DirectionToRotation(directionToNext), vpCords)
} else if dbefore.X < 0 {
if dNext.Y == -1 {
// Turn to the left from the left
drawTexture("To_Left.png", 270, vpCords)
} else if dNext.Y == 1 {
// Turn to the left from the left
drawTexture("To_Right.png", 270, vpCords)
}
} else if dbefore.X > 0 {
// Similar logic for turns to the right
if dNext.Y == -1 {
drawTexture("To_Right.png", 90, vpCords)
} else if dNext.Y == 1 {
drawTexture("To_Left.png", 90, vpCords)
}
} else if dbefore.Y < 0 {
if dNext.Y == 1 {
drawTexture("To_Left.png", 0, vpCords)
} else if dNext.Y == -1 {
drawTexture("To_Right.png", 0, vpCords)
} else if dNext.X == 1 {
drawTexture("To_Left.png", 0, vpCords)
} else {
drawTexture("To_Right.png", 0, vpCords)
}
} else if dbefore.Y > 0 {
// Similar logic for turns downward
if dNext.X == 1 {
drawTexture("To_Right.png", 180, vpCords)
} else if dNext.X == -1 {
drawTexture("To_Left.png", 180, vpCords)
}
}

//fmt.Printf("db: %f, %f | dn: %f, %f \n", dbefore.X, dbefore.Y, dNext.X, dNext.Y)
}
}
}

func drawTexture(partName string, rotation float32, cords rl.Vector2) {
rl.DrawTexturePro(
textures[partName],
rl.Rectangle{X: 0, Y: 0, Width: float32(textures[partName].Width), Height: float32(textures[partName].Height)},
rl.Rectangle{X: cords.X - 25, Y: cords.Y - 25, Width: float32(scaleX), Height: float32(scaleY)},
rl.Vector2{X: float32(scaleX / 2), Y: float32(scaleY / 2)},
rotation,
rl.White,
)
}

func DrawFruit(fruitPosition rl.Vector2) {
vpCords := GridToViewport(int32(fruitPosition.X), int32(fruitPosition.Y))
drawTexture("Fruit.png", 0, vpCords)
}
61 changes: 37 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ import (
rl "github.com/gen2brain/raylib-go/raylib"
)

var settings = LoadSettings()

const gridBoxSize = 50
const viewportWidth = 500
const viewportHeight = 500
const gridWidth = viewportWidth / gridBoxSize
const gridHeight = viewportHeight / gridBoxSize
const scaleX = viewportWidth / gridWidth
const scaleY = viewportHeight / gridHeight

var gridWidth int32 = settings.ViewportWidth / gridBoxSize
var gridHeight int32 = settings.ViewportHeight / gridBoxSize
var scaleX int32 = settings.ViewportWidth / gridWidth
var scaleY int32 = settings.ViewportHeight / gridHeight

var textures map[string]rl.Texture2D
var audio map[string]rl.Sound
var gameState int8 = -1
var score int8 = 0

func initialize() {
rl.InitWindow(viewportWidth, viewportHeight, "Snake")
rl.SetTargetFPS(2)
rl.InitWindow(settings.ViewportWidth, settings.ViewportHeight, "Snake")
rl.SetTargetFPS(settings.TargetFPS)
rl.InitAudioDevice()

LoadAudio("audio/")
LoadAudio("audio")
LoadTextures("sprites")
}

Expand All @@ -41,50 +42,63 @@ func draw(snakeParts []rl.Vector2, snakeHeadDirection rl.Vector2, fruitLocation

if gameState == 0 {
DrawSnake(snakeParts, snakeHeadDirection)
rl.DrawCircle(int32(fruitLocation.X), int32(fruitLocation.Y), gridBoxSize/2, rl.Blue)
DrawFruit(fruitLocation)
rl.DrawText(strconv.Itoa(int(score)), int32((gridWidth/2)*gridBoxSize)-10, gridBoxSize, 50, rl.White)
} else {
rl.DrawText("Press \"R\" to play ", int32((gridWidth/6)*gridBoxSize), gridBoxSize*2, 45, rl.White)
}

if settings.DevMode {
rl.DrawFPS(10, 10)
}

rl.EndDrawing()
}

func gameLoop(snakeHeadDirection *rl.Vector2, snakeParts []rl.Vector2, fruitLocation *rl.Vector2) {
func gameLoop(snakeHeadDirection *rl.Vector2, snakeParts []rl.Vector2, fruitLocation *rl.Vector2) []rl.Vector2 {
switch rl.GetKeyPressed() {
case rl.KeyUp:
case settings.UpKey:
snakeHeadDirection.Y = -1
snakeHeadDirection.X = 0
case rl.KeyDown:

case settings.DownKey:
snakeHeadDirection.Y = 1
snakeHeadDirection.X = 0
case rl.KeyLeft:

case settings.LeftKey:
snakeHeadDirection.X = -1
snakeHeadDirection.Y = 0
case rl.KeyRight:

case settings.RightKey:
snakeHeadDirection.X = 1
snakeHeadDirection.Y = 0

}

CalculateSnakePosition(*snakeHeadDirection, snakeParts)
collision := CheckCollisions(snakeParts, ViewportToGrid(*fruitLocation))
collision := CheckCollisions(snakeParts, *fruitLocation)

switch collision {
case "wall":
gameState = -1
rl.PlaySound(audio["Hit.wav"])

case "snake":
gameState = -1
rl.PlaySound(audio["Hit.wav"])

case "fruit":
rl.PlaySound(audio["swallow.waw"])
score++
newTail := rl.Vector2{X: -1, Y: -1}
snakeParts = append(snakeParts, newTail)
rl.PlaySound(audio["Swallow.wav"])
snakeParts = append(snakeParts, snakeParts[len(snakeParts)-1])
*fruitLocation = SpawnFruit(snakeParts)
score++

if score == (gridHeight*gridWidth)-1 {
if int32(score) == (gridHeight*gridWidth)-3 {
gameState = 1
}
}

return snakeParts
}

func main() {
Expand All @@ -95,19 +109,18 @@ func main() {
var fruitLocation = SpawnFruit(snakeParts)

for !rl.WindowShouldClose() {

if gameState == -1 {
snakeHeadDirection = rl.Vector2{X: 0, Y: -1}
snakeParts = []rl.Vector2{{X: 5, Y: 5}, {X: 5, Y: 6}, {X: 5, Y: 7}}
fruitLocation = SpawnFruit(snakeParts)

if rl.GetKeyPressed() == rl.KeyR {
if rl.GetKeyPressed() == settings.RestartKey {
gameState = 0
score = 0
}

} else {
gameLoop(&snakeHeadDirection, snakeParts, &fruitLocation)
snakeParts = gameLoop(&snakeHeadDirection, snakeParts, &fruitLocation)
}

draw(snakeParts, snakeHeadDirection, fruitLocation)
Expand Down
11 changes: 11 additions & 0 deletions settings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
width=500
height=500
fps=8
up=265
down=264
left=263
right=262
restart=82
dev_mode=false
black_square_color=#308913
light_square_color=#54f71d
Binary file added sprites/Fruit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b6f3701

Please sign in to comment.