menu

Wednesday, November 23, 2016

Developing a 2D Board game with Python / Pygame : Part 2

Hi all,

Please check Part 1 before continuing to this section
In the last section, we covered board generation, leaving the actual network connectivity creation to this section.

Part 2 : Connecting to the server

Step 1 : Configure the game server

The given server (included in the github link), is made in C# as a Visual Studio Solution. Thus to run it
1) Download and extract the source
2) Install Visual Studio
3) Open lk.ac.mrt.cse.pc11/Tanks_server.sln
4) Once done, run the solution to open up the UI and the CLI

NOTE: IF you need to access the server from another machine, you need to make it listen to an interface IP. For that open up App.config and change the IP Address to something you get from ipconfig command.

Now with some analysis of the code and structure of the game server, the following can be seen.



1) We need to initialize a socket connection to the game server on the port 6000
2) As soon as we initialize and send the command "JOIN#" to game server, server will reply back to port 7000/
3) Thus, before sending the Join command we need to start a server on our client side that listens to port 7000.
4) Once that is initialized, our server continue to listen to the commands from the game server, through which various updates are taken.


Step 2 : Initialize socket connection

To ease everyone's life, I came up with a ServerClient class that handles all the things mentioned above. So in order to connect, all you need is to import the class and call the methods accordingly.
First save the socketclient.py which contains the ServerClient .

Now we use this class to connect to the server,

Note: Here the IP I have added is an external one since my server is running on a different machine. If you have both your client and Game server in the same machine, use 127.0.0.1 instead.

Save the below code as the new tank.py and run it. (Make sure the game server is running first). If all goes okay, you will see a string in the form "I:P:0...." printing on your console output. And the game server CLI output will show some text that says you have connected.


Step 3 : Drawing the board

Now that we can connect to the Game server and properly talk with it, next is to decode the board explanation given by the game server into graphical interface itself.

The format of the initialization code is
I:P0:<list of brick cordinates>:<list of stone wall cordinates>:<list of water cordinates>

So we simply do some splits and get cordinates of each element and draw them on our board. Since we have only implemented Brick walls in the sample I will show how to draw in the following code.

Once you run the tanks.py (remember that you need to close the game server and re run it before running your tanks.py, EVERYTIME), you will see that the Brick walls are shown on your interface in the same places as shown in the game server UI.

Okaaaay, so now you have the basic connectivity working, and I believe that you can continue building from here onwards. First make sure you add the logic to show all 4 different terrain types. Then make new Sprites, Coin and Lifepack and add them the same way we have used before.

I will try to update some future posts on the development, like auto vanishing coins and animations, but I guess this would work for now.

Cyah all







3 comments:

  1. wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much
    self employment tax
    text preparation services
    tax accountant
    tax consultant



    ReplyDelete
  2. this is a very useful post... i get a step by step idea for building a 2D board game

    Best Interior Designers in Chennai

    ReplyDelete
  3. I Have Done PYTHON Training In Automation Minds In Course In Useful Really For Thank You Automation Minds
    http://www.automationminds.com/course/pythontraininginsholinganallur/

    ReplyDelete