Last active
June 2, 2021 10:32
-
-
Save lvguowei/e353023df711766f6405653ecd819cbc to your computer and use it in GitHub Desktop.
Breakout Pico-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _init() | |
cls() | |
ball_x = 10 | |
ball_dx = 2 | |
ball_y = 20 | |
ball_dy = 2 | |
ball_r = 2 | |
pad_x = 52 | |
pad_y = 120 | |
pad_dx = 0 | |
pad_dy = 0 | |
pad_w = 24 | |
pad_h = 3 | |
end | |
function _update() | |
local buttpressed = false | |
local next_x, nexty | |
if btn(0) then | |
--left | |
buttpressed = true | |
pad_dx = -5 | |
end | |
if btn(1) then | |
--right | |
buttpressed = true | |
pad_dx = 5 | |
end | |
if not(buttpressed) then | |
pad_dx = pad_dx / 2 | |
end | |
pad_x += pad_dx | |
next_x = ball_x + ball_dx | |
nexty = ball_y + ball_dy | |
if nextx > 127 or nextx < 0 then | |
nextx = mid(0,nextx,127) | |
ball_dx = -ball_dx | |
sfx(0) | |
end | |
if nexty > 127 or nexty < 0 then | |
nexty = mid(0,nexty,127) | |
ball_dy =- ball_dy | |
sfx(0) | |
end | |
if ball_box(nextx, nexty, pad_x, pad_y, pad_w, pad_h) then | |
if deflx_ballbox(ball_x, ball_y, ball_dx, ball_dy, pad_x, pad_y, pad_w, pad_h) then | |
ball_dx =- ball_dx | |
else | |
ball_dy =- ball_dy | |
end | |
sfx(1) | |
end | |
ball_x = nextx | |
ball_y = nexty | |
end | |
function _draw() | |
cls() | |
rectfill(0, 0, 127, 127, 1) | |
circfill(ball_x, ball_y, ball_r, 10) | |
rectfill(pad_x, pad_y, pad_x + pad_w, pad_y + pad_h, 7) | |
end | |
function ball_box(bx, by, box_x, box_y, box_w, box_h) | |
if by-ball_r > box_y+box_h then | |
return false | |
end | |
if by+ball_r < box_y then | |
return false | |
end | |
if bx-ball_r > box_x+box_w then | |
return false | |
end | |
if bx+ball_r < box_x then | |
return false | |
end | |
return true | |
end | |
function deflx_ballbox(bx, by, bdx, bdy, tx, ty, tw, th) | |
-- calculate wether to deflect the ball | |
-- horizontally or vertically when it hits a box | |
if bdx == 0 then | |
-- moving vertically | |
return false | |
elseif bdy == 0 then | |
-- moving horizontally | |
return true | |
else | |
-- moving diagonally | |
-- calculate slope | |
local slp = bdy / bdx | |
local cx, cy | |
-- check variants | |
if slp > 0 and bdx > 0 then | |
-- moving down right | |
cx = tx - bx | |
cy = ty - by | |
if cx <= 0 then | |
return false | |
elseif cy / cx < slp then | |
return true | |
else | |
return false | |
end | |
elseif slp < 0 and bdx > 0 then | |
-- moving up right | |
cx = tx - bx | |
cy = ty + th - by | |
if cx <= 0 then | |
return false | |
elseif cy / cx < slp then | |
return false | |
else | |
return true | |
end | |
elseif slp > 0 and bdx < 0 then | |
-- moving left up | |
cx = tx + tw - bx | |
cy = ty + th - by | |
if cx >= 0 then | |
return false | |
elseif cy / cx > slp then | |
return false | |
else | |
return true | |
end | |
else | |
-- moving left down | |
cx = tx + tw -bx | |
cy = ty - by | |
if cx >= 0 then | |
return false | |
elseif cy / cx < slp then | |
return false | |
else | |
return true | |
end | |
end | |
end | |
return false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment