Hey that's funny, I wrote one too having looked at the first version by you, Darknature, and I included a different speed for each missile. However your update has that too! ;)
I see the missiles move diagonally, and then after a while they move straight down. You can also decide right from the start where they have to go (x value), and then compute a DX value that will bring them there; so they will never hit the side of the screen and keep flying diagonally. This is what the code below does. Sorry if this is a bit overkill after the two previous examples already! :)
Good luck with your programming Duncki.
; #########
; CONSTANTS
; #########
Const SCREENWIDTH = 800
Const SCREENHEIGHT = 600
Const MISSILE_COUNT = 5
Const MISSILE_SIZE = 8
; ##################
; INITIALIZE PROGRAM
; ##################
Graphics(SCREENWIDTH,SCREENHEIGHT)
SetBuffer(BackBuffer())
SeedRnd(MilliSecs())
; ############
; MISSILE TYPE
; ############
Type MISSILE
Field f_x#,f_y#
Field f_dX#,f_dY#
Field f_speed#
Field f_startX,f_startY
End Type
; #################
; MISSILE FUNCTIONS
; #################
Function MISSILE_initialize(p_missile.MISSILE)
; assign new values to fields of p_missile
p_missile\f_x = Rand(0,SCREENWIDTH)
p_missile\f_y = 0
p_missile\f_startX = p_missile\f_x
p_missile\f_startY = p_missile\f_y
; take a random point on the ground as target for this missile
targetX = Rand(0,SCREENWIDTH)
totalDx = targetX - p_missile\f_startX
; compute how it has to fly (dx value) to reach that point
p_missile\f_dX = Float(totalDx) / Float(SCREENHEIGHT)
p_missile\f_dY = 1.0
; give a random speed to this missile
p_missile\f_speed = Rnd(0.5,1.5)
End Function
Function MISSILE_createAll()
; create as many as MISSILE_COUNT new missiles
For i = 1 To MISSILE_COUNT
missile.MISSILE = New MISSILE
MISSILE_initialize(missile)
Next
End Function
Function MISSILE_moveAll()
; move each missile
For missile.MISSILE = Each MISSILE
; change x and y fields, also take speed into account
missile\f_x = missile\f_x + missile\f_dX * missile\f_speed
missile\f_y = missile\f_y + missile\f_dY * missile\f_speed
; if missile touches ground, re-initialize it
If ((missile\f_y + MISSILE_SIZE/2) > SCREENHEIGHT) Then
MISSILE_initialize(missile)
End If
Next
End Function
Function MISSILE_drawAll()
; draw each missile
For missile.MISSILE = Each MISSILE
; line of smoke
Color(255,255,255)
Line(missile\f_startX,missile\f_startY,missile\f_x,missile\f_y)
; the missile
Color(255,0,0)
Oval(missile\f_x-MISSILE_SIZE/2,missile\f_y-MISSILE_SIZE/2,MISSILE_SIZE,MISSILE_SIZE,True)
Next
End Function
; #################
; MAIN PROGRAM LOOP
; #################
MISSILE_createAll()
While(Not(KeyHit(1)))
MISSILE_moveAll()
Cls
MISSILE_drawAll()
Flip
Wend
Delete Each MISSILE
End
|