I need help on a project (beginner)

Before you rotate your ship, you push matrix and then when done, popmatrix. This is how you keep everything from rotating I think…

.@D4rk_Code - Asteroids is a good beginner project. I would suggest you use meshes for the graphics.

I will be extending my SpaceWar! game tutorial with physics at some stage so the ships are attracted toward the sun in the centre of the screen.

In the meantime, if you want to look at another Asteroids version check out @juaxix’s version at: http://twolivesleft.com/Codea/Talk/discussion/114/asteroides-source-code#Item_21

I tried this version, and to be honest I don’t like it; controls horrendously for me…now the Spacewar! game, I like the tutorials so far, though they have all confused me to death, I believe I might use your triangle ship mesh for the ships if I can’t get the one that wrmichael drew me to animate, in the mean time I’m also working on an Enduro port for the Casio Prizm :stuck_out_tongue: With permission of course hahha!

Oh well I have a minimal level of understanding, but I guess I don’t fully understand how what you’re doing works

Also, I"m super bogged down at college so Developing this asteroids will be slowish, help is welcomed, and hopefully a link with the code will be available soon (week or 2)

I have been playing. I have a ship, two buttons. No throttle or firing yet… Just spinning

Button = class()

-- Fast Mesh Button Class courtesy of @Vega
-- 3 Aug 2012
--
-- Modified: - Call Back Functionality added
--           - pushStyle() & popStyle() added to draw()
--           - tapped status added
--           - pointInRect() function added
--           - changed vec2 location to x and y (to reduce typing!)
--
-- Version 1.3 (4 Aug 2012)

function Button:init(text,x,y,width,height)
    self.state = "normal"
    self.text = text
    self.textColor = color(255,255,255,192)
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.visible = true
    self.fontSize = 28
    self.font = "ArialRoundedMTBold"
    self.color1 = color(255, 255, 255, 96)
    self.color2 = color(128,128,128,32)
    self.presscolor1 = color(192, 224, 224, 128)
    self.presscolor2 = color(96, 192, 224, 128)
    self.verts = self:createVerts(self.width, self.height)
    self.myMesh = mesh()
    self.myMesh.vertices = triangulate(self.verts)
    self.vertColor = {}
    self:recolor()
    self.action = nil
    self.tapped = false
    self.actionpress= false
    self.istouched = false
end

function Button:setColors(c1,c2,p1,p2)
    self.color1 = c1
    self.color2 = c2
    self.presscolor1 = p1
    self.presscolor2 = p2
    self:recolor()
end

function Button:textOptions(fn, sz, col)
    self.font = fn
    self.fontSize = sz
    self.textColor = col
end

function Button:draw()
    if self.visible == true then
        pushStyle()
        pushMatrix()
        translate(self.x,self.y)
        self.myMesh:draw()
        fill(self.textColor)
        fontSize(self.fontSize)
        font(self.font)
        text(self.text, self.width/2,self.height/2)
        self:drawLines(self.verts)
        popMatrix()
        popStyle()
        if self.istouched then
            if self.actionpress then
                if self.action then
                    self.action()
                end
            end
        end
    end
end

function Button:touched(touch)
    self.tapped = false
    if self.visible then  
        if pointInRect(touch.x, touch.y, self.x, self.y, self.width, self.height) then
            if touch.state == BEGAN then
                self.tapped = true
                self.state = "pressing"
                self:recolor()
                self.istouched = true
                
            elseif touch.state == ENDED then
                if self.state == "pressing" then
                    self.state = "normal"
                    self.tapped = true
                    self:recolor()
                    
                end
                if self.actionpress == false then
                    if self.action then
                        self.action()
                        actiontook = true
                    end
                end
                self.istouched = false
            end
        else
            self.state = "normal"
            self:recolor()
        end
        
    end
    if touch.state == ENDED then
        self.istouched = false
    end
    
end

function Button:createVerts(w,h)
    local r
    local v = {}
    if w > 100 or h > 100 then
        if w>=h then r = math.round(h/100) else r = math.round(w/100) end
    else
        r = 1
    end
    v[1] = vec2(w,6*r)
    v[2] = vec2(w-r,4*r)
    v[3] = vec2(w-2*r,2*r)
    v[4] = vec2(w-4*r,r)
    v[5] = vec2(w-6*r,0)
    v[6] = vec2(6*r,0)
    v[7] = vec2(4*r,r)
    v[8] = vec2(2*r,2*r)
    v[9] = vec2(r,4*r)
    v[10] = vec2(0,6*r)
    v[11] = vec2(0,h-6*r)
    v[12] = vec2(r,h-4*r)
    v[13] = vec2(2*r,h-2*r)
    v[14] = vec2(4*r,h-r)
    v[15] = vec2(6*r,h)
    v[16] = vec2(w-6*r,h)
    v[17] = vec2(w-4*r,h-r)
    v[18] = vec2(w-2*r,h-2*r)
    v[19] = vec2(w-r,h-4*r)
    v[20] = vec2(w,h-6*r)
    return v
end

