Multiplayer Game Programming for Teens with Python: Part 2

I’m sure once in a while, your friends and you go online to play a multiplayer game. In this tutorial, you will learn about multiplayer game programming by creating a simple game. By .

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Finishing Touches

Add a new function to the main client game class (BoxesGame) as shown:

def initSound(self):
    pygame.mixer.music.load("music.wav")
    self.winSound = pygame.mixer.Sound('win.wav')
    self.loseSound = pygame.mixer.Sound('lose.wav')
    self.placeSound = pygame.mixer.Sound('place.wav')
    pygame.mixer.music.play()

This loads the music and sound files so that you can play them when needed. These .wav files were part of the resources package you downloaded at the beginning of Part 1 of the tutorial. I made these sound effects with cxfr and the sound effects come from Kevin MacLeod.

Now in __init__, add the following after initGraphics:

self.initSound()

Then add this code in the specified places:

#anywhere in Network_place
self.placeSound.play()

#anywhere in Network_win
self.winSound.play()

#anywhere in Network_lose
self.loseSound.play()

These play the sound effects in the appropriate spots.

Run the game again and make sure your computer's volume is up. Enjoy the groovy tunes!

Now that you've enhanced the game with music and sounds, let's make it so that you can play across your network instead of just on the same computer.

Replace self.Connect() in BoxesGame with this:

address=raw_input("Address of Server: ")
try:
    if not address:
        host, port="localhost", 8000
    else:
        host,port=address.split(":")
    self.Connect((host, int(port)))
except:
    print "Error Connecting to Server"
    print "Usage:", "host:port"
    print "e.g.", "localhost:31425"
    exit()
print "Boxes client started"

This lets you specify where the client should find the server. You will be prompted when you run the game to type in the server's IP address.

Now let's add the server version of this. Replace boxesServe = BoxesServer() in server.py with this:

# try:
address=raw_input("Host:Port (localhost:8000): ")
if not address:
    host, port="localhost", 8000
else:
    host,port=address.split(":")
boxesServe = BoxesServer(localaddr=(host, int(port)))

Run your game one last time.

Where to Go from Here?

Here is the finished sample project from the tutorial series.

Congrats! You have just finished your first multiplayer game using Python and PyGame. I hope you had fun with this project.

If you did and you want to keep working on it, here are some things you could try on your own:

  • Add random “bad” squares that cause the player who won them to lose a point.
  • Make some squares randomly worth more than one point.
  • Give the first player to join the game the ability to chose the board's size.
  • Make it possible for players to spend their turns removing lines added by the other player.

If you have any questions or comments about this tutorial, chime in the forum discussion below. Happy Pythoning!