New User, Have Questions

BlitzMax Forums/BlitzMax Beginners Area/New User, Have Questions

lesslucid(Posted 2009) [#1]
Hi!

I've just acquired BlitzMax, am very excited about it, although a little nervous of how well I will be able to manage with it. Anyway, I thought I might ask a couple of questions of the helpful people here?

1. I've read through and followed along with a couple of tutorials, and have managed to understand what's going on until they start talking about types and classes and methods and whatnot. I'm comfortable with if statements, comfortable with functions, with select and while and for/next and so on... but when I read code that has types in it, I have a vague feeling that I understand in an abstract sort of way what each line is doing, but no confidence at all that I could produce something myself that would do the same thing. Is it important to keep hammering away at this concept until I get it, or can I get away with playing with the stuff I can already understand until I'm more confident?

2. As an exercise today I made a little "pong" clone, with bats, a ball, bouncing, everything you can imagine! While I have the same kind of huge, insanely over-ambitious projects in mind that every starting game-creator begins with, I suspect that there might be some intermediate stages between pong and My Dream Game. Can you recommend something I could or should try to make as a small project that will teach me more than I learned from making pong but is still small and simple enough that I won't get lost and dejected halfway through? Any recommendations gratefully appreciated!

...thanks in advance for any help or advice!


Dabhand(Posted 2009) [#2]
1) http://www.blitzbasic.com/Community/posts.php?topic=42519

2) http://www.blitzbasic.com/Community/posts.php?topic=42538

Dabz

Edited: New link


