#!/usr/bin/env python
#-*- coding: utf8 -*-

from stuff_on_map import *
from pygame.locals import *
from locals import *
from utils import *
from player import Player
from render import Render
from event_manage import EventManager
import os
import sys
import world
import pygame
import pygame.joystick
import controllers
import time


PLAYERS = 2


def main():
    os.chdir(os.path.dirname(os.path.realpath(__file__)))
    pygame.mixer.init(buffer=512)

    play_map = world.Map()
    play_map.load(map_path("map2.map"))

    players = []

    for i in xrange(PLAYERS):
        player = Player()
        player.name = 'Player %d' % i
        players.append(player)

    pygame.joystick.init()
    for i in xrange(pygame.joystick.get_count()):
        if i >= len(players):
            break
        j = pygame.joystick.Joystick(i)
        j.init()
        players[i].controller = controllers.Gamepad(j)

    for player in players:
        if player.controller is not None:
            continue
        player.controller = controllers.Keyboard()

    for i, position in enumerate(play_map.player_starts):
        if i >= PLAYERS:
            break
        tank = Tank(position, play_map)
        players[i].tank = tank

    for player in players:
        if player.tank is None:
            raise Exception("Not enough start points for players!")

    game_world = world.World(play_map, players)
    render = Render(game_world)
    eventer = EventManager()

    clock = pygame.time.Clock()

    while 42:
        deltat = clock.tick(FRAMES)
        events = eventer.get_events()
        if eventer.game_stopped():
            break
        if eventer.toggled_full_screen():
            render.toggle_full_screen()
        game_state = game_world.tick(deltat, events)
        if game_state == GAME_OVER:
            break
        render.draw()

    pygame.mixer.stop()
    render.draw_end_screen()
    time.sleep(3)
    render.quit()
    sys.exit(0)


if __name__ == '__main__':
    main()

@@ -350,24 +350,103 @@
 def main():%0A
+    start_game(PLAYERS, %22map2.map%22)%0A%0A%0Adef start_game(players_count, map_name):%0A
     os.chdir
@@ -580,26 +580,24 @@
 ap_path(
-%22
 map
-2.map%22
+_name
 ))%0A%0A    
@@ -630,23 +630,29 @@
  xrange(
-PLAYERS
+players_count
 ):%0A     
@@ -1196,23 +1196,29 @@
 if i %3E= 
-PLAYERS
+players_count
 :%0A      

Move the game initialization logic to its own function