Discord
Login
Community
DARK THEME

how to make things fall off screen over and over

I am making a game where the player has to jump from line to line (there are 3 lines) to dodge falling objects, I am new to coding and especially new to the microstudio code. I tried making a loop to make the objects(or bricks) fall down and repeat but I cant figure it out

here is my code

init = function()
  blockyX = [-100,0,100]
  blockyI = 1
  brick = [-100,0,100]
  passed = [0,0,0]
end

movement = function()
  if (keyboard.press.RIGHT and blockyI < blockyX.length -1) then
    blockyI += 1 
  elsif (keyboard.press.LEFT and blockyI > 0) then
    blockyI -= 1
  end
end

update = function()
  i = 400
  while i <-200
    screen.drawSprite("brick",blockyX[random],i)
    i-=1 
  end
    
  
  movement()
end

draw = function()
  screen.clear("white")
  screen.setLineWidth(5)
  screen.drawLine(-100,150,-100,-150,"red")
  screen.drawLine(0,150,0,-150,"red")
  screen.drawLine(100,150,100,-150,"red")
  screen.drawSprite("blocky",blockyX[blockyI],-75)
end

you can see the loop I tried to do but it didn't work because I don't really know what I'm doing pls help

I think you're close to what you want. You'll only need to change a few things.

First, you need to keep track of two pieces of data: which x and y position to draw "brick" at. You are using the variable i for y, but it is getting reset to 400 every frame. You should initialize it in the init function. Also initialize a variable for the x position (it will refer to an array index in your case).

Second, you should avoid using while loops because they have a tendency to run infinitely if you aren't careful, which prevents your program from moving on and doing other things. In your case, you are actually wanting to test a condition, (if i < -200, do something), so replace the while loop with an if statement. Try to keep in mind: your update function will run 60 times per second. That is a kind of "built in" loop.

Third, random is an object, not a command. You need to call one of it's methods. In this case you can use random.nextInt(3) which will produce either 0, 1, or 2 randomly.

Fourth, all of your drawing commands need to be in the draw function or they won't work. You'll need to take your drawSprite out of the update function.

init = function()
  blockyX = [-100,0,100]
  blockyI = 1
  brick = [-100,0,100]
  passed = [0,0,0]
  i = 200
  currentX = 0
end

movement = function()
  if (keyboard.press.RIGHT and blockyI < blockyX.length -1) then
    blockyI += 1 
  elsif (keyboard.press.LEFT and blockyI > 0) then
    blockyI -= 1
  end
end

update = function()
  i-=10
  if i < -200 then 
    i = 200 
    currentX = random.nextInt(3)
  end
  movement()
end

draw = function()
  screen.clear("white")
  screen.setLineWidth(5)
  screen.drawLine(-100,150,-100,-150,"red")
  screen.drawLine(0,150,0,-150,"red")
  screen.drawLine(100,150,100,-150,"red")
  screen.drawSprite("blocky",blockyX[blockyI],-75)
  screen.drawSprite("brick", blockyX[currentX], i)
end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community