[FEAT] Pong & Breakout included

This commit is contained in:
2021-12-10 08:18:02 +01:00
parent d101da4a43
commit 12383f9309
2 changed files with 209 additions and 0 deletions

94
breakout.lua Normal file
View File

@@ -0,0 +1,94 @@
function init()
setmode(1)
reset()
end
function reset()
bx,by=20,28
dx,dy=-1+rnd(1)*2,-1
px=18
wait=1
speed=6
bricks = {}
for i=0,35 do bricks[i]=COLOR_RED end
for i=36,71 do bricks[i]=COLOR_BROWN end
for i=72,107 do bricks[i]=COLOR_GREEN end
for i=108,143 do bricks[i]=COLOR_YELLOW end
end
function update()
-- move ball
wait=wait-1
if wait==0 then
wait=speed
bx=bx+dx
by=by+dy
if speed<6 then
if bx==2 or bx==37 then dx=-dx play("o3l0c") end
if by<9 then
local index=flr(bx/2)-1+(by-1)*18
if bricks[index]~=COLOR_BLACK then
play("o5l0c")
bricks[index]=COLOR_BLACK
dy=-dy
else
if by==1 then dy=-dy play("o3l0c") end
end
end
if by==28 and bx>=px and bx<=px+4 then
play("o4l0c")
dy=-dy
end
if by==29 then
play("l0o3bagfedc")
reset()
end
else
if bx==2 or bx==37 then dx=-dx end
if by==9 or by==29 then dy=-dy end
end
end
-- move paddle
if btn(KEY_LEFT) and px>2 then px=px-1 end
if btn(KEY_RIGHT) and px<34 then px=px+1 end
-- clear screen
paper(COLOR_BLACK)
cls()
-- draw white border
ink(COLOR_WHITE)
print("\150\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\154\156",1,0)
for i=1,29 do
print("\149",1,i)
print("\149",38,i)
end
-- draw bricks
for i=0,143 do
color(0,bricks[i])
print("\095\003",2+2*(i%18),1+flr(i/18))
end
--draw ball
color(COLOR_WHITE,COLOR_BLACK)
print("\233",bx,by)
-- draw paddle
ink(COLOR_LIGHT_BLUE)
for i=0,3 do print("\131",px+i,29) end
if speed==6 then
ink(rnd(16))
print("BREAKOUT",16,13)
ink(COLOR_WHITE)
print("Press '1' to play EASY",9,18)
print("Press '2' to play NORMAL",8,20)
print("Press '3' to play HARD",9,22)
if btn(KEY_1) then reset() speed=4 end
if btn(KEY_2) then reset() speed=3 end
if btn(KEY_3) then reset() speed=2 end
end
end

115
pong.lua Normal file
View File

@@ -0,0 +1,115 @@
function init()
-- mode 1: 40x30 characters
setmode(1)
-- prepare everything for the match
reset_match()
p1,p2=0,0 -- reset scores
end
function update()
cls()
-- ball movement
-- 'wait' is a counter to skip some cycles before moving the ball, so it starts slower
-- 'speed' defines how many cycles to skip. Every 5 hits to the paddles 'speed' goes down by 1, effectively increasing the ball's speed
-- 'speed' is reset every point
wait=wait-1
if wait==0 then
wait=speed
-- move ball
bx=bx+dx
by=by+dy
-- if ball hits top or down walls, bounce
if by==0 or by==29 then
dy=-dy
if playing then play("a") end -- if we are not playing, be quiet
end
-- during a match, paddles bounce the ball and left and right walls award points
-- but before or after a match the ball just bounces around
if playing then
-- if ball hits left or right ball, reset the ball and award a point to the opposing paddle
if bx==0 then p2=p2+1 reset_ball() end
if bx==39 then p1=p1+1 reset_ball() end
-- if ball hits paddle, bounce
if (bx==1 and by>=y1 and by<=y1+4) or (bx==38 and by>=y2 and by<=y2+4) then
play("c")
dx=-dx
-- also, increase hits. Every 5 hits the ball will speed up (only if it's not already as fast as possible)
hits=hits+1
if (hits%5 == 0) and (speed>0) then speed=speed-1 end
end
else
-- when not playing, just bounce freely
if bx==0 or bx==39 then dx=-dx end
end
end
-- player 1 controls
if btn(KEY_Q) and y1>0 then y1=y1-1 end
if btn(KEY_A) and y1<25 then y1=y1+1 end
-- player 2 controls
if btn(KEY_UP) and y2>0 then y2=y2-1 end
if btn(KEY_DOWN) and y2<25 then y2=y2+1 end
-- draw net
if playing then for i=0,29 do print("\145",20,i) end end
-- draw score
print(p1,18,3)
print(p2,22,3)
-- draw ball
print("\143",flr(bx),flr(by))
-- draw paddles
for i=0,4 do
print("\143",1,y1+i)
print("\143",38,y2+i)
end
if not playing then
-- while not playing show the messages
if p1==10 then print("PLAYER 1 WINS!", 14, 8)
elseif p2==10 then print("PLAYER 2 WINS!", 14, 8)
else print("PONG!", 18, 10) end
print("Press SPACE to play", 11, 20)
if btn(KEY_SPACE) then
start_match()
end
end
end
function start_match()
playing=true
p1,p2=0,0 -- reset scores
end
function reset_match()
play("l2drl2d#l4e>l2crr<e>crr<l4e>crrrl2r cl4dl2d#l4el2cl4del2r<b>l4dl2rl4crrrl2r< drl2d#l4e>l2crr<e>crr<l4e>crrrl2r< l2argl4f#l2a>l4cl2errdl4cl2<al4>drrrl2r< l2drl2d#l4e>l2crr<e>crr<l4e>crrrl2r cl4dl2d#l4el2cl4del2r<b>l4dl2rl4crrrl2r<")
playing=false
bx,by=20,15 -- init ball's position
dx,dy=1,1 -- init ball's direction
y1,y2=14,14 -- init paddle's y coordinate
wait=1 -- reset wait counter
speed=4 -- reset speed
hits=0 -- reset hits counter
end
function reset_ball()
play("l0o3bagfedc") -- play sad tune
bx,by=20,15 -- reset ball's position
speed=4 -- reset speed
hits=0 -- reset hits counter
dx=-dx -- invert x direction
-- si algu arriba a 10, la partida acaba
if p1==10 or p2==10 then
playing=false
play("o5l1crl0ergrl4o6c")
end
end