Discord
Login
Community
DARK THEME

Bullet spawning and moving -code question

This is my code so far, and I have a bit of trouble spawning a bullet and getting the bullet to move. I tried making a class but there is not a tutorial on using a class so I went back to this simple code.

When a player press the mouse (touching) a bullet spawns and then the bullet moves up (Y direction +1). I get the bullet to spawn but it dosnt seem to move

[p]

init = function() enemyvalue = 200 bulletX = 0 bulletY = -32 end

update = function() Enemy() Player()

bulletY = bulletY + 1 end

draw = function()

//bullet stuff Bullet = function() screen.drawSprite("bullet",bulletX,bulletY,16) end

//Player stuff Player = function(s) PlayerX = 0 PlayerY = -64 Playerlocation = screen.drawSprite("plane",PlayerX,PlayerY,32) //logic if touch.touching then Bullet() end end

//enemy stuff Enemy = function() spawnX = 0 spawnY = 64 screen.drawSprite("ufo", spawnX, spawnY, 32) screen.drawText(enemyvalue, spawnX, spawnY,16, "#FFF") end

end

end

[/p]

I would suggest checking out these tutorials created by mrLman:

https://microstudio.dev/community/resources/game-programming-tutorials-by-mrlman/52/

What mrpiay says :) And maybe also the standard microStudio tutorials so that you get a better feeling for how everything works.

But good attempt, you nearly got it right :)

init
Yes, here one defines initial values and other things that need to be defined before the game starts

update
Here game values get updated as needed. You got that right by calling the Enemy() and Player() function here. Also you update the bullet position. Problem, it would never stop updating it even when the bullet leaves the screen. You have an infinity bullet =)

draw
And nearly right, here you draw everything to the screen. Only problem I see is that it looks like that you define the Enemy and Player functions inside the draw loop.
So those functions would be reinitialized every time the draw function runs. Functions like that should be defined outside the draw loop, you can add them to the bottom of the source code after the draw...end.
Or you put them into a separate tab, just check other games.

I hope I didn't write too much, I thought you might like to hear some thoughts to your code.

Here an example just for the bullet, I left the rest out. Just to give you an idea.

init = function() 
  bulletON   = false    // bullet not active
end

update = function()
  // test if the player touches the screen, only when the bullet is inactive
  if touch.press and not bulletON then
    bulletX = touch.x   // bullet start in x, replace with what you need
    bulletY = -40       // bullet start in y
    bulletON = true     // bullet is active
  end
  
  if bulletON then      // move the bullet only when it is active
    bulletY +=1         // add 1 in y
    if bulletY>100 then // if bullet leaves the screen let it die
      bulletON = false  // extend this part with a collision check for the ufo
    end
  end
end

draw = function()
  screen.clear()
  if bulletON then      // only draw the bullet if it is active
    screen.drawSprite("bullet",bulletX,bulletY,16)
  end  
end

You notice that I added an extra parameter that keeps track if the bullet is active or not bulletON
I hope that gives you an idea how things could be approached.

Live Long and Tinker

P.S.

And for comparison the same with function calls, to clarify what I said before:

init = function() 
  bulletON   = false    // bullet not active
end

update = function()
  checkBullet()
end

draw = function()
  screen.clear()
  drawBullet()
end

// additional functions list

checkBullet = function()
  // test if the player touches the screen, only when the bullet is inactive
  if touch.press and not bulletON then
    bulletX = touch.x   // bullet start in x, replace with what you need
    bulletY = -40       // bullet start in y
    bulletON = true     // bullet is active
  end
  
  if bulletON then      // move the bullet only when it is active
    bulletY +=1         // add 1 in y
    if bulletY>100 then // if bullet leaves the screen let it die
      bulletON = false  // extend this part with a collision check for the ufo
    end
  end  
end

drawBullet = function()
  if bulletON then      // only draw the bullet if it is active
    screen.drawSprite("bullet",bulletX,bulletY,16)
  end   
end

Now you can add all the extra functions for your plane and UFO. Like movePlayer{}, drawPlayer{}, moveUFO(), etc.

Cheers =)

Tanks. The example code and explanation really helps a lot. The infinity bullet will be destroyed once hit hits the enemy :)

I also didn't realize I had to clear the screen before drawing again

Post a reply

Progress

Status

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