Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

A Network Based Multi-Player Eater Game

Rate me:
Please Sign up or sign in to vote.
4.03/5 (30 votes)
10 Jan 20054 min read 71.2K   1.5K   39   12
An article on network game programming.

Sample Image - EaterGameII.jpg

Introduction

For many people, one of the main reasons for connecting more than one computer together at home or at work place is for playing games. Whether playing with or against your friends and family, there are many games that come network-ready which offer more variety to gaming than single player stand-alone computer games. To add even more competitive zest to these games, there are also several games that network across platforms, allowing Mac and PC users to try to get the virtual best of one another.

Network games (local and Net-based) are popular applications of home computer networks. Network gaming (on LANs) and Internet gaming (across the Internet) include board, card, role playing and other multi-player or human vs. computer game environments. In this project we develop a multi-player network game from a single player single machine game called the Eater game. The development framework used is Microsoft .NET with C# as the programming language. The communication model used is a client/server model.

Background

Computer games are a relatively new innovation in the overall scheme of things. They have been around in different forms since the beginning of computers and in a lot of ways were essential in the route that computers have taken in becoming a part of our every day lives.

Broadly, games can be classified into five major regions: board games, card games, athletic games, children’s games, and computer games. Computer games are played on five types of computers: expensive dedicated machines for the arcades, inexpensive dedicated machines (“handhelds”), multi program home games, machines such as the ATARI 2600 and the ATARI 5200, personal computers and large mainframe computers. The computer acts as opponent and referee in most of these games; in many of them it also provides animated graphics. The most common form of computer game is the skill and action (“S&A”) game emphasizing hand eye coordination. These S&A games are frequently violent in nature. There are many other areas of computer gaming: adventure games, fantasy role playing games, and war games. In our cursory overview, these other computer games are eclipsed by the sheer volume of the skill and action games. A computer game is primarily formed of five basic elements. Those are graphics, sound, interface, game play and story.

The Eater Game

The Eater Game is a simple game written in C#. In this game, the user moves a packman like player around the form and gobbles up red dots. The objective is to get all the dots in as quick time as you can. At the end of the game, the player who has eaten the highest number of stones wins.

Implementation involved the modification of the original eater game into a two-player network game. The UML diagram of the modified eater game is as shown in figure below. The three new classes added were "GAMESETUP", "HOST", and "REPLY". The "FORM" class was modified to act as both client and server depending on the player's choice. Multi-Threading was used to make the method for scanning keyboard work independently without disturbing the other processes of the game. The "HOST" and the "REPLY" classes were designed based on the TCP-IP model of communication i.e., as a connection oriented model. The "GAMESETUP" class is fired when the game is invoked. If the player chooses to start the game as a host, then the "FORM" is started in server mode and it keeps waiting for other player to start. The second player starts the game and chooses to join as client from the "GAMESETUP" form. This fires the "FORM" class in client mode. Once done, both the players start playing the game across the network. The player who eats the most number of stones wins.

Sample Image - Eater2_UML.gif

The most important part of this project was the modeling of classes for communication and integrating their objects into the game. The detailed code for the method that is called when a key is pressed is as given below. In this method, the value of the particular key pressed is passed to the remote player using "host.sendObject(1,result)". This is used to update the eater position on the remote player's screen.

C#
private void Form_Server_KeyDown(object sender,
                                  System.Windows.Forms.KeyEventArgs e)
        {
            
            string result = e.KeyData.ToString();
            Invalidate(TheEater.GetFrame());
            switch (result)
            {
                case "Left":
                    host.sendObject(1,result);
                    TheEater.MoveLeft(ClientRectangle);
                    Invalidate(TheEater.GetFrame());
                    break;
                case "Right":
                    host.sendObject(1,result);
                    TheEater.MoveRight(ClientRectangle);
                    Invalidate(TheEater.GetFrame());
                    break;
                case "Up":
                    host.sendObject(1,result);
                    TheEater.MoveUp(ClientRectangle);
                    Invalidate(TheEater.GetFrame());
                    break;
                case "Down":
                    host.sendObject(1,result);
                    TheEater.MoveDown(ClientRectangle);
                    Invalidate(TheEater.GetFrame());
                    break;
                default:
                    break;

            }

            int hit = CheckIntersection();
            if (hit != -1)
            {
                TheScore1.Increment();
                Invalidate(TheScore1.GetFrame());
                Invalidate(((Stone)Stones[hit]).GetFrame()); 
                Stones.RemoveAt(hit);
                if (Stones.Count == 0)
                {
                    MessageBox.Show("PLAYER 1 WINS!");
                    Application.Exit();
                }
            }
        }

The "listenToCLient" method which keeps listening for the moves made by the remote player is as given below. This method obtains the result of the move made by the player at the remote end and passes it to a method for updating the screen of the local player.

C#
public void listenToClient()
        {
            MessageBox.Show("ready to accept connections from the clients");
            while (true)
            {
                if (host == null)
                {
                    return;
                }
                if (host != null)
                {
                    
                    string move = (string) host.c.getObject(1);
                    if (move != null)
                    {
                        doClientMove(move);
                    }
                }
            }

This game is fully functional and fully playable. However, many improvements can be made. The game now supports only two players and can be modified to include more players. The number of stones can be user defined and their positions can be random. That's enough of work for any future developer.

Acknowledgments

This project is a result of my course ICS 571 (Client-Server Programming) and a tribute to my course professor Dr. Nasir Darwish.

This game has been adopted from Mike Gold's simple eater game written in C#,in July 2001.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
Saudi Arabia Saudi Arabia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionPlease sir Pin
Onwuegbuchi panthelion male30-Aug-20 10:21
Onwuegbuchi panthelion male30-Aug-20 10:21 
GeneralJust one thing Pin
sabrown10020-Oct-07 11:03
sabrown10020-Oct-07 11:03 
Generalan advise Pin
code_machine8-Jun-05 17:07
code_machine8-Jun-05 17:07 
GeneralMissing images Pin
alekop6-Jun-05 6:46
alekop6-Jun-05 6:46 
GeneralRe: Missing images Pin
Domanost6-Oct-05 8:26
Domanost6-Oct-05 8:26 
The code for your game looks great. It would be better if the pics for your pacman dude was there.
GeneralRe: Missing images Pin
Member 33297003-Aug-10 5:27
Member 33297003-Aug-10 5:27 
GeneralRe: Missing images Pin
Member 33297003-Aug-10 6:09
Member 33297003-Aug-10 6:09 
GeneralCommunications Pin
Darryl Borden11-Jan-05 9:27
Darryl Borden11-Jan-05 9:27 
GeneralRe: Communications Pin
C#_Prof11-Jan-05 20:19
C#_Prof11-Jan-05 20:19 
GeneralQuality Pin
TRox10-Jan-05 10:14
TRox10-Jan-05 10:14 
GeneralRe: Quality Pin
TRox10-Jan-05 10:17
TRox10-Jan-05 10:17 
GeneralRe: Quality Pin
klasmannen23-Jan-05 1:01
klasmannen23-Jan-05 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.