Discord
Login
Community
DARK THEME

HELP! Collectable Coins!

I am on a Project and I need to make collectable coins for the player. He needs to collect a few of them before the game goes on. WHAT TO DO? can someone show me an example?

Okay, you first need to create an object class or even an object for your parts, but I recommend using classes because you can define the behavior of multiple objects with them.

So before starting anything, you need to know what init, update, and draw are for.

engine function
init The init function is a function that runs only once in your game; it is useful for unsetting variables.
update The update function is the most important function of Micro Studio; it executes 60 times per second. This is where motion detection, collision detection, and other functions should be located.
draw He draws the sprite shapes and we can also write the words

Now that we've covered the main components of a project, we can create variables and other things. We'll create an object variable for the room and another object for the player, like these in the init


init = function()
  coin = object //coin here is the silver coin
    x = 100
    y = 50
    width = 20
    height = 20
    give = 1 //give and a number that the piece will gild if it is touched
  end
  
  player = object //and the player object, our player
    x = 60
    y = 0
    width = 50
    height = 50
    score = 0
    speed = 3
    vision = object
      x = 0
      y = 0
    end
  end
//x and y are the position, and width and height are the size; you can simply define them as numbers or variables.

Now that we have our two objects, here's the collision function. I'm not going to explain AABB collisions in detail because it's a bit complex, but know that it's a function you can use here it is

checkCollision = function(object1, object2)
  return abs(object1.x - object2.x) < (object1.width + object2.width)/2 and
  abs(object1.y - object2.y) < (object1.height + object2.height)/2
end

You probably see that the variables width and height are used; I recommend defining your numbers with variables so they can be used easily.

  if checkCollision(player, coin) then
    player.score += coin.give
    coin.x = random.next() * 150 - 70
    coin.y = random.next() * 150 - 70
  end

Let's get back to our sheep. Here's the function that works until we calculate the collision with the player: checkCollision(player, coin). Why? Because the function is located right at the top, so to use the ready-made function, we'll just say if checkCollision(plus the objects we want to detect the collision) Here we tell the computer if object 1 and object 2 touch there is a collision, add 1 to the player's score and change the position of the piece with random.next()....

 if keyboard.UP    then player.y += player.speed end
  if keyboard.DOWN  then player.y -= player.speed end
  if keyboard.RIGHT then player.x += player.speed end
  if keyboard.LEFT  then player.x -= player.speed end

We don't have the classic player movements, or we just tell the computer if the keyboard is pressed up (which is UP), then the player's x-axis plus their speed (which will make them move up) and so on immediately

  screen.clear()

  screen.drawSprite("icon" player.x, player.y, player.width, player.height)
  screen.fillRect(coin.x, coin.y, coin.width, coin.height, "yellow")

  screen.drawText("Score: " + player.score, 0, 80, 20, "white")
end

And finally, we draw the entities with e draw and with the object coordinates. Without this tool, it would be done on the computer and we wouldn't see anything. Always replace the x and x, width and height with the x, y, width and height of the object you want to draw.

I hope this explanation helps you. In case you need it, here's a simple example I made for you.

checkCollision = function(object1, object2)
  return abs(object1.x - object2.x) < (object1.width + object2.width)/2 and
  abs(object1.y - object2.y) < (object1.height + object2.height)/2
end

init = function()
  coin = object
    x = 100
    y = 50
    width = 20
    height = 20
    give = 1
  end
  
  player = object
    x = 60
    y = 0
    width = 50
    height = 50
    score = 0
    speed = 3
    vision = object
      x = 0
      y = 0
    end
  end
  
  list_coin = []
  list_coin.push(coin)
end

update = function()
  for c in list_coin 
    if checkCollision(player,c) then
      player.score += c.give
      c.x = random.next()* 150 - 70
      c.y = random.next()* 150 - 70
    end
  end
  
  if keyboard.UP then player.y += player.speed player.vision.y = 5 
  elsif keyboard.DOWN then player.y -= player.speed  player.vision.y = -5
  elsif keyboard.RIGHT then player.x += player.speed  player.vision.x = 5
  elsif keyboard.LEFT then player.x -= player.speed player.vision.x = -5
  else
    player.vision.x = 0
    player.vision.y = 0
  end
end

draw = function()
  screen.clear("rgb(0,170,255)")
  screen.fillRect(player.x,player.y,player.width,player.height,"rgb(0,255,85)")
  screen.drawRect(player.x,player.y,player.width,player.height,"rgb(0,105,35)")
  screen.fillRect(player.x,player.y,35,35,"white")
  screen.fillRect(player.x + player.vision.x ,player.y + player.vision.y ,20,20,"rgb(0,105,35)")
  screen.fillRect(player.x + player.vision.x ,player.y + player.vision.y - 5 ,20,10,"rgb(0,70,23)")
  screen.fillRect(player.x + player.vision.x ,player.y + player.vision.y ,10,15,"black")
  screen.fillRect(player.x + player.vision.x + 6 ,player.y + player.vision.y + 5,5,5,"white")
  for c in list_coin 
    screen.fillRect(c.x,c.y,c.width,c.height,"rgb(255,170,0)")
    screen.drawRect(c.x,c.y,c.width,c.height,"rgb(105,70,0)")
    screen.fillRect(c.x,c.y,5,10,"rgb(197,131,0)")
  end
  screen.drawText(player.score,player.x,player.y+35,15,"rgb(0,98,147)")
end

Good luck with your game ^o^

Post a reply

Progress

Status

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