Discord
Login
Community
DARK THEME

My game doesn't go

Good morning I can't understand why my game doesn't go, this is the full code: init = function() player = object x = 0 y = 0 speed = 1 end end initCamera = function() camera = object x = 0 y = 0 end end initCoin = function() coin1 = object end randomizeCoinPos = function() coin1.x = randRange(-200, 200) coin1.y = randRange(-100, 100) end score = 0 end

update = function() 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 end updateCamera = function() camera.x = clamp(player.x, -200, 200) camera.y = clamp(player.y, -650, 650) end update = function() if distance(player.x, player.y, coin1.x, coin1.y) < 25 then score += 1 randomizeCoinPos = function() coin1.x = randRange(-200, 200) coin1.y = randRange(-100, 100) end end

draw = function() screen.clear("rgb(57,0,587)") screen.drawMap("polonuovo", 0-camera.x,0-camera.y,1000,1500) screen.drawSprite("alessandro", player.x-camera.x, player.y-camera.y, 30, 30) screen.drawSprite("coin", coin1.x, coin1.y, 20, 20) screen.drawText("Coins: " + score, 120, 80, 16, "rgb(170,57,94)") end

checkCollision = function(x, y, map_name, map_draw_width, map_draw_height) local grid_x = floor((x + map_draw_width / 2) / (map_draw_width / maps[map_name].width)) local grid_y = floor((y + map_draw_height / 2) / (map_draw_height / maps[map_name].height)) return maps[map_name].get(grid_x, grid_y) 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

clamp = function(value, lower_limit, upper_limit) local val = max(value, lower_limit) val = min(val, upper_limit) return val end

randRange = function(low, high) return (high - low) * random.next() + low end

Assuming pasted code is the one being worked on, then there are several issues here.

For example:

initCoin = function() 
  coin1 = object 
  end 
  randomizeCoinPos = function() 
    coin1.x = randRange(-200, 200) 
    coin1.y = randRange(-100, 100) 
  end 
  score = 0 
end

This is probably what you wanted:

initCoin = function() 
  coin1 = object 
    randomizeCoinPos = function() 
      coin1.x = randRange(-200, 200) 
      coin1.y = randRange(-100, 100) 
    end 
    score = 0 
  end 
end

Also, even though there are a lot of initXXX() functions, none of them are called in the actual init().

And in the update() the other updateXXX() are also missing.

And there are 2 update() functions defined - the last one in the file will be the one running.

And...

Weeelll... I've fixed the issues and you can see it running in the (private link) here ->

https://microstudio.io/Martellus/llfchelpdesk/5T9S5EM5/

See if you can see the various issues and fix them in your version :D

Good Luck!

PS: To show code use three BACKWARDS ticks like this: ``` my code is here ``` and it should show up clean.

More info on MarkDown, which is what this forum uses, here ->

https://www.markdownguide.org/basic-syntax/

Full "corrected" code here:

init = function() 
  /// added to help with the map and canvas 
  mymap = maps["polonuovo"]
  myMapWidth = mymap.block_width*mymap.width
  myMapHeight = mymap.block_height*mymap.height
  player = object 
    x = 0 
    y = 0 
    speed = 2 /// made it a bit faster
  end 
  initCamera()
  initCoin()
end 
initCamera = function() 
  camera = object 
    x = 0 
    y = 0 
  end 
end 
initCoin = function() 
  coin1 = object 
    score = 0 
    randomizeCoinPos = function() 
      coin1.x = randRange(myMapWidth/2*-1, myMapWidth/2 ) 
      coin1.y = randRange(myMapHeight/2*-1, myMapHeight) 
      coin1.x = clamp(coin1.x, myMapWidth/2*-1, myMapWidth/2 )
      coin1.y = clamp(coin1.y, myMapHeight/2*-1, myMapHeight/2 ) 
    end 
  end 
end
update = function() 
  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 
  player.x = clamp(player.x, myMapWidth/2*-1, myMapWidth/2 )
  player.y = clamp(player.y, myMapHeight/2*-1, myMapHeight/2 ) 
  if distance(player.x, player.y, coin1.x, coin1.y) < 25 then 
    coin1.score += 1 
    coin1.randomizeCoinPos() 
  end 
  updateCamera()  
end 
updateCamera = function() 
  camera.x = clamp(player.x, myMapWidth/2*-1, myMapWidth/2 ) 
  camera.y = clamp(player.y, myMapHeight/2*-1, myMapHeight) 
end 
draw = function() 
  screen.clear("rgb(57,0,587)") 
  screen.drawMap("polonuovo", 0-camera.x,0-camera.y,myMapWidth,myMapHeight) 
  screen.drawSprite("alessandro", player.x-camera.x, player.y-camera.y, 30, 30) 
  screen.drawSprite("coin", coin1.x-camera.x, coin1.y-camera.y, 20, 20) 
  screen.drawText("Coins: " + coin1.score, 120, 80, 16, "rgb(170,57,94)") 
end
checkCollision = function(x, y, map_name, map_draw_width, map_draw_height) 
  local grid_x = floor((x + map_draw_width / 2) / (map_draw_width / maps[map_name].width))
  local grid_y = floor((y + map_draw_height / 2) / (map_draw_height / maps[map_name].height)) 
  return maps[map_name].get(grid_x, grid_y) 
end
distance = function(x1, y1, x2, y2) 
  return sqrt(pow(x2 - x1 , 2) + pow(y2 - y1 , 2)) 
end
clamp = function(value, lower_limit, upper_limit) 
  return min(max(value, lower_limit) , upper_limit)
end
randRange = function(low, high) 
  return (high - low) * random.next() + low 
end

And we are done :D

This is really hard to read and makes it hard to help.

Did you read my response to your Camera question? Put triple punctuation ``` marks at the begin and end of your source code to format it. On a standard keyboard it is at the top left of your keyboard (left of the '1').

As said, it is hard to read this way and I don't have the time to copy/paste it into a new project to clean it up.
One other option to ask for help would be that you 'Publish' it and post the link here. BUT please tick the [Unlisted] box while the project is still in development.

First impression, you did the same mistake you made in your first request.
You defined some functions, like initCamera for example, but you never actually call the function.
If you only define it, the initCamera = function() ... end part, it is ready to go, but you need to call it with initCamera() to actually activate it.
Go back to my other response, have a look and let me know if it makes sense.

I also noticed that you defined the update function twice.
I think if you do that the second definition will overwrite the first one, but I can't check now because I am on my phone.
Also, the second update definition is not closed with an end.

My 'real' work starts now, maybe read up on functions and how they are used =)

P.S. SNAP I see Martellus took the time to clean it up while I was typing my blur, HAHA

YEAH, good on you Martellus =)

It is Sunday. I am stuck on ideas for Wobbles. This sort of thing helps me forget my own worries :D

Thanks for the help, i'm stupid i have answer to you on gmail therefore you don't have see the answer, anyway tanks for the help Martellus and ThinkerSmith :)

Post a reply

Progress

Status

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