|
|
@@ -31,26 +31,19 @@ func quit(screen tcell.Screen) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, gamestate chan int) {
|
|
|
+func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, gamestate chan int, score *Score) {
|
|
|
|
|
|
// Destined to die, even before I was born.
|
|
|
defer quit(screen)
|
|
|
|
|
|
- var score int = 0
|
|
|
- var scoreCounter int = 1
|
|
|
-
|
|
|
// Board size
|
|
|
var screenx = 80
|
|
|
var screeny = 24
|
|
|
|
|
|
- // Have snake start at the center of the board
|
|
|
- var snakex int = (screenx / 2) - 1
|
|
|
- var snakey int = (screeny / 2) - 1
|
|
|
-
|
|
|
// Last direction we went in
|
|
|
var lastpress int = 1
|
|
|
|
|
|
- var snake Snake = initSnake(&snakex, &snakey)
|
|
|
+ var snake Snake = initSnake((screenx/2)-1, (screeny/2)-1)
|
|
|
var apple Apple = placeApple(&snake)
|
|
|
|
|
|
for {
|
|
|
@@ -68,22 +61,25 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|
|
|
|
|
if snake.head.x == apple.x && snake.head.y == apple.y {
|
|
|
apple = placeApple(&snake)
|
|
|
- score += scoreCounter
|
|
|
- scoreCounter++
|
|
|
- snake.length = 3 + score
|
|
|
+ score.score += score.scoreCounter
|
|
|
+ score.scoreCounter++
|
|
|
+ snake.length = 3 + score.score
|
|
|
}
|
|
|
|
|
|
// Call it quits once the snake hits the border.
|
|
|
if snake.head.x == 0 || snake.head.x == screenx-1 {
|
|
|
+ score.reason = 2
|
|
|
close(gamestate)
|
|
|
return
|
|
|
} else if snake.head.y == 0 || snake.head.y == screeny-1 {
|
|
|
+ score.reason = 2
|
|
|
close(gamestate)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// Check if head intersects with any parts of the body
|
|
|
if hitsTail(&snake, snake.head.x, snake.head.y) {
|
|
|
+ score.reason = 3
|
|
|
close(gamestate)
|
|
|
return
|
|
|
}
|
|
|
@@ -105,7 +101,7 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|
|
|
|
|
}
|
|
|
|
|
|
-func keyboardProcessor(screen tcell.Screen, keypresses chan int, gamestate chan int) {
|
|
|
+func keyboardProcessor(screen tcell.Screen, keypresses chan int, gamestate chan int, score *Score) {
|
|
|
defer close(keypresses)
|
|
|
|
|
|
for {
|
|
|
@@ -117,8 +113,10 @@ func keyboardProcessor(screen tcell.Screen, keypresses chan int, gamestate chan
|
|
|
switch ev := ev.(type) {
|
|
|
case *tcell.EventKey:
|
|
|
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
|
|
|
+ score.reason = 1
|
|
|
return
|
|
|
} else if ev.Rune() == 'q' || ev.Rune() == 'Q' {
|
|
|
+ score.reason = 1
|
|
|
return
|
|
|
} else if ev.Key() == tcell.KeyCtrlL {
|
|
|
screen.Sync()
|