-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
269 additions
and
55 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.