Compare commits
No commits in common. "53e6bfdf4d27bde6af6ebe7f0df0f0360c3acb97" and "c5827793efd75d87a0fe4d94fe9ec17bece32a0d" have entirely different histories.
53e6bfdf4d
...
c5827793ef
2
game.go
2
game.go
@ -93,7 +93,7 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|||||||
|
|
||||||
// Draw the screen
|
// Draw the screen
|
||||||
drawBox(screen, style, 0, 0, screenx-1, screeny-1)
|
drawBox(screen, style, 0, 0, screenx-1, screeny-1)
|
||||||
//drawCoords(screen, style, &snake)
|
drawCoords(screen, style, &snake)
|
||||||
drawApple(screen, style, &apple)
|
drawApple(screen, style, &apple)
|
||||||
drawSnake(screen, style, &snake)
|
drawSnake(screen, style, &snake)
|
||||||
drawScore(screen, style, score)
|
drawScore(screen, style, score)
|
||||||
|
67
snake.go
67
snake.go
@ -35,8 +35,6 @@ func initSnake(snakex *int, snakey *int) Snake {
|
|||||||
|
|
||||||
func drawSnake(screen tcell.Screen, style tcell.Style, snake *Snake) {
|
func drawSnake(screen tcell.Screen, style tcell.Style, snake *Snake) {
|
||||||
|
|
||||||
var tail []rune
|
|
||||||
|
|
||||||
// Reverse tail, chop of the excesses,
|
// Reverse tail, chop of the excesses,
|
||||||
snake.tail = ReverseSlice(snake.tail)
|
snake.tail = ReverseSlice(snake.tail)
|
||||||
if len(snake.tail) > snake.length {
|
if len(snake.tail) > snake.length {
|
||||||
@ -45,32 +43,9 @@ func drawSnake(screen tcell.Screen, style tcell.Style, snake *Snake) {
|
|||||||
// reverse it back to the original order, replace the tail in `snake`
|
// reverse it back to the original order, replace the tail in `snake`
|
||||||
snake.tail = ReverseSlice(snake.tail)
|
snake.tail = ReverseSlice(snake.tail)
|
||||||
|
|
||||||
for i, j := range snake.tail {
|
|
||||||
// The Tail end
|
|
||||||
if i == 0 {
|
|
||||||
left := getDirection(j, snake.tail[i+1])
|
|
||||||
right := getDirection(j, snake.tail[i+1])
|
|
||||||
tail = append(tail, getLineType(left, right))
|
|
||||||
|
|
||||||
// The Bit behind the head
|
|
||||||
} else if i == len(snake.tail)-1 {
|
|
||||||
left := getDirection(j, snake.tail[i-1])
|
|
||||||
right := getDirection(j, snake.head)
|
|
||||||
tail = append(tail, getLineType(left, right))
|
|
||||||
|
|
||||||
// The body of the snake
|
|
||||||
} else {
|
|
||||||
left := getDirection(j, snake.tail[i-1])
|
|
||||||
right := getDirection(j, snake.tail[i+1])
|
|
||||||
tail = append(tail, getLineType(left, right))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw the body
|
// Draw the body
|
||||||
for i, segment := range snake.tail {
|
for _, segment := range snake.tail {
|
||||||
screen.SetContent(segment.x, segment.y, tail[i], nil, style)
|
screen.SetContent(segment.x, segment.y, '+', nil, style)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the head, make sure it is on top of everything
|
// Draw the head, make sure it is on top of everything
|
||||||
@ -134,41 +109,3 @@ func validateDirection(snake *Snake, direction int) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDirection(from Position, to Position) int {
|
|
||||||
|
|
||||||
var direction int
|
|
||||||
|
|
||||||
if from.x-to.x == 1 {
|
|
||||||
direction = Left
|
|
||||||
} else if from.x-to.x == -1 {
|
|
||||||
direction = Right
|
|
||||||
} else if from.y-to.y == 1 {
|
|
||||||
direction = Up
|
|
||||||
} else if from.y-to.y == -1 {
|
|
||||||
direction = Down
|
|
||||||
}
|
|
||||||
|
|
||||||
return direction
|
|
||||||
}
|
|
||||||
|
|
||||||
func getLineType(from int, to int) rune {
|
|
||||||
|
|
||||||
var lineType rune
|
|
||||||
|
|
||||||
if (from == Left && to == Down) || (from == Down && to == Left) {
|
|
||||||
lineType = '┐'
|
|
||||||
} else if (from == Up && to == Right) || (from == Right && to == Up) {
|
|
||||||
lineType = '└'
|
|
||||||
} else if (from == Left && to == Right) || (from == Right && to == Left) {
|
|
||||||
lineType = '─'
|
|
||||||
} else if (from == Left && to == Up) || (from == Up && to == Left) {
|
|
||||||
lineType = '┘'
|
|
||||||
} else if (from == Right && to == Down) || (from == Down && to == Right) {
|
|
||||||
lineType = '┌'
|
|
||||||
} else if (from == Up && to == Down) || (from == Down && to == Up) {
|
|
||||||
lineType = '│'
|
|
||||||
}
|
|
||||||
|
|
||||||
return lineType
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user