My first custom game! please review


function setup()

    x1=WIDTH/2
    x2=WIDTH/2
    speedX=5
    Hullx=WIDTH/2
    Hully=20
    y1=165
    y2=225
    col1 = colr()
    col2 = clor()
    col3 = corl()
    p=math.random(1,4)
    hullWidth=290
    dropperWidth=290
    dropperx=math.random(0,WIDTH-290)
    droppery=HEIGHT-60
    speeddrop=1
    Score=0       end

function draw()

    background(27, 151, 212, 255)
    x2=x2-speedX
    droppery=droppery-speeddrop
    
    if droppery>=y2+55 and droppery<=y2+65 and dropperx<=x2+3 and dropperx>=x2-3 then Score=Score+4 
    pushStyle()
    fill(0, 0, 0, 255)   -- the scores text will have color black
    font("TimesNewRomanPS-BoldMT")  -- the font type for the score number
    fontSize(80)   -- the font size for the score number
    text("PERFECT!",WIDTH/2,500)   -- the position of the text of score
    popStyle() 
    sound()  end
    
     if droppery>=y2+55 and droppery<=y2+65 and dropperx~=x2 and dropperx>x2 and WIDTH==1024
     then dropperWidth=dropperWidth-math.abs(dropperx-x2) 
     hullWidth=hullWidth-math.abs(dropperx-x2)
     droppery=HEIGHT-60 
     dropperx=math.random(0,WIDTH-290)
     speeddrop=math.random(1,2)
     Score=Score+1 end
    
       if droppery>=y2+55 and droppery<=y2+65 and dropper~=x2 and dropperx<x2 and WIDTH==1024
       then dropperWidth=dropperWidth-math.abs(x2-dropperx) 
       hullWidth=hullWidth-math.abs(x2-dropperx)
      droppery=HEIGHT-60 
      dropperx=math.random(0,WIDTH-290) 
      speeddrop=math.random(1,2) 
      Score=Score+1 end
    
     if droppery>=y2+55 and droppery<=y2+65 and dropperx~=x2 and dropperx>x2 and WIDTH==768
     then dropperWidth=dropperWidth-math.abs(dropperx-x2) 
     hullWidth=hullWidth-math.abs(dropperx-x2)
     droppery=HEIGHT-60 
     dropperx=math.random(0,WIDTH-290) 
     speeddrop=math.random(1,5) 
     Score=Score+1 end
    
       if droppery>=y2+55 and droppery<=y2+65 and dropper~=x2 and dropperx<x2 and WIDTH==768
       then dropperWidth=dropperWidth-math.abs(x2-dropperx)
       hullWidth=hullWidth-math.abs(x2-dropperx)
       droppery=HEIGHT-60 
       dropperx=math.random(0,WIDTH-290)
       speeddrop=math.random(1,5)
       Score=Score+1 end
    
    if x2>=-15 and x2<=15 then speedX=-5 end
    if x2>=WIDTH-hullWidth then speedX=5 end
    
    if dropperWidth<=0 then sprite("Project:Custom Image", WIDTH/2, HEIGHT/2,WIDTH,HEIGHT) end
     
    if p==1 then sprite("Space Art:Part Gray Hull 2",Hullx,Hully,360,300) end
    if p==2 then sprite("Space Art:Part Green Hull 2",Hullx,Hully,360,300) end
    if p==3 then sprite("Space Art:Part Red Hull 2",Hullx,Hully,360,300) end
    if p==4 then sprite("Space Art:Part Yellow Hull 2",Hullx,Hully,360,300) end
    
    pushStyle()
    fill(col1)
    rect(x1-146,y1,290,60)
    popStyle()
    
    pushStyle()
    fill(col2)    
    rect(x2,y2,hullWidth,60)
    popStyle()   
   
    pushStyle()
    fill(col3) 
    rect(dropperx,droppery,dropperWidth,60)
    popStyle()
    
    pushStyle()
    fill(0, 0, 0, 255)   -- the scors text will have color black
    font("ArialMT")  -- the font type for the score number
    fontSize(80)   -- the font size for the score number
    text(Score,WIDTH/2+5,27)   -- the position of the text of score
    popStyle() 
        
    end
    
        function colr()
        a = math.random(0,255)
        b = math.random(0,255)
        c = math.random(0,255)
        return color(a,b,c)
         end
        
        function clor()
        d = math.random(0,255)
        e = math.random(0,255)
        f = math.random(0,255)
        return color(d,e,f)
         end

        function corl()
        g = math.random(0,255)
        h = math.random(0,255)
        i = math.random(0,255)
        return color(g,h,i)
         end
    function touched (touch)
    if touch.state==BEGAN and speedX==5 then speedX=0.005 end
    if touch.state==BEGAN and speedX==-5 then speedX=-0.005 end
    
    if touch.state==ENDED and speedX==0.005 then speedX=5  end
    if touch.state==ENDED and speedX==-0.005 then speedX=-5  end
    
    if dropperWidth<=0 and touch.state==BEGAN then restart() end
