@TurtleProgrammer,
You seem to be wanting to write a particular application although as you provide no feedback so who would know.
Earlier you wanted to know how to drag an image. Here you can drag an image of a red disc.
Notice how by moving the code into subroutines and functions that return a value you end up with reusable code and a short readable MAIN.
Code: Select all
screenres 640,480,32 '32 bit color
color rgb(0,0,0),rgb(255,255,255):cls
dim shared as integer mx,my,mb,ox,oy,xd,yd 'mouse coordinates and button state
type BMIMAGE
as integer x
as integer y
as integer w
as integer h
as any ptr img 'pointer to bitmap image
end type
type BOX
as integer x
as integer y
as integer w
as integer h
as ulong c
end type
sub drawBox(b as BOX)
line (b.x,b.y)-(b.x+b.w,b.y+b.h),b.c,bf
line (b.x,b.y)-(b.x+b.w,b.y+b.h),rgb(0,0,0),b 'give it a black border
end sub
dim shared as BOX boxes(1 to 4) 'create a list of boxes
for i as integer = 1 to 4
boxes(i).x = 10+i*60 'move position for each box 60 pixels to right
boxes(i).y = 10
boxes(i).w = 50
boxes(i).h = 50
next i
'give each box a color
boxes(1).c = rgb(0,255,0) 'green
boxes(2).c = rgb(200,100,0) 'orange
boxes(3).c = rgb(0,0,255) 'blue
boxes(4).c = rgb(255,0,0) 'red
sub displayBoxes()
for i as integer = 1 to 4
drawBox(boxes(i))
next i
end sub
dim shared as any ptr image1
image1 = imagecreate(50,50)
line image1,(0,0)-(49,49),rgb(100,50,200),bf 'back ground color
circle image1,(25,25),24,rgb(255,0,0),,,,f 'draw red disc on image1
dim shared as BMIMAGE redDisc
redDisc.x = 100
redDisc.y = 100
redDisc.w = 50
redDisc.h = 50
redDisc.img = image1
sub update()
screenlock
cls
displayBoxes()
put (redDisc.x,redDisc.y),image1,trans
screenunlock
end sub
update()
function mouseOn(pic as BMIMAGE) as boolean
getmouse mx,my,,mb
return mb=1 and mx>pic.x and mx<pic.x+pic.w and my>pic.y and my<pic.y+pic.h
end function
sub dragImage(pic as BMIMAGE)
ox = mx 'save mouse position
oy = my
while mb=1
getmouse mx,my,,mb
if mx<>ox or my<>oy then 'mouse had moved
xd = mx-ox ''change in position of mouse
yd = my-oy
pic.x = pic.x + xd 'add that to image position
pic.y = pic.y + yd
ox = mx 'save latest mouse position
oy = my
update 'show change
end if
sleep 2
wend
end sub
'MAIN PROGRAM
do
if mouseOn(redDisc) then
dragImage(redDisc)
end if
sleep 2
loop until multikey(&H01)