This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TNT_Nolan's profile picture

How to track when you run in to food

Started by TNT_Nolan, 02 June 2016 - 04:14 PM
TNT_Nolan #1
Posted 02 June 2016 - 06:14 PM
How can I track when I run in to a piece of food or a can of soda?


// Made by Nolan Bukhart

init()

function init()
    day = 1
    food = 100


    maxX = 58
    maxY = 18
    minX = 2
    minY = 2

    blackDisplay(day,food)
    createGameBoard(maxX,maxY,minX,minY)
end




function createGameBoard(maxX,maxY,minX,minY)

    //Images

    playerimg = paintutils.loadImage("../Imgs/player")
    enemy1img = paintutils.loadImage("../Imgs/enemy1")
    enemy2img = paintutils.loadImage("../Imgs/enemy2")
    foodimg = paintutils.loadImage("../Imgs/food")
    sodaimg = paintutils.loadImage("../Imgs/soda")

    //Setup the Colors
    term.setBackgroundColor(colors.lime)
    term.clear()
    paintutils.drawBox(1,1,59,19,colors.gray)

    numberOfSoda = Random.Range(1,3)
    numberOfFood = Random.range(1,3)

    sodaPlaced = 0
    foodPlaced = 0

    //Spawn Soda

    while numberOfSoda => cansPlaced do
        X = Random.Range(minX,maxX)
        Y = Random.Range(minY,maxY)
        paintutils.drawImage(X,Y,sodaimg)
        cansPlaced = cansPlaced + 1
    end

    //Spawn Food

    while numberOfFood => foodPlaced do
        X = Random.Range(minX,maxX)
        Y = Random.Range(minY,maxY)
        paintutils.drawImage(X,Y,foodimg)
        foodPlaced = foodPlaced + 1
    end

    //Spawn the Player

    paintutils.drawImage(4,4,playerimg)


end

function blackDisplay(day, food)
    term.setBackgroundColor(colors.black)
    term.clear()
    term.setCursorPos(24,9)
    term.setTextColor(colors.white)
    print("Day "..day)
    term.setCursorPos(20,10)
    print("Food left, "..food)
end
Sewbacca #2
Posted 02 June 2016 - 06:22 PM
Erm… I think, you have to implement that the player can walk first of all, then you can check the position of the player and the food/soda. If the positions are equal, then the player walked into the food/soda.
Edited on 02 June 2016 - 04:22 PM
TNT_Nolan #3
Posted 02 June 2016 - 06:31 PM
Do you have any idea of how I could do that?
Sewbacca #4
Posted 02 June 2016 - 06:58 PM
You need something like that:


local player = {moving = false, direction = 0, x = 0, y = 0} -- You have to set x, y to the start position

-- Move have to draw the player one field left or right and set the position
local function move()
  if player.moving then
	if player.direction == 1 then
	  <CODE> -- Moves the player left
	elseif player.direction == 2 then
	  <CODE> -- Moves the player right
	end
  end
end

local function lookup()
  while true do
	local event, key = os.pullEvent() -- Get the event
	if event == 'key' then -- If a key is pressed then work out
	  if key == keys.left or key == keys.d then -- Moves the player left
		player.direction = 1 -- The direction, the player is going
		player.moving = true
	  elseif key == keys.right or key == keys.a then -- Moves the player right
		player.direction = 2 -- The direction, the player is going
		player.moving = true
	  elseif key == keys.space then -- Let the player jump
		jump()
	  end
	elseif event == 'key_up' then -- elseif a key was released, then stop the workout
	  if key == keys.left or key == keys.d then -- Stop moving the player left
		player.direction = 0
		player.moving = false
	  elseif key == keys.right or key == keys.a then -- Stop moving the player right
		player.direction = 0
		player.moving = false
	  end
	end
  end
end

local function Update()
  while true do
	move()
  end
end

parallel.waitForAll(lookup, Update)
Edited on 02 June 2016 - 05:06 PM