PDA

View Full Version : Online torpedo game



asdfarkangel
24-10-2010, 11:21 AM
Hi. Me and my friend are just about to start a Torpedo game. And we have some ideas like the field will be handled as a 10x10 matrix and we can do most of the coding it's not too tough. What we don't know however is how will we be able to make this work online. How should we make the packages work but not too slow. And first of all how do we make the program send the packages. I don't know if there exists a topic about something similar like this if there is I am absolutely okay with just a link to that topic altough I haven't found anything like that. Thanks in forward. :)

AthenaOfDelphi
24-10-2010, 05:25 PM
I would suggest you have a read through some of the threads here (http://www.pascalgamedevelopment.com/forumdisplay.php?48-Network).

They should help you get an idea of what's involved. After that, you'll need to get a handle on what information you need to exchange between clients and then come up with some suitable structures/communications protocol.

asdfarkangel
24-10-2010, 06:35 PM
oh then it won't be as easy as we thought:D well anyways thanks. If I can get SDL work in this life maybe the game will be fine:D

AthenaOfDelphi
24-10-2010, 08:28 PM
Depending on how much data you need to transfer (and importantly, what data - synchronisation etc.), it can be quite easy.

I don't know what compiler you're using, but lets say you're using Delphi, you can create a class descended from TPersistent. The data you want to transfer can be stored in that class as published properties... then you can make use of the streaming mechanisms in Delphi and say the Indy components and simply stream the class over the socket connection.

I've never written a game that uses this kind of socket connection so I can't comment further, but I have used this technique to squirt 12MB objects over a TCP/IP connection without too many hitches. But, it doesn't have any real error detection etc. So you may be better off defining a communications protocol (I've always used HDLC as a base for such protocols) which can handle failed transmissions and other errors such as corruption etc.

asdfarkangel
24-10-2010, 10:09 PM
I didn't get most of this since my skills in network programming... well let's just say you can't divide with it. Anyways this will be useful when I finally understand it so thanks. It's not your fault I assure you I'm just way too noob in this. I'm using free pascal. And I have never used ANY other includes and barely even use units except for crt. Altough I'm trying to get JEDI-SDL work I'm too noob to even do that. There's another topic of mine about that... So all this, a program with multiple components like not just a simple executable and maybe 1 or 2 binary and text files is totally new to me. I thought it would be so much more simple. They said Game Maker is good for start well game maker is nothing compared to this. It's like telling a 5 year-old to make the plans of a whole house after he've been playing with wood-cubes for a couple of hours. There I just pushed a button and there was my object I didn't need any constructor thing I don't even know what the hell is that but I see it everywhere in the examples. It's hard to learn from the examples when the theory of the coding is explained but I barely know the functions and procedures used because they're either Object Pascal or in units I've never used before. Altough if I could finally create an object and make it "work" somehow I would see, yes it is kind of similar to game maker but other than that, not at all. I don't even know how these work in pascal. I mean objects. Are their procedures executed as they exist? Or should I call them? Oh don't worry you don't have to answer these stupid questions I'll search for them but I'm just scared:D When I finally think I know a little about this langauge I find out how little exactly that is.

WILL
25-10-2010, 04:50 AM
If you want to program in Object Pascal, just program in Object Pascal, it's not so scarry once you get into it more. :) As neat as Game Maker is, it won't really teach you how to use the tools or learn the language. Game 'design' concepts sure, but you need programming experience to really break ground in learning it. It'll come, just don't get too discouraged if it's not coming right away. We all went through this and we want to help so you are good to go as long as you keep trying and make the effort. Trust me, it's worth it. ;)

Along with this fine pep talk, I'm going to give a caution. I'd stay away from networking or online anything for now. Keep your game local. Networking is an aspect of game programming that can even make commercial game developers for EA Game cry. Save this for something that you will learn a bit later. Instead try your hand at simple rule-based AI for computer players or adding joystick/gamepad support so that you and a buddy can play and test it on one machine. You can do some cool stuff with both those areas without splitting your head open on networking so early.

Games with simple yet an addictive quality will do you the most good. Your torpedo game seems like such a game. Keep the things you have to program to a minimum so as not to overload your project or you'll lose interests fast.