end

This is just the alpha of the game, its definately not done just yet but the game is playable and I would love to hear what you guys think about it

Make sure to use three ~ above and below your code so it’s formatted correctly - right now it’s hard to copy the code. (There’s an edit button in the top right of your post)

~~~

Your code

~~~

nice. reminds me of the arcade game stacker. :-bd Whats with the weird thing that appears when you lose?

Added the 3 ~'s before and after the code.

You have a lot of code repetition with the if tests, which makes it harder to modify and debug.

Try something like this (I may have messed up the conditions but you’ll get the idea)

if droppery>=y2+55 and droppery<=y2+65 then
    local drop,widthAdjust
    if WIDTH==1024 then
        drop=2
        if  dropperx>x2 then widthAdjust=dropperx-x2
        elseif dropperx<x2 then widthAdjust=x2-dropperx
        end
    elseif WIDTH==768 then
         drop=5
         if dropperx>x2 then  widthAdjust=dropperx-x2 
         elseif dropperx<x2 then  widthAdjust=x2-dropperx
          end
    end
    if widthAdjust then
        droppery=HEIGHT-60 
        dropperx=math.random(0,WIDTH-290) 
        speeddrop=math.random(1,drop)
        dropperWidth=dropperWidth-math.abs(widthAdjust) 
        hullWidth=hullWidth-math.abs(widthAdjust)
        Score=Score+1
    end
end

Jrohanian when you lose it says “Perfect!” it looks wierd because it only stays for 1 frame.
I don’t know how to make it stay longer

Ignatz thankyou for the suggestion, it looks good. I will definately make some changes to my code

@Fazil0015 - to make text stay on the screen you need a variable like so

--as soon as you have lost, write this
state="Lost"
timer=5 --set timer for 5 seconds

--in the draw function, check the value of state
if state=="Lost" then
   timer=timer-DeltaTime --reduce timer by time since last draw
   if timer<0 then 
        state="" --or set it to something else like "Restart"
        break --stop showing text
   else
      --write text on screen
   end

@Fazil0015 no, the tower thing

Just a bug: if you let it run and dont touch it, you get a “perfect!”

Are you sure because that doesn’t happen for me. The x postion of the spawning the dropper is random so you can’t get perfect unless you pause it on the right moment :slight_smile:

Happens everytime for me

Try playing it with your ipad vertically or sideways

Ignatz can you give please me an example of making the text stay for more than 1 frame in a conditional. That skeleton is very good and I understand but it doesn’t work in my code

@Fazil0015 Here’s a quick example.


function setup()
    pause=0    
end

function draw()
    background(40, 40, 50)
    fill(255)
    if pause>0 then
        text("PERFECT",WIDTH/2,HEIGHT/2)
        pause=pause-1
    else
        text("tap screen",WIDTH/2,HEIGHT/2)
    end
end

function touched(t)
    if t.state==BEGAN then
        pause=120   -- pause for 2 seconds
    end
end