#2 Start at the center of the screen. Always goes right. Sorta dies when it hits the border, but only when you are pressing a key. If it runs automatically into the border the game freezes.
This commit is contained in:
parent
1f58fa5dbd
commit
de33a25864
81
game.go
81
game.go
@ -14,6 +14,11 @@ const (
|
||||
Down
|
||||
)
|
||||
|
||||
const (
|
||||
Over = iota
|
||||
Running
|
||||
)
|
||||
|
||||
func initilize() (tcell.Screen, tcell.Style, error) {
|
||||
style := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
|
||||
|
||||
@ -38,9 +43,12 @@ func quit(screen tcell.Screen) {
|
||||
}
|
||||
}
|
||||
|
||||
func game_director(screen tcell.Screen, style tcell.Style, keypresses chan int) {
|
||||
func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, gamestate chan int) {
|
||||
|
||||
//var score int
|
||||
// Destined to die, even before I was born.
|
||||
defer quit(screen)
|
||||
|
||||
//var score int = 0
|
||||
//var scoreCounter int = 1
|
||||
//var field [80][24]int
|
||||
// fill up the field:
|
||||
@ -49,56 +57,59 @@ func game_director(screen tcell.Screen, style tcell.Style, keypresses chan int)
|
||||
// 2: apple
|
||||
// 3: border
|
||||
|
||||
x, y := screen.Size()
|
||||
drawBox(screen, 0, 0, x-1, y-1, style)
|
||||
|
||||
var snakex int = 3
|
||||
var snakey int = 12
|
||||
//var lastpress int
|
||||
var screenx = 80
|
||||
var screeny = 24
|
||||
var snakex int = (screenx / 2) - 1
|
||||
var snakey int = (screeny / 2) - 1
|
||||
var lastpress int = 1
|
||||
|
||||
for {
|
||||
|
||||
// Take input or sleep
|
||||
select {
|
||||
case press := <-keypresses:
|
||||
snakeDirection(press, &snakex, &snakey)
|
||||
lastpress = press
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
snakeDirection(lastpress, &snakex, &snakey)
|
||||
}
|
||||
|
||||
if snakex == 0 || snakex == screenx-1 {
|
||||
gamestate <- Over
|
||||
return
|
||||
} else if snakey == 0 || snakey == screeny-1 {
|
||||
gamestate <- Over
|
||||
return
|
||||
}
|
||||
|
||||
// Clean the screen
|
||||
screen.Clear()
|
||||
|
||||
drawBox(screen, 0, 0, x-1, y-1, style)
|
||||
updateScore(screen, style, snakex)
|
||||
|
||||
select {
|
||||
case press := <-keypresses:
|
||||
if press == Left {
|
||||
snakex--
|
||||
} else if press == Right {
|
||||
snakex++
|
||||
} else if press == Up {
|
||||
snakey--
|
||||
} else if press == Down {
|
||||
snakey++
|
||||
}
|
||||
case <-time.After(1 * time.Second):
|
||||
snakex++
|
||||
}
|
||||
|
||||
//updateSnakePos(&snakex, &snakey)
|
||||
// Draw the screen
|
||||
drawBox(screen, 0, 0, screenx-1, screeny-1, style)
|
||||
drawCoords(screen, style, &snakex, &snakey)
|
||||
drawSnake(screen, style, snakex, snakey)
|
||||
|
||||
// Draw the screen
|
||||
screen.Show()
|
||||
|
||||
if snakex > 75 {
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func keyboardProcessor(screen tcell.Screen, keypresses chan int) {
|
||||
func keyboardProcessor(screen tcell.Screen, keypresses chan int, gamestate chan int) {
|
||||
defer close(keypresses)
|
||||
|
||||
for {
|
||||
|
||||
select {
|
||||
case state := <-gamestate:
|
||||
if state == Over {
|
||||
return
|
||||
}
|
||||
case <-time.After(10 * time.Millisecond):
|
||||
}
|
||||
|
||||
// Poll for an event
|
||||
ev := screen.PollEvent()
|
||||
|
||||
@ -110,7 +121,6 @@ func keyboardProcessor(screen tcell.Screen, keypresses chan int) {
|
||||
// updateScore(screen, style, snakex)
|
||||
// screen.Sync()
|
||||
case *tcell.EventKey:
|
||||
//fmt.Println("Key: ",ev.Rune())
|
||||
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
|
||||
return
|
||||
} else if ev.Rune() == 'q' || ev.Rune() == 'Q' {
|
||||
@ -129,6 +139,7 @@ func keyboardProcessor(screen tcell.Screen, keypresses chan int) {
|
||||
keypresses <- Up
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
8
main.go
8
main.go
@ -8,15 +8,15 @@ func main() {
|
||||
|
||||
// Create a channel to push key-presses through
|
||||
keypresses := make(chan int)
|
||||
gamestate := make(chan int)
|
||||
|
||||
screen, style, err := initilize()
|
||||
if err != nil {
|
||||
log.Fatalln("Initlization error: ", err)
|
||||
}
|
||||
defer quit(screen)
|
||||
|
||||
go game_director(screen, style, keypresses)
|
||||
keyboardProcessor(screen, keypresses)
|
||||
|
||||
quit(screen)
|
||||
go gameDirector(screen, style, keypresses, gamestate)
|
||||
keyboardProcessor(screen, keypresses, gamestate)
|
||||
|
||||
}
|
||||
|
24
render.go
24
render.go
@ -6,14 +6,6 @@ import (
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
/*
|
||||
func renderScreen(&screen tcell.Screen, event tcell.Event, score int) {
|
||||
if checkSize(*screen) == false {
|
||||
return
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func checkSize(screen tcell.Screen) bool {
|
||||
var retval bool = true
|
||||
x, y := screen.Size()
|
||||
@ -26,16 +18,22 @@ func checkSize(screen tcell.Screen) bool {
|
||||
return retval
|
||||
}
|
||||
|
||||
func updateScore(screen tcell.Screen, style tcell.Style, score int) {
|
||||
var x, y int
|
||||
_, y = screen.Size()
|
||||
x = 5
|
||||
func drawScore(screen tcell.Screen, style tcell.Style, score int) {
|
||||
var x, y int = 5, 24
|
||||
for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(score), 10) + " ]") {
|
||||
screen.SetContent(x, y-1, r, nil, style)
|
||||
x++
|
||||
}
|
||||
}
|
||||
|
||||
func drawCoords(screen tcell.Screen, style tcell.Style, snakex *int, snakey *int) {
|
||||
var x, y int = 25, 24
|
||||
for _, r := range []rune("[ x:" + strconv.FormatInt(int64(*snakex), 10) + " y: " + strconv.FormatInt(int64(*snakey), 10) + " ]") {
|
||||
screen.SetContent(x, y-1, r, nil, style)
|
||||
x++
|
||||
}
|
||||
}
|
||||
|
||||
func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style) {
|
||||
if y2 < y1 {
|
||||
y1, y2 = y2, y1
|
||||
@ -69,6 +67,4 @@ func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style) {
|
||||
s.SetContent(x2, y2, '╝', nil, style)
|
||||
}
|
||||
|
||||
//drawText(s, x1+1, y1+1, x2-1, y2-1, style, text)
|
||||
updateScore(s, style, 10)
|
||||
}
|
||||
|
16
snake.go
16
snake.go
@ -4,10 +4,18 @@ import (
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
func updateSnakePos(x *int, y *int) {
|
||||
*x = *x + 1
|
||||
}
|
||||
|
||||
func drawSnake(screen tcell.Screen, style tcell.Style, x int, y int) {
|
||||
screen.SetContent(x, y, '🐍', nil, style)
|
||||
}
|
||||
|
||||
func snakeDirection(press int, snakex *int, snakey *int) {
|
||||
if press == Left {
|
||||
*snakex--
|
||||
} else if press == Right {
|
||||
*snakex++
|
||||
} else if press == Up {
|
||||
*snakey--
|
||||
} else if press == Down {
|
||||
*snakey++
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user