function Button:drawLines(v)
    noSmooth()
    strokeWidth(1)
    stroke(0, 0, 0, 192)
    for i=1, #v-1 do
        line(v[i].x,v[i].y,v[i+1].x,v[i+1].y)
    end
    line(v[#v].x,v[#v].y,v[1].x,v[1].y)   
end

function Button:recolor()
    local lt, dk
    if self.state == "normal" then 
        lt = self.color1
        dk = self.color2
    else
        lt = self.presscolor1
        dk = self.presscolor2
    end
    for i=1,3 * #self.verts - 6 do
        if self.myMesh.vertices[i].y > self.height/2 then
            self.vertColor[i] = lt
        else
            self.vertColor[i] = dk
        end
    end
    self.myMesh.colors = self.vertColor
end

-- Math Utilities

function math.round(value)   
    -- math.round function courtesy of Vega.   
    return math.floor(value + 0.5)
    
end

function pointInRect(pointX, pointY, x, y, w, h)
    
    -- Returns true if point (pointX, pointY) is within the rectangle
    -- with lower left corner at (x, y) with a width of w and a
    -- height of h.
    --
    -- Reefwing Software (www.reefwing.com.au)
    -- Version 1.0
    
    if pointX >= x and pointX <= x + w and pointY >= y and pointY <= y + h then
        return true
    else
        return false
    end
    
end

-- main asteroids
shipr =0
-- Use this function to perform your initial setup
function rotateleft()
    shipr = shipr + 10
    if shipr >= 360 then
        shipr = 0
    end
end

function rotateright()
    shipr = shipr - 10
    if shipr <= 0 then
        shipr = 360
    end
end


function setup()
 --   print("Hello World!")
    x = 10
    as = Asteroids()
    
local shipr =0
btn = Button("<=",100,100,100,50)
btn.action = function ()
        --shipr = shipr +10
        rotateleft()
       -- print(shipr)
    end

btn.actionpress = true

btn2 = Button("=>",300,100,100,50)
btn2.action = function ()
        --shipr = shipr +10
        rotateright()
       -- print(shipr)
    end

btn2.actionpress = true

cs = {}
table.insert(cs,btn)
table.insert(cs,btn2)

table.insert(cs,as)

end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)
    
    pushMatrix()
    translate(HEIGHT/2,WIDTH/2)
    -- Draw our triangle invader
    -- 60 pixels high, 40 pixels wide
  --  print("sr:"..shipr)
    rotate(shipr)
    line( 0, 30, 20, -30 )
    line( 0, 30,-20, -30 )
    line( -20, -30, 20, -30 )
    x = x + 10
    -- Do your drawing here
    popMatrix()
  -- sprite(img,200,200,50,50)
    
    for a,control in pairs(cs) do
        control:draw()
    end
    
end
function touched(touch)  
    for a,control in pairs(cs) do
        control:touched(touch)
    end
   
end
Asteroids = class()

function Asteroids:init()
    -- you can accept and set parameters here
    self.x = 600
    self.y = 50
    self.w = 50
    self.h = 50
    
   img = image(8, 8)
img:set(1,4,0,255,0,255)
img:set(1,5,0,255,0,255)
img:set(1,6,0,255,0,255)
img:set(2,1,0,255,0,255)
img:set(2,2,0,255,0,255)
img:set(2,3,0,255,0,255)
img:set(2,4,0,255,0,255)
img:set(2,5,0,255,0,255)
img:set(2,6,0,255,0,255)
img:set(2,7,0,255,0,255)
img:set(3,1,0,255,0,255)
img:set(3,2,0,255,0,255)
img:set(3,3,0,255,0,255)
img:set(3,4,0,255,0,255)
img:set(3,5,0,255,0,255)
img:set(3,6,0,255,0,255)
img:set(3,7,0,255,0,255)
img:set(4,1,0,255,0,255)
img:set(4,2,0,255,0,255)
img:set(4,3,0,255,0,255)
img:set(4,4,0,255,0,255)
img:set(4,5,0,255,0,255)
img:set(4,6,0,255,0,255)
img:set(4,7,0,255,0,255)
img:set(5,1,0,255,0,255)
img:set(5,2,0,255,0,255)
img:set(5,3,0,255,0,255)
img:set(5,4,0,255,0,255)
img:set(5,5,0,255,0,255)
img:set(5,6,0,255,0,255)
img:set(5,7,0,255,0,255)
img:set(6,2,0,255,0,255)
img:set(6,3,0,255,0,255)
img:set(6,4,0,255,0,255)
img:set(6,5,0,255,0,255)
img:set(6,6,0,255,0,255)
img:set(6,7,0,255,0,255)
img:set(7,3,0,255,0,255)
img:set(7,4,0,255,0,255)
img:set(7,5,0,255,0,255)
img:set(7,6,0,255,0,255)

    self.mysprite = img
    
end

function Asteroids:draw()
    -- Codea does not automatically call this method
    sprite(self.mysprite,self.x,self.y)
end

function Asteroids:touched(touch)
    -- Codea does not automatically call this method
end

Thanks! The button class is telling me it expects a table and gets a nil value so it’s broke, I’ll keep the asteroids one! I’m looking into physics today, as I got a day off school

does the button class give you a line the error is on? it should work… I can rotate left and right in my little app.

I have each class in its own code window. I merged them together for simplicity of cut and paste.

Hello World!
error: error: [string “Button = class()…”]:277: bad argument #1 to ‘pairs’ (table expected, got nil)

Pausing playback

At – main is where the main program is. Maybe but the button class into its own code window?

Everything is its own class, I have main, asteroids, button, and soon probably ship

Freeze, Space Police!

I assume you made a copy-and-paste error. It may not be immediately obvious as many of your own code is badly formatted. So if I may make a suggestion …

Apart from that: Make sure you have a function setup() that gets called and that it looks like this:


function setup()
    . . .
    cs = {}
    table.insert(cs, btn)
    table.insert(cs, btn2)
    table.insert(cs, as)
end

There you have it, a table named cs to pass it to pairs.

Attempting the grafting now.