AlmarM Posted April 21, 2010 Share 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 Minesweeper A minesweeper game created in autoit, source available. _Mouse_UDF An UDF for registering functions to mouse events, made in pure autoit. 2D Hitbox Editor A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes. Link to comment Share on other sites More sharing options...
Herb191 Posted June 14, 2010 Share Posted June 14, 2010 Thanks just what I was looking for. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now