Absolutely crazy problem

Hi guys, I’ve been trying to fix this for the whole day now it works great but when it ends the game the whole screen flashes randomly. But if you open the parameters frame to the left it fixes the problem but the text it’s supposed to show is gone.

ship_pos = vec2(WIDTH * .5,40)
enemies = {}
bullets = {}
framebullet = 0
frameenemy = 0
current_rate = 30
score = 0
lives = 3
txt = nil
txtframe = 0
roundtime = 0
reduce = false
vel = 3
stop = false
canmove = false
canclr = true

function touched(t)
    if t.state == BEGAN then
        canmove = true
    elseif t.state == ENDED then
        canmove = false
    end
end

function draw()
    if canmove then
        if CurrentTouch.x < WIDTH * .5 and not stop then
            ship_pos.x = ship_pos.x - 5
        elseif not stop then
            ship_pos.x = ship_pos.x + 5
        end
    end
    roundtime = roundtime + 1
    framebullet = framebullet + 1
    frameenemy = frameenemy + 1
    if canclr then
        smooth()
        background(0,0,0)
    end
    sprite("Project:Apple ship",ship_pos.x,ship_pos.y,30,30)
    if framebullet > 50 and not stop then
        framebullet = 0
        local projectile = vec2(ship_pos.x,ship_pos.y + 5)
        table.insert(bullets,projectile)
    end
    if frameenemy > current_rate and not stop then
        frameenemy = 0
        local newenemy = vec2(math.random(math.floor(WIDTH * .9)),HEIGHT * .9)
        table.insert(enemies,newenemy)
    end
    for i,v in pairs(bullets) do
        if v.y + 4 > HEIGHT and not stop then
            table.remove(bullets,i)
        end
        if not stop then
            v.y = v.y + 4
            fill(255, 255, 255, 255)
            ellipse(v.x,v.y,18,18)
        end
    end
    for i,v in pairs(enemies) do
        if v.y < HEIGHT * .004 and not stop then
            table.remove(enemies,i)
        end
        if not stop then
            v.y = v.y - vel
            sprite("Project:spaceship",v.x,v.y,30,30)
        end
        if ship_pos:dist(v) < 18 and not stop then
            table.remove(enemies,i)
            txt = "You lost a life! You have "..lives-tonumber("1").." more."
        end
        for ii,v2 in pairs(bullets) do
            if v2:dist(v) < 15 and not stop then
                score = score + 50
                table.remove(enemies,i)
                table.remove(bullets,ii)
            end
        end
    end
    font("Optima-ExtraBlack")
    fontSize(30)
    fill(255, 255, 255, 255)
    text("Score: "..score,WIDTH * .8,HEIGHT * .95)
    text("Lives: "..lives,WIDTH * .8,HEIGHT * .9)
    if txt and txtframe <= 70 and not stop then
        if reduce == false then
            reduce = true
            lives = lives - 1
        end
        txtframe = txtframe + 1
        fill(255, 0, 0, 255)
        text(txt,WIDTH * .5,HEIGHT * .5)
    end
    if txtframe >= 70 and not stop then
        txtframe = 0
        reduce = false
        txt = nil
    end
    if lives <= 0 and not stop then
        stop = true
        canclr = false
    end
    if roundtime > 89 and not stop then
        roundtime = 0
        if current_rate > 3 then
            vel = vel + .12
            current_rate = current_rate - 1
        end
        
    end
end

To specify further, by end game I mean the if statement near the bottom of this code where it checks if the lives are less or equal to 0

I’ve fixed your code display, you need to put three ~ on a line before and after your code

The flickering is because when the lives get to zero, you stop clearing the display. If you comment out the line setting canclr to false -- canclr = false then the display will continue to be cleared and the flickering stops.

(Remember that when you post code in the forums other people won’t have your sprites, so it’s probably a good idea to quickly swap your sprites for the built in ones before you post.)

Ultimately though, this is an issue with handling the switching of state, in this case, from gameplay to a game over display. You should think about adding a simple state manager. If you type “finite state machine” into the search box, you’ll see lots of approaches to this issue.

The idea is to try to avoid having a super-long draw function with long-branching if statements for each state the game can be in, as these will eventually become a nightmare to read.

This is my favourite solution to this:

https://codea.io/talk/discussion/comment/63797/#Comment_63797