I guess the functions are:
Function AngleToDX:Float(angle:Float)
Return Cos(angle)
End Function
Function AngleToDY:Float(angle:Float)
Return Sin(angle)
End Function
Function PointsToDistance#(x1#,y1#,x2#,y2#)
Return Sqr(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)))
End Function
Function PointsToAngle#(x1#,y1#,x2#,y2#)
Local direction#= ATan2(y1-y2,x1-x2)+180
While direction > 360
direction:-180
Wend
While direction<0
direction:+180
Wend
Return direction
EndFunction
Okay... so try this:
SuperStrict
Graphics 800,600,0
SetBlend ALPHABLEND
SeedRnd MilliSecs()
Global img:TImage = LoadImage("bg.png")
AutoMidHandle True
Global playerImg:TImage = LoadImage("player.png")
AutoMidHandle False
Global player:TPlayer = New TPlayer
player.image = playerImg
player.cx = 400
player.cy = 300
Global alien:TAlien = New TAlien
alien.image = playerImg
alien.x = Rand (0,800)
alien.y = Rand (0,600)
alien.acceleration = 0.3
Global en:TEnemy = New TEnemy
en.x = 50
en.y = 50
While Not KeyHit(KEY_ESCAPE)
drawBackground()
player.update()
alien.update()
en.update()
player.draw()
alien.draw()
Flip 1
Wend
End
Type TEntity
Field x:Float, y:Float
Field dx:Float, dy:Float
Field rotation:Float
Field acceleration:Float = 0
Field image:TImage
End Type
Type TPlayer Extends TEntity
Field cx:Float, cy:Float
Const ACCELERATION:Float = 0.1
Method draw()
SetRotation rotation
DrawImage image,cx,cy
SetRotation 0
End Method
Method controls()
If KeyDown(KEY_UP)
dx:+Sin(rotation)*ACCELERATION
dy:+-Cos(rotation)*ACCELERATION
End If
If KeyDown(KEY_DOWN)
dx:-Sin(rotation)*ACCELERATION
dy:+Cos(rotation)*ACCELERATION
End If
If KeyDown(KEY_LEFT)
rotation:-4
End If
If KeyDown(KEY_RIGHT)
rotation:+4
End If
End Method
Method update()
controls()
x :+ dx
y :+ dy
End Method
End Type
Type TAlien Extends TEntity
Field n:Int
Method draw()
SetRotation rotation
SetColor 0,255,0
DrawImage image,x,y
SetColor 255,255,255
SetRotation 0
End Method
Method trackPlayer()
Local radius:Float = 100
dx = (player.cx - x)
dy = (player.cy - y)
Local dist:Float = Sqr(dx * dx + dy * dy)
If dist >= radius
dx:/dist * acceleration
dy:/dist * acceleration
n = rotation + 90
Else
dx = dx+Cos(n)*(radius - 2)
dy = dy+Sin(n)*(radius - 2)
n :+1
EndIf
rotation = point_direction#(x,y,player.cx,player.cy)
End Method
Method update()
trackPlayer()
x :+ dx -player.dx
y :+ dy -player.dy
End Method
End Type
Type TEnemy
Field x:Float,y:Float
Field dx:Float,dy:Float
Field angle:Int
Field shoottimer:Int
Field radius:Float = 100
Const SPEED:Float = 5
Method Create:TEnemy()
' ListAddLast(EnemyList,Self)
x = 50
y = 50
End Method
Method Update()
DrawText PointsToDistance(x,y,400,300),0,20
dx = (player.cx - x)
dy = (player.cy - y)
Local dist:Float = Sqr(dx * dx + dy * dy)
If PointsToDistance(x,y,400,300) >= radius
angle = PointsToAngle(x,y,400,300)
dx = AngleToDX(angle)*SPEED
dy = AngleToDY(angle)*SPEED
angle:+180
Else
angle:+4
dx:+AngleToDX(angle) * (radius- 2)
dy:+AngleToDY(angle) * (radius- 2)
End If
x:+dx
y:+dy
x:+ -player.dx
y:+ -player.dy
If shoottimer > 20
' PlaySound shoot_sound
' Local bul:TBullet = New TBullet.Create(x,y,angle)
shoottimer = 0
End If
SetRotation angle - 90
SetColor 0,255,255
DrawImage playerImg,x,y
SetColor 255,255,255
SetRotation 0
shoottimer:+1
End Method
End Type
Function AngleToDX:Float(angle:Float)
Return Cos(angle)
End Function
Function AngleToDY:Float(angle:Float)
Return Sin(angle)
End Function
Function point_direction#(x1#,y1#,x2#,y2#)
Local direction#= ATan2(y1-y2,x1-x2)+180
While direction > 360
direction:-180
Wend
While direction<0
direction:+180
Wend
Return direction + 90
EndFunction
Function PointsToDistance#(x1#,y1#,x2#,y2#)
Return Sqr(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)))
End Function
Function PointsToAngle#(x1#,y1#,x2#,y2#)
Local direction#= ATan2(y1-y2,x1-x2)+180
While direction > 360
direction:-180
Wend
While direction<0
direction:+180
Wend
Return direction
EndFunction
Function drawBackground()
TileImage img,-(player.x/4),-(player.y/4)
SetAlpha .7
TileImage img,-(player.x/2),-(player.y/2)
SetAlpha .4
TileImage img,-player.x,-player.y
SetAlpha 1
End Function
Using the same images you supplied...
red = player
green = my alien
blue = your type
So your type is now:
Type TEnemy
Field x:Float,y:Float
Field dx:Float,dy:Float
Field angle:Int
Field shoottimer:Int
Field radius:Float = 100
Const SPEED:Float = 5
Method Create:TEnemy()
ListAddLast(EnemyList,Self)
x = 50
y = 50
End Method
Method Update()
DrawText PointsToDistance(x,y,400,300),0,20
dx = (player.cx - x)
dy = (player.cy - y)
Local dist:Float = Sqr(dx * dx + dy * dy)
If PointsToDistance(x,y,400,300) >= radius
angle = PointsToAngle(x,y,400,300)
dx = AngleToDX(angle)*SPEED
dy = AngleToDY(angle)*SPEED
angle:+180
Else
angle:+4
dx:+AngleToDX(angle) * (radius- 2)
dy:+AngleToDY(angle) * (radius- 2)
End If
x:+dx
y:+dy
x:+ -pdx
y:+ -pdy
If shoottimer > 20
PlaySound shoot_sound
Local bul:TBullet = New TBullet.Create(x,y,angle)
shoottimer = 0
End If
SetRotation angle - 90
DrawImage player_img,x,y
SetRotation 0
shoottimer:+1
End Method
End Type
You may have to do this in your bullet code:
Local bul:TBullet = New TBullet.Create(x,y,angle-180)