Player Module

This module implement two classes for Bomberman players, and one exception for their deaths.

PlayerServer:

The class PlayerServer is a high-level representation of a Bomberman player, for the server.

Player:

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.

Example:

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

exception Player.PlayerDeath(player, ingury=1, msg='You died :(')[source]

Bases: exceptions.Exception

Exception Class for handling the death of a Player.

Attributes:
  • player – the player which is dying.
  • ingury – the ingury receive by the player.
(ex: player had 2 pv, and receive 3 damages : ingury=3).
  • msg – the message annoucing the death, which have to be printed to the player.
Example:
You can annouce the death of a player player0 with this :
>>> raise PlayerDeath(player0, ingury=3, msg='You died :(')
>>> # In this example, player0 had 2 pv, and receive 3 damages : so he died.
__init__(player, ingury=1, msg='You died :(')[source]

Construction of a PlayerDeath exception.

player = None

the player which is dying.

ingury = None

the ingury receive by the player.

msg = None

the message annoucing the death, which have to be printed to the player.

__str__(self) → str[source]

A pretty string for the exception. (with colors if self.player supports color).

__weakref__

list of weak references to the object (if defined)

class Player.PlayerServer(pseudo='YouCanSetYourPseudoWithCommand--pseudo', color='white', pv=3, nb_bomb=0, x=0, y=0, templatebomb=force=4; timer=4; power=1)[source]

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.

(About his connection : here the player is not yet connected to the server)
  • id – an integer representing the player uniquely both in the client and the server.
(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.
__init__(pseudo='YouCanSetYourPseudoWithCommand--pseudo', color='white', pv=3, nb_bomb=0, x=0, y=0, templatebomb=force=4; timer=4; power=1)[source]

Simple constructor of a player, not yet connected to anything.

About:
  • [id] is PORT2, the port of the socket assuring the connection between the player and his server, as soon as available; and is an increasing integer (from 0) otherwise (increase by one for each new Player).
  • templatebomb is the default bomb which will be dropped by the player.
Example:
>>> # 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")
pseudo = None

his pseudo.

color = None

his favorite color.

pv = None

number of point of life (PV).

nb_bomb = None

number of bomb dropped in the field (by default, this is limited to 1).

x = None

his position in x axis,

y = None

his position in y axis,

templatebomb = None

the default bomb which will be dropped by the player.

id = None

an integer representing the player uniquely both in the client and the server.

__str__()[source]

A simple conversion from a Player to a string. The string returned can be used to print informations about the player.

__repr__()[source]

Toplevel representation of [self].

drop()[source]

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.

Example:
>>> 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).
move(i=None, j=None)[source]

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)
hurt(ingury=1)[source]
A shortcut to reduce the [pv] attribute of a Player by a number [ingury] of ingury.
This method trigger here the eventual death of the player. Print an ANSIColored message to signal the death of the player in case.

Warning

Might raise a PlayerDeath exception if the player dies after suffering [ingury].

Example :
>>> i,j = player0.x, player0.y
>>> if board[i, j].bomb:
>>>    player0.hurt(ingury = board[i, j].bomb.force)
>>> else:
>>>    print 'No ingury ! Ouf...'
__weakref__

list of weak references to the object (if defined)

class Player.Player(info_server=('0.0.0.0', 12882), pseudo='YouCanSetYourPseudoWithCommand--pseudo', color='white', pv=3, nb_bomb=0, x=0, y=0, templatebomb=force=4; timer=4; power=1)[source]

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.
Examples:
On each intersting methods here defined, an example is shown,
from creating a player, stored in ‘player1’, to several examples of methods.
id_global = -1
A strictly increasing integer.
Each new instance of Player have this number in his attribute id.
__init__(info_server=('0.0.0.0', 12882), pseudo='YouCanSetYourPseudoWithCommand--pseudo', color='white', pv=3, nb_bomb=0, x=0, y=0, templatebomb=force=4; timer=4; power=1)[source]

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.

About:
  • [info_server] is a tuple (HOST, PORT), giving the information about the server.

    While there is no connection, it is (‘None’, id).

  • [id] is PORT2 if there is a connection,

    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.

Example:
>>> # 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")
pseudo = None

his pseudo.

color = None

his favorite color.

pv = None

number of point of life (PV).

nb_bomb = None

number of bomb dropped in the field (by default, this is limited to 1).

x = None

his position in x axis,

y = None

his position in y axis,

templatebomb = None

the default bomb which will be dropped by the player.

id = None
About the id :
an integer representing the player uniquely both in the client and the server. A good client shall receive it from the server (when phasis #2 begin).
info_connection = None

a simple string containing (HOST, PORT2).

info_server = None

the information about a server on which he is connected to (a tuple (HOTS, PORT)). (new from Player.PlayerServer)

socket_player = None

The connection is done in connect.

__repr__(self) <=> repr(self)[source]

Representation of [self] as a string.

hurt(ingury=1)[source]

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

Example:
>>> i, j = player0.x, player0.y
>>> if board[i, j].bomb:
>>>   player0.hurt(ingury = board[i, j].bomb.force)
>>> else:
>>>   print "No ingury ! Ouf..."
connect(info_server=('0.0.0.0', 12882))[source]

Try to connect to the server. Exactly the same way it’s done when creating the player.

except it raise exception properly here.
Example:
>>> # From the client, trying to connect to a new server.
>>> player1.connect('bomberman-server.crans.org', 13882)
close()[source]

Try to close nicely the connection with the server.

send(msg='', v=True)[source]

A try to send a message [msg] to the server.

About:
  • [msg] have not to terminate by a new line caracter (backslash + n), it is added automatically.
  • [msg] is not send if it’s non empty.
  • [v] if true, print debug informations.
Example:
>>> # 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)
__getstate__()[source]

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.
Example of Saving:
>>> output = open('player1.pkl', 'wb')
>>> pickle.dump(player1, output, -1)
>>> output.close()
__setstate__(dict)[source]

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.
Example of Loading:
>>> pkl_file = open('player1.pkl', 'rb')
>>> player1 = pickle.load(pkl_file)
>>> pkl_file.close()
>>> print player1 # !warning! : the connection is a new one.

Table Of Contents

Previous topic

Bomb Module

Next topic

Board Module

This Page