When I click a pixel I want a value to get higher.
So, if the pixel is for example yellow and I have 4 different color values.. Can I in any way detect if the color of the pixel clicked is yellow?
Ok.. How do I do that?
Take a look into the Gem Miner Programm it is exactly doing what you request.
It stores the Color and the coordinates in a table to determine the score value.
You should know that cause you released the game ;D
Take a look into the Gem Miner Programm it is exactly doing what you request.
It stores the Color and the coordinates in a table to determine the score value.
You should know that cause you released the game ;D
True.. But only like 10% of that code is completly mine..
Take a look into the Gem Miner Programm it is exactly doing what you request.
It stores the Color and the coordinates in a table to determine the score value.
You should know that cause you released the game ;D
True.. But only like 10% of that code is completly mine..
And thats a good Example why its bad to be a coding Monkey instead of bringing people to the point where they can do the things on their own :)/>
I will later add an explenation about how to save position and color to a table now i go to lunch :)/>
Think of coordinates in a plane. Just use "coordinate" keys in a table. myTable[x][y]. Familiar looking, right? Now, you need to be able to set it first, right? Well, we do that in "local gem = {x, y, color}". And remember that the data for each gem is stored in one table, the "gems" table. So to get the color of a gem, we need to reference that table, and grab a "gem data table". Look to where we put "– Do stuff here, – Do more stuff here". In there is the part where we figure out which gem was clicked on. It handles the referencing and stuff. v[1], v[2], and v[3] all represent the three values of a gem: Its x-coordinate, y-coordinate, and color. So you'd use v[3] to get the color of the gem.
I know that may not have made much sense to you, sorry about that :unsure:/>. I'm about to go to bed. Worst time to try to explain things. But I hope it helped a little. Maybe when lord gets back, he can provide a much better explanation :P/>. An explanation that details saving colors in a table from scratch, instead of referencing your existing code.
Just wanted to say, there are multiple ways of implementing this. Implementing this in a more specific way instead of a general process would probably optimize and make it easier to code. Can you give us a more specifics on what you're trying to do?
local screen = {}
local event, button, x, y = os.pullEvent("mouse_click")
screen[x][y] -- We want to accomplish this, and that returns the color of that pixel
-- But if we fill it in like this:
screen[y][x]
--It will be less tables in a table. I think its just a good practice
local width, height = term.getSize() -- get the screen values
for i = 1, height do -- simple loop
screen[i] = {} -- create a table for each height
for j = 1, width do -- another loop, different variable though
screen[i][j] = colors.black
-- So we set the first index to table we just created. And the second one is
-- for the index for that table
end
end
local screen = {
[1] = { colors.black, colors.black, colors.black, --[[ etc. for the width ]] },
[2] = { --[[see index 1, its the same]] },
--etc..
local event, button, x, y = os.pullEvent("mouse_click")
local colorClicked = screen[y][x]
It returns in the form of a number, so then you can create another table wich sets all the colors. This is confusing, but this part you must take for granted, at least, the loop. If you dont know what Im doing with the table, just ask again :P/>
local color = {} -- notice this is not colors, if you use colors, you are overwriting stuff and thats bad
for k, v in pairs(colors) do -- look this loop up on the interwebs :3
color[v] = k
end
-- Now we can get the real color:
local event, button, x, y = os.pullEvent("mouse_click")
local colorClicked = color[ screen[y][x] ] -- we set the index of color to the pixel.
-- So assuming the color was black, there actuall stands:
-- color[ 32768 ], and that will return the string "black"
I would prefer to use a table of objects as opposed to a map of pixels onscreen, as it's way easier to manage and much faster to loop through.
Well, with one pixel clicked, I want it to dissapear, add to the value, and spawn a new.
I have the spawning thing done.
function playGame()
-- Variables --
slc = 1
minX = 13
maxX = w-1
minY = 2
maxY = h-1
maxGem = 20
spawnedGems = 0
newX = math.random(minX, maxX)
newY = math.random(minY, maxY)
local function frame()
term.setBackgroundColor(colors.black)
term.clear()
paintutils.drawLine(12, 1, 12, h, colors.red)
paintutils.drawLine(12, 1, w, 1, colors.red)
paintutils.drawLine(12, h, w, h, colors.red)
paintutils.drawLine(w, 1, w, h, colors.red)
paintutils.drawLine(1, 1, 1, h, colors.red)
paintutils.drawLine(1, 1, 1, h, colors.red)
paintutils.drawLine(2, 1, 2, h, colors.red)
paintutils.drawLine(3, 1, 3, h, colors.red)
paintutils.drawLine(4, 1, 4, h, colors.red)
paintutils.drawLine(5, 1, 5, h, colors.red)
paintutils.drawLine(6, 1, 6, h, colors.red)
paintutils.drawLine(7, 1, 7, h, colors.red)
paintutils.drawLine(8, 1, 8, h, colors.red)
paintutils.drawLine(9, 1, 9, h, colors.red)
paintutils.drawLine(10, 1, 10, h, colors.red)
paintutils.drawLine(11, 1, 11, h, colors.red)
term.setBackgroundColor(colors.lime)
term.setTextColor(colors.black)
term.setCursorPos(5, h-4)
print(" Exit ")
end
function spawnGem()
if spawnedGems >= maxGem then
playGame()
else
paintutils.drawPixel(newX, newY, color)
spawnedGems = spawnedGems + 1
gem()
end
end
function gem()
while true do
newX = math.random(minX, maxX)
newY = math.random(minY, maxY)
colorNum = math.random(1,4)
if colorNum == 1 then
color = colors.orange
spawnGem()
elseif colorNum == 2 then
color = colors.yellow
spawnGem()
elseif colorNum == 3 then
color = colors.lime
spawnGem()
elseif colorNum == 4 then
color = colors.lightBlue
spawnGem()
end
end
end
frame()
gem()
end
-- Variables --
debug = "Debug"
w, h = term.getSize()
-- Values --
limeName = "Lime gem: "
limeSell = 10
lblueName = "Light blue gem: "
lblueSell = 15
yellowName = "Yellow gem: "
yellowSell = 50
orangeName = "Orange gem: "
orangeSell = 100
-- Stats --
money = 0
playerName = "nil"
limeGems = 0
lblueGems = 0
yellowGems = 0
orangeGems = 0
-- End Stats --
-- End Variables --
-- Game --
function playGame()
-- Variables --
slc = 1
minX = 13
maxX = w-1
minY = 2
maxY = h-1
maxGem = 20
spawnedGems = 0
newX = math.random(minX, maxX)
newY = math.random(minY, maxY)
local function frame()
term.setBackgroundColor(colors.black)
term.clear()
paintutils.drawLine(12, 1, 12, h, colors.red)
paintutils.drawLine(12, 1, w, 1, colors.red)
paintutils.drawLine(12, h, w, h, colors.red)
paintutils.drawLine(w, 1, w, h, colors.red)
paintutils.drawLine(1, 1, 1, h, colors.red)
paintutils.drawLine(1, 1, 1, h, colors.red)
paintutils.drawLine(2, 1, 2, h, colors.red)
paintutils.drawLine(3, 1, 3, h, colors.red)
paintutils.drawLine(4, 1, 4, h, colors.red)
paintutils.drawLine(5, 1, 5, h, colors.red)
paintutils.drawLine(6, 1, 6, h, colors.red)
paintutils.drawLine(7, 1, 7, h, colors.red)
paintutils.drawLine(8, 1, 8, h, colors.red)
paintutils.drawLine(9, 1, 9, h, colors.red)
paintutils.drawLine(10, 1, 10, h, colors.red)
paintutils.drawLine(11, 1, 11, h, colors.red)
term.setBackgroundColor(colors.lime)
term.setTextColor(colors.black)
term.setCursorPos(5, h-4)
print(" Exit ")
end
function spawnGem()
if spawnedGems >= maxGem then
playGame()
else
paintutils.drawPixel(newX, newY, color)
spawnedGems = spawnedGems + 1
gem()
end
end
function gem()
while true do
newX = math.random(minX, maxX)
newY = math.random(minY, maxY)
colorNum = math.random(1,4)
if colorNum == 1 then
color = colors.orange
spawnGem()
elseif colorNum == 2 then
color = colors.yellow
spawnGem()
elseif colorNum == 3 then
color = colors.lime
spawnGem()
elseif colorNum == 4 then
color = colors.lightBlue
spawnGem()
end
end
end
frame()
gem()
end
function frame2()
slc = 0
term.setBackgroundColor(colors.white)
term.clear()
term.setCursorPos(math.floor(w-string.len("Logged in as: "..playerName))/2, 2)
print("Logged in as: "..playerName )
paintutils.drawPixel(1, 4, 32)
paintutils.drawPixel(1, 6, 8)
paintutils.drawPixel(1, 8, 16)
paintutils.drawPixel(1, 10, 2)
term.setBackgroundColor(1)
term.setCursorPos(3, 4)
print(limeName ..limeGems)
term.setCursorPos(3, 6)
print(lblueName ..lblueGems)
term.setCursorPos(3, 8)
print(yellowName ..yellowGems)
term.setCursorPos(3, 10)
print(orangeName ..orangeGems)
term.setBackgroundColor(32)
term.setCursorPos(10, 15)
print("Play")
term.setCursorPos(20, 15)
print("Sell Gems")
end
function game()
term.clear()
term.setBackgroundColor(1)
term.setTextColor(32768)
term.clear()
term.setCursorPos(math.floor(w-string.len("Name: "))/2, 2)
write("Name: ")
playerName = read()
if playerName == "" then
game()
else
frame2()
end
end
-- End Functions --
game()
-- User Interaction --
while true do
event, key, x, y = os.pullEvent()
if event == "mouse_click" then
if slc == 0 then
if x >= 10 and x <= 18 and y == 15 and key == 1 then
playGame()
elseif x >= 20 and x <= 30 and y == 15 and key == 1 then
sellGems()
elseif slc == 1 then
if x >= 5 and x <= 10 and y == h-4 and key == 1 then
slc = 0
frame2()
end
end
end
end
end
-- Variables --
debug = "Debug"
w, h = term.getSize()
-- Values --
limeName = "Lime gem: "
limeSell = 10
lblueName = "Light blue gem: "
lblueSell = 15
yellowName = "Yellow gem: "
yellowSell = 50
orangeName = "Orange gem: "
orangeSell = 100
-- Stats --
money = 0
playerName = "nil"
limeGems = 0
lblueGems = 0
yellowGems = 0
orangeGems = 0
-- End Stats --
-- End Variables --
-- Game --
function playGame()
-- Variables --
slc = 1
minX = 13
maxX = w-1
minY = 2
maxY = h-1
maxGem = 20
spawnedGems = 0
newX = math.random(minX, maxX)
newY = math.random(minY, maxY)
local function frame()
term.setBackgroundColor(colors.black)
term.clear()
paintutils.drawLine(12, 1, 12, h, colors.red)
paintutils.drawLine(12, 1, w, 1, colors.red)
paintutils.drawLine(12, h, w, h, colors.red)
paintutils.drawLine(w, 1, w, h, colors.red)
paintutils.drawLine(1, 1, 1, h, colors.red)
paintutils.drawLine(1, 1, 1, h, colors.red)
paintutils.drawLine(2, 1, 2, h, colors.red)
paintutils.drawLine(3, 1, 3, h, colors.red)
paintutils.drawLine(4, 1, 4, h, colors.red)
paintutils.drawLine(5, 1, 5, h, colors.red)
paintutils.drawLine(6, 1, 6, h, colors.red)
paintutils.drawLine(7, 1, 7, h, colors.red)
paintutils.drawLine(8, 1, 8, h, colors.red)
paintutils.drawLine(9, 1, 9, h, colors.red)
paintutils.drawLine(10, 1, 10, h, colors.red)
paintutils.drawLine(11, 1, 11, h, colors.red)
term.setBackgroundColor(colors.lime)
term.setTextColor(colors.black)
term.setCursorPos(5, h-4)
print(" Exit ")
end
function spawnGem()
if spawnedGems >= maxGem then
playGame()
else
paintutils.drawPixel(newX, newY, color)
spawnedGems = spawnedGems + 1
gem()
end
end
function gem()
while true do
newX = math.random(minX, maxX)
newY = math.random(minY, maxY)
colorNum = math.random(1,4)
if colorNum == 1 then
color = colors.orange
spawnGem()
elseif colorNum == 2 then
color = colors.yellow
spawnGem()
elseif colorNum == 3 then
color = colors.lime
spawnGem()
elseif colorNum == 4 then
color = colors.lightBlue
spawnGem()
end
end
end
frame()
gem()
end
function frame2()
slc = 0
term.setBackgroundColor(colors.white)
term.clear()
term.setCursorPos(math.floor(w-string.len("Logged in as: "..playerName))/2, 2)
print("Logged in as: "..playerName )
paintutils.drawPixel(1, 4, 32)
paintutils.drawPixel(1, 6, 8)
paintutils.drawPixel(1, 8, 16)
paintutils.drawPixel(1, 10, 2)
term.setBackgroundColor(1)
term.setCursorPos(3, 4)
print(limeName ..limeGems)
term.setCursorPos(3, 6)
print(lblueName ..lblueGems)
term.setCursorPos(3, 8)
print(yellowName ..yellowGems)
term.setCursorPos(3, 10)
print(orangeName ..orangeGems)
term.setBackgroundColor(32)
term.setCursorPos(10, 15)
print("Play")
term.setCursorPos(20, 15)
print("Sell Gems")
end
function game()
term.clear()
term.setBackgroundColor(1)
term.setTextColor(32768)
term.clear()
term.setCursorPos(math.floor(w-string.len("Name: "))/2, 2)
write("Name: ")
playerName = read()
if playerName == "" then
game()
else
frame2()
end
end
-- End Functions --
game()
-- User Interaction --
while true do
event, key, x, y = os.pullEvent()
if event == "mouse_click" then
if slc == 0 then
if x >= 10 and x <= 18 and y == 15 and key == 1 then
playGame()
elseif x >= 20 and x <= 30 and y == 15 and key == 1 then
sellGems()
elseif slc == 1 then
if x >= 5 and x <= 10 and y == h-4 and key == 1 then
slc = 0
frame2()
end
end
end
end
end
I have re-indented it for you because.. It wasnt intendented properly :P/>/>Spoiler
-- Variables -- debug = "Debug" w, h = term.getSize() -- Values -- limeName = "Lime gem: " limeSell = 10 lblueName = "Light blue gem: " lblueSell = 15 yellowName = "Yellow gem: " yellowSell = 50 orangeName = "Orange gem: " orangeSell = 100 -- Stats -- money = 0 playerName = "nil" limeGems = 0 lblueGems = 0 yellowGems = 0 orangeGems = 0 -- End Stats -- -- End Variables -- -- Game -- function playGame() -- Variables -- slc = 1 minX = 13 maxX = w-1 minY = 2 maxY = h-1 maxGem = 20 spawnedGems = 0 newX = math.random(minX, maxX) newY = math.random(minY, maxY) local function frame() term.setBackgroundColor(colors.black) term.clear() paintutils.drawLine(12, 1, 12, h, colors.red) paintutils.drawLine(12, 1, w, 1, colors.red) paintutils.drawLine(12, h, w, h, colors.red) paintutils.drawLine(w, 1, w, h, colors.red) paintutils.drawLine(1, 1, 1, h, colors.red) paintutils.drawLine(1, 1, 1, h, colors.red) paintutils.drawLine(2, 1, 2, h, colors.red) paintutils.drawLine(3, 1, 3, h, colors.red) paintutils.drawLine(4, 1, 4, h, colors.red) paintutils.drawLine(5, 1, 5, h, colors.red) paintutils.drawLine(6, 1, 6, h, colors.red) paintutils.drawLine(7, 1, 7, h, colors.red) paintutils.drawLine(8, 1, 8, h, colors.red) paintutils.drawLine(9, 1, 9, h, colors.red) paintutils.drawLine(10, 1, 10, h, colors.red) paintutils.drawLine(11, 1, 11, h, colors.red) term.setBackgroundColor(colors.lime) term.setTextColor(colors.black) term.setCursorPos(5, h-4) print(" Exit ") end function spawnGem() if spawnedGems >= maxGem then playGame() else paintutils.drawPixel(newX, newY, color) spawnedGems = spawnedGems + 1 gem() end end function gem() while true do newX = math.random(minX, maxX) newY = math.random(minY, maxY) colorNum = math.random(1,4) if colorNum == 1 then color = colors.orange spawnGem() elseif colorNum == 2 then color = colors.yellow spawnGem() elseif colorNum == 3 then color = colors.lime spawnGem() elseif colorNum == 4 then color = colors.lightBlue spawnGem() end end end frame() gem() end function frame2() slc = 0 term.setBackgroundColor(colors.white) term.clear() term.setCursorPos(math.floor(w-string.len("Logged in as: "..playerName))/2, 2) print("Logged in as: "..playerName ) paintutils.drawPixel(1, 4, 32) paintutils.drawPixel(1, 6, 8) paintutils.drawPixel(1, 8, 16) paintutils.drawPixel(1, 10, 2) term.setBackgroundColor(1) term.setCursorPos(3, 4) print(limeName ..limeGems) term.setCursorPos(3, 6) print(lblueName ..lblueGems) term.setCursorPos(3, 8) print(yellowName ..yellowGems) term.setCursorPos(3, 10) print(orangeName ..orangeGems) term.setBackgroundColor(32) term.setCursorPos(10, 15) print("Play") term.setCursorPos(20, 15) print("Sell Gems") end function game() term.clear() term.setBackgroundColor(1) term.setTextColor(32768) term.clear() term.setCursorPos(math.floor(w-string.len("Name: "))/2, 2) write("Name: ") playerName = read() if playerName == "" then game() else frame2() end end -- End Functions -- game() -- User Interaction -- while true do event, key, x, y = os.pullEvent() if event == "mouse_click" then if slc == 0 then if x >= 10 and x <= 18 and y == 15 and key == 1 then playGame() elseif x >= 20 and x <= 30 and y == 15 and key == 1 then sellGems() elseif slc == 1 then if x >= 5 and x <= 10 and y == h-4 and key == 1 then slc = 0 frame2() end end end end end
Also when you dont understand the logic of your own game… Still dont get why …then you really should start smaller and train up until you can program such things.