116 lines
3.5 KiB
Lua
116 lines
3.5 KiB
Lua
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
|