AlmarM 22 Posted April 21, 2010 (edited) Hi! Im currently learning how to make a Tower Defence game at my school. They shown us how to make something 'follow' something. So I wrote a little example, mayby useful for other. expandcollapse popup#include <Math.au3> Global $_Timer Global $fX = 10, $fY = 10 Global $tX = 400, $tY = 570 $GUI = GUICreate("ATan2", 800, 600) $Follower = GUICtrlCreateLabel("", $fX, $fY, 20, 20) GUICtrlSetBkColor($Follower, 0x00FF00) $Target = GUICtrlCreateLabel("", $tX, $tY, 20, 20) GUICtrlSetBkColor($Target, 0xFF0000) $Label = GUICtrlCreateLabel("", 10, 10, 150, 20) $_Timer = TimerInit() GUISetState() While 1 Switch GUIGetMsg() Case -3 Exit EndSwitch $xDist = $tX - $fX $yDist = $tY - $fY $Angle = _ATan2($yDist, $xDist) $xSpeed = Cos($Angle) * 3 $ySpeed = Sin($Angle) * 3 $fX += Round($xSpeed, 0) $fY += Round($ySpeed, 0) GUICtrlSetPos($Follower, $fX, $fY) Sleep(20) If ($fX >= $tX - 10 And $fX <= $tX + 19) Then GUICtrlSetData($Label, "Status: Hit!") Else GUICtrlSetData($Label, "Status: Following...") EndIf If (TimerDiff($_Timer) >= 2000) Then Teleport($Target, Random(10, 770), Random(10, 570)) $_Timer = TimerInit() EndIf WEnd Func Teleport($cObj, $iX, $iY) GUICtrlSetPos($cObj, $iX, $iY) $tX = $iX $tY = $iY EndFunc EDIT: Another example: expandcollapse popup#include <GUIConstantsEx.au3> #include <Math.au3> Global $Radius = 200 Global $hGraphic Global $eX = 30, $eY = 350, $Speed = 4 Global $Bullet, $BulletX = 230 + 13, $BulletY = 200 + 13 Global $xSpeed, $ySpeed $GUI = GUICreate("", 500, 430) $Tower = GUICtrlCreateLabel("", 230, 200, 30, 30) GUICtrlSetBkColor($Tower, 0xFF0000) $Enemy = GUICtrlCreateLabel("", 30, 350, 10, 10) GUICtrlSetBkColor($Enemy, 0x00FF00) $hGraphic = GUICtrlCreateGraphic(230 + 15, 200 + 15, $Radius, $Radius) GUICtrlSetGraphic($hGraphic, $GUI_GR_PIE, 0, 0, $Radius, 0, 360) GUICtrlSetGraphic($hGraphic, $GUI_GR_REFRESH) GUICtrlSetGraphic($hGraphic, $GUI_GR_CLOSE) $Bullet = GUICtrlCreateLabel("", $BulletX, $BulletY, 5, 5) GUICtrlSetBkColor($Bullet, 0x0000FF) GUISetState() While 1 Switch GUIGetMsg() Case -3 Exit EndSwitch $Pos = WinGetPos($GUI) $eX = MouseGetPos(0) - $Pos[0] - 7 $eY = MouseGetPos(1) - $Pos[1] - 27 GUICtrlSetPos($Enemy, $eX, $ey) If (IsInRange($eX, $eY)) Then MoveBullet() Else $BulletX += $xSpeed $BulletY += $ySpeed GUICtrlSetPos($Bullet, $BulletX, $BulletY) EndIf If ($BulletX > 500 Or $BulletX < 0 Or $BulletY < 0 Or $BulletY > 500) Then Reset() EndIf Sleep(20) WEnd Func IsInRange($iX, $iY) $xDiff = 230 - $iX $yDiff = 200 - $iY $Distance = Sqrt(($xDiff * $xDiff) + ($yDiff * $yDiff)) If ($Distance < $Radius) Then Return True Else Return False EndIf EndFunc Func Reset() GUICtrlSetPos($Bullet, 230 + 13, 200 + 13) $BulletX = 230 + 13 $BulletY = 200 + 13 $xSpeed = 0 $ySpeed = 0 EndFunc Func MoveBullet() $xDist = $eX - $BulletX $yDist = $eY - $BulletY $Angle = _ATan2($yDist, $xDist) $xSpeed = Cos($Angle) * 4 $ySpeed = Sin($Angle) * 4 $BulletX += $xSpeed $BulletY += $ySpeed GUICtrlSetPos($Bullet, $BulletX, $BulletY) EndFunc Edited April 21, 2010 by AlmarM MinesweeperA minesweeper game created in autoit, source available._Mouse_UDFAn UDF for registering functions to mouse events, made in pure autoit.2D Hitbox EditorA 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes. Share this post Link to post Share on other sites
Herb191 4 Posted June 14, 2010 Thanks just what I was looking for. Share this post Link to post Share on other sites