Skip to content

Commit d5f476d

Browse files
author
Kaiser
committed
added sound
1 parent d232530 commit d5f476d

File tree

16 files changed

+155
-46
lines changed

16 files changed

+155
-46
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
# Gong! - The Go Pong
22

3-
Retro style Pong game based upon ebiten game framework.
3+
Retro styled classic [Pong](https://en.wikipedia.org/wiki/Pong) clone based upon [ebiten](https://ebiten.org/) game framework.
44

55
With go installed, just type
66

77
```bash
8-
go install github.com/jenska/gong
8+
git clone http://github.com/jenska/gong
9+
cd gong
10+
go run main.go
911
```
1012

1113
to start
1214

13-
![Screenshot](screenshot.png)
15+
![Screenshot](assets/screenshot.png)
16+
17+
## Thanks to
18+
19+
[OpenGameArt.Org](https://opengameart.org/)
1420

1521
## Soon to come
1622

1723
- ~~update package structure~~
24+
- ~~stereo sounds~~
1825
- AI support (beat the computer opponent, computer vs computer becomes more interessting)
19-
- stereo sounds
2026
- WebAssembly support

assets/lost.wav

15 KB
Binary file not shown.

assets/menu_select.wav

52.2 KB
Binary file not shown.

assets/ping.wav

6 KB
Binary file not shown.

assets/pong.wav

6.22 KB
Binary file not shown.
File renamed without changes.

assets/win.wav

72.2 KB
Binary file not shown.

game/ball.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ func (b *ball) reset() {
2929
}
3030

3131
func (b *ball) update(g *Gong) {
32-
b.visible = g.state == play || g.state == interrupt
3332
if g.state == play {
3433
b.x += b.xVelocity
3534
b.y += b.yVelocity
3635

3736
if b.y-ballRadius > windowHeight {
37+
playSound(ping)
3838
b.yVelocity = -b.yVelocity
3939
b.y = windowHeight - ballRadius
4040
} else if b.y+ballRadius < 0 {
41+
playSound(ping)
4142
b.yVelocity = -b.yVelocity
4243
b.y = ballRadius
4344
}
4445
}
46+
b.visible = g.state == play || g.state == interrupt
4547
}

game/game.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package game
22

33
import (
44
"image/color"
5+
"math"
56
"os"
67
"time"
78

@@ -52,6 +53,9 @@ var (
5253

5354
// NewGong creates a new gong object
5455
func NewGong() *Gong {
56+
ebiten.SetWindowSize(windowWidth, windowHeight)
57+
ebiten.SetWindowTitle("Gong! - The Go Pong")
58+
ebiten.SetCursorMode(ebiten.CursorModeHidden)
5559
g := &Gong{}
5660
g.reset()
5761
return g
@@ -66,48 +70,61 @@ func (g *Gong) reset() {
6670
newHUD(),
6771
}
6872
g.state = start
69-
7073
}
7174

7275
// Layout sets the screen layout
7376
func (g *Gong) Layout(outsideWidth, outsideHeight int) (int, int) {
7477
return windowWidth, windowHeight
7578
}
7679

80+
func isMenuSelected(key ebiten.Key) bool {
81+
if inpututil.IsKeyJustPressed(key) {
82+
playSound(menuSelect)
83+
return true
84+
}
85+
return false
86+
}
87+
7788
// Update game state and sprites
7889
func (g *Gong) Update(screen *ebiten.Image) error {
7990
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
8091
os.Exit(0)
8192
}
8293
switch g.state {
8394
case start:
84-
if inpututil.IsKeyJustPressed(ebiten.KeyH) {
95+
if isMenuSelected(ebiten.KeyH) {
8596
g.state = controls
86-
} else if inpututil.IsKeyJustPressed(ebiten.KeyA) {
87-
g.isComputer1 = true
97+
} else if isMenuSelected(ebiten.KeyA) {
98+
g.isComputer1, g.isComputer2 = true, false
8899
g.state = play
89-
} else if inpututil.IsKeyJustPressed(ebiten.KeyV) {
100+
} else if isMenuSelected(ebiten.KeyV) {
90101
g.state = play
91-
} else if inpututil.IsKeyJustPressed(ebiten.KeyB) {
102+
} else if isMenuSelected(ebiten.KeyB) {
92103
g.isComputer1, g.isComputer2 = true, true
93104
g.state = play
105+
} else if isMenuSelected(ebiten.KeyF) {
106+
ebiten.SetFullscreen(!ebiten.IsFullscreen())
107+
} else if isMenuSelected(ebiten.KeyW) {
108+
sl.volume = math.Min(1.0, sl.volume+0.1)
109+
} else if isMenuSelected(ebiten.KeyS) {
110+
sl.volume = math.Max(0.0, sl.volume-0.1)
94111
}
95112
case controls:
96-
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
113+
if isMenuSelected(ebiten.KeySpace) {
97114
g.state = start
98115
}
99116
case pause:
100-
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
117+
if isMenuSelected(ebiten.KeySpace) {
101118
g.state = play
102-
} else if inpututil.IsKeyJustPressed(ebiten.KeyR) {
119+
} else if isMenuSelected(ebiten.KeyR) {
103120
g.reset()
104121
}
105122
case play:
106-
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
123+
if isMenuSelected(ebiten.KeySpace) {
107124
g.state = pause
108125
}
109126
case gameOver:
110-
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
127+
if isMenuSelected(ebiten.KeySpace) {
111128
g.reset()
112129
}
113130
case interrupt:
@@ -125,6 +142,7 @@ func (g *Gong) Update(screen *ebiten.Image) error {
125142
// Draw updates the game screen elements drawn
126143
func (g *Gong) Draw(screen *ebiten.Image) {
127144
screen.Fill(screenColor)
145+
128146
for _, object := range g.objects {
129147
object.draw(screen)
130148
}

game/hud.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import (
1414
)
1515

1616
const (
17-
fontSize = 30
18-
smallFontSize = fontSize / 2
17+
fontSize = 30
18+
smallFontSize = fontSize / 2
19+
ghostTextShift = 10
1920
)
2021

2122
type hud struct {
@@ -77,12 +78,16 @@ func (h *hud) update(g *Gong) {
7778
case start:
7879
h.hints = []string{
7980
"",
80-
" GONG! ",
81+
"GONG!",
8182
"",
82-
"H -> HELP ",
83-
"V -> TWO PLAYERS ",
84-
"A -> AI VS PLAYER ",
85-
"B -> AI VS AI "}
83+
"V -> TWO PLAYERS ",
84+
"A -> AI VS PLAYER",
85+
"B -> AI VS AI ",
86+
"",
87+
"F -> Fullscreen ",
88+
fmt.Sprintf("W/S -> Volume %3d%% ", int(sl.volume*100)),
89+
"H -> HELP ",
90+
"ESC -> Exit "}
8691
case controls:
8792
h.hints = []string{
8893
"",
@@ -92,9 +97,7 @@ func (h *hud) update(g *Gong) {
9297
"",
9398
"PLAYER 2: ",
9499
"ARROW UP ",
95-
"ARROW DOWN ",
96-
"",
97-
"ESC -> EXIT"}
100+
"ARROW DOWN "}
98101
h.message = "SPACE -> main menu"
99102
case pause:
100103
h.hints = []string{
@@ -105,7 +108,6 @@ func (h *hud) update(g *Gong) {
105108
case play, interrupt:
106109
h.message = "SPACE -> PAUSE"
107110
h.scores = fmt.Sprintf("%s: %d <-> %s: %d", player1, g.score1, player2, g.score2)
108-
109111
case gameOver:
110112
winner := player1
111113
if g.score2 > g.score1 {
@@ -127,6 +129,6 @@ func (h *hud) draw(screen *ebiten.Image) {
127129

128130
func drawText(y int, str string, font font.Face, size int, screen *ebiten.Image) {
129131
x := (windowWidth - len(str)*size) / 2
130-
text.Draw(screen, str, font, x+10, y, ghostColor)
132+
text.Draw(screen, str, font, x+ghostTextShift, y, ghostColor)
131133
text.Draw(screen, str, font, x, y, objectColor)
132134
}

0 commit comments

Comments
 (0)