Files
functional-programming-snake/scala/solutions/pong/View.scala
T

30 lines
1.0 KiB
Scala

package snake
package pong
import PongModel.*
/** m11 pong view: the same Scene shapes as the snake view, so the same drawScene and runtime draw it. */
object PongView:
private def text(x: Int, y: Int, str: String, size: Int): Shape = Shape.Text(x, y, str, "#ffffff", size, "center")
private def paddle(column: Int, y: Int): Shape =
Shape.Rect(column * PCELL, y * PCELL, PCELL, PADDLE * PCELL, "#ffffff")
private def overlay(state: PongState): Scene = state.status match
case PongStatus.Over =>
List(
text(320, 200, if state.score.player > state.score.cpu then "You Win!" else "CPU Wins", 32),
text(320, 230, "Press R to restart", 16)
)
case PongStatus.Playing => Nil
def pongView(state: PongState): Scene =
List(
Shape.Rect(0, 0, 640, 400, "#111111"),
paddle(PLAYER_X, state.player),
paddle(CPU_X, state.cpu),
Shape.Rect(state.ball.x * PCELL, state.ball.y * PCELL, PCELL, PCELL, "#ffeb3b"),
text(320, 30, s"${state.score.player} ${state.score.cpu}", 24)
) ++ overlay(state)