|
@@ -45,12 +45,6 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|
|
|
|
|
|
|
var score int = 0
|
|
var score int = 0
|
|
|
var scoreCounter int = 1
|
|
var scoreCounter int = 1
|
|
|
- //var field [80][24]int
|
|
|
|
|
- // fill up the field:
|
|
|
|
|
- // 0: empty
|
|
|
|
|
- // 1: snake
|
|
|
|
|
- // 2: apple
|
|
|
|
|
- // 3: border
|
|
|
|
|
|
|
|
|
|
// Board size
|
|
// Board size
|
|
|
var screenx = 80
|
|
var screenx = 80
|
|
@@ -63,30 +57,38 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|
|
// Last direction we went in
|
|
// Last direction we went in
|
|
|
var lastpress int = 1
|
|
var lastpress int = 1
|
|
|
|
|
|
|
|
- var apple Apple = placeApple(&snakex, &snakey)
|
|
|
|
|
|
|
+ var snake Snake = initSnake(&snakex, &snakey)
|
|
|
|
|
+ var apple Apple = placeApple(&snake)
|
|
|
|
|
|
|
|
for {
|
|
for {
|
|
|
|
|
|
|
|
// Take input or sleep
|
|
// Take input or sleep
|
|
|
select {
|
|
select {
|
|
|
case press := <-keypresses:
|
|
case press := <-keypresses:
|
|
|
- snakeDirection(press, &snakex, &snakey)
|
|
|
|
|
|
|
+ snakeDirection(press, &snake)
|
|
|
lastpress = press
|
|
lastpress = press
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
- snakeDirection(lastpress, &snakex, &snakey)
|
|
|
|
|
|
|
+ snakeDirection(lastpress, &snake)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if snakex == apple.x && snakey == apple.y {
|
|
|
|
|
- apple = placeApple(&snakex, &snakey)
|
|
|
|
|
|
|
+ if snake.head.x == apple.x && snake.head.y == apple.y {
|
|
|
|
|
+ apple = placeApple(&snake)
|
|
|
score += scoreCounter
|
|
score += scoreCounter
|
|
|
scoreCounter++
|
|
scoreCounter++
|
|
|
|
|
+ snake.length = 3 + score
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Call it quits once the snake hits the border.
|
|
// Call it quits once the snake hits the border.
|
|
|
- if snakex == 0 || snakex == screenx-1 {
|
|
|
|
|
|
|
+ if snake.head.x == 0 || snake.head.x == screenx-1 {
|
|
|
close(gamestate)
|
|
close(gamestate)
|
|
|
return
|
|
return
|
|
|
- } else if snakey == 0 || snakey == screeny-1 {
|
|
|
|
|
|
|
+ } else if snake.head.y == 0 || snake.head.y == screeny-1 {
|
|
|
|
|
+ close(gamestate)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Check if head intersects with any parts of the body
|
|
|
|
|
+ if hitsTail(&snake, snake.head.x, snake.head.y) {
|
|
|
close(gamestate)
|
|
close(gamestate)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -95,10 +97,10 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|
|
screen.Clear()
|
|
screen.Clear()
|
|
|
|
|
|
|
|
// Draw the screen
|
|
// Draw the screen
|
|
|
- drawBox(screen, 0, 0, screenx-1, screeny-1, style)
|
|
|
|
|
- drawCoords(screen, style, &snakex, &snakey)
|
|
|
|
|
- drawApple(screen, style, apple.x, apple.y)
|
|
|
|
|
- drawSnake(screen, style, snakex, snakey)
|
|
|
|
|
|
|
+ drawBox(screen, style, 0, 0, screenx-1, screeny-1)
|
|
|
|
|
+ drawCoords(screen, style, &snake)
|
|
|
|
|
+ drawApple(screen, style, &apple)
|
|
|
|
|
+ drawSnake(screen, style, &snake)
|
|
|
drawScore(screen, style, score)
|
|
drawScore(screen, style, score)
|
|
|
|
|
|
|
|
// Draw the screen
|
|
// Draw the screen
|