Simple Rocket or Bullet Code?

BlitzMax Forums/BlitzMax Beginners Area/Simple Rocket or Bullet Code?

IKG(Posted 2006) [#1]
I've been looking all over the code archives, but I can't find anything like what I'm looking for. I'm trying to grasp how to make multiple objects spawn and have the same rules.

Does anyone have some really simple, working, code that does something like fire multiple rockets or bullets that blow up at a certain time? Anything that has to do with creating more than one object, and having the same rules for each created object.

Scouse was kind enough to give me something near to what I want, but it included some hard function (that I don't know yet) and doesn't run.

I'm not going to use the code for one of my games. I need it for learning purposes.

If anyone can help with this, thanks!


assari(Posted 2006) [#2]
Go through this tutorial Learning 2D Game Programming With BlitzMax. Specifically look at Part 5:Adding Missiles


Perturbatio(Posted 2006) [#3]



IKG(Posted 2006) [#4]
Whoops, thanks assari.

Thanks also Perturbatio, but that's a little too complex for a Max begginner like me.


kronholm(Posted 2006) [#5]
Hope you can use this, commented every line. Sorry about the comments looking a mess, it's bound to happen with this forum :)

'fire multiple rockets that blow up at a certain time
Graphics 800,600,0						'init gfx window

Global rocketlist:TList = CreateList()				'make a list for active rockets
Global gravity# = -0.07						'gravity
SeedRnd(MilliSecs())						'make a quick "random" seed
HideMouse

Repeat								'mainloop, runs until escape pressed
	If MouseHit(1) Then trocket.fire(5)			'fire 5 rockets on LMB click, change to mousedown for autofire
	DrawRect MouseX(),580,10,40
	For Local rocket:trocket = EachIn rocketlist	'run thru rocketlist
		rocket.drawandmove()				'draw a rocket
	Next							'move to next object in rocketlist
	GCCollect()							'collect garbage
	Flip ; Cls						'flip backbuffer and clear it
Until KeyHit(key_escape)

Type trocket										
	Field x#,y#						'position of the rocket
	Field xvel#=Rand(-2,2),yvel#=Rand(5,8)			'velocity of the rocket
	Field timetolive = 0, timetodie = 100			'a crude timer deciding when rocket dies
	Function fire(number)					'the "fire x number of rockets function"
		For Local i=1 To number				'simply creates x number of trockets and adds them to list
			Local newrocket:trocket = New trocket
			newrocket.x = MouseX()+5 ; newrocket.y = 580	'set coordinates to mouse
			rocketlist.addlast(newrocket)
		Next
	EndFunction
	Method drawandmove()				'move and draw a rocket
		x:+xvel ; y:-yvel					'add the veloceties
		yvel:+gravity								'add the gravity to the y velocity
		DrawOval x,y,1,1							'draw the rocket itself
		timetolive:+1								'add to the timetolive counter
		If timetolive >= timetodie Then die()		'counter check
	EndMethod
	Method die()									'death method
		DrawText "BOOM",x-20,y						' :)
		Local rocket:trocket = Self					
		rocketlist.remove(rocket)					'remove rocket from list
		rocket = Null								'die.
	EndMethod
EndType



IKG(Posted 2006) [#6]
Wow! Thanks so much, kronholm! Just what I was looking for :D

Only thing I don't understand is "Methods" and "Lists", but I should go somewhere else for that I suppose.


kronholm(Posted 2006) [#7]
There's nothing to understand almost :)

A list is like a giant shopping bag where you can put all your rockets. Then you can sort through the list one rocket at a time.

When you run through a list, you assign in the for/each loop, the object to a local variable. In this case, rocket will always refer to the current rocket that's in the list.

And here's where a method comes in, because if you then say rocket.drawandmove(), inside the for/each loop of the list, on the current rocket object, it will run that method on that specific rocket. This means that within the method, you don't have to refer directly to a certain instance of the type (e.g. rocket number 523), but can instead just type x = 15. This is the same as rocket523.x = 15.

Does that make any sense?


assari(Posted 2006) [#8]
IKG, suggests you go through the tutorials, the answers to your questions on lists (Part 4) and methods (Part 1) are all there :)


IKG(Posted 2006) [#9]
Thanks so much, guys. This really cleared up a lot of things for me :D


IKG(Posted 2006) [#10]
I'm sorry, quick question:

How does the current rocket in the list not get pushed down when the next one is created? In other words, how does a "Method" repeat the same rules to a single rocket and keep track of it, if a new one is created right after it? Do those functions in the main loop repeat the rules for the entire list?

I hope this made sense...


Ryan Burnside(Posted 2006) [#11]
Well because the instances are usually stored in a list the list is looped through and all instances are updated.
Each object is responsabe for itself , values and all.


Blitzplotter(Posted 2006) [#12]
@kron : a good example of the power of bmax.


kronholm(Posted 2006) [#13]
Thank you plotter, I was trying to demonstrate how easy it could be. I've added it to the misc codearchives.