_Skully(Posted 2009) [#3]
Hi lesslucid,

Don't get nervous about types/classes and whatnot... once you get what is going on under the hood you should understand how they benefit you in the long run.

Think of types as "Something" in the game that you want to track... usually something that occurs more than once and has common code to Move it etc.

Types are a means to encapsulate everything to do with those objects into one point of reference.

What types save you from is this...

CreatureCount=0
Dim CreatureX(100)
Dim CreatureY(100)
Dim CreatureSpeedX(100)
Dim CreatureSpeedY(100)
Dim CreatureImage(100)
dim CreatureAlive(100)


In the code above you have to track how many creatures you have and when one dies you have to now manage "holes" in the array. You also have to predict the maximum number of creatures your going to have at any time in the game... Types relieve you from that headache

it now becomes:
Global Creatures:TList=new TList
Type Creature
   Field X:int
   Field Y:int
   Field SpeedX:Int
   Field SpeedY:Int
   Field Image:TImage

   Method MoveCreature()
        Self.x:+Self.SpeedX
        Self.y:+Self.SpeedY
   End Method

   Function NewCreature:Creature()
        local c:Creature=new Creature
        c.x=0
        c.y=0
        c.SpeedX=Rand(-1,1)
        c.SpeedY=Rand(-1,1)
        c.Image=CreateImage(32,32)
        Creatures.AddLast(c)   ' adds this creature to the creatures list so the garbage collector doesn't wipe it out.
        return c
   End Function
End Type


Now to use that code you

local c:Creature=Creature.NewCreature()
c.MoveCreature()


A "Light" example but hopefully will shed some light on types

Cheers


lesslucid(Posted 2009) [#4]
Thanks very much to both of you!

Skully, your example has helped me get a bit closer to understanding what's going on with Types... I think... but still not well enough to write functioning code. :( Could you take a look at this mangled attempt to make your code do something and show me what I'm doing wrong?

Thanks again for your help! I'd be absolutely stuck without it!




Brucey(Posted 2009) [#5]
To begin with, I seriously recommend you add either Strict or SuperStrict to the beginning of your app.

This will help you solve some basic syntax errors.

:-)


lesslucid(Posted 2009) [#6]
Ok, I've added Strict to the beginning, and also made some modifications based on what I could understand from Wave's tutorial just to make it easier for me to understand.

...and I'd left out cls and flip, no wonder I couldn't see anything!

OK, this code now works more or less the way I would expect it to:




Flemmonk(Posted 2009) [#7]
You could re-write your Pong game from scratch or see if you can make it smaller then it already is. Or you could write the next best thing, Pong but you destroy bricks with your ball.

Here is how you can use Functions inside of Types:
Strict

Graphics 640, 480, 0

Global CreatureList:TList = CreateList()

Type Creature
	Field X:Int
	Field Y:Int
	Field SpeedX:Int
	Field SpeedY:Int
	'Field Image:TImage

	Function Create:Creature()
      	Local this:Creature = New Creature
		this.X = Rand(100, 200)
		this.Y = Rand(100, 200)
		this.SpeedX = Rand(1, 3)
		this.SpeedY = Rand(1, 3)
		CreatureList.AddLast this
		Return this
	EndFunction
	
	Method MoveCreature()
		Self.x:+Self.SpeedX
		Self.y:+Self.SpeedY
	End Method

	Method DrawCreature()
		SetColor(200,200,200)
		DrawRect(Self.x, Self.y, 20, 20)
	End Method

End Type

While Not KeyHit(Key_Escape) 
	
	Cls 

	If KeyHit(Key_Z) 
		Local NewCreature:Creature = Creature.Create()
	EndIf

	For Local o:Creature = EachIn CreatureList:TList
		o.MoveCreature()
		o.DrawCreature()
	Next

	DrawText("Pressing Z should make randomly moving rectangles appear", 10, 10)

	Flip
	
Wend


Oh yeah and try to use TABs


Dabhand(Posted 2009) [#8]

Oh yeah and try to use TABs



Yeah, Regal Kingsize or Lambert and Butler will do, really useful aid when reading docs through the night! ;)

Dabz


lesslucid(Posted 2009) [#9]
G'day Flemmonk,

If I put that same function outside of the Creature Type, would it still work? Is putting the function inside the Type just good practice or does it affect the way the code works?

Also, I'm not sure I follow why the function adds "this" to the CreatureList *and* Returns this. Wouldn't adding "this" to the CreatureList on its own do the job?

Sorry for all the questions, but thanks for all the answers - they really help me!


_Skully(Posted 2009) [#10]
Having the function in the type is just a good practice when the function will only take action on that particular type (its just another level of keeping all the code together... you could move the function outside the type and it would make no difference.

Adding "this" to the Creaturelist will do the job, but in many/most cases you need to get a reference to the newly created object (creature in this case) to make adjustments to it... returning "this" just provides that reference to the calling function if needed.


lesslucid(Posted 2009) [#11]
Latest and greatest version of my test code for those generous enough to offer a critique:




lesslucid(Posted 2009) [#12]
Ah, thanks Skully - I understand.

Well, I guess the next thing for me to do is develop my new understanding into a simple Arkanoid clone. :)


Flemmonk(Posted 2009) [#13]
You are right, you can have it outside the type, however that would mean you would have to name it 'CreateCreature / CreatureCreate', otherwise you might want to use 'Create' for another function.

Additionally if someone wanted to look up the function 'Creature.Create' they would know it is in the 'Creature' type.


lesslucid(Posted 2009) [#14]
Ok, I have a new question!

Can I Return an array from a function, and use that function to create new arrays? The following code doesn't work and I'm not sure if it's because I'm doing something wrong syntactically or if the thing I'm trying to do doesn't exist:



...and if I can't do this, what's the best way for me to return multiple values from a function?

Thanks for any help!


Gabriel(Posted 2009) [#15]
Yes, you can do that. If you used SuperStrict instead of Strict, it might have helped you spot the problem here.

You've declared a function, but you haven't said what it is going to return. It is assumed that a function which doesn't declare a return type will return an integer. I think SuperStrict forces you to declare a return type or will throw an error when you try to return.

So the solution is simply to tell it that you're going to return an array of integers.

Here's the solution, SuperStrict-ified.

SuperStrict

'MakeArray creates an array with 1-3 in it
Function MakeArray:Int[]()
	Local TempArray:Int[] = [1, 2, 3]
	Return TempArray
End Function

Global ThisArray:Int[] = MakeArray()

Print ThisArray[0]

End



lesslucid(Posted 2009) [#16]
Fantastic, thankyou!


lesslucid(Posted 2009) [#17]
So, I'm back with more questions... hope it's alright!

Following Flemmonk's advice, I created a breakout-style game... and it works! It even has some features that are missing from the version in assari's tutorial, with the ball reflecting properly from bricks and with a variable angle of bounce from the paddle depending on where the ball strikes.

So... my question is, should I keep working on it? I've seen advice in various places saying "finish what you start, don't flit from one project to the next just because it gets hard, you learn more by sticking with something until it gets better". And I can see the sense in that advice, and I'm sure I could learn a lot from improving my breakout game, by adding things like multiple levels, different types of bricks, powerups, a proper high-score table, and so on... some of those things I only have a vague idea how to make, and some, none at all. So I would learn a lot.

However, on the downside, I really have no interest in breakout as a game, or in action games generally. The reason I want to learn to program is to make turn-based games. So the thought of spending a whole lot of time polishing and improving a game that I'm never going to actually like feels a bit de-motivating for me. Still... I understand at the moment the purpose of what I'm doing is not really to make stuff so much as to learn how to make stuff, so if it's the best way forward I probably can make myself keep at it.

And on a related note: whether it's now or later, I'm going to move on and make a simple turn-based game as a basis for starting to learn how to make turn-based, rather than real-time, games. Any suggestions for a good place to start? I was thinking of maybe implementing a computer version of Hive*, but... not sure if that would be aiming too high to begin with.

Any thoughts, comments or suggestions greatly appreciated!

* - See http://www.boardgamegeek.com/boardgame/2655


TaskMaster(Posted 2009) [#18]
I would move on if I were you. Start by getting the beginning framework done, then add features as you go. Just like you did with the breakout game.


H&K(Posted 2009) [#19]
So... my question is, should I keep working on it? I've seen advice in various places saying "finish what you start, don't flit from one project to the next just because it gets hard, you learn more by sticking with something until it gets better". And I can see the sense in that advice, and I'm sure I could learn a lot from improving my breakout game, by adding things like multiple levels, different types of bricks, powerups, a proper high-score table, and so on... some of those things I only have a vague idea how to make, and some, none at all. So I would learn a lot.
You are mistakeing "finishing" for "Making really good", if its playable, and doesnt need to be re-run to play again, then its, "1.0" finsihed.
I would go even further and say that if all the "objects/classes" are finished, then its finished. That is if every class is NOW plugable into other programs then etc.
If you do start to improve it ("1.1" it) then finish 1.1, but then it is finished again


lesslucid(Posted 2009) [#20]
Hi! I return with more questions!

1) I know that if I SetMaskColour and then LoadImage an image with that colour as its background, the background will be transparent - both in appearance and for collisions. Will the same thing happen in I load images that already have a transparent background? Or will they "look" transparent on the screen but collide according to the boxy shape of the whole thing?

2) If I'm getting along perfectly fine with version 1.33, is there any reason to upgrade to version 1.36? Or do the refinements in more recent version just affect sophisticated stuff that, as a beginner, I'm unlikely to run into?


lesslucid(Posted 2010) [#21]
sorry, asked question, then found answer for myself...