Reproduce multiple confusing error messages

This commit is contained in:
Richard Feldman 2022-04-17 20:58:24 -04:00
parent 3dce2a00df
commit cbe0b0fb4c
No known key found for this signature in database
GPG Key ID: 7E4127D1E4241798

View File

@ -1,6 +1,6 @@
app "breakout"
packages { pf: "platform" }
imports [ pf.Game.{ Bounds, Elem, Event } ]
imports [ pf.Game.{ Bounds, Elem, Event, Rgba } ]
provides [ program ] { Model } to pf
paddleWidth = 0.2 # width of the paddle, as a % of screen width
@ -8,8 +8,14 @@ paddleHeight = 50 # height of the paddle, in pixels
paddleSpeed = 65 # how many pixels the paddle moves per keypress
blockHeight = 80 # height of a block, in pixels
blockBorder = 0.025 # border of a block, as a % of its width
ballSize = 55
numRows = 4
numCols = 8
numBlocks = numRows * numCols
Model : {
blocks : List Block,
# Screen height and width
height : F32,
width : F32,
@ -24,9 +30,18 @@ Model : {
dBallY : F32, # delta y - how much it moves per tick
}
Block : {
left : F32,
top : F32,
color : Rgba,
status : [ Active, Removed, Fading F32 ],
}
init : Bounds -> Model
init = \{ width, height } ->
{
blocks: initBlocks width,
# Screen height and width
width,
height,
@ -43,6 +58,31 @@ init = \{ width, height } ->
dBallY: 4,
}
initBlocks : F32 -> List Block
initBlocks = \width ->
blockWidth = width / numCols
List.map (List.range 0 numBlocks) \index ->
col =
Num.rem index numCols
|> Result.withDefault 0
|> Num.toF32
row =
index // numCols
|> Num.toF32
red = col / Num.toF32 numCols
green = row / Num.toF32 numRows
blue = Num.toF32 index / Num.toF32 numBlocks
color = { r: red * 0.8, g: 0.2 + green * 0.6, b: 0.2 + blue * 0.8, a: 1 }
left = Num.toF32 col * blockWidth
top = Num.toF32 row * blockHeight
{ left, top, color, status: Active }
update : Model, Event -> Model
update = \model, event ->
when event is
@ -56,6 +96,7 @@ tick : Model -> Model
tick = \model ->
model
|> moveBall
|> updateBlocks
moveBall : Model -> Model
moveBall = \model ->
@ -82,37 +123,43 @@ moveBall = \model ->
{ model & ballX, ballY, dBallX, dBallY }
updateBlocks : Model -> Model
updateBlocks = \model ->
ball = { left: model.ballX, top: model.ballY, width: ballSize, height: ballSize }
blocks = List.map model.blocks \block ->
when block.status is
Removed -> block
Active ->
if isOverlapping block ball then
{ block & status: Fading 1 }
else
block
Fading amount ->
if amount <= 0 then
{ block & status: Removed }
else
{ block & status: Fading (amount - 0.1) }
{ model & blocks }
isOverlapping = \rect1, rect2 ->
(rect1.left + rect1.width >= rect2.left)
&& (rect2.left + rect2.width >= rect1.left)
&& (rect1.top + rect1.height >= rect2.top)
&& (rect2.top + rect2.height >= rect1.top)
render : Model -> List Elem
render = \model ->
numRows = 4
numCols = 8
numBlocks = numRows * numCols
blocks = List.map (List.range 0 numBlocks) \index ->
col =
Num.rem index numCols
|> Result.withDefault 0
|> Num.toF32
row =
index // numCols
|> Num.toF32
red = col / Num.toF32 numCols
green = row / Num.toF32 numRows
blue = Num.toF32 index / Num.toF32 numBlocks
color = { r: red * 0.8, g: 0.2 + green * 0.6, b: 0.2 + blue * 0.8, a: 1 }
{ row, col, color }
blockWidth = model.width / numCols
rects =
List.joinMap blocks \{ row, col, color } ->
left = Num.toF32 col * blockWidth
top = Num.toF32 (row * blockHeight)
List.joinMap model.blocks \{ left, top, color, status } ->
border = blockBorder * blockWidth
a =
when status is
Fading alpha -> alpha
Active -> 1
Removed -> 0
outer = Rect
{
@ -136,8 +183,8 @@ render = \model ->
ball =
color = { r: 0.7, g: 0.3, b: 0.9, a: 1.0 }
width = 50
height = 50
width = ballSize
height = ballSize
left = model.ballX
top = model.ballY