Code: Select all
--[[
Breakout clone, Copyright (c) 2005 Nigel Brine <wilhelm.klink@gmail.com>
]]
System.usbDiskModeActivate()
red = Color.new(255, 0, 0);
black = Color.new(0, 0, 0);
white = Color.new(255, 255, 255);
breakout = Image.createEmpty(480,272)
breakout:clear(black)
-- define globals
frameskip = 0
score = 0
high = 0
paddle = {}
ball = {}
function updatePaddle()
-- clear paddle
breakout:fillRect(paddle.x, paddle.y, paddle.w, paddle.h, black)
-- update position
if (paddle.x + paddle.dx*paddle.vx < 1) then
paddle.x = 1
elseif (paddle.x + paddle.dx*paddle.vx + paddle.w > 480) then
paddle.x = 480-paddle.w
else
paddle.x = paddle.x + paddle.dx*paddle.vx
end
-- redraw paddle
breakout:fillRect(paddle.x, paddle.y, paddle.w, paddle.h, red)
end
function updateBall()
-- clear ball
breakout:fillRect(ball.x, ball.y, ball.w, ball.h, black)
-- update position and angle
if (ball.x + ball.v*math.sin(ball.ang) < 1) then
ball.x = 1
ball.y = ball.y - ball.v*math.cos(ball.ang)
ball.ang = 2*math.pi-ball.ang
elseif (ball.x + ball.v*math.sin(ball.ang) > 480-ball.w) then
ball.x = 480-ball.w
ball.y = ball.y - ball.v*math.cos(ball.ang)
ball.ang = 2*math.pi-ball.ang
elseif (ball.y - ball.v*math.cos(ball.ang) < 1) then
ball.y = 1
ball.x = ball.x + ball.v*math.sin(ball.ang)
ball.ang = math.pi-ball.ang
elseif (ball.y - ball.v*math.cos(ball.ang) >= paddle.y-ball.h) and (ball.y - ball.v*math.cos(ball.ang) <= paddle.y-ball.h-ball.v*math.cos(ball.ang)) and (ball.x + ball.v*math.sin(ball.ang) >= paddle.x) and (ball.x + ball.v*math.sin(ball.ang) <= paddle.x + paddle.w) then
ball.ang = math.pi-ball.ang
ball.x = ball.x + ball.v*math.sin(ball.ang)
ball.y = ball.y - ball.v*math.cos(ball.ang)
--ball.y = paddle.y
--ball.ang = math.pi-ball.ang
elseif (ball.y - ball.v*math.cos(ball.ang) > 272-ball.h) then
--ball.y = 272-ball.h
--ball.x = ball.x + ball.v*math.sin(ball.ang)
--ball.ang = math.pi-ball.ang
ball.isin = 0 -- ball is out of play
else
ball.x = ball.x + ball.v*math.sin(ball.ang)
ball.y = ball.y - ball.v*math.cos(ball.ang)
end
-- redraw ball
if ball.isin == 1 then
breakout:fillRect(ball.x, ball.y, ball.w, ball.h, white)
end
end
function newGame()
score = 0
paddle.x = 30 -- position of paddle
paddle.y = 250
paddle.w = 60 -- dimensions of paddle
paddle.h = 10
paddle.dx = 0 -- direction of paddle, -1 = left, +1 = right
paddle.vx = 20 -- velocity of paddle
ball.x = 200
ball.y = 100
ball.w = 5
ball.h = 5
ball.v = 5
ball.ang = math.pi/4 -- trajectory of ball
ball.isin = 0 -- is ball in play?
end
function keyboardControl()
screen.waitVblankStart()
pad = Controls.read()
anax = pad:analogX()
if math.abs(anax) > 32 then
if anax > 0 then
paddle.dx = 1
else
paddle.dx = -1
end
paddle.vx = math.exp(0.028*math.abs(anax));
elseif pad:left() then
paddle.dx = -1
paddle.vx = 5
elseif pad:right() then
paddle.dx = 1
paddle.vx = 5
else
paddle.dx = 0
paddle.vx = 0
end
if pad:square() and ball.isin == 0 then
ball.isin = 1
ball.x = 200
ball.y = 100
end
end
-- init
newGame()
-- game loop
while not Controls.read():start() do
for i=0,frameskip do
keyboardControl()
end
updatePaddle()
updateBall()
screen:blit(0, 0, breakout)
screen.waitVblankStart()
screen.flip()
end