Custom Game Suggestions

This is a custom game that I made and I am looking for suggestions to better the game and/or condense the code. Thanks.


function setup()

    displayMode(FULLSCREEN)
    
    c1 = physics.body(CIRCLE,65)
    c1.x = WIDTH/2
    c1.y = 100
    c1.gravityScale = 0
   
    state = 1
    
    highscore = readLocalData("highscore")
    if not highscore then
        highscore = 0
    end
   
    r1 = physics.body(POLYGON,
                      vec2(-10,10),
                      vec2(-10,-10),
                      vec2(10,-10),
                      vec2(10,10))    
    r2 = physics.body(POLYGON,
                      vec2(-10,10),
                      vec2(-10,-10),
                      vec2(10,-10),
                      vec2(10,10))    
    r3 = physics.body(POLYGON,
                      vec2(-10,10),
                      vec2(-10,-10),
                      vec2(10,-10),
                      vec2(10,10))    
    r4 = physics.body(POLYGON,
                      vec2(-10,10),
                      vec2(-10,-10),
                      vec2(10,-10),
                      vec2(10,10))    
    r5 = physics.body(POLYGON,
                      vec2(-10,10),
                      vec2(-10,-10),
                      vec2(10,-10),
                      vec2(10,10))    
    r6 = physics.body(POLYGON,
                      vec2(-10,10),
                      vec2(-10,-10),
                      vec2(10,-10),
                      vec2(10,10))    
    r7 = physics.body(POLYGON,
                      vec2(-10,10),
                      vec2(-10,-10),
                      vec2(10,-10),
                      vec2(10,10))
    r8 = physics.body(POLYGON,
                      vec2(-10,10),
                      vec2(-10,-10),
                      vec2(10,-10),
                      vec2(10,10))
    r9 = physics.body(POLYGON,
                      vec2(-10,10),
                      vec2(-10,-10),
                      vec2(10,-10),
                      vec2(10,10))
    
    r1.x = math.random(56.889,967.111)
    r1.y = HEIGHT+200    
    r2.x = math.random(56.889,967.111)
    r2.y = HEIGHT+200    
    r3.x = math.random(56.889,967.111)
    r3.y = HEIGHT+200    
    r4.x = math.random(56.889,967.111)
    r4.y = HEIGHT+200    
    r5.x = math.random(56.889,967.111)
    r5.y = HEIGHT+200
    r6.x = math.random(56.889,967.111)
    r6.y = HEIGHT+200
    r7.x = math.random(56.889,967.111)
    r7.y = HEIGHT+200
    r8.x = math.random(56.889,967.111)
    r8.y = HEIGHT+200 
    r9.x = math.random(56.889,967.111)
    r9.y = HEIGHT+200
    
    score = 0 
end    

function draw()    
    background(40, 40, 50)
    fill(38, 29, 233, 255)
    if state == 1 then
        font("Copperplate-Bold")
        fontSize(50)
        fill(255, 255, 255, 255)
        text("Touch and Hold To Start",500,HEIGHT/2)
        
    end     
    if state == 2 then 
        wave1() 
        reset()
        score = score+.1
        font("Copperplate-Bold")
        fontSize(50)
        fill(255, 255, 255, 255)
        textMode(CORNER)
        text(score)
        fill(255, 20, 0, 255)
        ellipse(c1.x,c1.y,65)
        
    end    
    if state == 3 then
        font("Copperplate-Bold")
        fill(242, 242, 242, 255)
        fontSize(50)
        text("GAME OVER",350,HEIGHT/2)
        score = score+0
        font("Copperplate-Bold")
        fontSize(50)
        fill(255, 255, 255, 255)
        text(score)
        if score > highscore then
                highscore = score
            saveLocalData("highscore",score)
            end
        text("Highscore",365,350)
        fontSize(100)
        text(highscore,385,200)
        
    end    
    endgame()
end

function touched(touch)
    c1.x = touch.x
    c1.y = touch.y
    if touch.state == ENDED then    
        state = 3 
    end
    if touch.state == BEGAN then
        setup() 
        state = 2       
    end    
end

