Game help :)

okay im newish to codea and i have been posting all these little things that i need help with in separate sections, instead ill post everything here

heres my first problem i am messing around with. I think i know whats wrong but i dont know how to fix it

if lives <= 15 then
m = sprite"Space Art:Red Ship Damaged"
else
m = “Space Art:Red Ship”
sprite(m)

that works, however when testing, the health gets down to 15, it changes sprite but then things disappear, i think the new sprite covers up all the text i had, aswell as the bullets shot out from the ship… anyone know how to fix it?

It might be easier if you posted the full code (with ~~~ at the top and bottom so it formats correctly).

I think this would be better, though:

if lives <= 15 then
    m = "Space Art:Red Ship Damaged"
else
    m = "Space Art:Red Ship"
end
sprite(m, PUT X HERE, PUT Y HERE)

ill give it a shot

here:

 if lives <= 15 then 
 f = "Space Art:Red Ship Damaged"
 end
 
if lives >= 15 then
f = "Space Art:Red Ship"
end

sprite(f)

i did what you said by moving the end up and spaced it out abit and now it works! thanks!

Just FYI: Spacing it out doesn’t help the code, It’s just easier to read
(How Hypocritical of me)

it also helps you notice there was an “end” missing before “sprite” in the first post above

@OwenClaxton One thing you might want to change: the second if statement. Right now, the first one is checking if lives is less than or equal to 15. The second is checking if lives is greater than or equal to 15. If lives is 15, both if statements are true, and the one last defined will be the one that works. Both if statements say, as a second question, that the code inside it gets fired also if lives is 15, as the = part in <= or >= says.

All of that could be avoided, too, if you used an else. If then else end. Like this:

function setup() -- This is the beginning of the function named setup, which is ran at the start of the program.
    
    local b = true -- Create a "local" variable of the name b, with a boolean (fancy word for true/false) value of true. "local" means that this variable cannot be used in any other function than setup, and is useful for temporary variables.
    
    if b then -- If b is true, run this code:
        
        print("b is true!") -- Simply prints "b is true!" in the output.
        
    else -- The if statement failed, meaning b is false. So we run this code instead:
        
        print("b is false!") -- Simply prints "b is false!" in the output.
        
    end
    
    -- Since we defined "b" manually, not from user input, and we set it to true, "b" will always be true. If you want a different outcome, change the "b = true" part to "b = false"
    
end -- This means that we are done writing the code in setup, and after this, we could make, say, a function named draw, which is called every frame to put pictures on the screen.

ah thanks.
I have another problem lol

so lets say i have 5 numbers; 1,2,3,4,5
is there a piece of coding where when it runs it could randomly choose one of those?
for example

random.number(1,2,3,4,5)
if random.number = 1 then
do this
if random number = 2 then
do this
etc

If your numbers are just in order like 1,2,3,4,5, etc. then you can use this.

function setup()
number = math.random(5)
end

function draw()

if number == 1 then 

print("Did you hear of the guy who got hit with a can of soda? at least it was a soft drink.")

elseif number == 2 then

print("I'm reading a book about anti-gravity, its hard to put down.")

end 
     end

But if your numbers are like 1, 4, 8, 11, 23, etc. then use:

function setup()
numbers = {1,4,8,11,23} -- Put all your numbers in a table
number = numbers[math.random(#numbers)] -- Set the number value to one of our numbers.
end

function draw()
if number == 11 then

print("No puns this time, just an inspirational quote")

elseif number == 23 then 

print("Don't judge each day by the harvest you reap but by the seeds that you plant")

end
end

Thanks!!

whats the difference between elseif and else? is it like if that previous option wasn’t true, then is this one true?

@OwenClaxton Depends really, in your case, if you have only two numbers then you can use else, but if you have five different numbers then you don’t want every single one to pass the “else” test would you?

ah thanks :slight_smile:

@OwenClaxton

function setup()
    number = 1 -- Change this to get a different outcome
    if number == 1 then -- Is the number equal to 1?
        print("The number is 1!")
    elseif number == 2 then -- The number isn't 1, as the above if failed. Is it 2?
        print("The number is 2!")
    elseif number == 3 then -- The number isn't 2 either, so we check if it's 3.
        print("The number is 3!")
    else -- It's not 1, 2, or 3. So we run this:
        print("The number is not 1, 2, or 3!")
    end
end

You don’t have to have the else at the end, either.

Elseifs are just a compacted version of:

if a then
    -- Blah 1
else
    if b then
        -- Blah 2
    else
        if c then
            -- Blah 3
        end
    end
end

The user and all related content has been deleted.

hmmm…

Is there any way to set up a menu?

Do a forum search for menu. There’s plenty of examples.

okie dokie

@Owen, you’re not taking any trouble to look for things before asking other people to give you answers.

Your questions on if tests and random numbers could be answered very quickly by just googling “random number Lua” for example, or looking at the Lua section of the help built into Codea. I do that myself all the time. And you can search the forum for things like menus.

While other users are happy to help, it should not be with basic stuff you can’t be bothered to look up for yourself. So please look before you ask. If you can’t find it and there is no tutorial on it, that is the time to ask.

First download the dependency code into its own project on your iPad

Then go to the project where you want to use it, and in the code editor, press the plus symbol at upper right. Find the dependency project on the list and touch it so it is ticked.

Now you can use that code as though it was in your project