Discord
Login
Community
DARK THEME

Trying to draw sprites of stars scattered around the world, but camera keeps them locked in place

Hello I am making an open world game where you have to collect stars, I saw the tutorials on scrolling and camera and I am using that is there a way to make it so I can place stars down around the map randomly ? the current way I have them set up is that the sprite when drawn is locked to the camera so moves when camera moves and such https://microstudio.io/jherna56/bearystars/SBR7BR8Y/ link to game to see what i mean

It is hard to make any suggestions without knowing what your code looks like so far.

You could publish it and tick the 'unlisted' option. Then you can share the link so that we can see the code and it won't show up in the Explore section.

In principle you could use the random functions to draw Stars on top of your map. You would need to memorize their positions though, lists might be a good option. But there are many ways to do it, always depends on the situation.

https://microstudio.io/i/jherna56/bearystars/ here is the link. why would i need to memorize their positions though if its supposed to be random placements?

Now you need a way to collect them. And once collected you want them to disappear from the screen. So you need to keep track of them somehow. I'm on the phone right now but will have a look later when I'm home.

Maybe you're lucky and one of the others here has a look in the meantime :)

First impression as far as I can see from my phone, try this:

drawStar = function()
  screen.drawSprite("bluestar",star.x-camera.x,star.y-camera.y,20)
end

That negates the camera movement for the stars, so that they should stay with the map.
Can't test on my phone, so don't kill me if it wasn't that, LOL.

P.S. and maybe go to Settings and change the mode to 'portrait' and the screen ratio to 'force 16:9' or whatever suits. I noticed on my phone that I couldn't see the remaining Stars Text, only the first letter.

Yea that was it! LOL thanks I thought about this but didn't think it'd work so I didn't try it !! Im locking my game back up now!

@TinkerSmith is absolutely right. Indeed, it is necessary to take away the camera position not only for the player but also for the stars. I understand you are using the lesson from @mrLman to implement the camera? Unfortunately, I can't see your code now, it's already closed :-)

You also want to place stars randomly with the ability to collect them and use a counter, am I right?

As @TinkerSmith said, this can be done in many ways. I have sketched an example of how this can be implemented.

I'll send the whole code here. I will also open access to the project so that you can see it in action. I'll probably explain how it works a little later. :-) In the meantime, you can disassemble this code. If you have any questions, then ask them here, I will definitely answer as soon as I'm not busy :-)

    init = function()
      initCamera()
      level = object
        width = 1024
        height = 512
      end
      player = object
        name ="player"
        x = 0
        y = 0
        width = 16
        height = 16
        speed = 2
        totalStars = 0
      end
      Stars = class
        name = "star"
        display = true
        width = 16
        height = 16
        drawStars = function(list)
          for star in list
            screen.setAlpha(star.display)
            screen.drawSprite(star.name,star.x-camera.x,star.y-camera.y,star.width,star.height)
            screen.setAlpha(1)
          end
        end
      end
      ListRandomPositionX = []
      for x = -level.width/2+Stars.width/2 to level.width/2-Stars.width/2 by Stars.width
        ListRandomPositionX.push(x)
      end
      ListRandomPositionY = []
      for y = -level.height/2+Stars.height/2 to level.height/2-Stars.height/2 by Stars.height
        ListRandomPositionY.push(y)
      end
      starsList = []
      for i = 0 to 49 //set the number of stars as desired (now there are 50)
        star = new Stars
        star.x = ListRandomPositionX[random.nextInt(ListRandomPositionX.length+1)]
        star.y = ListRandomPositionY[random.nextInt(ListRandomPositionY.length+1)]
        starsList.push(star)
      end
    end
    
    update = function()
      updateCamera()
      if keyboard.LEFT then player.x -= player.speed end
      if keyboard.RIGHT then player.x += player.speed end
      if keyboard.UP then player.y += player.speed end
      if keyboard.DOWN then player.y -= player.speed end
      player.x = clamp(player.x,-level.width/2+8,level.width/2-8)
      player.y = clamp(player.y,-level.height/2+8,level.height/2-8)
      for star in starsList
        if distance(player.x,player.y,star.x,star.y) < 16 then
          if star.display == 1 then
            player.totalStars += 1
          end
          star.display = false
        end
      end
    end
    
    draw = function()
      screen.clear("rgb(170,227,255)")
      screen.drawMap("map1",-camera.x,-camera.y,level.width,level.height)
      Stars.drawStars(starsList)
      screen.drawSprite(player.name,player.x-camera.x,player.y-camera.y,player.width,player.height)
      screen.drawText("stars: "+player.totalStars+"/"+starsList.length,80,80,20,"rgb(255,255,255)")
    end
    
    //function
    clamp = function(value, lower_limit, upper_limit)
      local val = max(value, lower_limit)
      val = min(val, upper_limit)
      return val
    end
    
    distance = function(x1, y1, x2, y2)
      local a = x2 - x1
      local b = y2 - y1
      local c = sqrt(pow(a, 2) + pow(b, 2))
      return c
    end
    
    initCamera = function()
      camera = object
        x = 0
        y = 0
      end
    end
    
    updateCamera = function()
      camera.x = player.x
      camera.y = player.y
      camera.x = clamp(camera.x,-334,334)
      camera.y = clamp(camera.y,-156,156)
    end

Here is the link to the project: https://microstudio.io/i/Constantine_RetroGamer/test2/

And extra Stars for @Constantine_RetroGamer for this nice detailed example that without doubt will be of use to many :)

Live Long and Tinker

:-)

Post a reply

Progress

Status

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