Also check out my tutoruials on making an Artillery game I use Lazarus and JEDI-SDL and I provide the source code you can download and compile yourself. It might help you figure out your setup for making games too.

asdfarkangel
25-10-2010, 06:51 AM
Thanks I've already looked at your artillery tutorial but since that was over my level too I didn't get it too much but I found the landsmoothing procedure very interesting and other theories whiches understanding didn't do much with a (for me) complicated code. Yes I was thinking about that staying with offline games would be a right choice. First we will design this game for a simple PC if that is done maybe we will try to make it work online. As for the simple AI I already have a little experience in that. It would be a very interesting project writing an AI for a Torpedo game. I will think about that:D I would need some kind of method not just random shots. Maybe we will even add difficulty settings to make the game less boring. Now I have to go to class. The Biophisics test awaits me.

chronozphere
26-10-2010, 06:36 AM
Thanks I've already looked at your artillery tutorial but since that was over my level too I didn't get it too much


The artillery tutorial is not really complicated IMHO. What is your programming experience so far? :)

As you said, it is best to start things as simple as possible. Even after a lot of years of programming, I still underestimate many projects. Just take your time to learn SDL. Have a look at some examples/open-source-games and you'll learn alot.

WILL
26-10-2010, 08:06 AM
Would you say that you understand Object Oriented Programming and the Object Pascal language as well as just Pascal? If you have a hard time with OOP then I can see how you might have a difficult time with my tutorial. It is written with the assumption that you understand basic object class structures. I don't go much further into it than that, but if you are having trouble with the OOP portion of the code, I can understand where I lost you in it.

I suppose I could start writing a couple of smaller beginner tutorials about the basics of objects and classes and their usage in a simple to learn way. At least that's how I learned it so at least from me I'd be able to explain it to you and others in a "plain" form of language. I'd offer the basics of SDL, but to be honest I think that our friends at 'Freepascal meets SDL' have that covered. ;)