function wave1()
    rect(r1.x,r1.y,56.889,56.889)
    rect(r2.x,r2.y,56.889,56.889)
    rect(r3.x,r3.y,56.889,56.889)
    rect(r4.x,r4.y,56.889,56.889)
    rect(r5.x,r5.y,56.889,56.889)
    rect(r6.x,r6.y,56.889,56.889)
    rect(r7.x,r7.y,56.889,56.889)
    rect(r8.x,r8.y,56.889,56.889)
    rect(r9.x,r9.y,56.889,56.889)    
end

function reset()
    if r1.y < -56.889 then
        r1.y = HEIGHT+100
        r1.x = math.random(56.889,967.111)
        end
    if r2.y < -56.889 then
        r2.y = HEIGHT+100
        r2.x = math.random(56.889,967.111)
        end
    if r3.y < -56.889 then
        r3.y = HEIGHT+100
        r3.x = math.random(56.889,967.111)
        end
    if r4.y < -56.889 then
        r4.y = HEIGHT+100
        r4.x = math.random(56.889,967.111)
        end
    if r5.y < -56.889 then
        r5.y = HEIGHT+100
        r5.x = math.random(56.889,967.111)
        end
    if r6.y < -56.889 then
        r6.y = HEIGHT+100
        r6.x = math.random(56.889,967.111)
        end
    if r7.y < -56.889 then
        r7.y = HEIGHT+100
        r7.x = math.random(56.889,967.111)
        end
    if r8.y < -56.889 then
        r8.y = HEIGHT+100
        r8.x = math.random(56.889,967.111)
        end
    if r9.y < -56.889 then
        r9.y = HEIGHT+100
        r9.x = math.random(56.889,967.111)
        end
    end

function endgame()
    if c1:testOverlap(r1) then
        state = 3
    end
    if c1:testOverlap(r2) then
        state = 3
    end
    if c1:testOverlap(r3) then
        state = 3
    end
    if c1:testOverlap(r4) then
        state = 3
    end
    if c1:testOverlap(r5) then
        state = 3
    end
    if c1:testOverlap(r6) then
        state = 3
    end
    if c1:testOverlap(r7) then
        state = 3
    end
    if c1:testOverlap(r8) then
        state = 3
    end
    if c1:testOverlap(r9) then
        state = 3
    end    
end

@JackMoody3 You need to learn about tables. That will cut the size of your code and make it easier to add to.

@dave1707 I agree, what is a good resource for learning about tables?

Tap on the “Wiki” option under Codea Talk at the top of the forum. A there are a lot of tutorials there.

Okay, thankyou

@JackMoody3

Tables aren’t that hard, just think of them as a way of storing multiple values:

x = {"Hello ", "World!"}
print(x[1])

So x is the table, and to get the nth index of that table you use [n] after the table.

I’m pretty sure you can store any values in a table.

There are two different ways you can use tables, as arrays, or as dictionaries.

In an array you can basically do what I did above:

x = {1, 2, 3, 4}

Remember how you would say [n] to get the nth value of the array? In a dictionary you can control the indexes:

x = {["Hello"] = "Hi ", ["Earth"] = "World!"}

Which can also be shortened with some syntactic sugar:

x = {Hello = "Hi ", Earth = "World!"}

Thank you Lua :smiley:

So then you can do this:

print(x["Hello"])

Or some more syntactic sugar!

print(x.Hello)

You can also create new indexes after you make the table in 3 ways:

x[n] = "Hello"
table.insert(x, n, "Hello")
rawset(x, n, Hello)

Aside from that Lua provides a whole table of different functionalities (we’ve already used one before)

The table is literally called just that, table.

Here’s a few main ones:

table.insert
table.remove
table.unpack

Another important note to remember, is that tables are stored by reference not by value:

x = {}
y = x
--Any changes to x or y will change the other:
x[1] = "Hi"
print(y[1])
--> Hi

Looping through a table COULD be done with a normal for loop:

x = {"Hi ", "Planet ", "Earth"}
for I = 1,#x do
print(x[I])
end

Or with pairs/ipairs:

for I,v in pairs(x) do
print(v)
end

Important note: #x won’t work if x is a dictionary!

In the generic for loop used above, I is the index, v is the value.

Difference between pairs and pairs:

pairs: Ignores nil Indexes

ipairs: Stops at the first nil index it finds

If you have any questions don’t hesitate to PM me!

https://coolcodea.wordpress.com/2014/10/01/169-why-tables-and-classes-are-so-useful/

You can also just think of a 1d array as a matrix. They are actually sort of simpler then matrixes.