Compare commits
No commits in common. "b17918f05221c238a50def6b1bce1faf7dab72f7" and "44724cbbb6f1b7002150190c50ec7cb58036edda" have entirely different histories.
b17918f052
...
44724cbbb6
43
apple.go
43
apple.go
@ -2,46 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
"github.com/gdamore/tcell/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// getFoodItem returns an emoji from a string used to represent food
|
// getFoodItem returns an emoji from a string used to represent food
|
||||||
// Takes no arguments, but returns an emoji/rune from a predefined string
|
// Takes no arguments, but returns an emoji/rune from a predefined string
|
||||||
// func getFoodItem() rune {
|
func getFoodItem() rune {
|
||||||
// var foods string = "🐵🐱🐷🐁🐇🐿🦇🐓🐣🐸🦎🍎🍏"
|
var foods string = "🐵🐱🐷🐁🐇🐿🦇🐓🐣🐸🦎🍎🍏"
|
||||||
// foodsRune := []rune(foods)
|
foodsRune := []rune(foods)
|
||||||
// randomIndex := rand.Intn(len(foodsRune))
|
randomIndex := rand.Intn(len(foodsRune))
|
||||||
// return foodsRune[randomIndex]
|
return foodsRune[randomIndex]
|
||||||
// }
|
|
||||||
|
|
||||||
func placeApple(snakex *int, snakey *int) Apple {
|
|
||||||
var x, y int
|
|
||||||
var found bool
|
|
||||||
|
|
||||||
for !found {
|
|
||||||
|
|
||||||
x = rand.Intn(80)
|
|
||||||
y = rand.Intn(24)
|
|
||||||
|
|
||||||
if x != *snakex && y != *snakey {
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
if x == 0 || x == 79 || y == 0 || y == 23 {
|
|
||||||
found = false
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
apple := Apple{
|
|
||||||
x: x,
|
|
||||||
y: y,
|
|
||||||
}
|
|
||||||
|
|
||||||
return apple
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func drawApple(screen tcell.Screen, style tcell.Style, x int, y int) {
|
|
||||||
screen.SetContent(x, y, '', nil, style)
|
|
||||||
}
|
}
|
||||||
|
21
game.go
21
game.go
@ -43,8 +43,8 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|||||||
// Destined to die, even before I was born.
|
// Destined to die, even before I was born.
|
||||||
defer quit(screen)
|
defer quit(screen)
|
||||||
|
|
||||||
var score int = 0
|
//var score int = 0
|
||||||
var scoreCounter int = 1
|
//var scoreCounter int = 1
|
||||||
//var field [80][24]int
|
//var field [80][24]int
|
||||||
// fill up the field:
|
// fill up the field:
|
||||||
// 0: empty
|
// 0: empty
|
||||||
@ -52,19 +52,12 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|||||||
// 2: apple
|
// 2: apple
|
||||||
// 3: border
|
// 3: border
|
||||||
|
|
||||||
// Board size
|
|
||||||
var screenx = 80
|
var screenx = 80
|
||||||
var screeny = 24
|
var screeny = 24
|
||||||
|
|
||||||
// Have snake start at the center of the board
|
|
||||||
var snakex int = (screenx / 2) - 1
|
var snakex int = (screenx / 2) - 1
|
||||||
var snakey int = (screeny / 2) - 1
|
var snakey int = (screeny / 2) - 1
|
||||||
|
|
||||||
// Last direction we went in
|
|
||||||
var lastpress int = 1
|
var lastpress int = 1
|
||||||
|
|
||||||
var apple Apple = placeApple(&snakex, &snakey)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
// Take input or sleep
|
// Take input or sleep
|
||||||
@ -76,13 +69,6 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|||||||
snakeDirection(lastpress, &snakex, &snakey)
|
snakeDirection(lastpress, &snakex, &snakey)
|
||||||
}
|
}
|
||||||
|
|
||||||
if snakex == apple.x && snakey == apple.y {
|
|
||||||
apple = placeApple(&snakex, &snakey)
|
|
||||||
score += scoreCounter
|
|
||||||
scoreCounter++
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call it quits once the snake hits the border.
|
|
||||||
if snakex == 0 || snakex == screenx-1 {
|
if snakex == 0 || snakex == screenx-1 {
|
||||||
close(gamestate)
|
close(gamestate)
|
||||||
return
|
return
|
||||||
@ -98,8 +84,6 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
|
|||||||
drawBox(screen, 0, 0, screenx-1, screeny-1, style)
|
drawBox(screen, 0, 0, screenx-1, screeny-1, style)
|
||||||
drawCoords(screen, style, &snakex, &snakey)
|
drawCoords(screen, style, &snakex, &snakey)
|
||||||
drawSnake(screen, style, snakex, snakey)
|
drawSnake(screen, style, snakex, snakey)
|
||||||
drawApple(screen, style, apple.x, apple.y)
|
|
||||||
drawScore(screen, style, score)
|
|
||||||
|
|
||||||
// Draw the screen
|
// Draw the screen
|
||||||
screen.Show()
|
screen.Show()
|
||||||
@ -146,6 +130,7 @@ func keyboardProcessor(screen tcell.Screen, keypresses chan int, gamestate chan
|
|||||||
// This function needs to know if the game is over.
|
// This function needs to know if the game is over.
|
||||||
select {
|
select {
|
||||||
case <-time.After(10 * time.Millisecond):
|
case <-time.After(10 * time.Millisecond):
|
||||||
|
|
||||||
case _, ok := <-gamestate:
|
case _, ok := <-gamestate:
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
|
17
main.go
17
main.go
@ -4,34 +4,19 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Position struct {
|
|
||||||
x int
|
|
||||||
y int
|
|
||||||
}
|
|
||||||
|
|
||||||
type Apple Position
|
|
||||||
|
|
||||||
//type Snake Position
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Create channles to manage keypresses and the gamestate
|
// Create a channel to push key-presses through
|
||||||
keypresses := make(chan int)
|
keypresses := make(chan int)
|
||||||
gamestate := make(chan int)
|
gamestate := make(chan int)
|
||||||
|
|
||||||
// Initialize tcell and clean up when needed.
|
|
||||||
screen, style, err := initilize()
|
screen, style, err := initilize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Initlization error: ", err)
|
log.Fatalln("Initlization error: ", err)
|
||||||
}
|
}
|
||||||
defer quit(screen)
|
defer quit(screen)
|
||||||
|
|
||||||
// Spawn the Game Director in its own go routine
|
|
||||||
// We give it channels to control...
|
|
||||||
go gameDirector(screen, style, keypresses, gamestate)
|
go gameDirector(screen, style, keypresses, gamestate)
|
||||||
|
|
||||||
// A simple function that captures keyboard presses...
|
|
||||||
// ...and sends it to the GameDirector.
|
|
||||||
keyboardProcessor(screen, keypresses, gamestate)
|
keyboardProcessor(screen, keypresses, gamestate)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
22
render.go
22
render.go
@ -6,17 +6,17 @@ import (
|
|||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func checkSize(screen tcell.Screen) bool {
|
func checkSize(screen tcell.Screen) bool {
|
||||||
// var retval bool = true
|
var retval bool = true
|
||||||
// x, y := screen.Size()
|
x, y := screen.Size()
|
||||||
// if x < 80 {
|
if x < 80 {
|
||||||
// retval = false
|
retval = false
|
||||||
// }
|
}
|
||||||
// if y < 24 {
|
if y < 24 {
|
||||||
// retval = false
|
retval = false
|
||||||
// }
|
}
|
||||||
// return retval
|
return retval
|
||||||
// }
|
}
|
||||||
|
|
||||||
func drawScore(screen tcell.Screen, style tcell.Style, score int) {
|
func drawScore(screen tcell.Screen, style tcell.Style, score int) {
|
||||||
var x, y int = 5, 24
|
var x, y int = 5, 24
|
||||||
|
Loading…
x
Reference in New Issue
Block a user