Your torpedo game sounds like a good game to start with. I assume that the game mechanics will be much like Battleship, am I right? If so, this is good! Start with something like that. My first games where Pong, Arkanoid and Tron.(or Snakes or whatever other names it's given)

asdfarkangel
26-10-2010, 12:50 PM
Yes I have serious troubles with OOP both with syntaxes and methods. My torpedo game would be like this:
Each player has a 10x10 board (A..J and 1..10). On that you set up your ships by giving the coordinates. Then the players take turns and shoots at coordinates like A9. If there is a ship of the opponent player they can shoot again. If there isn't then it's the opponent's turn. When all parts of a ship has been hit it sinks. The player who first sinks all of the other player's ships wins the game. It is really not that hard. I'm working on that I finally got SDL working now. It would be possible to leave OOP out of this but I need to practise so I made an object for the AI. And anyways I think it's more practical to handle the AI variables and procedures as part of an AI object than totally global. However I get some stupid error I don't get... The parts of the code which could be related to the error look like these:


type
sq=record
x:integer;
y:integer;
end;
PAI=^TAI;
TAI=object
currenttarget,nexttarget:sq;
procedure ClearRegion(r:integer); {this is a basic AI procedure I'm not finished with it yet it is for making it not totally random}
procedure Shoot(c1,c2:integer);
end;

var
AI: PAI;
Procedure AI.ClearRegion(r:integer);
begin
{This is not finished yet}
end;
Procedure AI.Shoot(c1,c2:integer);
begin
{This is pretty long and yet not complete}
end;
begin
New(AI);
{program parts which are not too important}
Repeat
AI.Shoot(AI.currenttarget.x,AI.currenttarget.y);
Until RegionCleared(Region(AI.currenttarget.x,AI.current target.y));
{RegionCleared(r:integer):boolean and Region(c1,c2:integer):integer are functions defined correctly they work well I've made test programs for those.
(the AI.ClearRegion procedure is called inside the AI.Shoot procedure it's not yet working correctly I know that but the errors I get seem to have nothing to do with that...)

Now I've got two errors. First is like "Until expected AI.Shoot found". wtf??
Second is at the definition of procedure AI.Shoot(c1,c2) it says "class" type expected. I've tried setting up PAI as class type and AI as PAI type but then it said: unkown identifier: class. how could class be an unkown identifier for God's sake????:D}

end.Any ideas?:D I remember the times when I had errors like runtime error 103 or 201 with simple pascal and I didn't know what to do with them. Now they are like routines: "Oh error 103 must have called close(f); in a loop somewhere and forgot to Reset(f);". I know times will come when this will be the same with OOP but I'm not there yet.. And sorry for this superlong post if you didn't have the patience reading this through I understand if you did well thanks:D

asdfarkangel
26-10-2010, 07:46 PM
okay I finally solved this now the program actually runs without an error message. which is a good thing:D Now I just have to finish those functions and procedures and check if everything works fine(since running w/o error message doesn't mean it works well too:D) And correct a major bug with the SDL because now it freezes for some reason, it should not be a big problem. I think my beta version will be ready latest by the end of the week. I finally start to get the syntax and basic concepts of OOP too. And during I'm learning OOP I'm also learning the usage of pointers. Since my older programs were not big and the most complicated method was searching/modifying something in a binary file I didn't have memory issues and didn't need pointers at all, but now I want my game to run fluently.

Brainer
26-10-2010, 08:18 PM
No offence, but I'd suggest learning the syntax first before you start writing a game so you won't bite off more than you can chew. :) If I may suggest, I'd start by reading thoroughly the Delphi Basics (http://www.google.pl/url?sa=t&source=web&cd=1&ved=0CBgQFjAA&url=http%3A%2F%2Fwww.delphibasics.co.uk%2F&rct=j&q=delphibasics&ei=2TbHTOqgHcSKswbXi6XhDQ&usg=AFQjCNEVPLVJ03bnhzhSQs6XasbVb6erpA&sig2=xRTPjorhae7-S0SCfa1L4A&cad=rja) website.

asdfarkangel
26-10-2010, 09:49 PM
:D I have never learned like that. I can not just learn the syntaxes I am physically unable to do that. I learn the syntaxes by writing programs.
Now I got the most interesting error ever. So the main program looks like this:


BEGIN
Assign(ships,'C:\ships.dat');
GenerateShips;
ReadShips;
DrawBoard;
New(AI);
ReadShot;
Readln;
{parts not written yet}
SDL_FREESURFACE(screen);
SDL_FREESURFACE(boardpict);
SDL_FREESURFACE(hitpict);
SDL_FREESURFACE(misspict);
SDL_QUIT;
Readln;
END.

Now the error is a simple runtime error #201 it should be a range check error. But if I place a Breakpoint ANYWHERE in the program and by this I mean the subprocedures, a function, or the main program, even just before the last END, the program does not return this error. I'm wondering why is it doing that since in the main program there isn't any loop that should overload a variable or anything like that and the procedures should be okay as the program runs just until the last Readln; line if I put the breakpoint there. However if I do that, the program seems to skip some procedures(especially the ReadShot procedure) and just freezes. It might be a wrong code(with the ReadShot procedure) but since I couldn't solve inputing strings with SDL I had to read the coordinates with mouse and it creates a whole bunch of complications. Besides. I've been having an error with SDL ever since I got it: The SDL screen simply freezes irreversibly if I change to another window. This is a big problem as I can not use F7 to find the exact source of errors and I have to restart the whole IDE after every breakpoint check and inputing things becomes very complicated too.

asdfarkangel
28-10-2010, 08:53 PM
Okay I have got rid of all of the major bugs. The game is now theoritically playable. What is left to be done:
1) The ships are yet just random squares they don't form a ship this should really not be a problem I just write an organising procedure and there it is.
2) Graphics. Now I always use simple sprites for testing(red square, gray square etc...) but a game should really not have graphs like that:D
3) Sounds. Should not be a problem either.
4) A menu system with some kind of in-game menu with nice functions. Any good games have these why won't I put them in mine? I think it won't be hard altough it could be a bit tricky. Especially the in-game part.
5) This is the most interesting part: make it work online. I think I will save this for later... I'm just not skilled enough for that yet.

