Howdy, Stranger!

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

Need help math.randomseed

edited August 2012 in General Posts: 5

Hello I am new to codea as well as coding I am trying to make a game similar to bit invader only horizontal. So far I have created a spaceship sprite which is movable as well as a background with some stars.

-- Use this function to perform your initial setup
function setup()
    x = WIDTH/2
    y = HEIGHT/2
    c = color(0, 0, 0, 255)
    end

-- This function gets called once every frame
function draw()
    background(0, 0, 0, 225)

  sprite("SpaceCute:Star",374,374)
sprite("SpaceCute:Star",100,150)
sprite("SpaceCute:Star",400,550)
sprite("SpaceCute:Star",600,200)
sprite("SpaceCute:Star",150,650)
sprite("SpaceCute:Star",650,700)
    -- Do your drawing here
   sprite("SpaceCute:Rocketship",150,y)
end

function touched(touch)
x = x
y = touch.y
end

I am looking to have objects come on to the screen horizontally and have you control the spaceship to avoid them. However I am a stuck at how to get the objects to come onto the screen infinitely and how to get them to come on at random points in the right hand side. I believe you use something called math.random seed then enter coordinates. For example, (math.randomseed1,100) and then it will randomly chose between the 1 and 100 coordinates to come on. Deos anyone know how to apply this to my game. Please keep it as simple as possible and any help is greatly appreciated.

Comments

  • dave1707dave1707 Mod
    edited August 2012 Posts: 8,464

    @Frazamataza

    I added some code to your program to just give you an idea of what you can do. You'll have to use a table to have multiple objects going at once. If you have questions, just ask.

    function setup() 
        displayMode(FULLSCREEN)  
          
        x = WIDTH/2    
        y = HEIGHT/2     
        c = color(0, 0, 0, 255) 
    
        x1=0  
        y1=0  
    end
    
    function draw()    
        background(0, 0, 0, 225)
        
        x1=x1-5
        if x1<1 then
            rand()
        end
    
        sprite("SpaceCute:Star",x1,y1)    -- change x1 and y1 to use a table
        
        sprite("SpaceCute:Star",100,150) 
        sprite("SpaceCute:Star",400,550)
        sprite("SpaceCute:Star",600,200) 
        sprite("SpaceCute:Star",150,650) 
        sprite("SpaceCute:Star",650,700)     
    
        sprite("SpaceCute:Rocketship",150,y) 
    end
    
    function touched(touch) 
        x = x y = touch.y 
    end
    
    function rand()
        y1=math.random(50,HEIGHT-50)
        x1=WIDTH
    end
    
    
  • Posts: 115

    Good stuff guys. Thanks for sharing.

  • thanks dave, very helpful, just got to figure out collision then I am done.

Sign In or Register to comment.