Skip to content

Commit

Permalink
Display scores
Browse files Browse the repository at this point in the history
  • Loading branch information
splieth committed Oct 14, 2019
1 parent 73d509f commit 3f585d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions game/ball.go
Expand Up @@ -68,16 +68,16 @@ func (ball *Ball) HandleCollision(g *Game) {
case RightPaddle, LeftPaddle:
ball.direction.x = ball.direction.x * -1
case RightWall:
g.scores[0] += 1;
g.scores[0] += 1
g.goooooooal()
case LeftWall:
g.scores[1] += 1;
g.scores[1] += 1
g.goooooooal()
}
}
}

func (b * Ball) center(g *Game) {
func (b *Ball) center(g *Game) {
b.position.x = (g.ballCanvas.width / 2) + canvasPadding
b.position.y = (g.ballCanvas.height / 2) + canvasPadding
}
10 changes: 10 additions & 0 deletions game/game.go
@@ -1,7 +1,9 @@
package game

import (
"fmt"
"github.com/gdamore/tcell"
"strings"
"time"
)

Expand Down Expand Up @@ -106,9 +108,17 @@ func (g *Game) draw() {
g.ball.Draw(g.screen)
g.leftPaddle.Draw(g.screen)
g.rightPaddle.Draw(g.screen)
g.updateScores()
g.screen.Show()
}

func (g *Game) updateScores() {
text := strings.Trim(strings.Replace(fmt.Sprint(g.scores), " ", ":", -1), "[]")
for i, r := range text {
g.screen.SetContent(g.scoreCanvas.x+i, g.scoreCanvas.y, r, nil, tcell.StyleDefault.Foreground(tcell.ColorWhite))
}
}

func (g *Game) pollScreenEvents() {
for {
ev := g.screen.PollEvent()
Expand Down

0 comments on commit 3f585d0

Please sign in to comment.