2
[QUESTION] Mouse input for more than one spot?
Started by Mackan90096, Apr 10 2013 08:41 PM
58 replies to this topic
#1
Posted 10 April 2013 - 08:41 PM
Hi there!
I am making a game where if you click on a pixel a number gets bigger..
I wonder if you can have pixels randomly appear and when clicked dissapear..
Well.. My question is if you can have mouse detection for all of them without having a code snippet for all of the pixels?
I am making a game where if you click on a pixel a number gets bigger..
I wonder if you can have pixels randomly appear and when clicked dissapear..
Well.. My question is if you can have mouse detection for all of them without having a code snippet for all of the pixels?
#2
Posted 10 April 2013 - 08:43 PM
If you do it in an Object-Oriented way it will mean that you wont have to have heaps of identical code. you can just loop through all the pixels and 'ask' if they have been clicked with a function like wasClicked or something of the such...
#3
Posted 10 April 2013 - 08:51 PM
Yeah.. That sounds like a good idea..
Thanks
But.. how do I do that?
Thanks
But.. how do I do that?
#4
Posted 10 April 2013 - 08:58 PM
make a table. and store a table inside of it. in the second table store things such as the x position, y position...
then make a function that loops through the main table checking if the clicking position matches to any of the pixels inside it. if they match, remove them.
then make a function that loops through the main table checking if the clicking position matches to any of the pixels inside it. if they match, remove them.
#5
Posted 10 April 2013 - 09:00 PM
huh?
#6
Posted 10 April 2013 - 09:23 PM
What exactly do you mean?
Can you explain it a bit simpler?
Can you explain it a bit simpler?
#7
Posted 10 April 2013 - 09:55 PM
local tab = { {1, 1}, {15, 3} }
#8
Posted 10 April 2013 - 09:55 PM
What?
#10
Posted 10 April 2013 - 10:52 PM
There is a table within a table. A box within a box. The inner tables store the x and y positions of the pixel. The outer table holds all of the inner tables.
Each time you make a new pixel appear, add a table of coordinates to the main table.
Then, when something is clicked, you just check to see if the coordinates clicked are the same as the pixel coordinates.
It's 11:58 PM (by my clock), I can't be bothered to explain it any further right now.
Each time you make a new pixel appear, add a table of coordinates to the main table.
Then, when something is clicked, you just check to see if the coordinates clicked are the same as the pixel coordinates.
It's 11:58 PM (by my clock), I can't be bothered to explain it any further right now.
#11
Posted 10 April 2013 - 11:05 PM
Ok.. But when you have time.. How do I check to see if the coordinates clicked are the same as the pixel coordinates?
#12
Posted 10 April 2013 - 11:10 PM
I'll try to explain it to the best of my ability.
This function here allows you to create a new pixel by adding a table to the "pixels" table, with the values of it, x and y, set to what you've given it respectively. If I wanted to make 20 random pixels:
And then we just make a loop, first drawing the pixels, then getting a mouse click, and checking them all against the mouse click.
local pixels = {} function newPixel(x, y) table.insert(pixels, {x=x, y=y}) end
This function here allows you to create a new pixel by adding a table to the "pixels" table, with the values of it, x and y, set to what you've given it respectively. If I wanted to make 20 random pixels:
local w,h = term.getSize() for i=1, 20 do newPixel(math.random(1, w), math.random(1, h)) end
And then we just make a loop, first drawing the pixels, then getting a mouse click, and checking them all against the mouse click.
while true do -- "start off by clearing the screen with the color black before drawing anything" term.setBackgroundColor(colors.black) term.clear() -- "we start at 1, and add 1 until we reach the length of the pixels table" -- "this is only where we draw the pixels" for i=1, #pixels do -- "for simplification, we get the current pixel and store it in a variable." local pixel = pixels[i] -- "then access the pixel's data, go to it's position, and write a white space." term.setCursorPos(pixel.x, pixel.y) term.setBackgroundColor(colors.white) term.write(' ') end -- "we get a click event from the user" local _, button, mx, my = os.pullEvent('mouse_click') -- "then loop through the buttons again." for i=1, #pixels do -- "again, storing it" local pixel = pixels[i] -- "checking for a like position" if pixel.x == mx and pixel.y == my then -- "if the positions match up, draw a lime green space for half a second." term.setCursorPos(pixel.x, pixel.y) term.setBackgroundColor(colors.lime) term.write(' ') sleep(0.5) -- "since we've already found a pixel to click, no need to check for others." -- "so we break out of the loop." break end end -- "after the pixel is green for half a second, the loop resumes, and all pixels are redrawn white again." end
#13
Posted 10 April 2013 - 11:23 PM
I want it to when the pixel is clicked remove it and regen it afterwards at random and when its clicked add to a number.
I want to have it inside a frame...
And how do i use it kingdaro?
I want to have it inside a frame...
And how do i use it kingdaro?
#14
Posted 10 April 2013 - 11:47 PM
In the code handling the pixel clicks, increment the variable you want to increase by one.
#15
Posted 10 April 2013 - 11:50 PM
Umm... could you show an example?
#16
Posted 10 April 2013 - 11:57 PM
Here's my current code:
-- Sprites -- bar1 = paintutils.loadImage("/sprites/bars/.bar1") gameBar = paintutils.loadImage("/sprites/bars/.game1") -- Gems -- gem1 = paintutils.loadImage("/sprites/gems/.gem1") gem1Name = "Orange Gem" gem1Status = true gem2 = paintutils.loadImage("/sprites/gems/.gem2") gem2Name = "Lime Gem" gem2Status = true gems = 10 gemprice = 50 money = 0 multi = 2 function Main() slc = 0 term.clear() term.setBackgroundColor(16384) term.setTextColor(1) paintutils.drawImage(gameBar, 1,1) term.setCursorPos(1,1) print("Money: "..money) term.setCursorPos(1,2) print("Gems: "..gems) term.setCursorPos(1,3) print("Multiplier:") term.setCursorPos(1,4) print(multi) paintutils.drawImage(gem1, 13,2) while true do event, button, X, Y = os.pullEvent("mouse_click") if event == "mouse_click" then if slc == 0 then if X == 13 and Y == 2 and button == 1 then gems = gems + (1*multi) Main() end end end end end Main()
#17
Posted 11 April 2013 - 12:00 AM
If someone else doesn't beat me to it, I'll post some code tomorrow (I just got on my iPod and shut off my computer, so I'm not going to type code)
But someone probably will
But someone probably will
#18
Posted 11 April 2013 - 12:00 AM
Ah... Ok thanks in advance Smiley
#19
Posted 11 April 2013 - 12:03 AM
Ah, quick question: Are those sprite files just images of differently colored pixels (1x1 rectangles)?
#20
Posted 11 April 2013 - 12:05 AM
Yeah.. as of current
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users