Skip to content

Commit

Permalink
add drawing game loop
Browse files Browse the repository at this point in the history
  • Loading branch information
KalebHawkins committed Jun 24, 2024
1 parent 86e5a70 commit 708e70b
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 8 deletions.
Binary file added assets/ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion assets/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ var (
EmpireStateNF_ttf []byte

//go:embed background.png
Background []byte
Background_png []byte

//go:embed paddle.png
Paddle_png []byte

//go:embed ball.png
Ball_png []byte
)
Binary file added assets/paddle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion docs/GameObjects.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ classDiagram
+ Run() error
+ Reset()
+ drawMainMenu(screen *ebiten.Image)
+ drawGameLoop(screen *ebiten.Image)
}
class Cfg {
+ screenWidth int
+ screenHeight int
+ WindowTitle string
+ faceSource *text.GoTextFaceSource
+ background *ebiten.Image
+ backgroundImage *ebiten.Image
+ paddleImage *ebiten.Image
+ ballImage *ebiten.Image
}
class paddle {
Expand Down
9 changes: 9 additions & 0 deletions pong/ball.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pong

import "github.com/hajimehoshi/ebiten/v2"

type ball struct {
x, y int
dx, dy int
sprite *ebiten.Image
}
10 changes: 10 additions & 0 deletions pong/paddle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pong

import "github.com/hajimehoshi/ebiten/v2"

type paddle struct {
x, y int
dx int
score int
sprite *ebiten.Image
}
98 changes: 92 additions & 6 deletions pong/pong.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ import (

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"gtihub.com/KalebHawkins/pong/assets"
)

const (
fontSize = 48
titleFontSize = fontSize * 1.5
paddleWidth = 20
)

// Game is a structure containing the game data and configuration.
type Game struct {
*Cfg
state

playerOne paddle
playerTwo paddle
ball
}

// Cfg contains the Game's configuration data.
Expand All @@ -34,6 +40,15 @@ type Cfg struct {
faceSource *text.GoTextFaceSource
// backgroundImage is the background image
backgroundImage *ebiten.Image
// paddleImage is the image used for the paddle
paddleImage *ebiten.Image
// ballImage is the image used for the ball
ballImage *ebiten.Image

// verticalLine is used as the central delimiter of the screen vertically..
verticalLine *ebiten.Image
// horizontalLine is used as the central delimiter of the screen horizontally.
horizontalLine *ebiten.Image
}

// NewGame returns a Game instance to be ran.
Expand All @@ -43,24 +58,63 @@ func NewGame(cfg *Cfg) *Game {
panic(fmt.Sprintf("failed to load font file: %v", err))
}

bgImg, _, err := ebitenutil.NewImageFromReader(bytes.NewReader(assets.Background))
bgImg, _, err := ebitenutil.NewImageFromReader(bytes.NewReader(assets.Background_png))
if err != nil {
panic(fmt.Sprintf("failed to background texture: %v", err))
}

paddleImg, _, err := ebitenutil.NewImageFromReader(bytes.NewReader(assets.Paddle_png))
if err != nil {
panic(fmt.Sprintf("failed to load paddle texture: %v", err))
}

ballImg, _, err := ebitenutil.NewImageFromReader(bytes.NewReader(assets.Ball_png))
if err != nil {
panic(fmt.Sprintf("failed to load ball texture: %v", err))
}

cfg.faceSource = fs
cfg.backgroundImage = bgImg
cfg.paddleImage = paddleImg
cfg.ballImage = ballImg
cfg.verticalLine = ebiten.NewImage(1, cfg.ScreenHeight)
cfg.horizontalLine = ebiten.NewImage(cfg.ScreenWidth, 1)

return &Game{
Cfg: cfg,
state: mainMenu,
state: gameLoop,
playerOne: paddle{
x: 10 + paddleWidth/2,
y: cfg.ScreenHeight / 2,
dx: 0,
score: 0,
sprite: ebiten.NewImageFromImage(cfg.paddleImage),
},
playerTwo: paddle{
x: cfg.ScreenWidth - paddleWidth/2 - 10,
y: cfg.ScreenHeight / 2,
dx: 0,
score: 0,
sprite: ebiten.NewImageFromImage(cfg.paddleImage),
},
ball: ball{
x: cfg.ScreenWidth / 2,
y: cfg.ScreenHeight / 2,
dx: 0,
dy: 0,
sprite: ebiten.NewImageFromImage(cfg.ballImage),
},
}
}

// Update manages user input, handles physics processes and updates game states.
func (g *Game) Update() error {
switch g.state {
case mainMenu:
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
g.state = gameLoop
}
case gameLoop:

}

Expand All @@ -69,10 +123,29 @@ func (g *Game) Update() error {

// Draw draws the appropriate images to the screen based on game state.
func (g *Game) Draw(screen *ebiten.Image) {
imgOpts := &ebiten.DrawImageOptions{}
imgOpts.GeoM.Scale(3, 2)
screen.DrawImage(g.backgroundImage, imgOpts)

op := &ebiten.DrawImageOptions{}
g.Cfg.verticalLine.Fill(color.White)
op.GeoM.Translate(-float64(g.Cfg.verticalLine.Bounds().Dx())/2, 0)
op.GeoM.Translate(float64(g.Cfg.ScreenWidth/2), 0)
screen.DrawImage(g.Cfg.verticalLine, op)

op = &ebiten.DrawImageOptions{}
g.Cfg.horizontalLine.Fill(color.White)
op.GeoM.Translate(0, -float64(g.Cfg.horizontalLine.Bounds().Dy()/2))
op.GeoM.Translate(0, float64(g.Cfg.ScreenHeight)/2)
screen.DrawImage(g.Cfg.horizontalLine, op)

switch g.state {
case mainMenu:
g.drawMainMenu(screen)
case gameLoop:
g.drawGameLoop(screen)
}

}

// Layout returns the screen's logical width and height.
Expand All @@ -89,10 +162,6 @@ func (g *Game) Run() error {
}

func (g *Game) drawMainMenu(screen *ebiten.Image) {
imgOpts := &ebiten.DrawImageOptions{}
imgOpts.GeoM.Scale(3, 2)
screen.DrawImage(g.backgroundImage, imgOpts)

titleTextFace := &text.GoTextFace{
Source: g.Cfg.faceSource,
Size: titleFontSize,
Expand All @@ -116,3 +185,20 @@ func (g *Game) drawMainMenu(screen *ebiten.Image) {
opts.ColorScale.ScaleWithColor(color.Black)
text.Draw(screen, "Single Player\nMultiplayer", menuTextFace, opts)
}

func (g *Game) drawGameLoop(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(g.playerOne.sprite.Bounds().Dx())/2, -float64(g.playerOne.sprite.Bounds().Dy())/2)
op.GeoM.Translate(float64(g.playerOne.x), float64(g.playerOne.y))
screen.DrawImage(g.playerOne.sprite, op)

op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(g.playerTwo.sprite.Bounds().Dx())/2, -float64(g.playerTwo.sprite.Bounds().Dy())/2)
op.GeoM.Translate(float64(g.playerTwo.x), float64(g.playerTwo.y))
screen.DrawImage(g.playerTwo.sprite, op)

op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(g.ball.sprite.Bounds().Dx())/2, -float64(g.ball.sprite.Bounds().Dy())/2)
op.GeoM.Translate(float64(g.ball.x), float64(g.ball.y))
screen.DrawImage(g.ball.sprite, op)
}

0 comments on commit 708e70b

Please sign in to comment.