Skip to content

Instantly share code, notes, and snippets.

@cmontella
Created March 31, 2019 02:46
Show Gist options
  • Save cmontella/e87fa738301b61309140666940c08fc5 to your computer and use it in GitHub Desktop.
Save cmontella/e87fa738301b61309140666940c08fc5 to your computer and use it in GitHub Desktop.
# Bricks
## The Game
game setup
#system/timer = [resolution: 15 tick: 0 hours: 0 minutes: 0 seconds: 0]
#app/main = [root: "drawing" direction: _ contains: [#game]]
game area
#game = [|type class contains parameters|
#paddle-control
"canvas" _ [#elements] [width: 400 height: 400]]
controller slider
#paddle-control = [type: "slider" class: _ contains: _ parameters: [min: 0 max: 300 value: 0]]
draw the game area
pos = #paddle-control{1,4}{1,3}
start = pos
end = pos + 100
#elements = [|shape parameters|
"circle" [cx: #ball.x cy: #ball.y radius: 10 fill: "#000000"]
"line" [x1: start y1: 350 x2: end y2: 350 stroke: "#000000"]]
## The Ball
block
#ball = [x: 20 y: 20 vx: 1 vy: 3]
update ball position
~ #system/timer.tick
#ball.x := #ball.x + #ball.vx
#ball.y := #ball.y + #ball.vy
bounce the ball off the paddle
~ #ball.y
pos = #paddle-control{1,4}{1,3}
start = pos
end = pos + 100
ix = #ball.y > 340
ix1 = #ball.x > start
ix2 = #ball.x < end
ix3 = #ball.y < 342
first = ix & ix1
second = first & ix2
third = second & ix3
#ball.vy{third} := -#ball.vy
bounce the ball off the ceiling
~ #ball.y
ceiling = #ball.y < 10
#ball.vy{ceiling} := -#ball.vy
bounce the ball off the walls
~ #ball.x
ix = #ball.x > 390
ix2 = #ball.x < 10
#ball.vx{ix | ix2} := -#ball.vx
reset the ball if it makes it past the paddle
~ #ball.y
ix = #ball.y > 390
#ball.x{ix} := 20
#ball.y{ix} := 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment