From fdf5be8be20487e7d4a4370d3ad3cf0647dd0707 Mon Sep 17 00:00:00 2001 From: KalebHawkins Date: Mon, 24 Jun 2024 16:12:59 -0500 Subject: [PATCH] add pause menu --- docs/GameObjects.md | 2 +- pong/pong.go | 20 +++++++++++++++++++- pong/states.go | 1 - 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/GameObjects.md b/docs/GameObjects.md index d6dad4c..14e4f91 100644 --- a/docs/GameObjects.md +++ b/docs/GameObjects.md @@ -10,7 +10,7 @@ classDiagram + playerOne paddle + playerTwo paddle + ball ball - + + isPaused bool + Update() error + Draw(*ebiten.Image) diff --git a/pong/pong.go b/pong/pong.go index 88ce5dd..41ced15 100644 --- a/pong/pong.go +++ b/pong/pong.go @@ -26,6 +26,7 @@ type Game struct { playerOne paddle playerTwo paddle ball + isPaused bool } // Cfg contains the Game's configuration data. @@ -115,7 +116,9 @@ func (g *Game) Update() error { g.state = gameLoop } case gameLoop: - + if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyEscape) { + g.isPaused = !g.isPaused + } } return nil @@ -144,6 +147,9 @@ func (g *Game) Draw(screen *ebiten.Image) { g.drawMainMenu(screen) case gameLoop: g.drawGameLoop(screen) + if g.isPaused { + g.drawPauseMenu(screen) + } } } @@ -202,3 +208,15 @@ func (g *Game) drawGameLoop(screen *ebiten.Image) { op.GeoM.Translate(float64(g.ball.x), float64(g.ball.y)) screen.DrawImage(g.ball.sprite, op) } + +func (g *Game) drawPauseMenu(screen *ebiten.Image) { + op := &text.DrawOptions{} + op.GeoM.Translate(float64(g.Cfg.ScreenWidth)/2, fontSize) + op.LineSpacing = fontSize + op.PrimaryAlign = text.AlignCenter + op.ColorScale.ScaleWithColor(color.Black) + text.Draw(screen, "GAME PAUSED", &text.GoTextFace{ + Source: g.Cfg.faceSource, + Size: fontSize, + }, op) +} diff --git a/pong/states.go b/pong/states.go index cdaa335..f7f10ea 100644 --- a/pong/states.go +++ b/pong/states.go @@ -6,6 +6,5 @@ type state int const ( mainMenu state = iota gameLoop - paused gameOver )