This module implement two classes for Bomberman players, and one exception for their deaths.
The class PlayerServer is a high-level representation of a Bomberman player, for the server.
The class Player is the same thing, but without additionnal plumbing, for the client. All the plumbing about connection to the server is hidden, using the socket module.
- The following method are availables to use network :
- connect – to initialize the connection,
- send – to send a message,
- close – to close the connection.
- And those methods have to be used, I don’t want to see anything durty like :
>>> f = player0.socket_player.mfile() >>> f.write("I can send messages to my server !") >>> f.close() >>> # Don't do this !- The good way to do is :
>>> player0.send("I can send messages to my server !") >>> # Do this !
Warning
The player’s pseudos are checked to be small enough, so they have to be smaller than 32 caracters (otherwise they will be cut).
Bases: exceptions.Exception
Exception Class for handling the death of a Player.
(ex: player had 2 pv, and receive 3 damages : ingury=3).
>>> raise PlayerDeath(player0, ingury=3, msg='You died :(')
>>> # In this example, player0 had 2 pv, and receive 3 damages : so he died.
the player which is dying.
the ingury receive by the player.
the message annoucing the death, which have to be printed to the player.
A pretty string for the exception. (with colors if self.player supports color).
list of weak references to the object (if defined)
Bases: object
A Class to define a Bomberman player, for the server.
Attributes: (About the player)
- pseudo – his pseudo.
will be used for printing the player in the board in textual mode. It has to begin with a ASCII caracter, the other are free.
- color – his favorite color.
can be one of the following : black, red, green, yellow, blue, magenta, cyan, white.
(for now, it’s just PORT2 if there is a connection, or -1 if not). PORT2 is the port of the socket of the client, as it is saw FROM THE SERVER. But, no worry, everything is hidden !
(About the party)
- pv – number of point of life (PV).
- nb_bomb – number of bomb dropped in the field (by default, this is limited to 1).
- x – his position in x axis,
(integer between 0 and Nx-1, dimension of the board, typically Nx=8 or 12).
- y – his position in y axis,
(integer between 0 and Ny-1, dimension of the board, typically Ny=8 or 12).
- templatebomb – the default bomb’s caracteristic. This is a Bomb.BombNoOwner instance.
- direction – one of ‘right’, ‘left’, ‘front’, ‘back’. Represent the last direction’s move for the player.
Simple constructor of a player, not yet connected to anything.
>>> # On the server, for example hosted on the computer naereen-corp.crans.org,
>>> # while listening to the port 9312, detect a new client, which claimed to be Naereen,
>>> # and said to like green. So the server is creating a local version of the player, to the local version of the game.
>>> player0=PlayerServer("Naereen", "green")
his pseudo.
his favorite color.
number of point of life (PV).
number of bomb dropped in the field (by default, this is limited to 1).
his position in x axis,
his position in y axis,
the default bomb which will be dropped by the player.
an integer representing the player uniquely both in the client and the server.
A simple conversion from a Player to a string. The string returned can be used to print informations about the player.
Give the bomb dropped by a player, as a Bomb.Bomb instance. Increase the current number of bomb owns by the player. The returned Bomb.Bomb have a symlink to the Player in attribute owner.
>>> board[player1.x, player1.y].bomb = player1.drop()
>>> # Ok
>>> player1.move( i = player1.x + 1)
>>> # Move the player to the right by one (warning without checking !)
>>> board[player1.x, player1.y].bomb = player1.drop()
>>> # **NOT OK** the player cannot drop an other bomb (the max number is currently 1).
A simple way to move the player from his spot to (i,j).
Warning
The move have to be valid, IT IS NOT RE CHECK HERE. Therefore, i,j are checked to be valid positive integers here.
Example:
- Move the player to the right by one (without checking !) :
>>> player1.move(player1.x + 1, player1.y)
Example 2:
- You can also specify only the direction to change :
>>> player1.move(i = player1.x + 1)
Warning
Might raise a PlayerDeath exception if the player dies after suffering [ingury].
>>> i,j = player0.x, player0.y
>>> if board[i, j].bomb:
>>> player0.hurt(ingury = board[i, j].bomb.force)
>>> else:
>>> print 'No ingury ! Ouf...'
list of weak references to the object (if defined)
Bases: Player.PlayerServer
A Class to define a Bomberman player, for the client.
Attributes: (About the player)
- pseudo – his pseudo.
will be used for printing the player in the board in textual mode. It have to begin with a ASCII caracter, the other are free.
- color – his favorite color.
can be one of the following : black, red, green, yellow, blue, magenta, cyan, white.
(About his connection : here the player is connected to the server) (Currently, pickling is in test, and __setstate__ and __getstate__ are used to save and restore the connection.)
- info_server – the information about a server on which he is connected to (a tuple (HOTS, PORT)). (new from Player.PlayerServer)
- socket_player – the socket identifying his connection, (HOST, PORT2), with PORT2 != PORT. (new from Player.PlayerServer)
- info_connection – a simple string containing (HOST, PORT2).
- id – an integer representing the player uniquely both in the client and the server.
(About the party)
- pv – number of point of life (PV).
- nb_bomb – number of bomb dropped in the field (by default, this is limited to 1).
- x – his position in x axis,
(integer between 0 and Nx-1, dimension of the board, typically Nx=8 or 12).
- y – his position in y axis,
(integer between 0 and Ny-1, dimension of the board, typically Ny=8 or 12).
- templatebomb – the default bomb’s caracteristic. This is a Bomb.BombNoOwner instance.
Simple constructor of a player. He/She will be named [pseudo], and colorised [color], and connected on the server [info_server]. The connection is established with a socket, given by [socket_player] attribute. ** This step could failed : so creating a connected player could failed. For now, it can’t.
While there is no connection, it is (‘None’, id).
otherwise it is a unique integer (strictly increasing for each new Player.PlayerServer).
templatebomb is the default bomb which will be dropped by the player.
>>> # For example on the computer client.crans.org, user 'Naereen', who likes green,
>>> # want to play on Bomberman with the server hosted at naereen-corp.crans.org, on the port 9312.
>>> player1=Player(('naereen-corp.crans.org', 9312), color="green", pseudo="Naereen")
his pseudo.
his favorite color.
number of point of life (PV).
number of bomb dropped in the field (by default, this is limited to 1).
his position in x axis,
his position in y axis,
the default bomb which will be dropped by the player.
a simple string containing (HOST, PORT2).
the information about a server on which he is connected to (a tuple (HOTS, PORT)). (new from Player.PlayerServer)
The connection is done in connect.
A shortcut to reduce the [pv] attribute of a Player by a number [ingury] of ingury. Handle here the death of the player. Print an ANSIColored message to signal the death of the player in case.
Might raise a PlayerDeath exception if the player dies after suffering [ingury].
>>> i, j = player0.x, player0.y
>>> if board[i, j].bomb:
>>> player0.hurt(ingury = board[i, j].bomb.force)
>>> else:
>>> print "No ingury ! Ouf..."
Try to connect to the server. Exactly the same way it’s done when creating the player.
except it raise exception properly here.
>>> # From the client, trying to connect to a new server.
>>> player1.connect('bomberman-server.crans.org', 13882)
A try to send a message [msg] to the server.
>>> # send a new color to the server, and change it locally
>>> player1.color='red'
>>> # but don't do this, use ParseMessage[In|Out] modules for this
>>> player1.send('color=%s' % player1.color)
Used to save the Player.Player instance, for example with pickle.
Warning: .. warning:
The socket (attribute *socket_player*) is **not destroyed** by this operation,
and the Player.Player *self* is **still** connected to his server.
>>> output = open('player1.pkl', 'wb')
>>> pickle.dump(player1, output, -1)
>>> output.close()
Used to reload the Player.Player instance, for example with pickle.
Warning: .. warning:
The socket (attribute *socket_player*) is **re created** by this operation,
and the Player.Player *self* is **re connected** to his server.
>>> pkl_file = open('player1.pkl', 'rb')
>>> player1 = pickle.load(pkl_file)
>>> pkl_file.close()
>>> print player1 # !warning! : the connection is a new one.