Now as you can see most of the remaining parts of the game takes more time than skill to get them done. And that's what I hate in designing games(I have like a dozens of game maker games all done with complicated calculations, AI and stuff like that but I was way too lazy to do the health-life counts, animations, spirtes, musics, backgrounds, level-designs and all the easy stuff...). Someone said I could lose interest if I made the game too hard to design for my skills. Well actually I usually lose interest at these parts of the game. When the hard parts are done and just time-work is what's left. Anyways I will finish this I've decided it. I just messed up too many hours and nerves on it not to do so:D However this is my very first game written in Pascal and still I got many help from you guys here(for which I am very grateful) I think I could be proud of myself. I really also solved things by myself I wouldn't have imagined I could a few weeks ago. Also this one simple game at least doubled my programming skills. It gave me some insight into OOP and SDL too. It's been a useful week.

WILL
28-10-2010, 09:22 PM
Sounds great, I expect a screenshot and a link/attachment copy of your finished first version for us to try. ;)

It makes a huge difference when you take smaller steps to learn the basics at the beginning. By posting your projects you can also let others see how it's coming along as well. Some errors or bugs can get pointed out and we can give tips on optimizing and polishing up any glitches or bugs we run into. Plus it helps to show activity in the area of programming here too.

If you need further help with graphics (I recommend starting with the BMP bitmap graphics format before you go off and try getting PNG and all kinds of other formats involved) sound or anything else, just ask, we like helping others. (Just try to post in the related forum categories ;)) As for the 'time consuming stuff', we all go through that too. :) I personally get some joy out of looking at the project running where you don't see much of anything in my game window/screen, but after plugging away at it for a couple of hours I give it a run and wow what a difference you see after. That's what has always kept me going making games for all these years.

Though lately I mostly write about game development and manage sites and edit magazines. :P

Nice job! Keep up the good work and it'll be completed in no time.

asdfarkangel
29-10-2010, 11:32 AM
I will load it up somewhere and give links As soon as it is finished. I will then accept opinions, suggestions and bug reports. I realised however I will also need a better AI. Since the squares where ships are located is totally random shooting at random places is yet okay but after the organisation, shipparts will be adjecent to each other so the AI will take that into concideration and count differently if it made a hit. For now the shooting procedure is like this:


Procedure TAI.RandomShot;

var c1,c2,i:integer;
neednewshot:longint;
begin
Repeat
neednewshot:=0;
c1:=random(10);
c2:=random(10)+1;
for i:=1 to n do {n is a global integer variable it stores how many squares have the ai shot at.}

begin
if alreadyshot[i]=c1*10+c2 then neednewshot:=neednewshot+1; {alreadyshot is an array of bytes of the object type TAI which stores at what squares tha ai has already shot at to make sure it won't shoot there again}

end; //This is in begin-end because I might add some additional factors later.
Until neednewshot=0;
alreadyshot[n]:=c1*10+c2;
n:=n+1;

if fleet[c1+1][c2]=3 then
begin
hits:=true;
misses:=false; //These two are variables of the object type TAI
fleet[c1+1][c2]:=1; {global 10x10 matrix variable it stores the shipplaces of the player}
playershipcount:=playershipcount-1;
ReDrawBoard_Playerships('hit',c1+1,c2);
if playershipcount=0 then defeated;
end
else
begin
hits:=false;
misses:=true;
fleet[c1+1][c2]:=2;
ReDrawBoard_Playerships('nothit',c1+1,c2);
end;
end;
{ReDrawBoard_Playerships(hit:string,x,y:byte) is a procedure to draw either a red(hit) or gray(miss) square at the shot coordinates.}
Now my plans are to make a procedure TAI.AccurateShot(x,y:byte); so inside the TAI.AITurn procedure there will be a check. Until a hit is made the TAI.RandomShot will be called. If the AI hits it will shoot at adjecent squares to that coordinate(x,y) until there aren't any adjecent squares left to be shot near any hit squares. Then the TAI.RandomShot will be called again etc. This isn't as easy as I thought I think however I will be able to do it. But for now I'm packing because I'm going home. Have a nice weekend everyone!:)

anflecher
06-12-2011, 11:56 PM
I do not know what you are using a compiler, but you can say you are using Delphi, you can create a class from TPersistent descendants. You want to transfer the data can be stored in this class of property released ... then you can use in the Delphi streaming mechanism, and said, Indy components and simply flow through the socket connection class.