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.
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.
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.
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.