Howdy, Stranger!

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

In this Discussion

Help with speed/direction of sprite

I created this little game in May 2014. I remember copying off another discussion for the velocity part (dx and dy). The current code does everything I want it to do, except changing the direction of the UFO more (so that it's not just back and forth). Should I use the built-in Physics engine (which I have very little experience with?)

function setup()
    displayMode(FULLSCREEN)
    score= 0
    hit= 0
    x=WIDTH/2 --starting x
    y=HEIGHT/2 --starting y
    d= .5 * (WIDTH/10.5 + HEIGHT/8) --diameter of ball
    dx=  0
    dy= 0
    k= 0
    alrighty= 0
    rcol= 0
    bcol= 0
    gcol= 0 
    alphacol= 0
    highscore= readLocalData("highscore")
    if not highscore then
        highscore= 0
    end
end

function draw()
    pushStyle()
    font("GillSans-Bold")
    fill(255)
    if score == 0 then
        alrighty = ElapsedTime 
        background(0)
    elseif score == 1 then
        alrighty = ElapsedTime
    elseif score > 0 then
        background(rcol,gcol,bcol,alphacol)
    end
    sprite("Space Art:UFO",x,y,d)
    textAlign(CENTER)
    text("Your high score is " .. highscore .. ".\nScore: " .. score .. "\nTap the screen when the UFO is about to hit a wall.\n\nYou have survived for " .. string.format("%.f",(ElapsedTime - alrighty)) .. " seconds.", WIDTH/2,HEIGHT/1.38)
    fontSize(55)
    if x>WIDTH-d/2 or x<d/2 or y>HEIGHT-d/2 or y<d/2 then
        y=HEIGHT/2 x=WIDTH/2
        saveLocalData("highscore", highscore)
        print("Your high score is " .. highscore .. ". You just got a score of " .. score ..  ", in " .. string.format("%.f",(ElapsedTime - alrighty)) .. " seconds!")
        score=0
        dx=0
        dy=0
        k=0
    else
        x=x+dx
        y=y+dy
    end
    if score > highscore then
        highscore = score
    end
    popStyle()
end

function touched(touch)
    if touch.state == BEGAN then
        k=k + 1
        if k == 1 then
            dx=4
            dy=4
            score=score + 1
        end
    rcol=math.random(210)
    bcol=math.random(210)
    gcol=math.random(190)
    alphacol=math.random(10,70)
    if dx > 0 then
            dx= math.random(-10,-5) --the amount x changes (was -10,-5 before oct 23 2015) for this line and next
            dy= math.random(-10,-5) --the amount y changes
            score= score + 1
    elseif dx < 0 then
            dx=math.random(8,14) --changes direction and speed to a random number between 5 and 10 if it is a negative value.
            dy=math.random(8,14) --previous vals: 5, 10
            text(dx.. " " .. dy,WIDTH/2,300)
            fill(255, 255, 255, 255)
            score=score + 1
    end
    if y > HEIGHT - 101 or y < 101 then
        score= score + 9
    elseif x > WIDTH - 101 or x < 101 then
        score= score + 9
    end
    end
end

Comments

  • dave1707dave1707 Mod
    Posts: 8,618

    @McCann Try working with this touched routine. You might have to change it for what you want to do.

    function touched(touch)
        if touch.state==BEGAN then
            rcol=math.random(210)
            bcol=math.random(210)
            gcol=math.random(190)
            alphacol=math.random(10,70)
            if dx>0 then
                dx=math.random(-10,-3)
            else
                dx=math.random(3,10)
            end
            if dy>0 then
                dy=math.random(-10,-3)
            else
                dy=math.random(3,10)
            end
        end
        if y > HEIGHT - 101 or y < 101 then
            score= score + 9
        elseif x > WIDTH - 101 or x < 101 then
            score= score + 9
        end
    end
    
Sign In or Register to comment.