Pause button

I would like to include a pause button in my application.
This is my application:

amount = math.random(10,350)
p={}
ps = amount
supportedOrientations(ANY)
function setup()
    displayMode(FULLSCREEN)
   for i=0,ps do
       p[i]= {x=math.random(WIDTH*12)/12,
y=math.random(HEIGHT*8)/8, ox=0.0, oy=0.0, vx=math.random(20)-10,
vy=math.random(20)-10}
   end
    colors = {color(255, 255, 255, 255), color(0, 237, 255, 255),color(255, 0, 212, 255),color(76, 248, 15, 255),color(20, 39, 245, 255),color(239, 255, 0, 255),color(245, 11, 3, 255) }
    stcolor = colors[math.random(#colors)]
end

function draw()
    noSmooth()
   background(2, 2, 2, 255)
   fill(255, 0, 160, 255)
   stroke(stcolor)
   strokeWidth(4)
 
   for i=0,ps do
       p[i].ox= p[i].x
       p[i].oy= p[i].y
       p[i].x = p[i].x + p[i].vx
       p[i].y = p[i].y + p[i].vy
    
 
       if p[i].x<0 then
           p[i].x=0
           p[i].vx= -p[i].vx
           sound(SOUND_BLIT, 24216)
       end
 
       if p[i].y<0 then
           p[i].y=0
           p[i].vy= -p[i].vy
         sound(SOUND_JUMP, 34327)
       end
 
       if p[i].x>WIDTH then
           p[i].x=WIDTH
           p[i].vx= -p[i].vx
         sound(SOUND_JUMP, 16358)
       end
 
       if p[i].y>HEIGHT then
           p[i].y=HEIGHT
           p[i].vy= -p[i].vy
        sound(SOUND_BLIT, 5520)
       end
 
       p[i].vx = p[i].vx*0.98
       p[i].vy = p[i].vy*0.98
 
       line(p[i].ox, p[i].oy, p[i].x, p[i].y)
   end
end
 
function touched(t)
   a=5
   for i=0,ps do
       d= (p[i].x-t.x)*(p[i].x-t.x) + (p[i].y-t.y)*(p[i].y-t.y)
       d= math.sqrt(d)
       p[i].vx = p[i].vx - a/d*(p[i].x-t.x)
       p[i].vy = p[i].vy - a/d*(p[i].y-t.y)
   end
 
end

I think in the function setup() I should set a Boolean to false and in function draw() I should have an if statement that states if the Boolean is false make a sprite of the pause button in the right hand corner and if it is true make the play counterpart button appear also in the right hand corner. In function touched(t) I would have another if statement that says that if the pause button is touched the Boolean would be set to true and the statement would also say that if the play button is pressed it would set the Boolean to false. If the Boolean is true the application would run but if it was false it would not run. If this how you are to do please post back my code with necessary corrections as I have trouble understanding what people tell me to do for I am ten and if this not how you do it please tell me what to do like make new classes or etcetera.

I tried adding a pause to your program, but when paused, the screen was very jittery. I tried physics.pause(), but you’re not using any physics functions so that didn’t work. Pressing the normal pause button at the bottom of the screen worked, but I don’t think that’s what you’re after. Will try other options later when I have more time unless someone else does it.

Maybe are you pausing what happens in the touched() function?

Add the lines indicated below. The program will pause when you touch the upper left corner of the screen. Touching anywhere else will resume the program. As I said in the earlier post, the screen is a little jittery when paused.


pause = 0    -- add this as the first line
 
function draw()
    if pause==1 then    -- add these 3 lines in draw
        return
    end

   noSmooth()
   background(2, 2, 2, 255)
   fill(255, 0, 160, 255)


function touched(t)
    if t.state == BEGAN then    -- add these 8 lines in touched
        if pause == 1 then
            pause = 0
        end
        if t.x<50 and t.y>HEIGHT-50 then
            pause = 1
        end
    end  

    a=5

This variation of your code may avoid the jitters of a paused state (a quick triple-tap to pause; a single touch to restart):


supportedOrientations(ANY)
displayMode(FULLSCREEN)
ps = math.random(10, 350)
p={}
paused = false

function setup()
   for i = 0, ps do
        p[i]= {
            x = math.random(WIDTH * 12)/12,
            y = math.random(HEIGHT * 8)/8,
            ox = 0.0, oy = 0.0,
            vx = math.random(20) - 10,
            vy = math.random(20) - 10
        }
    end
    local colors = {
        color(255, 255, 255, 255), 
        color(0, 237, 255, 255),
        color(255, 0, 212, 255),
        color(76, 248, 15, 255),
        color(20, 39, 245, 255),
        color(239, 255, 0, 255),
        color(245, 11, 3, 255)
    }
    local stcolor = colors[math.random(#colors)]
    noSmooth()
    stroke(stcolor)
    strokeWidth(4) 
end

function draw()
    background(0)
    -- If not paused then update
    if not paused then
        for i = 0, ps do
            p[i].ox = p[i].x
            p[i].oy = p[i].y
            p[i].x = p[i].x + p[i].vx
            p[i].y = p[i].y + p[i].vy
            if p[i].x < 0 then
                p[i].x = 0
                p[i].vx = -p[i].vx
                sound(SOUND_BLIT, 24216)
            end 
            if p[i].y < 0 then
                p[i].y = 0
                p[i].vy = -p[i].vy
                sound(SOUND_JUMP, 34327)
            end
            if p[i].x > WIDTH then
                p[i].x = WIDTH
                p[i].vx = -p[i].vx
                sound(SOUND_JUMP, 16358)
            end 
            if p[i].y > HEIGHT then
                p[i].y = HEIGHT
                p[i].vy = -p[i].vy
                sound(SOUND_BLIT, 5520)
            end 
            p[i].vx = p[i].vx * 0.98
            p[i].vy = p[i].vy * 0.98
        end
    end
    -- In all cases, draw current state
    for i = 0, ps do
       line(p[i].ox, p[i].oy, p[i].x, p[i].y)
    end
end
 
function touched(t)
    if not paused then
        if t.state == ENDED and t.tapCount == 3 then -- Triple-tap to pause
            paused = true
            return
        end
        local a=5
        for i = 0, ps do
            local d = (p[i].x - t.x)*(p[i].x - t.x) + (p[i].y - t.y)*(p[i].y - t.y)
            d = math.sqrt(d)
            p[i].vx = p[i].vx - a/d * (p[i].x - t.x)
            p[i].vy = p[i].vy - a/d * (p[i].y - t.y)
        end
        return
    end
    paused = false -- Release the pause
end

If you want to see your code with what looks like a button, try this.


pause = 0
amount = math.random(10,350)
p={}
ps = amount
supportedOrientations(ANY)
function setup()
    displayMode(FULLSCREEN)
   for i=0,ps do
       p[i]= {x=math.random(WIDTH*12)/12,
y=math.random(HEIGHT*8)/8, ox=0.0, oy=0.0, vx=math.random(20)-10,
vy=math.random(20)-10}
   end
    colors = {color(255, 255, 255, 255), color(0, 237, 255, 255),color(255, 0, 212, 255),color(76, 248, 15, 255),color(20, 39, 245, 255),color(239, 255, 0, 255),color(245, 11, 3, 255) }
    stcolor = colors[math.random(#colors)]
end

function draw()
    if pause==1 then
        fill(255,0,0)
        stroke(255)
        rect(0,HEIGHT-40,80,40)
        fill(255)
        text("start",40,HEIGHT-20)
        return
    end
    
   noSmooth()
   background(2, 2, 2, 255)

   fill(255,0,0)
   stroke(255)
   rect(0,HEIGHT-40,80,40)
   fill(255)
   text("stop",40,HEIGHT-20)

   fill(255, 0, 160, 255)
   stroke(stcolor)
   strokeWidth(4)

   for i=0,ps do
       p[i].ox= p[i].x
       p[i].oy= p[i].y
       p[i].x = p[i].x + p[i].vx
       p[i].y = p[i].y + p[i].vy
    
  
       if p[i].x<0 then
           p[i].x=0
           p[i].vx= -p[i].vx
           sound(SOUND_BLIT, 24216)
       end
 
       if p[i].y<0 then
           p[i].y=0
           p[i].vy= -p[i].vy
         sound(SOUND_JUMP, 34327)
       end
 
       if p[i].x>WIDTH then
           p[i].x=WIDTH
           p[i].vx= -p[i].vx
         sound(SOUND_JUMP, 16358)
       end
 
       if p[i].y>HEIGHT then
           p[i].y=HEIGHT
           p[i].vy= -p[i].vy
        sound(SOUND_BLIT, 5520)
       end
 
       p[i].vx = p[i].vx*0.98
       p[i].vy = p[i].vy*0.98

       line(p[i].ox, p[i].oy, p[i].x, p[i].y)
   end
end
 
function touched(t)
    if t.x<50 and t.y>HEIGHT-50 and t.state == BEGAN then
        if pause==1 then
            pause=0
        else
            pause = 1
        end
        return
    end  
      
   a=5
   for i=0,ps do
       d= (p[i].x-t.x)*(p[i].x-t.x) + (p[i].y-t.y)*(p[i].y-t.y)
       d= math.sqrt(d)
       p[i].vx = p[i].vx - a/d*(p[i].x-t.x)
       p[i].vy = p[i].vy - a/d*(p[i].y-t.y)
   end
 
end

Thanks
This is now my code


pause = 1
amount = math.random(10,350)
p={}
ps = amount
supportedOrientations(ANY)
function setup()
    displayMode(FULLSCREEN)
   for i=0,ps do
       p[i]= {x=math.random(WIDTH*12)/12,
y=math.random(HEIGHT*8)/8, ox=0.0, oy=0.0, vx=math.random(20)-10,
vy=math.random(20)-10}
   end
    colors = {color(255, 255, 255, 255), color(0, 237, 255, 255),color(255, 0, 212, 255),color(76, 248, 15, 255),color(20, 39, 245, 255),color(239, 255, 0, 255),color(245, 11, 3, 255) }
    stcolor = colors[math.random(#colors)]
    
    noSmooth()
    fill(255, 255, 255, 255)
    font("ArialMT")
    fontSize(18)
    text("Tap in the upper left hand corner to pause and tap", 260, 720)
  
    noSmooth()
    fill(120, 107, 107, 255)
    font("Copperplate-Bold")
    fontSize(94)
    text("Bernie's", 327, 556)
    
    noSmooth()
    fill(119, 109, 109, 255)
    font("Copperplate-Bold")
    fontSize(72)
    text("Particle Engine", 329, 339)
    
    noSmooth()
    fill(255, 255, 255, 255)
    font("ArialMT")
    fontSize(18)
    text(" anywhere to resume", 543, 720)
end
function draw()
        if pause==1 then
        return
    end

    noSmooth()
   background(2, 2, 2, 255)
   fill(255, 0, 160, 255)
   stroke(stcolor)
   strokeWidth(4)
    for i=0,ps do
       p[i].ox= p[i].x
       p[i].oy= p[i].y
       p[i].x = p[i].x + p[i].vx
       p[i].y = p[i].y + p[i].vy
    
 
       if p[i].x<0 then
           p[i].x=0
           p[i].vx= -p[i].vx
           sound(SOUND_BLIT, 24216)
       end
 
       if p[i].y<0 then
           p[i].y=0
           p[i].vy= -p[i].vy
         sound(SOUND_JUMP, 34327)
       end
 
       if p[i].x>WIDTH then
           p[i].x=WIDTH
           p[i].vx= -p[i].vx
         sound(SOUND_JUMP, 16358)
       end
 
       if p[i].y>HEIGHT then
           p[i].y=HEIGHT
           p[i].vy= -p[i].vy
        sound(SOUND_BLIT, 5520)
       end
 
       p[i].vx = p[i].vx*0.98
       p[i].vy = p[i].vy*0.98
 
       line(p[i].ox, p[i].oy, p[i].x, p[i].y)
    end
end
 
function touched(t)
   a=5
   for i=0,ps do
       d= (p[i].x-t.x)*(p[i].x-t.x) + (p[i].y-t.y)*(p[i].y-t.y)
       d= math.sqrt(d)
       p[i].vx = p[i].vx - a/d*(p[i].x-t.x)
       p[i].vy = p[i].vy - a/d*(p[i].y-t.y)
   end
   if t.state == BEGAN then
        if pause == 1 then
            sound(SOUND_POWERUP, 29888)
            pause = 0 
        end
        if t.x<50 and t.y>HEIGHT-50 then
            sound(SOUND_POWERUP, 29885)
            pause = 1
        end
   end
end

It is very jittery. I believe it is because it is a bit crude and because the pause behavior needs to be polished since before the pause button it was not jittery. Could you please fix it and post it ?

@hpmargulies

You did a good job of writing the original code that you submitted here. I’m sure we could fix your code and post it, but that would not benefit you. The best way to learn to program is thru trial and error. I believe you would benefit more to take the suggestions that I and mpilgrem have given you and try to update your code yourself. If you still have problems then everyone here would be more than happy to help you, but the best way to learn is to try to do it first and then ask for help.

Hello @hpmargulies. If you compare the draw() function in my previous post to your current code, you’ll see how I fixed the jitters. I suggest you ask yourself the question: When my application is in a ‘paused’ state, what do I want to be drawn (up to 60 times a second) in the viewer?

Thanks for all the help