Source

Fat x Fast / FatxFast / modes / multimode.py

Full commit
# This file is part of FatxFast.
#
#    FatxFast is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FatxFast is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.5"


from FatxFast.modes import scoredmode
from FatxFast.objects import player
from FatxFast import camera
import pygame
from FatxFast.gui.boxwidget import BoxWidget
from FatxFast.objects import buffs
from FatxFast.physics.physics import Physics
from FatxFast.tilemap.editor import tile

class MultiMode(scoredmode.ScoredMode):

    def __init__(self, scene, **kwargs):  
        self.second_camera = camera.Camera()  
        self.player2 = None
        super(MultiMode, self).__init__(scene, **kwargs) 
        self.camera.rect.width = (self.camera.rect.width/2)-(tile.TILEWIDTH/2)
        self.second_camera.rect.width = self.camera.rect.width

    def create_players(self):
        super(MultiMode, self).create_players()
        self._create_player("player2", self.second_camera)
        self._load_player_assets(self.player2)

    def end(self, player, text=""):
        self.scene.pointsystem.increase_points(player)
        player_scores = [(player.name, player.points) 
            for player in self.scene.players]
        winner = max(player_scores, key=lambda player: player[1])
        text = """Game finished. The winner is {0}.
        Points scored {1}.""".format(winner[0], winner[1])
        super(scoredmode.ScoredMode, self).end(player, text=text)
    
    def keys_held(self):
        if self.player2 is None:
            return
        keys = super(MultiMode, self).keys_held()
        player2 = self.player2
        if keys[self.hotkeys["player2-up"]]:
            player2.set_accel(y=-Physics.accelspeed)
        elif keys[self.hotkeys["player2-down"]]:
            player2.set_accel(y=Physics.accelspeed)
        else:
            player2.set_accel(y=0)
        if keys[self.hotkeys["player2-left"]]:
            player2.set_accel(x=-Physics.accelspeed)
        elif keys[self.hotkeys["player2-right"]]:
            player2.set_accel(x=Physics.accelspeed)
        else:
            player2.set_accel(x=0)  
#        if keys[self.hotkeys["player2-turbo"]]:
#            self.player2.turbo()
    
    def key_pressed(self, key, utf, mod):
        super(MultiMode, self).key_pressed(key, utf, mod)
#        if key == self.hotkeys["player2-map"]:
#            self.open_map(self.player2)

    def level_up(self, player):
        if self.scene.levelsystem.increase_level():
            super(MultiMode, self).level_up(player)
        else:
            self.end(player)

    def resume(self): 
        super(MultiMode, self).resume()
        self.scene.hud.add_player(self.player2, self.second_camera)

    def update(self, dt): 
        super(MultiMode, self).update(dt)
        self.second_camera.update()
        self.scene.time += dt
        if self.scene.time <= 0:
            self.end()