Missles like in "Missle Command"

BlitzPlus Forums/BlitzPlus Programming/Missles like in "Missle Command"

duncki(Posted 2003) [#1]
Is there anyone who now how can i make
lines there come from the Top like in
Missles Command.
I have mo good idea.
Thanks
duncki


DarkNature(Posted 2003) [#2]
This should be close enough to get you started

Graphics 640,480

Type missile
	Field x,y
	Field startx,starty
	Field tx,ty
End Type

For i=0 To 5
	m.missile=New missile
	m\x=Rnd(GraphicsWidth())
	m\y=0
	m\tx=Rnd(GraphicsWidth())
	m\ty=GraphicsHeight()
	m\startx=m\x
	m\starty=m\y
Next

Function movemissiles()

	For m.missile=Each missile
		If m\x<m\tx m\x=m\x+1
		If m\x>m\tx m\x=m\x-1
		If m\y<m\ty m\y=m\y+1
		Color 255,255,255
		Line m\startx,m\starty,m\x,m\y
		Color 255,0,0
		Oval m\x-2,m\y-2,4,4
	Next

End Function

tim=CreateTimer(30)
SetBuffer(BackBuffer())

While Not KeyHit(1)

	WaitTimer(tim)
	Cls
	movemissiles()
	Flip

Wend
End


have fun.


duncki(Posted 2003) [#3]
Thank you it works!


smilertoo(Posted 2003) [#4]
thing i couldnt work out was how to erase the trail.


DarkNature(Posted 2003) [#5]
Here ya go:

Graphics 640,480



Type missile

	Field x,y

	Field startx,starty

	Field tx,ty
	
	Field speed#
	
	Field dead

End Type



For i=0 To 5

	m.missile=New missile

	m\x=Rnd(GraphicsWidth())

	m\y=0

	m\tx=Rnd(GraphicsWidth())

	m\ty=GraphicsHeight()

	m\startx=m\x

	m\starty=m\y
	
	m\speed#=Rnd(0.5 , 2.5)
	
	m\dead=False

Next



Function movemissiles()



	For m.missile=Each missile
	
		If m\x<m\tx m\x=m\x+1

		If m\x>m\tx m\x=m\x-1

		If m\y<m\ty m\y=m\y+m\speed#

		Color 255,255,255

		Line m\startx,m\starty,m\x,m\y

		Color 255,0,0

		Oval m\x-2,m\y-2,4,4

		If m\y>=m\ty ; or its been shot down
			m\dead=True
		End If
		
		If m\dead Delete m

	Next



End Function



tim=CreateTimer(30)

SetBuffer(BackBuffer())



While Not KeyHit(1)



	WaitTimer(tim)

	Cls

	movemissiles()

	Flip



Wend

End


Have fun.


Foppy(Posted 2003) [#6]
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



duncki(Posted 2003) [#7]
Thanks to all for your help!
I think the Version from Foppy is that , that is near the
Original.