Debug/Improving Small Game

Hello everyone. This is a little game I’ve been working on for my high school class (surprise surprise :slight_smile: ). I’m looking for suggestions and some help debugging the code. It is a little glitchy, such as the fact that the score does not always increase. I know that this is because I am using random integers for their original placement, but I am not sure how to keep the random placement but still have the score increase correctly. There are some other small issues, but other than that it works fairly well. I am open to suggestions, criticism, or pretty much anything you have to say.

And yes, I know I should probably learn about tables. :))

By the way, when you die just tap the screen and it should reset. Thanks everyone!


function setup()
    
    y = 100
    run = true
    x = math.random(700, 800)
    x2 = math.random(950, 1050)
    x3 = math.random(1250, 1350)
    r = math.random(50,150)
    r2 = math.random(50, 150)
    r3 = math.random(50, 150)
    score = 0
    speed = 5
    
    ball = physics.body(CIRCLE, 50)
    ball.y = 250
    ball.gravityScale = 12
    
    ground = physics.body(EDGE, vec2(-1, -25), vec2(1000, -25))
    ground.y = 250
    
end


function draw()
    
    background(255, 255, 255, 255)
    
    sprite("Cargo Bot:Level Select Frame", HEIGHT/2, 700, 100, 100)
    
    pushStyle()
    fill(54, 87, 51, 255)
    ellipse(100, ball.y, 50, 50)
    sprite("Documents:camo", WIDTH/2, -70)
    rect(x, 249, 50, r)
    rect(x2, 249, 50, r2)
    rect(x3, 249, 50, r3)
    font("Verdana-Bold")
    fontSize(48)
    text(score, HEIGHT/2, 700)
    popStyle()
    
    if x <= -50 then
        x = 750
        r = math.random(50, 150)
    end
    
    if x2 <= -50 then
        x2 = 750
        r2 = math.random(50, 150)
    end
    
    if x3 <= - 50 then
        x3 = 750
        r3 = math.random(50, 150)
    end
    
    if run == true then
        x = x - speed
        x2 = x2 - speed
        x3 = x3 - speed
    end
    
    if x <= 110 and x >= 25 or x2 <= 110 and x2 >= 25 or x3 <= 110 and x3 >= 25 then
        if ball.y <= r + 275 or ball.y <= r2 + 275 then
            run = false
            fill(255, 255, 255, 255)
            rect(30, HEIGHT/2 - 65, 690, 125)
            sprite("Cargo Bot:Level Select Frame", HEIGHT/2 - 18, WIDTH/2 + 15, 825, 175)
            fill(54, 87, 51, 255)
            font("Verdana-Bold")
            fontSize(100)
            text("YOU DEAD", WIDTH/2, HEIGHT/2)
            if CurrentTouch.state == BEGAN then
                restart()
            end
        end
    end 
    
    if x == 100 or x2 == 100 or x3 == 100 then
        score = score + 1
    end
    
end

function touched()
    if run == true then
        if ball.y <= 300 then
            if CurrentTouch.state == BEGAN then
                ball.linearVelocity = vec2(0, 1400)
            end
        end
    end
    
    if CurrentTouch.state == BEGAN then
        run = true
    end
end

Without you having to learn tables, here is what your code would look like if you did learn them. It might change your mind, they can be pretty useful. Also you can apply a lot of different things with tables, like making as many items as you want by adjusting my for loops.

function setup()
y = 100
x = { math.random(700, 800),math.random(950, 1050),math.random(1250, 1350)}
r = {r = math.random(50,150),r2 = math.random(50, 150),r3 = math.random(50, 150)}
run = true
score = 0
speed = 5
ball = physics.body(CIRCLE, 50)
ball.y = 250
ball.gravityScale = 12
ground = physics.body(EDGE, vec2(-1, -25), vec2(1000, -25))
ground.y = 250
end
function draw()
background(255, 255, 255, 255)
sprite("Cargo Bot:Level Select Frame", HEIGHT/2, 700, 100, 100)
pushStyle()
fill(54, 87, 51, 255)
ellipse(100, ball.y, 50, 50)
sprite("Documents:camo", WIDTH/2, -70)
for i = 0, 3 do
rect(x[i], 249, 50, r[i])
end
font("Verdana-Bold")
fontSize(48)
text(score, HEIGHT/2, 700)
popStyle()
for i = 0, 3 do
if x[i] <= - 50 then
    x[i] = 750
    r[i] = math.random(50, 150)
end
end

if run == true then
for i = 0, 3 do
x[i] = x[i] - speed
end
end

if x[0] <= 110 and x[0] >= 25 or x[1] <= 110 and x[1] >= 25 or x[2] <= 110 and x[2] >= 25 then
    if ball.y <= r[0] + 275 or ball.y <= r[1] + 275 then
        run = false
        fill(255, 255, 255, 255)
        rect(30, HEIGHT/2 - 65, 690, 125)
        sprite("Cargo Bot:Level Select Frame", HEIGHT/2 - 18, WIDTH/2 + 15, 825, 175)
        fill(54, 87, 51, 255)
        font("Verdana-Bold")
        fontSize(100)
        text("YOU DEAD", WIDTH/2, HEIGHT/2)
        if CurrentTouch.state == BEGAN then
            restart()
        end
    end
end 
if x == 100 or x[1] == 100 or x[2] == 100 then
    score = score + 1
end
end
function touched() if run == true then if ball.y <= 300 then if CurrentTouch.state == BEGAN then ball.linearVelocity = vec2(0, 1400) end end end

if CurrentTouch.state == BEGAN then
    run = true
end
end

@jrohanian

Wow, thanks! That definitely looks much more efficient. Thanks for taking the time to show me! I think you’ve convinced me to read up and get tables down.

@jrohanian Did you try running your changes after you made them. I tried and ran into a lot of errors.

@dave1707 no didn’t test it. I’ll fix it now.

@Zeb_Pease I edited the code to work, but the most important thing is you are convinced on the importance of tables for effective game making. Also tried your game out, really neat, and also, try making some spikes! Try running this code.

function setup()
tri = mesh()
verts = vec2(700,100)
end
function draw()
background(255,255,255,255)
strokeWidth(5)
pushStyle()
fill(0,0,0,255)
-- define each corner of the triangle
tri.vertices = {vec2(verts.x,verts.y), vec2(verts.x+100,verts.y),vec2(verts.x+50,verts.y+100)}
-- draw the triangle
tri:draw()
popStyle()
verts.x = verts.x - 1

end