Blitz on Rails

Community Forums/Showcase/Blitz on Rails

Warpy(Posted 2009) [#1]
Something different this time, even more abstract than usual.

I've been thinking lately that I'm fed up with implementing the same set of basic mechanics every time I start a game - init graphics, write out the main loop, set up object types with positions and physics and whatnot, and so on.

So my first thought was, how about making my own little language where all I need to do is add in the bits I'm interested in?

Well, writing my own compiler or interpreter is out, because that's a load of work I have no interest in, so my next thought was a code generator. If you've ever used Ruby on Rails, you'll be familiar with its use of ActiveRecord, where you define a model using fairly legible syntax like has_many friends, and so on. It pluralises and capitalises everything appropriately so you can read your code almost like natural writing.

I've already got a parser written, I thought, so I'll have a go at that!

Here's an example input:
a drawable is a thing
	belongs to a list
	can draw
	can update
		add velocity to position

a ship is a kind of drawable
	belongs to a list
	requires a name
	requires a position
	has many friend ships
	has many bullets
	has a velocity
	has a direction number
	can shoot
		make a bullet
	can turn
	can accelerate
	
a bullet is a kind of drawable
	belongs to a ship
	requires a position
	has a velocity
	inherits velocity
	inherits position

an asteroid is a kind of drawable
	belongs to a list
	requires a position
	requires a velocity
	requires a radius number
	can attract
		takes an asteroid
	can update
		attract every other asteroid
	
main loop
	update every drawable
	draw every drawable
	
velocity is a vector
position is a vector
name is a string


and here's the code generated:

Global Asteroids:TList=New TList
Type TAsteroid Extends TDrawable
	Field pX#, pY#
	Field radius#
	Field vX#, vY#

	Method New()
		Asteroids.addlast Self

	End Method

	Function CreateAsteroid:TAsteroid( pX#, pY#, vX#, vY#, radius# )

	End Function


	Method Attract( Asteroid:TAsteroid )
	End Method


	Method Update()
		For asteroid:TAsteroid=EachIn Asteroids
			If asteroid <> Self
				attract asteroid
			Endif
		Next
		
	End Method


	Method Draw()
	End Method

End Type

Type TBullet Extends TDrawable
	Field pX#, pY#
	Field parentship:TShip
	Field vX#, vY#

	Method New()

	End Method

	Function CreateBullet:TBullet( pX#, pY# )

	End Function


	Method Draw()
	End Method


	Method Update()
		pX :+ vX
		pY :+ vY
	End Method

End Type

Global Drawables:TList=New TList
Type TDrawable

	Method New()
		Drawables.addlast Self

	End Method

	Function CreateDrawable:TDrawable(  )

	End Function


	Method Draw()
	End Method


	Method Update()
		pX :+ vX
		pY :+ vY
	End Method

End Type

Global Ships:TList=New TList
Type TShip Extends TDrawable
	Field direction#
	Field Name$
	Field pX#, pY#
	Field vX#, vY#
	Field myBullets:TList
	Field Friends:TList

	Method New()
		Ships.addlast Self

		myBullets = New TList
		Friends = New TList
	End Method

	Function CreateShip:TShip( Name$, pX#, pY# )

	End Function


	Method Accelerate()
	End Method


	Method Shoot()
		bullet:TBullet = New TBullet
		bullet.vX = vX
		bullet.vY = vY
		bullet.pX = pX
		bullet.pY = pY
		bullet.parentShip = Self
		myBullets.addlast bullet
	End Method


	Method Turn()
	End Method


	Method Draw()
	End Method


	Method Update()
		pX :+ vX
		pY :+ vY
	End Method

End Type


Graphics 600,600,0
Setblend ALPHABLEND


While Not (KeyHit(KEY_ESCAPE) or AppTerminate())
	For drawable:TDrawable=EachIn Drawables
		drawable.update
	Next
	
	For drawable:TDrawable=EachIn Drawables
		drawable.draw
	Next
	
	Flip
	Cls
Wend



I've only spent a few hours on it, so it's not structured brilliantly, but if I make it just a little bit cleverer, I think it could be a very interesting avenue of inquiry.


Ked(Posted 2009) [#2]
That's the coolest thing I have ever seen.


jkrankie(Posted 2009) [#3]
That's pretty good, you should see what else you can get it to do.

Cheers
Charlie


Zenith(Posted 2009) [#4]
This is really cool! :)


GaryV(Posted 2009) [#5]
We are doomed if Warpy ever decides to use his powers for evil...


Grey Alien(Posted 2009) [#6]
Awesome, this is how I imagine programming will be in the future except you'll speak to the computer and it will make some assumptions for you already so you don't need to specify every detail. Only problem with that is any pleb could be a programmer then and I'd be out of a job ;-p


BlitzSupport(Posted 2009) [#7]
Yeah, what GaryV said. Please swear here and now to only use your powers for good, ie. for lazy programmers like me.


Doiron(Posted 2009) [#8]
Warpy is definitely on to something: that's the most annoying part every time a start a new project from scratch, even though I try to recycle as much as possible by using heavily structured code.

That parser reminds me a lot of the latest Inform, but applied to abstract game logic. Very clever, very interesting, and also the implementation looks well thought of!


Nate the Great(Posted 2009) [#9]
wow thats a pretty cool parser/grammar recognition thing you have going.


slenkar(Posted 2009) [#10]
I think this would be great for beginers but personally it would just get in my way, as I have been using blitz for years and i am very comfortable with it.


Doiron(Posted 2009) [#11]
I find it only partially true.

Out of the box is mostly useful for beginners, which can then adopt the the outputted template, and since every coder grows it's own style and codebase during time it isn't fit for everyone.

However, regardless of expertise I still find repetitive tasks tedious, so the concept alone is interesting: it actually made me think of coding something like that for myself, as the icing on top of my codebase. :)


nawi(Posted 2009) [#12]
I think writing stuff in a natural language will just take more time and effort, and since you still need to know how to program I don't see much use for this. But sure, it's a cool program, and might be of use to create the holy grail of programming language: just tell the computer in a natural language "what to do".


Warpy(Posted 2009) [#13]
To be clear, this is just a frameworking device, which you use at the start to get the structure of your program set up. Obviously, game logic and proper programming would be really long-winded in natural language.


ImaginaryHuman(Posted 2009) [#14]
I think it's a pretty interesting approach Warpy. Definitely nice that you didn't have to type out all that code by hand - I mean, you had to spend time once writing the generator etc but you didn't have to sit and hand-write it, so like you said it's a good timesaver. I presume you would actually build this around a large library of functionality which would actually fill in the blanks for you - ie fill the functions with code to actually move objects and draw stuff etc?

In a way it's kind of like a higher-level WYSIWYG game editor like Torque or something, but in the form of english language rather than clicking buttons and dragging boxes and stuff. Interesting.


ShadowTurtle(Posted 2009) [#15]
hm... Basic 2.0?


GIB3D(Posted 2009) [#16]
I've wanted to make something, before I knew a lot about blitz I wanted to make a program that you can create 3D Models with. Not just any old program though, it would SOMEHOW (which is the problem) be connected to your mind so you could simply think of a model, and poof, it appears on a screen, along with a texture if you want it to.

Or for programming a game, you could have something conencted to your mind while you sleep and you could record your dream as either a video or a game. That'd be awesome.


Sokurah(Posted 2009) [#17]
@Green_Fire

Last night I dreamt I was being chased by a Terminator and for a "normal" guy like me without military training or weapons that was pretty (annoyingly) exciting (read: stressing) experience.

...would've made a great game though. :-)