PDA

View Full Version : Programming style



Chobley
05-04-2008, 01:26 PM
I'm trying to be a bit more structured whilst I'm learning Pascal...I know there are style guides - but don't know if there are programming techniques. I get easy muddled up and have looked at some previous code I have done at it all looks rather messy. The plan I have come up with (don't think it is original) is something along the lines of this;

program MyBigGame;

procedure SetUps();
begin
{ Code here intializes variables etc }
end;

procedure GamerInput();
begin
{ Code here takes instructions from keyboard and mouse }
end;

procedure RefreshScreen();
begin
{ code here does all the foreground and background stuff }
end;

{ Main }
BEGIN
SetUps;
repeat
GamerInput;
Logic;
RefreshScreen;
until GameOver=True;
END.

Now on top of this have decided that in MyBigGame to label any associated 'sub-procedures' as follows;

procedure R_Score();
begin
{ Code to draw the score to the screen }
end;

procedure L_Physics();
begin
{ Code that does the collision detection etc. }
end;

So you see the prefix R_ is associated with the "main procedure" RefreshScreen; and L_ is associated with the "main procedure" Logic; ...and so on...

That way I can clearly see what fits where....

Now my next question is; is this good practice? How do other people do it? I'm an amateur, and don't work anywhere near the industry...but really want to do this one day as a professional career in some form. My "IDE of choice" at the moment is MIDlet Pascal...decided to focus around that familiarity whilst learning before I move up to a more complex IDE...

(oops there should be TABS as well in the "Main" so sorry if it all lefthand sided)

JernejL
05-04-2008, 02:34 PM
If you do game procedurally, people usually separate various logic in separate units..

chronozphere
05-04-2008, 07:54 PM
I would use an object-oriented approch. It might be a little more work, but in the end it will make your project very structured and easy-to-maintain.

Take a look at this page. This is great reference for beginning pascal coders.

http://www.delphibasics.co.uk/Article.asp?Name=OO

Good luck!

Chobley
06-04-2008, 10:19 AM
Thanks for your responses....bookmarked that link chronozphere I shall take the bold step into OOP soon....at the moment alot of my code just looks like one heap of mess and was trying to get it in a way when a few months down the line i could decipher it alot easier...I hope...

Pascal just seems a tidy looking language to learn...I've not download delphi or freepascal...yet...just working with MIDlet Pascal....I hope to post some of my efforts on here one day!

wodzu
06-04-2008, 10:37 AM
Hi.

I think everyone develops his owny coding style with the time.. a lot of time;)
So if you know some ground rules, it should be ok. You have a base to start.
The other good way to learn is to read a sources from others. Read sources of freepascal or Delphi, those guys know how to write good code.

And if after some time you will look at your old code and you think "what a mess, I could do it a lot better now" thats the sign that you have moved forward;)

good luck

Chobley
06-04-2008, 11:42 AM
Thanks for your support Wodzu...I'm kinda of learning in my own special way if that makes sense...with no preconcieved ideas on how to do things...doing things that way i think may make me a more resourceful, better programmer?

Theres an awful lot learn, I've got a huge amount of questions that I need to find answers for myself. but there is lots of resources on this website, and you are all so helpful... and of course the internet is there...

also i'm picking bits up from other languges as well...so piece by piece something is slowly coming together.

I've got another question...open to anyone; I want to actually move onto Java; ultimately I would like to write mobile apps and games...but, a daft question it may seem; why do a lot of people choose Pascal? <--- thats probably a tough question to ask...but under my own admission as a beginner whilst I'm trying to get to grips with programming, I really don't know what I am talking about...but what are the advantages of Pascal lets say over Java?

marcov
15-04-2008, 10:11 AM
I've got another question...open to anyone; I want to actually move onto Java; ultimately I would like to write mobile apps and games...but, a daft question it may seem; why do a lot of people choose Pascal? <--- thats probably a tough question to ask...but under my own admission as a beginner whilst I'm trying to get to grips with programming, I really don't know what I am talking about...but what are the advantages of Pascal lets say over Java?

_in general_ Java tries to abstract you too much from the system. This might be different for certain mobile devices with a device specific SDK, but in general it is a bit sluggish.

Pascal is a very decent language, with some serious speed attached to it, that doesn't shield you from the system. Also it allows to generate binaries (e.g. .exe's on windows) that don't require installation or other files, making it also great for utils and even mid sized apps.

