Thanks Jak and Dave. I’m making good progress today and I’m quite pleased. Now got 4 rows of tiles 2 diff sizes, also changed the ball from a sprite to an ellipse, didn’t like the sprite. Added scoring and Also added a little damping to try to tone down the rebounds as it’s getting really fast.
No problems atm be which is a major plus, I’m now trying to figure out how to give a slight random variation to the ball direction when it hits the bat because sometimes the ball just goes up and down like mad with no sideways movement.
Here is my code! You think it’s getting decent?
-- Break
-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
xpos=512
e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
e3=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
ball_diam=30
p_ball = physics.body(CIRCLE,ball_diam/2)
p_ball.gravityScale = .85
p_ball.restitution = .75
p_ball.friction = 0.1
p_ball.linearVelocity = vec2(math.random(150),math.random(225))
p_ball.x = 375
p_ball.y = 300
p_ball.linearDamping=.25
score=0
gamestate=0 -- Game State (0 - Wait to start) (1 - In Play) (2 - Game Over & Show Score)
--add bat
bat=physics.body(EDGE,vec2(-70,109),vec2(70,109))
bat.gravityScale = 1
bat.restitution = 1.65 --1.4
bat.friction = 0
-- configure boxes
tiles={}
tiles2={}
tiles3={}
tiles4={}
for box = 1, 10 do
table.insert(tiles,vec2(275+box*42,600))
table.insert(tiles2,vec2(275+box*42,558))
end
for box=1,12 do
table.insert(tiles3,vec2(233+box*42,516))
table.insert(tiles4,vec2(233+box*42,473))
end
end
function draw()
-- define screen space and add collision detection
strokeWidth(5)
background(40,40,50)
color(255)
text("Score:",WIDTH-100,HEIGHT-50)
text(score,WIDTH-65,HEIGHT-50)
line(0,0,0,HEIGHT) -- draw left wall
line(0,HEIGHT,WIDTH,HEIGHT) -- draw top wall
line(WIDTH,0,WIDTH,HEIGHT) -- draw right wall
-- set bat screen limits and collision
xpos=CurrentTouch.x
if xpos<=77 then xpos=78
elseif xpos>=948 then xpos=945
end
bat.x=xpos
sprite("Planet Cute:Plain Block",xpos,100,150,55) -- bat
-- add ball and move towards bat
fill(255,0,0)
ellipse(p_ball.x,p_ball.y,30) -- ball
if p_ball.y <= 75 then
text("Ball Lost",350,350) -- placeholder for lost ball
end
-- for box=1,#tiles do -- draw first row of boxes
-- sprite("Cargo Bot:Crate Green 1",tiles[box].x,tiles[box].y)
-- end
-- box collision and removal
for id, tile in pairs(tiles) do -- Loops through table, id is the place in the table and tile holds the data stored there
sprite("Cargo Bot:Crate Green 1", tile.x, tile.y) -- Draw a sprite there
-- Collision detection
if p_ball.x >= tile.x - 21 and p_ball.x <= tile.x + 21
and p_ball.y >= tile.y - 21 and p_ball.y <= tile.y + 21 then
table.remove(tiles, id) -- If collided, remove tile
p_ball.linearVelocity = -p_ball.linearVelocity
score = score + 20
end
end
for id, tile in pairs(tiles2) do -- Loops through table, id is the place in the table and tile holds the data stored there
sprite("Cargo Bot:Crate Blue 1", tile.x, tile.y) -- Draw a sprite there
-- Collision detection
if p_ball.x >= tile.x - 21 and p_ball.x <= tile.x + 21
and p_ball.y >= tile.y - 21 and p_ball.y <= tile.y + 21 then
table.remove(tiles2, id) -- If collided, remove tile
p_ball.linearVelocity = -p_ball.linearVelocity
score = score + 20
end
end
for id, tile in pairs(tiles3) do -- Loops through table, id is the place in the table and tile holds the data stored there
sprite("Cargo Bot:Crate Red 1", tile.x, tile.y) -- Draw a sprite there
-- Collision detection
if p_ball.x >= tile.x - 21 and p_ball.x <= tile.x + 21
and p_ball.y >= tile.y - 21 and p_ball.y <= tile.y + 21 then
table.remove(tiles3, id) -- If collided, remove tile
p_ball.linearVelocity = -p_ball.linearVelocity
score = score + 10
end
end
for id, tile in pairs(tiles4) do -- Loops through table, id is the place in the table and tile holds the data stored there
sprite("Cargo Bot:Crate Yellow 1", tile.x, tile.y) -- Draw a sprite there
-- Collision detection
if p_ball.x >= tile.x - 21 and p_ball.x <= tile.x + 21
and p_ball.y >= tile.y - 21 and p_ball.y <= tile.y + 21 then
table.remove(tiles4, id) -- If collided, remove tile
p_ball.linearVelocity = -p_ball.linearVelocity
score = score + 10
end
end
end
function waiting()
-- Placeholder for Gamestate 0 (Press to play)
end
function showscore()
-- Placeholder for Gamestate 2 (Game Over & Show Score)
end