Array of Boxes
Author |
Message |
Prince Pwn
|
Posted: Fri Nov 03, 2006 12:51 pm Post subject: Array of Boxes |
|
|
Is there a way so I can make the boxes keep up with my mouse? If you hold down the left mouse button and drag a box to the side really fast it stops holding.
And how could I fix it so when I drag one box over another, the bottom box doesn't dissapear?
code: |
Mouse.ButtonChoose ("multibutton")
View.Set ("graphics:1000,600;offscreenonly")
var x, y, button, rad, col, boxNum : int
boxNum := 15 %number of boxes
rad := 30 %size of boxes
var boxX, boxY : array 1 .. boxNum of int %makes variables for each box
col := Rand.Int (0, 255) %sets default colour
for i : 1 .. boxNum %positions all the boxes
boxX (i) := 0 + i * rad
boxY (i) := maxy div 2
end for
loop
Mouse.Where (x, y, button)
%makes it so boxes can't go offscreen
if x <= 0 + rad div 2 then
x := rad div 2
elsif x >= maxx - rad div 2 then
x := maxx - rad div 2
end if
if y <= 0 + rad div 2 then
y := rad div 2
elsif y >= maxy - rad div 2 then
y := maxy - rad div 2
end if
for i : 1 .. boxNum %draws each box and checks their values
drawfillbox (boxX (i), boxY (i), boxX (i) + rad, boxY (i) + rad, col)
drawbox (boxX (i), boxY (i), boxX (i) + rad, boxY (i) + rad, black)
if button = 1 and x > boxX (i) and x < boxX (i) + rad
and y > boxY (i) and y < boxY (i) + rad then
%checks if you left click on any of the boxes
boxX (i) := x - rad div 2
boxY (i) := y - rad div 2
elsif button = 10 then
boxX (i) := x - rad div 2
boxY (i) := y - rad div 2
end if
col := Rand.Int (0, 255) %makes the boxes flash
end for
View.Update
cls
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Clayton
|
Posted: Fri Nov 03, 2006 4:40 pm Post subject: (No subject) |
|
|
The reason the box doesn't "stick" to the mouse is because your computer can't keep up with so many things working in the background (especially with Turing running ) and therefore ends up delaying certain things from running while it trys to run everything else, so your turing program ends up lagging, and the box can't keep up with the mouse. |
|
|
|
|
|
Piro24
|
Posted: Fri Nov 03, 2006 5:36 pm Post subject: (No subject) |
|
|
If is all you want to do is draw something along with the mouse, it's simple.
Use your Mouse.Where co ords (x,y,b)
and plug them into the variables of the box
ex.
Mouse.Where (x,y,b)
if b = 1 then
drawoval (x,y,20,20black)
end if |
|
|
|
|
|
Prince Pwn
|
Posted: Sat Nov 04, 2006 10:33 am Post subject: (No subject) |
|
|
Oh and I realised the reason why the box below the one dragged disappears, it doesn't really dissapear. It just hides ontop or goes underneath the box being dragged due to "if mouse touches box then locate middle of box on mouse pointer", found this out by looking at my updated code... But I wonder how I should solve it...
code: |
Mouse.ButtonChoose ("multibutton")
View.Set ("graphics:1000,600;offscreenonly")
var x, y, button, rad, col, boxNum, fSize : int
boxNum := 15 %number of boxes
rad := 50 %size of boxes
View.Set ("title:BoxMove " + intstr (rad))
if rad > 50 then
fSize := 10
elsif rad > 35 and rad <= 50 then
fSize := 7
elsif rad > 24 and rad <= 35 then
fSize := 5
else
fSize := 0
end if
var font : int := Font.New ("Comic Sans MS:" + intstr (fSize)) %font
var boxX, boxY : flexible array 1 .. boxNum of int %makes vars for each box
col := Rand.Int (0, 255) %sets default colour
var keys : array char of boolean
for i : 1 .. boxNum %positions all the boxes
boxX (i) := 0 + i * rad
boxY (i) := maxy div 2
end for
loop
Mouse.Where (x, y, button)
Input.KeyDown (keys)
put "Size ", rad
for i : 1 .. boxNum
if /*i = 1 or i = 2 or i = 4 or i = 5*/ i mod 5 not= 0 then
put "BOX ", i, " - ", "X: ", boxX (i), " Y: ", boxY (i), " " ..
else
put "BOX ", i, " - ", "X: ", boxX (i), " Y: ", boxY (i)
end if
end for
%makes it so boxes can't go offscreen
if x <= 0 + rad div 2 then
x := rad div 2
elsif x >= maxx - rad div 2 then
x := maxx - rad div 2
end if
if y <= 0 + rad div 2 then
y := rad div 2
elsif y >= maxy - rad div 2 then
y := maxy - rad div 2
end if
for i : 1 .. boxNum %draws each box and checks their values
drawfillbox (boxX (i), boxY (i), boxX (i) + rad, boxY (i) + rad, col)
drawbox (boxX (i), boxY (i), boxX (i) + rad, boxY (i) + rad, black)
if button = 1 and x > boxX (i) and x < boxX (i) + rad
and y > boxY (i) and y < boxY (i) + rad then
%checks if you left click on any of the boxes
boxX (i) := x - rad div 2
boxY (i) := y - rad div 2
elsif button = 10 then
boxX (i) := x - rad div 2
boxY (i) := y - rad div 2
end if
col := brightred %makes the boxes colour
if fSize not= 0 then
Font.Draw ("X:" + intstr (boxX (i)), boxX (i) + rad div 4,
boxY (i) + rad div 2, font, black)
Font.Draw ("Y:" + intstr (boxY (i)), boxX (i) + rad div 4,
boxY (i) + rad div 2 - rad div 3, font, black)
end if
end for
if keys (KEY_DELETE) or keys (KEY_BACKSPACE) then
delay (250)
Input.Flush
boxNum -= 1
elsif keys (KEY_INSERT) then
new boxX, upper (boxX)
%boxNum += 1
end if
View.Update
cls
end loop
|
|
|
|
|
|
|
richcash
|
Posted: Sat Nov 04, 2006 11:57 am Post subject: (No subject) |
|
|
Why don't you just make a variable like clicked to ensure that only one box is controlled by the mouse at one time? And to make it stop controlling it (when the user lets go of the button), we just have a boolean variable like buttonUp to keep track of whether or not the mouse is clicked. When the mouse is unclicked, just reset the clicked variable to control no boxes.
Something like this :
code: | View.Set ("offscreenonly")
var x, y, button, clicked : int
var boxNum := 12
var buttonUp : boolean
var boxX, boxY : array 1 .. boxNum of int
for i : 1 .. boxNum
boxX (i) := i * 50
boxY (i) := 150
end for
loop
Mouse.Where (x, y, button)
if button not= 0 and buttonUp then
for i : 1 .. boxNum
if x > boxX (i) and x < boxX (i) + 50 and y > boxY (i) and y < boxY (i) + 50 then
clicked := i
buttonUp := false
end if
end for
elsif button = 0 then
clicked := 0
buttonUp := true
end if
if clicked not= 0 then
boxX (clicked) := x - 25
boxY (clicked) := y - 25
end if
for i : 1 .. 12
Draw.FillBox (boxX (i), boxY (i), boxX (i) + 50, boxY (i) + 50, Rand.Int (0, 255))
end for
View.Update
delay (5)
cls
end loop |
|
|
|
|
|
|
Prince Pwn
|
Posted: Sat Nov 04, 2006 1:06 pm Post subject: (No subject) |
|
|
Sweet idea. And yours runs like 10x faster too and it doesn't clear when you overlap =) |
|
|
|
|
|
Prince Pwn
|
Posted: Sat Nov 04, 2006 1:12 pm Post subject: (No subject) |
|
|
Wait, what is slowing mine down? |
|
|
|
|
|
richcash
|
Posted: Sat Nov 04, 2006 1:44 pm Post subject: (No subject) |
|
|
I don't think mine is any faster, I even delayed it by 5. It's just that your first code only works when you go slow with the mouse (so it gives the illusion of being slow). Your second code has so much text, font and input slowing it down, and it also only works when you go slow. So I just think mine looks faster because it works.
Make sure you understand that code, otherwise it's useless to you! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|