Last active
June 4, 2016 02:01
-
-
Save LDDestroier/2fa920633df765d0e1f82bb759b73e93 to your computer and use it in GitHub Desktop.
Making more variable ball speeds.
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
--[[ | |
Breakout!! for CC | |
By EldidiStroyrr | |
get with: | |
pastebin get LTRYaSKt breakout | |
std pb LTRYaSKt breakout | |
TODO | |
-fix flickering | |
-make cooler | |
--]] | |
local level = 1 | |
local lives = 3 | |
local bdepth = 4 | |
local y_clip = 2 | |
local x_clip = 5 | |
local paused = false | |
local ballslow = 1 --divides ball speed by that | |
local prevColors = { | |
term.getTextColor(), | |
term.getBackgroundColor(), | |
} | |
local scr_x, scr_y = term.getSize() | |
board = { | |
length = 8, | |
x = math.floor(scr_x/2), | |
y = scr_y-1, | |
bgcolor = colors.white, | |
txtcolor = colors.white, | |
char = " ", | |
dir = 0, | |
canMove = true, | |
} | |
ball = { | |
x = math.floor(scr_x/2), | |
y = scr_y-5, | |
xv = 0.6, | |
yv = -0.6, | |
px = 1, | |
py = 1, | |
txtcolor = colors.white, | |
bgcolor = colors.white, | |
char = "O", | |
} | |
ro = function(a,b) | |
return math.floor(a % b) | |
end | |
sr = string.rep | |
f = math.floor | |
c = math.ceil | |
renderBricks = function(input) | |
term.current().setVisible(false) | |
for k,v in pairs(input) do | |
term.setCursorPos(v.x,v.y) | |
term.blit(v.char,v.color[1],v.color[2]) | |
end | |
term.current().setVisible(true) | |
end | |
remakeBricks = function(xclip,yclip,depth) | |
local output, entry = {}, 0 | |
local blitColors = "0123456789abcdef" | |
for a = yclip, depth do | |
for b = (xclip+1), scr_x-xclip do | |
entry = entry + 1 | |
local d = math.random(1,15) | |
local c = string.sub(blitColors,d,d) | |
output[entry] = {x = b, y = a, color = {c,c},char="L"} | |
end | |
end | |
return output | |
end | |
local cprint = function(msg,cy) | |
term.setCursorPos((scr_x/2)-math.floor(#msg/2), cy or (scr_y/2)) | |
print(msg) | |
end | |
local waitForEvents = function(...) | |
local targ = {...} | |
while true do | |
local evt = {os.pullEvent()} | |
for a = 1, #targ do | |
if evt[1] == targ[a] then | |
return table.unpack(evt) | |
end | |
end | |
end | |
end | |
titleScreen = function() | |
local scr_x, scr_y = term.getSize() | |
local title = "BREAKOUT!!" | |
term.setBackgroundColor(colors.white) | |
term.setTextColor(colors.black) | |
term.clear() | |
cprint(title,(scr_y/2)-2) | |
cprint("Press any key/click anywhere",(scr_y/2)-4) | |
sleep(0) | |
return waitForEvents("key","mouse_click") | |
end | |
clearLines = function(startY,endY) | |
local ori = {term.getCursorPos()} | |
for a = startY,endY do | |
term.setCursorPos(1,a) | |
term.clearLine() | |
end | |
term.setCursorPos(table.unpack(ori)) | |
return | |
end | |
clearDot = function(x,y) | |
local pX,pY = term.getCursorPos() | |
term.setCursorPos(x,y) | |
write(" ") | |
term.setCursorPos(pX,pY) | |
end | |
render = function(brd,blocktbl) | |
term.setBackgroundColor(colors.black) | |
term.setCursorPos(2,1) | |
term.write("Lives: "..lives) | |
local lvlcount = "Level: "..tostring(level) | |
term.setCursorPos(scr_x-#lvlcount,1) | |
term.write(lvlcount) | |
term.setCursorPos(brd.x,brd.y) | |
term.clearLine() | |
term.setTextColor(brd.txtcolor) | |
term.setBackgroundColor(brd.bgcolor) | |
term.write(sr(brd.char,brd.length)) | |
for k,v in pairs(blocktbl) do | |
term.setCursorPos(v.x,v.y) | |
term.blit(v.char,v.color[1],v.color[2]) | |
end | |
term.setCursorPos(ball.px,ball.py) | |
term.blit(" ","f","f") | |
term.setCursorPos(ball.x,ball.y) | |
term.setTextColor(ball.txtcolor) | |
term.setBackgroundColor(ball.bgcolor) | |
term.write(ball.char) | |
end | |
levelComplete = function() | |
local txt = "LEVEL COMPLETE!" | |
term.setCursorPos((scr_x/2)-(#txt/2),scr_y/2) | |
term.setTextColor(colors.gray) | |
term.write(txt) | |
sleep(0.1) | |
term.setTextColor(colors.lightGray) | |
term.write(txt) | |
sleep(0.1) | |
for a = 1, 8 do | |
term.setTextColor(colors.white) | |
term.write(txt) | |
sleep(0.3) | |
term.setTextColor(colors.black) | |
term.write(txt) | |
sleep(0.3) | |
end | |
sleep(0.3) | |
blocks = remakeBricks(x_clip,y_clip,bdepth) | |
lives = lives + 2 | |
level = level + 1 | |
board.x = math.floor((scr_x/2)-(board.length/2)) | |
ball.x = math.floor(scr_x/3) | |
ball.y = board.y - 3 | |
ball.xv = 0.6 | |
ball.yv = -0.6 | |
end | |
checkCollision = function() | |
local good = false | |
for a = 1, #blocks do | |
if blocks[a] then | |
if f(blocks[a].x) == f(ball.x + ball.xv) and f(blocks[a].y) == f(ball.y) then | |
ball.xv = -ball.xv | |
clearDot(f(blocks[a].x),f(blocks[a].y)) | |
good = true | |
end | |
if f(blocks[a].y) == f(ball.y + ball.yv) and f(blocks[a].x) == f(ball.x) then | |
ball.yv = -ball.yv | |
clearDot(f(blocks[a].x),f(blocks[a].y)) | |
good = true | |
end | |
if good then | |
table.remove(blocks,a) | |
good = false | |
end | |
end | |
end | |
end | |
animateDeath = function() | |
board.dir = 0 | |
local tbl = { | |
{"*","7","0"}, | |
{"x","7","8"}, | |
{"X","f","8"}, | |
{"X","f","7"}, | |
} | |
term.setBackgroundColor(colors.black) | |
term.setCursorPos(ball.x,ball.y) | |
term.write(" ") | |
term.setCursorPos(1,board.y) | |
term.clearLine() | |
for a = 1, #tbl do | |
term.setCursorPos(board.x,board.y) | |
local char,txt,bg = tbl[a][1]:rep(board.length),tbl[a][2]:rep(board.length),tbl[a][3]:rep(board.length) | |
term.blit(char,txt,bg) | |
sleep(0.05) | |
end | |
term.setCursorPos(board.x,board.y) | |
term.setBackgroundColor(colors.black) | |
term.clearLine() | |
sleep(0.5) | |
board.x = (scr_x/2)-(board.length/2) | |
end | |
animateGameOver = function() | |
board.canMove = false | |
animateDeath() | |
local txt = "GAME OVER" | |
term.setTextColor(colors.white) | |
term.setBackgroundColor(colors.black) | |
for a = 0, scr_y/2 do | |
term.clearLine() | |
term.setCursorPos((scr_x/2)-(#txt/2),scr_y-a) | |
term.write(txt) | |
sleep(0.1) | |
end | |
sleep(0.5) | |
return | |
end | |
togglePauseScreen = function(_paused) | |
local txt = "PAUSED ('P' to unpause)" | |
term.setBackgroundColor(colors.black) | |
if _paused then | |
term.setCursorPos((scr_x/2)-(#txt/2),scr_y/2) | |
term.blit(txt,sr("7",#txt),sr("f",#txt)) | |
sleep(0.1) | |
term.setCursorPos((scr_x/2)-(#txt/2),scr_y/2) | |
term.blit(txt,sr("8",#txt),sr("f",#txt)) | |
sleep(0.1) | |
term.setCursorPos((scr_x/2)-(#txt/2),scr_y/2) | |
term.blit(txt,sr("0",#txt),sr("f",#txt)) | |
sleep(0.1) | |
else | |
term.setCursorPos((scr_x/2)-(#txt/2),scr_y/2) | |
term.blit(txt,sr("8",#txt),sr("f",#txt)) | |
sleep(0.1) | |
term.setCursorPos((scr_x/2)-(#txt/2),scr_y/2) | |
term.blit(txt,sr("7",#txt),sr("f",#txt)) | |
sleep(0.1) | |
term.setCursorPos((scr_x/2)-(#txt/2),scr_y/2) | |
term.clearLine() | |
end | |
end | |
game = function() | |
term.setBackgroundColor(colors.black) | |
term.clear() | |
blocks = remakeBricks(x_clip,y_clip,bdepth) | |
local frame = 1 | |
while true do | |
if not paused then | |
board.x = board.x + board.dir | |
ball.x = math.floor(ball.x) | |
ball.y = math.floor(ball.y) | |
if frame == ballslow then | |
frame = 1 | |
if ball.x <= 1 or ball.x >= scr_x then | |
ball.xv = ball.xv * -1 | |
end | |
if ball.y <= 1 then | |
ball.yv = -ball.yv | |
elseif ball.y >= scr_y then | |
if lives > 1 then | |
board.canMove = false | |
animateDeath() | |
ball.x = scr_x/2 | |
ball.y = scr_y-8 | |
ball.xv = 0.6 | |
ball.yv = -0.6 | |
lives = lives - 1 | |
board.canMove = true | |
else | |
lives = lives - 1 | |
animateGameOver() | |
return "lose" | |
end | |
end | |
ball.px = ball.x | |
ball.py = ball.y | |
ball.y = math.floor(ball.y + ball.yv) | |
term.setBackgroundColor(colors.black) | |
checkCollision() | |
ball.x = math.floor(ball.x + ball.xv) | |
checkCollision() | |
if #blocks == 0 then levelComplete() end | |
if (ball.y+1 == board.y or ball.y == board.y) and (ball.x >= board.x and ball.x <= board.x+board.length) then | |
ball.yv = -ball.yv | |
if f(ball.x < board.x+(board.length/2)-1 then | |
ball.xv = ball.xv- | |
elseif ball.x > board.x+(board.length/2)+1 then | |
ball.xv = ball.xv- | |
end | |
end | |
sleep(0) | |
else | |
frame = frame + 1 | |
end | |
render(board,blocks) | |
end | |
sleep(0) | |
end | |
end | |
getInput = function() | |
local event, key | |
local downKeys = {left = false, right = false} | |
while true do | |
event, key, mx, my = os.pullEvent() | |
if event == "key" then | |
if board.canMove and not paused then | |
if key == keys.left and board.x > 1 then | |
board.dir = -1 | |
downKeys.left = true | |
elseif key == keys.right and board.x < (scr_x+1)-board.length then | |
board.dir = 1 | |
downKeys.right = true | |
end | |
else | |
board.dir = 0 | |
end | |
if key == keys.q then | |
return | |
end | |
if key == keys.p then | |
if not paused then --this is to fix a rendering problem | |
paused = true | |
togglePauseScreen(true) | |
else | |
togglePauseScreen(false) | |
paused = false | |
end | |
end | |
elseif event == "key_up" then | |
if key == keys.left then | |
downKeys.left = false | |
elseif key == keys.right then | |
downKeys.right = false | |
end | |
if (downKeys.left == false and downKeys.right == false) or (board.x <= 1 or board.x-board.length >= scr_x) then | |
board.dir = 0 | |
end | |
elseif event == "mouse_click" or event == "mouse_drag" then | |
if board.canMove and not paused then | |
board.x = mx-(board.length/2) | |
end | |
end | |
if board.x < 1 then board.x = 1 board.dir = 0 | |
elseif board.x > (scr_x+1)-board.length then board.x = (scr_x+1)-board.length board.dir = 0 end | |
end | |
end | |
outro = function(txtcolor,bgcolor,result) | |
term.setTextColor(txtcolor) | |
term.setBackgroundColor(bgcolor) | |
term.clear() | |
term.setCursorPos(1,1) | |
print("Thanks for playing!") | |
return result | |
end | |
titleScreen() | |
local startFuncs = { | |
game, | |
getInput, | |
} | |
parallel.waitForAny(table.unpack(startFuncs)) | |
outro(prevColors[1],prevColors[2]) | |
sleep(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment