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