Splitting up functions, introducing the game director

This commit is contained in:
Sacha Ligthert 2023-11-07 21:47:32 +01:00
parent b2928ec073
commit 9c1a9fc83d
5 changed files with 136 additions and 71 deletions

View File

123
game.go
View File

@ -1,63 +1,14 @@
package main package main
import ( import (
"fmt"
"log" "log"
"strconv" "time"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
) )
func updateScore(screen tcell.Screen, style tcell.Style, score int) { func initilize() (tcell.Screen, tcell.Style, error) {
var x, y int style := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
_, y = screen.Size()
x = 5
for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(score), 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
}
if x2 < x1 {
x1, x2 = x2, x1
}
// Fill background
for row := y1; row <= y2; row++ {
for col := x1; col <= x2; col++ {
s.SetContent(col, row, ' ', nil, style)
}
}
// Draw borders
for col := x1; col <= x2; col++ {
s.SetContent(col, y1, '═', nil, style)
s.SetContent(col, y2, '═', nil, style)
}
for row := y1 + 1; row < y2; row++ {
s.SetContent(x1, row, '║', nil, style)
s.SetContent(x2, row, '║', nil, style)
}
// Only draw corners if necessary
if y1 != y2 && x1 != x2 {
s.SetContent(x1, y1, '╔', nil, style)
s.SetContent(x2, y1, '╗', nil, style)
s.SetContent(x1, y2, '╚', nil, style)
s.SetContent(x2, y2, '╝', nil, style)
}
//drawText(s, x1+1, y1+1, x2-1, y2-1, style, text)
updateScore(s, style, 10)
}
func main() {
defaultStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
var err error var err error
@ -69,33 +20,48 @@ func main() {
if err != nil { if err != nil {
log.Fatalln("Error initializing screen: ", err) log.Fatalln("Error initializing screen: ", err)
} }
return screen, style, err
}
func quit(screen tcell.Screen) {
maybePanic := recover()
screen.Fini()
if maybePanic != nil {
panic(maybePanic)
}
}
func game_director(screen tcell.Screen, style tcell.Style) {
//var score int
//var scoreCounter int = 1
//var field [80][24]int
// fill up the field:
// 0: empty
// 1: snake
// 2: apple
// 3: border
x, y := screen.Size() x, y := screen.Size()
drawBox(screen, 0, 0, x-1, y-1, style)
screen.Clear() var snakex int = 3
var snakey int = 12
drawBox(screen, 0, 0, x-1, y-1, defaultStyle)
quit := func() {
// You have to catch panics in a defer, clean up, and
// re-raise them - otherwise your application can
// die without leaving any diagnostic trace.
maybePanic := recover()
screen.Fini()
if maybePanic != nil {
panic(maybePanic)
}
}
defer quit()
for { for {
screen.Show()
// Clean the screen
screen.Clear()
// Poll for an event
ev := screen.PollEvent() ev := screen.PollEvent()
// Fetch the type, and check if any other actions are required.
switch ev := ev.(type) { switch ev := ev.(type) {
case *tcell.EventResize: case *tcell.EventResize:
x, y := screen.Size() x, y := screen.Size()
drawBox(screen, 0, 0, x-1, y-1, defaultStyle) drawBox(screen, 0, 0, x-1, y-1, style)
fmt.Println("size=ok:", checkSize(screen)) updateScore(screen, style, snakex)
screen.Sync() screen.Sync()
case *tcell.EventKey: case *tcell.EventKey:
//fmt.Println("Key: ",ev.Rune()) //fmt.Println("Key: ",ev.Rune())
@ -108,6 +74,23 @@ func main() {
} else if ev.Rune() == 'C' || ev.Rune() == 'c' { } else if ev.Rune() == 'C' || ev.Rune() == 'c' {
screen.Clear() screen.Clear()
} }
default:
time.Sleep(500 * time.Millisecond)
} }
drawBox(screen, 0, 0, x-1, y-1, style)
updateScore(screen, style, snakex)
updateSnakePos(&snakex, &snakey)
drawSnake(screen, style, snakex, snakey)
// Draw the screen
screen.Show()
if snakex > 75 {
return
}
} }
} }

18
main.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"log"
)
func main() {
screen, style, err := initilize()
if err != nil {
log.Fatalln("Initlization error: ", err)
}
game_director(screen, style)
quit(screen)
}

View File

@ -1,6 +1,10 @@
package main package main
import "github.com/gdamore/tcell/v2" import (
"strconv"
"github.com/gdamore/tcell/v2"
)
/* /*
func renderScreen(&screen tcell.Screen, event tcell.Event, score int) { func renderScreen(&screen tcell.Screen, event tcell.Event, score int) {
@ -21,3 +25,50 @@ func checkSize(screen tcell.Screen) bool {
} }
return retval return retval
} }
func updateScore(screen tcell.Screen, style tcell.Style, score int) {
var x, y int
_, y = screen.Size()
x = 5
for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(score), 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
}
if x2 < x1 {
x1, x2 = x2, x1
}
// Fill background
for row := y1; row <= y2; row++ {
for col := x1; col <= x2; col++ {
s.SetContent(col, row, ' ', nil, style)
}
}
// Draw borders
for col := x1; col <= x2; col++ {
s.SetContent(col, y1, '═', nil, style)
s.SetContent(col, y2, '═', nil, style)
}
for row := y1 + 1; row < y2; row++ {
s.SetContent(x1, row, '║', nil, style)
s.SetContent(x2, row, '║', nil, style)
}
// Only draw corners if necessary
if y1 != y2 && x1 != x2 {
s.SetContent(x1, y1, '╔', nil, style)
s.SetContent(x2, y1, '╗', nil, style)
s.SetContent(x1, y2, '╚', nil, style)
s.SetContent(x2, y2, '╝', nil, style)
}
//drawText(s, x1+1, y1+1, x2-1, y2-1, style, text)
updateScore(s, style, 10)
}

13
snake.go Normal file
View File

@ -0,0 +1,13 @@
package main
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)
}