Personally, one of the gripes with Java is that on one level they pretend it is the same everywhere, and on the other you have to use special editions (J2EE, j2ME), versions, SDKs, widget sets (swt,awt,swing). So first they hook you onto the one to rule all attitude, but when you actually try to do that, you either are stuck with a one size fits all app (that e.g. has a non native feel on windows), or you have to differentiate again, negating the use of using Java in the first place.

That combined with the more difficult interaction with other software (be it OS or other apps not specifically written to communicate with Java) is why I only do it when I can't avoid it. (and if Windows is a major factor, that is almost never, beter go with .NET then)

For mobile games it is different, because sometimes you can't use native code (due to native bins not being allowed), but this is not a technical reason, but marketing.

But even then, even Java is not always as free either, so be careful, some Java devices will only run java apps when properly signed, and you have to register and pay for that. (e.g. carrier locked phones). Be sure to check your targets first if you can actually run custom Java code on them.

Chobley
17-04-2008, 07:06 AM
Thank you for that insight...all I have done is stuck around mobile phones, written a few test pieces of code and now applying what I have learnt into making a game.

I like the idea that Pascal as you said can do applications...that is something that I'd like to step into;

I see there is going to be a competition on here soon; so that would be a good incentive to download the Pascal desktop stuff and get cracking on that...time to move on a bit;

I'm getting more towards "I know a little bit about somethings" and where if I don't know something now; at least I know where to look in manuals to find out...I'm taking things one step at a time with familiarity, because very quickly it can blow your mind if you are a beginner - and then you just give up!

I hope to post a mobile Pascal game on here soon, it isn't great but the basis of it is stuff that I have learnt, my first attempt at structuring the code better, however still procedural based...for now...it should auto size as well for any Java enabled phone.

wodzu
22-04-2008, 11:16 AM
I hope to post a mobile Pascal game on here soon, it isn't great but the basis of it is stuff that I have learnt, my first attempt at structuring the code better, however still procedural based...for now...it should auto size as well for any Java enabled phone.

I really would like to see it! I made one simple game in Java for mobile few years ago but making it workable on different machines was awfull.

Chobley
24-04-2008, 04:09 PM
Wodzu...yeah I can do that; I don't think you can attach files here, i mean the .jar file.

Or can you?

It is quite simple I've tested it on my p990i (yes...i know...should not have bought one) and an old antiquated 128 x128 MIDP 1.0 device.

I don't have a website to upload it to either...yet...

It's a colour game, with levels and records also the High score; graphics based on not image files but by using drawing commands...

Now I can post the code on here for all to see or did you just want to see the code in how I did it?

If I do it that way; then you need to download MIDlet Pascal for which is was written in....you can go to www.midletpascal.com to get this...

(If you know MIDlet Pascal then you may know that their website has been inactive...well now it isn't)

wodzu
28-04-2008, 02:46 PM
Chobbley I was thinking rather about some screenshots if this is ok :-)

Chobley
28-04-2008, 05:02 PM
[img]http://www.postimage.org/Pqge0ir.jpg (http://www.postimage.org/image.php?v=Pqge0ir)

As you can see very simple!

Basically a random coloured ball goes from the top of the screen down, and the gamer has to press OK on their keypad to change the colour of the bar at the bottom to match the ball, you loose a live if you get it wrong. But gain a life on the next level where speed increases. It also records your high score. It starts to get a bit hectic as levels move on, but it inst impossible to finish.

But the basis of this is; I've learnt to use timers, worked out collision detection (when there isnt any in MIDlet Pascal), auto-sizing, storeing records in the phones flash memory and sound (plays "default sound" on device for backwards compatibility not through a "player").

It should work on any colour java enabled phone.

Thanks for taking an interest!

Chobley
28-04-2008, 05:04 PM
Oh, and forgot - command buttons also...

wodzu
29-04-2008, 07:35 AM
Thanks for the screenshot mate.

I think you have choosen a very good path for making games. A lot of people have tendency to take an overgrown project and leave it uncompleted. It is a way better to take something smaller and finish it.

Keep a good work! :)

Chobley
29-04-2008, 04:46 PM
That sounds good advice, you can get a bit ambitious (been there)...I'm writing another game, this time using images as sprites, its a lot more complex and under my own admission :) , not too bad :)

- well actually it is written, done all the sprites myself, everything though but the sounds (have though of customizing the sounds but have a limited development budget at the moment :) )

It's, complete - It just needs tweaking, if not re-writing to make that easy - the code has been thrown together and looks rather shabby, but the end result I'm quite pleased with.

Should get myself a website and get stuff uploaded on that....