Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sound effects to the game #6

Merged
merged 4 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions assets/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package assets

import "embed"

//go:embed *
var Assets embed.FS
Binary file added assets/gameOver.mp3
Binary file not shown.
Binary file added assets/hiss.mp3
Binary file not shown.
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ module github.com/liweiyi88/gosnakego

go 1.19

require github.com/gdamore/tcell/v2 v2.5.3
require (
github.com/faiface/beep v1.1.0
github.com/gdamore/tcell/v2 v2.5.3
)

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/hajimehoshi/go-mp3 v0.3.0 // indirect
github.com/hajimehoshi/oto v0.7.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8 // indirect
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067 // indirect
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/term v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
Expand Down
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo=
github.com/faiface/beep v1.1.0 h1:A2gWP6xf5Rh7RG/p9/VAW2jRSDEGQm5sbOb38sf5d4c=
github.com/faiface/beep v1.1.0/go.mod h1:6I8p6kK2q4opL/eWb+kAkk38ehnTunWeToJB+s51sT4=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/gdamore/tcell/v2 v2.5.3 h1:b9XQrT6QGbgI7JvZOJXFNczOQeIYbo8BfeSMzt2sAV0=
github.com/gdamore/tcell/v2 v2.5.3/go.mod h1:wSkrPaXoiIWZqW/g7Px4xc79di6FTcpB8tvaKJ6uGBo=
github.com/go-audio/audio v1.0.0/go.mod h1:6uAu0+H2lHkwdGsAY+j2wHPNPpPoeg5AaEFh9FlA+Zs=
github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38rVG498=
github.com/go-audio/wav v1.0.0/go.mod h1:3yoReyQOsiARkvPl3ERCi8JFjihzG6WhjYpZCf5zAWE=
github.com/hajimehoshi/go-mp3 v0.3.0 h1:fTM5DXjp/DL2G74HHAs/aBGiS9Tg7wnp+jkU38bHy4g=
github.com/hajimehoshi/go-mp3 v0.3.0/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
github.com/hajimehoshi/oto v0.7.1 h1:I7maFPz5MBCwiutOrz++DLdbr4rTzBsbBuV2VpgU9kk=
github.com/hajimehoshi/oto v0.7.1/go.mod h1:wovJ8WWMfFKvP587mhHgot/MBr4DnNy9m6EepeVGnos=
github.com/icza/bitio v1.0.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A=
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
github.com/jfreymuth/oggvorbis v1.0.1/go.mod h1:NqS+K+UXKje0FUYUPosyQ+XTVvjmVjps1aEZH1sumIk=
github.com/jfreymuth/vorbis v1.0.0/go.mod h1:8zy3lUAm9K/rJJk223RKy6vjCZTWC61NA2QD06bfOE0=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mewkiz/flac v1.0.7/go.mod h1:yU74UH277dBUpqxPouHSQIar3G1X/QIclVbFahSd1pU=
github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2/go.mod h1:3E2FUC/qYUfM8+r9zAwpeHJzqRVVMIYnpzD/clwWxyA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8 h1:idBdZTd9UioThJp8KpM/rTSinK/ChZFBE43/WtIy8zg=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20190220214146-31aff87c08e9/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067 h1:KYGJGHOQy8oSi1fDlSpcZF0+juKwk/hEMv5SiwHogR0=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6 h1:vyLBGJPIl9ZYbcQFM2USFmJBK6KI+t+z6jL0lbwjrnc=
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
Expand Down
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package main

import "github.com/liweiyi88/gosnakego/snake"
import (
"flag"

"github.com/liweiyi88/gosnakego/snake"
)

func main() {
snake.StartGame()
silent := flag.Bool("silent", false, "do not play sound")
flag.Parse()
snake.StartGame(*silent)
}
16 changes: 11 additions & 5 deletions snake/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Game struct {
Board *Board
Snake *Snake
screen tcell.Screen
sound Sound
}

func init() {
Expand Down Expand Up @@ -130,6 +131,7 @@ func (g *Game) over() {
g.mu.Lock()
defer g.mu.Unlock()
g.isOver = true
g.sound.GameOver()
}

// Display text in terminal.
Expand Down Expand Up @@ -202,6 +204,7 @@ func (g *Game) updateItemState() {
g.Snake.move(g.direction)

if g.Snake.CanEat(g.Apple) {
g.sound.Hiss()
g.Snake.eat(g.Apple)
g.score++
g.setNewApplePosition()
Expand Down Expand Up @@ -253,7 +256,6 @@ func (g *Game) run(directionChan chan int) {
if g.shouldContinue() {
g.updateItemState()
}

g.updateScreen()
}
}
Expand Down Expand Up @@ -298,7 +300,7 @@ func (g *Game) handleKeyBoardEvents(directionChan chan int) {
}

// Create a new game.
func newGame(board *Board) *Game {
func newGame(board *Board, silent bool) *Game {
screen, err := tcell.NewScreen()

if err != nil {
Expand All @@ -310,13 +312,17 @@ func newGame(board *Board) *Game {

defStyle := tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite)
screen.SetStyle(defStyle)

sound, err := NewSound(silent)
if err != nil {
log.Fatalf("%+v", err)
}
game := &Game{
Board: board,
Snake: newSnake(),
direction: Up,
speed: time.Millisecond * 100,
screen: screen,
sound: sound,
}

game.setNewApplePosition()
Expand All @@ -326,9 +332,9 @@ func newGame(board *Board) *Game {
}

// Start the snake game.
func StartGame() {
func StartGame(silent bool) {
directionChan := make(chan int, 10)
game := newGame(newBoard(50, 20))
game := newGame(newBoard(50, 20), silent)

go game.run(directionChan)
game.handleKeyBoardEvents(directionChan)
Expand Down
81 changes: 81 additions & 0 deletions snake/sound.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package snake

import (
"time"

"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"github.com/liweiyi88/gosnakego/assets"
)

const fileGameOver = "gameOver.mp3"
const fileHiss = "hiss.mp3"

type Sound interface {
GameOver()
Hiss()
}

type Muted struct{}

type UnMuted struct {
gameOver *beep.Buffer
hiss *beep.Buffer
}

func NewSound(silent bool) (Sound, error) {
if silent {
return Muted{}, nil
}
hiss, _, err := load(fileHiss)
if err != nil {
return UnMuted{}, err
}
gameOver, format, err := load(fileGameOver)
if err != nil {
return UnMuted{}, err
}
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
return UnMuted{
gameOver: gameOver,
hiss: hiss,
}, nil
}

func load(file string) (*beep.Buffer, beep.Format, error) {
f, err := assets.Assets.Open(file)
if err != nil {
return nil, beep.Format{}, err
}
streamer, format, err := mp3.Decode(f)
if err != nil {
return nil, beep.Format{}, err
}
buffer := beep.NewBuffer(format)
buffer.Append(streamer)
streamer.Close()
return buffer, format, nil
}

func (s UnMuted) GameOver() {
play(s.gameOver)
}

func (s UnMuted) Hiss() {
// separate goroutine to avoid snake seem stuck
go play(s.hiss)
}

func play(buffer *beep.Buffer) {
streamer := buffer.Streamer(0, buffer.Len())
done := make(chan bool)
speaker.Play(beep.Seq(streamer, beep.Callback(func() {
done <- true
})))
<-done
}

func (s Muted) GameOver() {}

func (s Muted) Hiss() {}