示例#1
0
def test_load_map():
    file_path = "./map/level1.amap"
    assert isinstance(load_map(file_path), list)

    error_file_path = "./map/level1.amappp"
    with pytest.raises(FileNotFoundError):
        load_map(error_file_path)

    int_file_path = 123
    with pytest.raises(TypeError):
        load_map(int_file_path)
示例#2
0
文件: server.py 项目: Zokol/xadir
	def handle_general(self, cmd, args):
		if cmd == 'MSG':
			msg = deserialize(args, 'unicode')
			print 'MSG', self.client_id, self.nicks, repr(msg)
			self.bcast_cmd('MSG', serialize((self.client_id, msg), 'tuple', ['int', 'unicode']), not_to_self = False)
		elif cmd == 'CHALLENGE_CREATE':
			clients, map, players = deserialize(args, 'tuple', [['list', 'int'], 'str', ['list', 'Player']])
			if self.client_id in self.serv.challenges:
				self.die('Duplicate challenge')
			else:
				self.serv.challenges[self.client_id] = (map, set(clients), {self.client_id: players})
				self.mcast_cmd('CHALLENGE_CREATED', serialize((self.client_id, clients, map), 'tuple', ['int', ['list', 'int'], 'str']), self.serv.challenges[self.client_id][1])
		elif cmd == 'CHALLENGE_ACCEPT':
			client_id, players = deserialize(args, 'tuple', ['int', ['list', 'Player']])
			if self.challenge_valid(client_id):
				self.serv.challenges[client_id][2][self.client_id] = players
				self.mcast_cmd('CHALLENGE_ACCEPTED_BY', serialize((client_id, self.client_id), 'tuple', ['int', 'int']), self.serv.challenges[client_id][1])
				self.challenge_cancel_all(except_ = client_id)
				if self.serv.challenges[client_id][1] == set(self.serv.challenges[client_id][2]):
					players = [(cid, p) for cid, ps in self.serv.challenges[client_id][2].items() for p in ps]
					map, (width, height), spawns = load_map(self.serv.challenges[client_id][0])
					map = Grid(width, height, map)
					map.cell_size = (1, 1)
					map.x = map.y = 0
					game = Game(map, spawns, [])
					teams = [(p.name, None, p.team) for cid, p in players]
					spawns = game.get_spawnpoints(teams)
					class Dummy: pass
					main = Dummy()
					main.map = game.map
					main.res = None
					game.all_players = [GamePlayer(name, [(char, x, y, 0) for char, (x, y) in zip(characters, spawn)], main, None) for (name, remote, characters), spawn in zip(teams, spawns)]
					game.client_ids = self.serv.challenges[client_id][1]
					for client in self.serv.clients:
						if client.client_id not in self.serv.challenges[client_id][1]:
							continue
						client.game = game
					self.mcast_cmd('GAME_START', serialize((self.serv.challenges[client_id][0], spawns, players), 'tuple', ['str', ['list', 'list', ':coord'], ['list', 'tuple', ['int', 'Player']]]), self.serv.challenges[client_id][1])
		elif cmd == 'CHALLENGE_REJECT':
			client_id = deserialize(args, 'int')
			if self.challenge_valid(client_id):
				self.challenge_cancel(client_id, self.client_id)
		elif cmd == 'GAME_MOVE':
			p, c, t = deserialize(args, 'tuple', ['int', 'int', ':coord'])
			self.push_actions(self.game.move(self.game.all_players[p].all_characters[c], t))
		elif cmd == 'GAME_ATTACK':
			p, c, t = deserialize(args, 'tuple', ['int', 'int', ':coord'])
			self.push_actions(self.game.attack(self.game.all_players[p].all_characters[c], t))
		elif cmd == 'GAME_ENDTURN':
			self.push_actions(self.game.end_turn())
		else:
			self.die('Unknown command: ' + repr(cmd))
示例#3
0
'''Screen width.'''
screen_height = 768
'''Screen height.'''
screen = pygame.display.set_mode((screen_width, screen_height))
'''Main display where things are drawn.'''
clock = pygame.time.Clock()
'''A clock to control the framerate.'''
running = True
'''True if the app is still running.'''

# Global objects

pygame.mixer.init(44100, -16, 2, 2048)  # setup mixer to avoid sound lag
from menu import menu
import game
game.load_map('data/maps/map1/map.tmx', reset_players=True)


def run():
    '''Holds the main loop.'''

    while running:
        # Reading inputs from the user
        handle_events()

        # Processing next step
        if game.active: game.update()
        if menu.active: menu.update()

        # Drawing everything
        if game.visible: game.draw()
示例#4
0

def create_player(player_id, fname):
    return game.Player(player_id, open(fname).read())


if __name__ == '__main__':
    if len(sys.argv) < 3:
        print 'usage: python run.py <usercode1.py> <usercode2.py> [<map file>]'
        sys.exit()

    try:
        players = [create_player(i, x) for i, x in enumerate(sys.argv[1:3])]
        g = game.Game(*players)

        map_name = 'maps/default.py'
        if len(sys.argv) > 3:
            map_name = sys.argv[3]

        game.load_map(map_name)

        if '--render' in sys.argv:
            game.Render(g)
        else:
            for i in range(game.settings.max_turns):
                g.run_turn()
            print g.get_scores()

    except codejail.SecurityError as e:
        print 'security breach by player %d: %s' % (e.player_id, e.message)
示例#5
0
import sys
import codejail

def create_player(player_id, fname):
    return game.Player(player_id, open(fname).read())

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print 'usage: python run.py <usercode1.py> <usercode2.py> [<map file>]'
        sys.exit()

    try:
        players = [create_player(i, x) for i, x in enumerate(sys.argv[1:3])]
        g = game.Game(*players)

        map_name = 'maps/default.py'
        if len(sys.argv) > 3 and sys.argv[3] != '--render':
            map_name = sys.argv[3]

        game.load_map(map_name)

        if '--render' in sys.argv:
            game.Render(g)
        else:
            for i in range(game.settings.max_turns):
                g.run_turn()
            print g.get_scores()

    except codejail.SecurityError as e:
        print 'security breach by player %d: %s' % (e.player_id, e.message)
示例#6
0
 def touched_by(self, character):
     dis = self.distance_to(character)
     if character.faction == PLAYER and dis < 50:
         game.load_map('data/maps/' + self.to_map)