package snake package pong // m11 pong model: the same ideas as the snake model, for a different game. /** The ball's direction of travel, one cell per frame on each axis. */ final case class Velocity(dx: Int, dy: Int) /** Which of the player's two keys are being held down right now. */ final case class Held(up: Boolean, down: Boolean) final case class Score(player: Int, cpu: Int) enum PongStatus: case Playing, Over /** The two keys that move the player's paddle. */ enum Paddle: case Up, Down /** * A whole match. Positions are in cells: `ball` is a cell of the 80x50 court; `player` and `cpu` are the y of the * top cell of each paddle. `seed` is the unsigned 32-bit random seed used to pick the direction of each serve. */ final case class PongState( ball: Point, vel: Velocity, player: Int, cpu: Int, held: Held, score: Score, status: PongStatus, seed: Long, frame: Int ) /** Everything that can happen in a match, as data. */ enum PongAction: case Press(key: Paddle) case Release(key: Paddle) case Frame case Restart object PongModel: // The court, in cells. val W: Int = 80 val H: Int = 50 /** Pixels per cell. */ val PCELL: Int = 8 /** Paddle height in cells. */ val PADDLE: Int = 8 /** Column of the player's paddle (left) and of the cpu's paddle (right). */ val PLAYER_X: Int = 2 val CPU_X: Int = 77 /** Points needed to win the match. */ val WIN: Int = 5 def initialPong(seed: Long): PongState = PongState( ball = Point(40, 25), vel = Velocity(1, 1), player = 21, cpu = 21, held = Held(up = false, down = false), score = Score(player = 0, cpu = 0), status = PongStatus.Playing, seed = seed, frame = 0 )