Skip to content

Commit 948eb23

Browse files
Small updates
1 parent fdd6e5c commit 948eb23

8 files changed

Lines changed: 66 additions & 51 deletions

File tree

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
gobra
2+
3+
# Binaries for programs and plugins
4+
*.exe
5+
*.exe~
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, built with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Dependency directories (remove the comment below to include it)
17+
# vendor/
18+
19+
# Go workspace file
20+
go.work
21+
go.work.sum
22+
23+
# env file
24+
.env

cmd/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"flag"
5-
"fmt"
65
"gobra/internal/gameplay"
76
"os"
87
"os/signal"
@@ -24,8 +23,7 @@ func main() {
2423
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
2524
go func() {
2625
<-sigs
27-
gameplay.Run = false // TODO: Cancel context
26+
gameplay.Run = false // TODO: Cancellable context
2827
}()
29-
fmt.Println(cfg.Vim)
3028
gameplay.Start(&cfg)
3129
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ require (
99

1010
require (
1111
github.com/ebitengine/purego v0.4.0 // indirect
12+
github.com/hajimehoshi/oto v1.0.1 // indirect
13+
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8 // indirect
14+
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067 // indirect
15+
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6 // indirect
1216
golang.org/x/sys v0.7.0 // indirect
1317
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
github.com/ebitengine/purego v0.4.0 h1:RQVuMIxQPQ5iCGEJvjQ17YOK+1tMKjVau2FUMvXH4HE=
22
github.com/ebitengine/purego v0.4.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
3+
github.com/hajimehoshi/oto v1.0.1 h1:8AMnq0Yr2YmzaiqTg/k1Yzd6IygUGk2we9nmjgbgPn4=
4+
github.com/hajimehoshi/oto v1.0.1/go.mod h1:wovJ8WWMfFKvP587mhHgot/MBr4DnNy9m6EepeVGnos=
35
github.com/hajimehoshi/oto/v2 v2.4.1 h1:iTfZSulqdmQ5Hh4tVyVzNnK3aA4SgjbDapSM0YH3Lc4=
46
github.com/hajimehoshi/oto/v2 v2.4.1/go.mod h1:guyF8uIgSrchrKewS1E6Xyx7joUbKOi4g9W7vpcYBSc=
57
github.com/rthornton128/goncurses v0.0.0-20231014161942-82671379df88 h1:bNvZ7P4l4rvg1O3qNDRJ4FNgKASMoG+V7OIpu4SIkgA=
68
github.com/rthornton128/goncurses v0.0.0-20231014161942-82671379df88/go.mod h1:AHlKFomPTwmO7H2vL8d7VNrQNQmhMi/DBhDnHRhjbCo=
79
github.com/rthornton128/goncurses v0.0.0-20240804152857-da6485a3b6d7 h1:HRzMcsVbf0rNRrRX/h43syyiRAJFbJFpZFQEUhBJPAo=
810
github.com/rthornton128/goncurses v0.0.0-20240804152857-da6485a3b6d7/go.mod h1:AHlKFomPTwmO7H2vL8d7VNrQNQmhMi/DBhDnHRhjbCo=
11+
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8 h1:idBdZTd9UioThJp8KpM/rTSinK/ChZFBE43/WtIy8zg=
12+
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
13+
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067 h1:KYGJGHOQy8oSi1fDlSpcZF0+juKwk/hEMv5SiwHogR0=
14+
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
15+
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6 h1:vyLBGJPIl9ZYbcQFM2USFmJBK6KI+t+z6jL0lbwjrnc=
16+
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
17+
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
18+
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
919
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
1020
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
21+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

internal/gameplay/controls.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ func initKeybindings(isVim bool) {
5454
keyUp = 'k'
5555
keyRight = 'l'
5656
} else {
57-
fmt.Println(" is not Vim")
5857
keyLeft = 'a'
5958
keyUp = 'w'
6059
keyDown = 's'

internal/gameplay/gameboard.go

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
gc "github.com/rthornton128/goncurses"
1313
)
1414

15-
// Trackers
1615
var (
1716
globalScore int
1817
startTime time.Time
@@ -54,7 +53,6 @@ type Screen struct {
5453
rows, cols int
5554
}
5655

57-
// Logo
5856
func drawBorder(stdscr *gc.Window) {
5957
stdscr.ColorOn(3)
6058
stdscr.Box(gc.ACS_VLINE, gc.ACS_HLINE)
@@ -71,46 +69,29 @@ func drawLogo(stdscr *gc.Window, rows, cols int) {
7169

7270
func initColors() {
7371
// Set up colors.
74-
if err := gc.InitPair(1, gc.C_GREEN, gc.C_BLACK); err != nil {
75-
log.Fatal("InitPair failed: ", err)
76-
}
77-
78-
if err := gc.InitPair(2, gc.C_RED, gc.C_BLACK); err != nil {
79-
log.Fatal("InitPair failed: ", err)
80-
}
81-
82-
if err := gc.InitPair(3, gc.C_YELLOW, gc.C_BLACK); err != nil {
83-
log.Fatal("InitPair failed: ", err)
84-
}
85-
86-
if err := gc.InitPair(4, gc.C_MAGENTA, gc.C_BLACK); err != nil {
87-
log.Fatal("InitPair failed: ", err)
88-
}
89-
90-
if err := gc.InitPair(5, gc.C_BLACK, gc.C_BLACK); err != nil {
91-
log.Fatal("InitPair failed: ", err)
92-
}
93-
94-
if err := gc.InitPair(6, gc.C_WHITE, gc.C_BLACK); err != nil {
95-
log.Fatal("InitPair failed: ", err)
96-
}
72+
tools.Check(gc.InitPair(1, gc.C_GREEN, gc.C_BLACK))
73+
tools.Check(gc.InitPair(2, gc.C_RED, gc.C_BLACK))
74+
tools.Check(gc.InitPair(3, gc.C_YELLOW, gc.C_BLACK))
75+
tools.Check(gc.InitPair(4, gc.C_MAGENTA, gc.C_BLACK))
76+
tools.Check(gc.InitPair(5, gc.C_BLACK, gc.C_BLACK))
77+
tools.Check(gc.InitPair(6, gc.C_WHITE, gc.C_BLACK))
9778
}
9879

99-
// InitFood initializes the foods position.
80+
// printFood prints the symbol for food at a given position.
10081
func printFood(stdscr *gc.Window, newFood *Food, rows, cols int) {
10182
stdscr.ColorOn(2)
10283
stdscr.MoveAddChar(newFood.y, newFood.x, foodChar)
10384
stdscr.ColorOff(2)
10485
}
10586

106-
// InitFood initializes the foods position
87+
// InitFood initializes the foods position.
10788
func initFood(stdscr *gc.Window, myFood *Food, rows, cols int) {
10889
if myFood.y == 0 && myFood.x == 0 {
10990
for !testFoodCollision(stdscr, myFood, rows, cols) {
11091
myFood.y = rand.Intn(rows)
11192
myFood.x = rand.Intn(cols)
11293
}
113-
// Start timer for score
94+
// Start the timer for the score calculation.
11495
startTime = time.Now()
11596
}
11697
}
@@ -268,7 +249,7 @@ func calcBoardSize(stdscr *gc.Window) {
268249
}
269250

270251
func SetupGameBoard() *gc.Window {
271-
// Setup stdscr buffer
252+
// Setup stdscr buffer.
272253
stdscr, err := gc.Init()
273254
calcBoardSize(stdscr)
274255
if err != nil {
@@ -283,10 +264,8 @@ func SetupGameBoard() *gc.Window {
283264
log.Fatal("Require a color capable terminal")
284265
}
285266

286-
// Initalize use of color
287-
if err := gc.StartColor(); err != nil {
288-
log.Fatal(err)
289-
}
267+
// Initalize the use of color.
268+
tools.Check(gc.StartColor())
290269

291270
gc.Echo(false)
292271
gc.Cursor(0) // Hide cursor

internal/gameplay/gobra.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ func Start(cfg *Config) {
8888

8989
var scoreLength int
9090
for Run {
91-
// Clear screen
91+
// Clear the screen.
9292
stdscr.Refresh()
9393
stdscr.Erase()
9494

95-
// Draw box around the screen (for collision detection)
95+
// Draw box around the screen (for collision detection).
9696
// TODO: Do we need to redraw everything?
9797
drawBorder(stdscr)
9898

@@ -111,35 +111,35 @@ func Start(cfg *Config) {
111111
stdscr.MovePrint(7, 1, rune(stdscr.MoveInChar(0, 0)))
112112
}
113113

114-
// setSnakeDir returns false when the user presses q to exit -> interrupt loop
114+
// setSnakeDir returns false when the user presses q to exit -> interrupt loop.
115115
if !HandleKeys(input, stdscr, &newFood) {
116116
break
117117
}
118118

119-
// Determine food position if not set yet
119+
// Determine the food position if not set yet.
120120
initFood(stdscr, &newFood, screen.rows, screen.cols)
121121

122122
//stdscr.Refresh()
123123

124-
// Display snake (alive or dead)
124+
// Display the snake (alive or dead).
125125
handleSnake(stdscr, screen.rows, screen.cols)
126126

127-
// Handle collisions
127+
// Handle collisions.
128128
if !handleCollisions(stdscr, &newFood, screen.rows, screen.cols) && cfg.Sound {
129129
sound.Play(sound.FreqA, 250*time.Millisecond)
130130
}
131131

132-
// Check if snake hit boundaries, if desired ports the snake to the other side of the screen
132+
// Check if snake hit boundaries, if desired ports the snake to the other side of the screen.
133133
boundaryCheck(cfg.NoBounds, screen.rows, screen.cols)
134134

135-
// Render food symbol
135+
// Render food symbol.
136136
printFood(stdscr, &newFood, screen.rows, screen.cols)
137137

138-
// Overwrite border once again
138+
// Overwrite border once again.
139139
drawBorder(stdscr)
140140
scoreLength = len(strconv.Itoa(globalScore))
141141

142-
// Write score to border
142+
// Write score to border.
143143
stdscr.ColorOn(4)
144144
stdscr.MovePrint(0, (screen.cols/2)-(scoreLength/2), globalScore)
145145
stdscr.ColorOff(4)
@@ -153,13 +153,13 @@ func Start(cfg *Config) {
153153
}
154154

155155
stdscr.ColorOff(3)
156-
// Refresh changes in screen buffer
156+
// Refresh changes in screen buffer.
157157
stdscr.Refresh()
158-
// Flush characters that have changed
158+
// Flush characters that have changed.
159159
tools.Check(gc.Update())
160160
}
161161

162-
gc.End() // Restore previous terminal state
162+
gc.End() // Restore previous terminal state.
163163
}
164164

165165
// MoveSnake updates the snake's position based on its current direction.

internal/sound/sound.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
bitDepthInBytes = flag.Int("bitdepthinbytes", 2, "bit depth in bytes")
1818
)
1919

20-
const FreqA = 300 // sound frequency
20+
const FreqA = 300 // This is the frequency of the generated sound.
2121

2222
type sineWave struct {
2323
freq float64

0 commit comments

Comments
 (0)