Code archives/Miscellaneous/2D Click 'n' Move

This code has been declared by its author to be Public Domain code.

Download source code

2D Click 'n' Move by Muzzer
(Posted 21 years ago)
Very basic example of clicking a position on the screen and having an image move to that position automatically.

Download the *.bb file and image used in the example here...

http://www.sitesled.com/members/avengedsevenfold/clicknmove.zip
;************************************************
;	2D Click 'n' Move Example - written by Muzzer 2006	
;************************************************		
;				
; 	This code should give beginners an idea about	  
; 	moving an image to a position clicked on screen 	
;
;	Experienced Blitz users may think it's written
;	incorrectly, and they're probably right.
;
;	Anyway, it works.
;
;	Soldier sprite ripped from Metal Slug and grabbed from http://www.emugifs.emuita.it
;
;************************************************


AppTitle "2D Click 'n' Move Example"

Graphics 640,480,0,2

SetBuffer BackBuffer()

Global LastClickX = 400		;these coords are the same as the image start position to 
Global LastClickY = 240		;prevent it movingas soon as the program runs

Global ShipX = 400			;Image start postions
Global ShipY = 240

imgMove = LoadImage("soldier.PNG")
MaskImage imgMove,255,0,255

;------------------
;Main Loop
;------------------

While Not KeyHit(1)
Cls

	;set the LastClick positions
	If MouseDown(1) Then LastClickX = MouseX() LastClickY =  MouseY()	;if left button is pressed, set the LastClick variables to the current MouseX() and MouseY() coordinates
			
	
;DrawImage imgBack,0,0
DrawImage imgMove,ShipX - 32, ShipY -32	; the -32 offsets the image so the centre moves to the clicked point

	If ShipX < LastClickX Then ShipX = ShipX + 2	;if the image's X position is less than the LastClickX coord we set, move 2 units to the right
	
	If ShipX > LastClickX Then ShipX = ShipX - 2	;if the image's X position is greater than LastClickX, move 2 units to the left
		
	If ShipY < LastClickY Then ShipY = ShipY + 2	;if the image is lower than the LastClickY variable, move the image down
	
	If ShipY > LastClickY Then ShipY = ShipY - 2	;if the image is higher than the LastCLickY variable, move the image up
	
	
	;------------------
	;Debug Text	- simply to display the coordinates on screen
	;------------------
	

	Text 20,15, "Last ClickX = " + LastClickX	;displays the last 'X' coordinates clicked
	Text 20,30, "Last ClickY = " + LastClickY	;displays the last 'Y' coordinates clicked
	
	;constantly displays the Mouse coordinates
	Text 20,45, "Mouse X = " + MouseX()			
	Text 20,60, "Mouse Y = " + MouseY()
	
Flip

Wend

End

Comments

Jams
(Posted 21 years ago)
Try this one:

Type point2
	Field x#, y#
End Type

;Calculates the distance between 2 2D points.
Function point2_CalcDistance#( A.point2, B.point2 )
	Local dX# = Abs( A\x - B\x )
	Local dY# = Abs( A\y - B\y )
	Return Sqr( (dX*dX) + (dY*dY) )
End Function

;Calculates the angle between 2 2D points.
Function point2_CalcAngle#( A.point2, B.point2 )
			Local dX# = A\x - B\x
			Local dY# = A\y - B\y
			Return ATan2( dX#, dY# ) +180
End Function

Local Pos.point2 = New point2; Current position.
Local Target.point2 = New point2; Target position.

;Move position and target to the centre of the screen.
Pos\x = 320: Pos\y = 240
Target\x = 320: Target\y = 240

Graphics 640,480, 0, 2
SetBuffer BackBuffer()
Repeat:Flip:Cls

	If MouseHit( 1 )
		;Update target with new mouse coords.
		Target\x = MouseX()
		Target\y = MouseY()
	EndIf

	;Check to see if the sprite is not already 'at' Target.
	If point2_CalcDistance( Pos, Target ) > 1
		Angle# = point2_CalcAngle( Pos, Target ); Calculate the angle needed to go to Target.
		Pos\x = Pos\x + Sin( Angle# );Move along x axis.
		Pos\y = Pos\y + Cos( Angle# );Move along y axis.
	EndIf

	Oval( pos\x-5, pos\y-5, 10, 10 ); Draw sprite

	Text 0,0,"Distance to target: "+ point2_CalcDistance( Pos, Target )
	Text 0,15,"Angle to target: "+ point2_CalcAngle( Pos, Target )

Until KeyHit(1)


Uses sin & cos to give more accurate movement. Also has 2 very useful functions to calculate the distance between two points, and to calculate the angle between 2 points.

octothorpe
(Posted 21 years ago)

Local dX# = A\x - B\x
Local dY# = A\y - B\y
Return ATan2( dX#, dY# ) +180



If you subtract A from B instead, you won't need to add 180. ;)

Also, you might want to multiply the results of Sin() and Cos() by a constant (SPEED) to make it obvious how to change the speed.

Jams
(Posted 21 years ago)
Ahh i thought there was something a little fishy about that +180, thanks octothorpe :)

Code Archives Forum