Exemple #1
0
    entity, position = entity_data['entity'], entity_data['position']
    entity_topic = '%s/%s.json' % (entity, entity)
    entity_data = alex.pubsub.get_current_message(entity_topic)
    width, height = entity_data['width'], entity_data['height']
    colour = list(entity_data['colour'])
    draw_rectangle(width, height, colour, position, window)

def draw_rectangle(width, height, colour, position, window):
    rectangle = Surface((width, height))
    pixels = surfarray.pixels3d(rectangle)
    pixels[:][:] = colour
    del pixels
    rectangle.unlock()
    window.blit(rectangle, position)

game_name = argv[1]
game_id = str(uuid4())
print "Starting client for game %s with id %s" % (game_name, game_id)

pubsub_url = getenv('ALEXANDRA_PUBSUB')
if request_game(game_name, game_id, pubsub_url) is False:
    print "Could not start game"
    exit(1)

alex = Alexandra(game_id)
window = init_window(alex.config['field_width'], alex.config['field_height'])
world_monitor = alex.pubsub.make_topic_monitor('world')
alex.pubsub.consume_topic('tick', lambda t: update(window,
                                                   world_monitor.latest(),
                                                   alex))
        return

    if NAME not in world['movements']:
        delta = randomchoice(DELTAS)
    else:
        movement = world['movements'][NAME]
        delta = (movement['to'][0] - movement['from'][0],
                 movement['to'][1] - movement['from'][1])
        if delta[0] == 0 and delta[1] == 0:
            delta = randomchoice(DELTAS)

    position = world['entities'][NAME]['position']
    new_position = (position[0] + delta[0], position[1] + delta[1])
    send_movement(position, new_position, world['tick'], alex)


def send_movement(from_position, to_position, tick, alex):
    movement = {
        'tick': tick,
        'entity': 'opponent_random',
        'index': INDEX,
        'from': from_position,
        'to': to_position
    }
    alex.pubsub.publish('movement.opponent_random', movement)


alex = Alexandra(argv[1])
init(alex)
alex.pubsub.consume_topic('world', lambda w: move(w, alex))