Introduction to Computer Science
Union College
Spring 2009

Multiplayer Online Pop-the-balloon

In this exercise, you are going to develop a client for playing the pop-the-balloon game (that you have seen earlier in this class) online against other people.

Pop-the-balloon protocol

Client Messages: So these are the messages that you can send to the server.

connect <player-name>
Tells the server to add a new player at the address the connect message is coming from and to attach the given name to this player.
disconnect
Tells the server to delete the player at the address that this message is coming from.
click <mouse_x> <mouse_y>
Tells the server that there was a mouse click that the specified coordinates.

Server Messages: So these are the messages that you are getting from the server.

position <red balloon x> <red balloon y> <green balloon x> <green balloon y>
Tells the client where to place the balloon pictures.
scores <player-name> <score> <player-name> <score>
Tells the client the names and scores of all currently logged-on players.
hit <score>
Tells the client that they had a hit and what their own current score is.

Start from this code. You also need the following files: red_balloon.gif, green_balloon.gif, and pop.wav.

Connecting to the server

The socket and the server address won't change throughout the game. So we can set them up as global constants.

Define the global variable to refer to the address of the server.

Create a UDP socket.

Send a connect message to the server.

When quitting pygame, send a disconnect message to the server and then close the socket.

Try it out, the server should now register that you are connecting and disconnecting.

Making the balloons move

Your balloons are not moving at the moment because you are not receiving and interpreting the position messages that the server is sending you.

So you need to add code to the game loop that reads the messages coming in from the server and reacts to them. The beginnings of that code are already there for you. It's commented out; you need to undo the commenting. The variable in_msgs tells you whether or not there is an incoming message waiting to be read.

If there is an incoming message (i.e., if len(in_msgs) > 0), then receive the message from the sender.

The message will come in as one long string. Split it apart so that you can tell what kind of message has come in (as indicated by the first keyword in the message text).

If the first keyword in "position", it will be followed by four numbers. The first number should become the x-coordinate of the red balloon, the second number should become the y-coordinate of the red balloon and the third and fourth number are the coordinates of the green balloon.

Once you have extracted the coordinates from the message and assigned them to the right variables representing the positions of the balloons, run your client again. The balloons should now be moving around.

Reacting to mouse clicks

There are two parts to this.

First, if a pygame.MOUSEBUTTONDOWN event happens, you need to get the mouse coordinates (pygame.mouse.get_pos() returns the x and y coordinates of the mouse as a tuple), and send a click message to the server. (You can run your client after this part to try it out.)

Second, if you get a hit message from the server, you need to play the popping sound (it is already loaded; remember that sound objects have a play() method which will play them back once), extract the score from the hit message and assign that new score value to your score variable.

Displaying all players

There are two parts to this: receiving the list of players and scores and then drawing the score list.

Verify that there is already a variable set up for the score list. It is initially set to the empty list. When you receive a scores message from the server, convert it into a list that contains the player names and scores and assign that value to the variable scorelist. You have to make some decisions here on what exactly you want the scorelist to look like. Is it just a list of strings alternating player names and scores? Or is it a list of tuples, where each tuple represents a player name and the associated score? Or is it a list of lists? All of these are possible solutions - you need to make the decision and then cosntruct the list from the message accordingly.

Then add code that draws the scorelist. This is similar to the code that you did for the last homework assignment to draw the list of high scores.

by Kristina Striegnitz