def __init__(self, arguments, address, port, refreshrate, playername, screensize): """Create a client that connects to the mMOSS server, interacts with the human player and displays game state. Arguments: arguments - argparse arguments object address - IP address (in text form e.g. "127.0.0.1") port - Internet port number refreshrate - Screen refresh rate (seconds per cycle) playername - Name of the player screensize - Tuple that specifies requested screen area (W,H) """ super(Client,self).__init__(arguments, address, port, refreshrate, playername, screensize) self.controller = Controller([A,S,D,W,Q,SPACE,TAB]) self.rotateheld = False self.lastrotate = 0.0 self.shipimage = pygame.image.load("mmossfiles/"+ self.arguments.ship_image+".png") self.shiprect = self.shipimage.get_rect() self.myshipobject = None self.outoffuel = False self.background = white self.screen = None
class Client(MMOSSClient): """The Client class overrides MMOSSClient and implements the user look and feel of the mMOSS game. """ def __init__(self, arguments, address, port, refreshrate, playername, screensize): """Create a client that connects to the mMOSS server, interacts with the human player and displays game state. Arguments: arguments - argparse arguments object address - IP address (in text form e.g. "127.0.0.1") port - Internet port number refreshrate - Screen refresh rate (seconds per cycle) playername - Name of the player screensize - Tuple that specifies requested screen area (W,H) """ super(Client, self).__init__(arguments, address, port, refreshrate, playername, screensize) self.controller = Controller([A, S, D, W, Q, SPACE, TAB]) self.rotateheld = False self.lastrotate = 0.0 self.shipimage = pygame.image.load("mmossfiles/" + self.arguments.ship_image + ".png") self.shiprect = self.shipimage.get_rect() self.myshipobject = None self.outoffuel = False self.background = black # # Handlers for event notifications from the server # def notifyConnected(self, protocol): """Perform any processing required when a connection is created with the game server. Call the base class handler. Arguments: protocol - Reference to the server connection. """ MMOSSClient.notifyConnected(self, protocol) def joinResponse(self, myid, thetime, gamewidth, gameheight): """After requesting that the server admit the client, the server will send a response message, which is handled in this method. Arguments: myid - The numeric ID of MY ship. thetime - The current server time. gamewidth - The width of the game field (pixels). gameheight - The height of the game field (pixels). """ super(Client, self).joinResponse(myid, thetime, gamewidth, gameheight) size = width, height = self.gamedimensions self.myshipobject.gamedimensions = size self.screen = pygame.display.set_mode(self.screensize) self.screen.fill(self.background) pygame.display.flip() def notifyPlayerStatsComplete(self): """Do something to let the player know what the current server/player statistics are. """ for player in self.playerstats.getPlayers(): print player # # Check for user input # def handleControls(self): """Keep track of the keys that have been pressed and issue an appropriate control message to the server. """ super(Client, self).handleControls() if self.myshipobject: if self.myshipobject.flevel == 0.0: self.outoffuel = True thrust = self.checkThrustControl() ccwthrust = self.checkRotationalThrustControl() bullete = self.checkFireControl() controlling, shooting = self.myshipobject.processCommand( self.servertime, thrust, ccwthrust, BULLETV, bullete) if controlling or shooting: self.protocol.sendClientControlEvent(self.myshipobject) self.checkStatsControl() self.checkQuitControl() def checkThrustControl(self): """Handle forward (W) and reverse (S) thrust keys. Returns: Thrust strength (force). """ fwd = self.controller.key(W) rev = self.controller.key(S) if fwd and not fwd.down and self.outoffuel: self.outoffuel = False if fwd and fwd.down and not self.outoffuel: thrust = THRUST elif rev and rev.down: thrust = -THRUST / 5 else: thrust = 0.0 return thrust def checkRotationalThrustControl(self): """Handle left (A) and right (D) rotational thrust keys. Returns: Rotational thrust strength (torque). CCW is +. """ ccwthrust = 0.0 helddown = False left = self.controller.key(A) if left: if left.down: ccwthrust = ROTATETHRUST if left.helddown: helddown = True right = self.controller.key(D) if right: if right.down: ccwthrust = ccwthrust - ROTATETHRUST if right.helddown: helddown = True if helddown and self.servertime < self.lastrotate + REPEAT_RATE: ccwthrust = 0.0 else: self.lastrotate = self.servertime return ccwthrust def checkFireControl(self): """Handle the fire (SPACE) key. Returns: Energy of the bullet (0.0 if none fired). """ fire = self.controller.key(SPACE) if fire and fire.down and not fire.helddown: bullete = BULLETE else: bullete = 0.0 return bullete def checkStatsControl(self): """Handle the "show stats" (TAB) key. """ tab = self.controller.key(TAB) if tab and tab.down and not tab.helddown: self.protocol.sendClientGenericRequest(REQUEST_STATSUPDATE) def checkQuitControl(self): """Handle the quit (Q) key. """ if self.controller.key(Q): self.protocol.sendClientGenericEvent(EVENT_QUIT) self.stop() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # # Periodic processing # def sendJoinRequest(self): """Create a new ship object and send a join request to the server. """ super(Client, self).sendJoinRequest() self.myshipobject = Ship(client=self, wmax=40, fmax=30, smax=30, image=self.shipimage, objectname=self.playername, isourship=True) self.protocol.sendClientJoinRequest(self.myshipobject, self.joinResponse) def eraseScreen(self): """Periodic call to erase screen objects. """ super(Client, self).eraseScreen() def writeScreen(self): """Periodic call to write screen objects. """ super(Client, self).writeScreen() def clientPoll(self): """Periodic processing to monitor player control/keyboard activity and update ship/asteroid/bullet positions on the screen. """ super(Client, self).clientPoll()