Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Need help in my code fast

edited February 2013 in Bugs Posts: 139
----------
---main---
----------
hero = nil
enemies = nil
bgLines = nil
explosion = nil
menu=0
GAME_PLAYING = 1
GAME_DEAD = 2
state = menu


-- Use this function to perform your initial setup
function setup() 
  state = menu
         button=Button(start)
 Button.action=function() Buttonpressed() 
  end  
 state = GAME_PLAYING  

    parameter.integer("BackgroundSpawnRate",0,3,2)
    parameter.integer("difficulty",1,3,3)

    hero = Invader()
    hero.position = vec2(WIDTH/2,HEIGHT/2)

    enemies = EnemyHorde()
    enemies.heroBullets = hero.bullets

    bgLines = StreamingLines()

    highscore = readLocalData("highscore")

    if not highscore then
        highscore = 0
    end
   end 


function touchingAtPos()
    if CurrentTouch.state == BEGAN or
       CurrentTouch.state == MOVING then
        return vec2( CurrentTouch.x, CurrentTouch.y )
    end

    return nil
end

-- This function gets called once every frame
function draw()
    drawbutton()
    displayMode()
    background(0, 0, 0, 255)


    sprite("Documents:minecraft", WIDTH/2, HEIGHT/2)
    bgLines.spawnRate = BackgroundSpawnRate

    bgLines:update()
    bgLines:draw()

    if state == GAME_PLAYING then

        -- Process touch
        touch = touchingAtPos()
        if touch then
         if CurrentTouch.x < (hero.position.x) then
            hero.position.x = hero.position.x - 5
         elseif CurrentTouch.x > (hero.position.x) then
            hero.position.x = hero.position.x + 5
         end
         if CurrentTouch.y < (hero.position.y) then
            hero.position.y = hero.position.y - 5 
         elseif CurrentTouch.y > (hero.position.y) then
            hero.position.y = hero.position.y + 5
         end
        end


        if enemies.killCount == 5 then
            enemies.spawnPattern = ENEMY_SPAWN_HARD
        end

        enemies:draw()

        hero:draw()

        -- Check if hero is hit
        for i,v in ipairs(enemies.units) do
            if v:dist(hero.position) < 40 then
                state = GAME_DEAD
                explosion = Explosion(hero.position)
                saveLocalData("highscore", highscore)

                if score >= highscore then
                    didGetHighScore = true
                end
            end
        end
    elseif state == GAME_DEAD then
        if explosion then
            explosion:draw()
            if explosion:isDone() then
                explosion = nil
            end
        end

        fontSize(60)
        textMode(CENTER)
        text("GAME OVER",WIDTH/2,HEIGHT/2)

        if didGetHighScore == true then
            fontSize(40)
            text("NEW HIGH SCORE!",WIDTH/2,HEIGHT/2 - 80)
            state=menu
        end

    end

    -- Draw scores
    if difficulty == 3 then
    score = enemies.killCount * 100
    fill(255)
    font("Futura-CondensedExtraBold")
    fontSize(30)
    textMode(CORNER)
    text("Score: "..score, 10, HEIGHT - 50)

     end
    if difficulty == 2 then
    score = enemies.killCount * 50
    fill(255)
    font("Futura-CondensedExtraBold")
    fontSize(30)
    textMode(CORNER)
    text("Score: "..score, 10, HEIGHT - 50)

     end
    if difficulty == 1 then
    score = enemies.killCount * 25
    fill(255)
    font("Futura-CondensedExtraBold")
    fontSize(30)
    textMode(CORNER)
    text("Score: "..score, 10, HEIGHT - 50)

     end

    -- update high score
    if score > highscore then
        highscore = score
    end

    hs = "High Score: "..highscore
    hswidth = textSize(hs)
    text(hs, WIDTH - hswidth - 10, HEIGHT - 50)

end
function drawbutton() 
    button.pos= vec2(400,HEIGHT/2)
    button:draw()
    end
    function buttonpressed()
        if buttonpressed()then state=GAME_PLAYING
            end
            function touched(touch)
                button:touched(touch)
            end
            end

First part

Comments

  • edited February 2013 Posts: 139
    ----------
    --button--
    ----------
    Button = class()
    
    function Button:init(x)
        -- you can accept and set parameters here
        self.displayname=start
        self.pos=vec2(0,0)    
        self.size=vec2(0,0)
        self.action=nil
        self.color=color(113,66,190,255)
    end
    
    function Button:draw()
        -- Codea does not automatically call this method
        fill(self.color)
        font("ArialRoundedMTBold")
        fontSize(22)
        local w,h=textSize(self.displayname)
        w = w + 20
        h = h + 30
        Roundrect(self.pos.x-w/2,
        self.pos.y-h/2,w,h,30)
        self.size=vec2(w,h)
        textMode(CENTER)
        fill(54,65,96,255)
        text(self.displayname,self.pos.x+2,self.pos.y-2)
        fill(255,255,255,255)
        text(self.displayname,self.pos.x,self.pos.y)
        popStyle()
    end
    function Button:hit(p)
        local l =self.pos.x-self.size.x/2
        local r =self.pos.x+self.size.x/2
        local t =self.pos.y-self.size.y/2
        local b =self.pos.y+self.size.y/2
        if p.x>l and 
           p.x<r and
           p.y>b and
           p.y<t then return true
        end
    return false
    end
    function Button:touched(touch)
        -- Codea does not automatically call this method
        if touch.state==ENDED and self:hit(vec2(touch.x,touch.y)) then if self.action then self.action() 
        end 
        end
    end
    

    Second part

  • edited February 2013 Posts: 139
    ---------------
    ---roundrect---
    ---------------    
    
    
    
        function roundRect(x,y,w,h,r)
        pushStyle()
    
        insetPos = vec2(x+r,y+r)
        insetSize = vec2(w-2*r,h-2*r)
    
        --Copy fill into stroke
        local red,green,blue,a = fill()
        stroke(red,green,blue,a)
    
        noSmooth()
        rectMode(CORNER)
        rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
    
        if r > 0 then
            smooth()
            lineCapMode(ROUND)
            strokeWidth(r*2)
    
            line(insetPos.x, insetPos.y, 
                 insetPos.x + insetSize.x, insetPos.y)
            line(insetPos.x, insetPos.y,
                 insetPos.x, insetPos.y + insetSize.y)
            line(insetPos.x, insetPos.y + insetSize.y,
                 insetPos.x + insetSize.x, insetPos.y + insetSize.y)
            line(insetPos.x + insetSize.x, insetPos.y,
                 insetPos.x + insetSize.x, insetPos.y + insetSize.y)            
        end
        popStyle()
    end
    

    Need help by finding what is Wrong with these 3 codes the main is the most important but the other 2 are codes I suspect to be messing it up

  • dave1707dave1707 Mod
    edited February 2013 Posts: 9,615

    I tried to run this code, but it's missing the functions Invader(), EnemyHoarde(), and SreeamingLines(). I found one error though, you have roundRect and Roundrect. Either make both of them Roundrect or roundRect.

    edit: Another error I found was the variable start. It's used in 2 places but I don't see where it's defined.

  • Posts: 139

    Oh all functions streaming enemy and evader all my other code

Sign In or Register to comment.