def train(model, eps=1000, speed=1):
    global exploitation_rate, losses

    for e in range(eps):
        frames = 0
        exploitation_rate = 1.0 - 0.2 * (1.25**-(e / 10))

        game.restart()
        prev_state, reward, score = game.tick(render=True, learn=True)
        prev_state = normalize(prev_state)
        state = prev_state

        while True:
            render = frames % speed == 0
            action = get_action(state)
            actions[action]()
            state, reward, score = game.tick(render=render, learn=True)
            state = normalize(state)

            memories.append([state, prev_state, action, reward])  #Remember
            reinforce()

            prev_state = state
            frames += 1

            if score != -1:
                if reward > 0:
                    print("Game solved after", e, "epochs")
                scores.append(score)
                break

        avg_loss = sum(losses) / len(losses)
        avg_score = sum(scores) / len(scores)
        print("Ep:{:4}, Score:{:2}, loss: {:.5f}, avg score: {:.2f}".format(
            e, scores[len(scores) - 1], avg_loss, avg_score))
Beispiel #2
0
def messages():
    data = json.loads(request.body.read())['data']

    room_id = data['roomId']
    room_type = data['roomType']
    person_id = data['personId']
    message_id = data['id']

    if person_id == MYID:
        return

    game.tick(room_id, room_type, person_id, message_id)
    return
Beispiel #3
0
def main():
    run = True

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            elif event.type == pygame.MOUSEBUTTONUP:
                game.mouse_released = True

        game.tick()
        game.draw(screen)
        pygame.display.update()
        clock.tick(game.fps)
Beispiel #4
0
def play_game(game, fps=30):
    """
    Asynchronously apply real-time game updates and broadcast state to all clients currently active
    in the game. Note that this loop must be initiated by a parallel thread for each active game

    game (Game object):     Stores relevant game state. Note that the game id is the same as to socketio
                            room id for all clients connected to this game
    fps (int):              Number of game ticks that should happen every second
    """
    status = Game.Status.ACTIVE
    while status != Game.Status.DONE and status != Game.Status.INACTIVE:
        with game.lock:
            status = game.tick()
        if status == Game.Status.RESET:
            with game.lock:
                data = game.get_data()
            socketio.emit('reset_game', { "state" : game.to_json(), "timeout" : game.reset_timeout, "data" : data}, room=game.id)
            socketio.sleep(game.reset_timeout/1000)
        else:
            socketio.emit('state_pong', { "state" : game.get_state() }, room=game.id)
        socketio.sleep(1/fps)
    
    with game.lock:
        data = game.get_data()
        socketio.emit('end_game', { "status" : status, "data" : data }, room=game.id)

        if status != Game.Status.INACTIVE:
            game.deactivate()
        cleanup_game(game)
Beispiel #5
0
def main(initial_state = None):
    game.initialize()
    if initial_state:
        game.set_state(initial_state)
    state = game.state()
    state.play_music()
    while True:
        for event in pygame.event.get():
            state.handle_event(event)
        state.update()
        if state.refresh_screen:
            state.draw_to_screen()
            game.refresh_screen()
            state.refresh_screen = False
        state = game.state()
        game.tick()
    return
Beispiel #6
0
def main_loop(initial_state):
    game.set_state(initial_state)
    state = game.state()
    while True:
        for event in pygame.event.get():
            state.handle_event(event)
        state.update()
        if state.refresh_screen:
            state.draw(game.screen())
            game.refresh_screen()
            state.refresh_screen = False
        elif state.dirty_list:
            state.draw(game.screen())
            game.refresh_subscreen(state.dirty_list)
        state.dirty_list = []
        state = game.state()
        game.tick()
    return
Beispiel #7
0
def enter_game(stdscr):
    """Initializes game.

    Args:
        stdscr: The standard screen passed into the curses wrapper.

    Side effects:
        Stores the screen object in shared_data['scr']
        Stores the lock object in shared_data['game_lock']
        Simulates game and logs actions to shared_data['log']
        Starts a thread to receive client key presses.
        Starts a thread to receive server updates.
    """
    shared_data['scr'] = stdscr
    stdscr.clear()

    game_lock = thread.allocate_lock()
    shared_data['game_lock'] = game_lock

    # Wait for first game state to be received
    receive_message()

    # Start a thread to receive client key presses
    thread.start_new_thread(key_listener, ())
    # And a thread to receive server updates
    thread.start_new_thread(server_listener, ())

    # Loop to simulate the game on the client
    while True:
        time.sleep(SECS_PER_TICK)
        with game_lock:
            game = shared_data['game']
            game.tick()
            client_receive.debug(game.num_ticks)
            # Append 'tick' actions to log.
            shared_data['log'].append((time.time(), 'tick'))

            game.draw_screen(stdscr, shared_data['username'])
            stdscr.refresh()
Beispiel #8
0
async def on_message(message):
    message_delete = message
    message_author = message.author
    message_author_id = message.author.id
    message_channel = message.channel
    message_content = message.content.lower()

    if message_content.startswith(bot_trigger + 'join'):
        game.join_player(message_author_id)
        await bot.send_message(
            message.channel, "<@%s> has joined the game." % message_author_id)

        print(message_author)

        print(game.players)
    if message_content.startswith(bot_trigger + 'leave'):
        game.leave_player(message_author_id)
        await bot.send_message(message.channel,
                               "<@%s> has left the game." % message_author_id)

        print(game.players)
    if message_content.startswith(bot_trigger + 'purge'):
        game.purge()
        await bot.send_message(message.channel, "All users purged!")

        print(game.players)

    if message_content.startswith(bot_trigger + 'startgame'):
        if game.is_live:
            await bot.send_message(message.channel, 'Game is still ongoing.')
        else:
            game.start_game()

            for player in game.players:
                await bot.send_message(discord.User(id=player.name),
                                       embed=embeded_message(
                                           player.role,
                                           game._game_data['locations'][
                                               game.location]['Location']))

            await bot.send_message(message.channel, "The game has started!")

            location_title = "Spyfall - Locations List"
            location_content = ''
            for i in game.loc_list:
                location_content += "**%s**\n" % i

            loc_embed = discord.Embed(title=location_title,
                                      description=location_content)

            locs = await bot.send_message(message.channel, embed=loc_embed)

            # Create a simple timer to time the game.
            time = await bot.send_message(message.channel,
                                          game.get_formatted_time())

            while game.is_live and game.tick():
                await bot.edit_message(time, game.get_formatted_time())
                await asyncio.sleep(1)

            # Loop exited, game has ended or has run out of time. End it and clear messages.
            await bot.delete_message(locs)
            await bot.delete_message(time)

            # If game is still live, it means the spy has not been revealed yet even though the time is up.
            # Players still have a last chance to vote who the spy is before ending the game.
            if game.is_live:
                await bot.send_message(
                    message.channel,
                    "Time's up! Vote who you think the spy is now.")

    if message_content.startswith(bot_trigger + 'players'):
        playing = 'Current Players:\n'
        for player in game.players:
            playing += "<@%s>" % player.name
            playing += ' '

        await bot.send_message(message.channel, playing)

    if message_content.startswith(bot_trigger + 'reveal'):
        reveal_title = 'Spyfall - Reveal'
        reveal_location = 'The Location Was --> %s\n' % (
            game._game_data['locations'][game.location]['Location'])

        reveal_players = ''
        for player in game.players:
            reveal_players += '<@%s> --> %s\n' % (player.name, player.role)

        reveal_content = reveal_location + reveal_players

        reveal_embed = discord.Embed(title=reveal_title,
                                     description=reveal_content)

        await bot.send_message(message.channel, embed=reveal_embed)
        game.end_game()

    if message_content.startswith(bot_trigger + 'settings'):
        command = message_content.split()
        setting = command[1]
        value = command[2]

        if setting == 'time':
            try:
                game.set_time(int(value))
                await bot.send_message(
                    message.channel,
                    'Game time set to {} seconds.'.format(game.round_time))
            except ValueError:
                # Show error message if an invalid integer was entered for time.
                await bot.send_message(
                    message.channel,
                    'Invalid value specified for "{}".'.format(setting))
        else:
            await bot.send_message(message.channel,
                                   'Invalid settings command.')
Beispiel #9
0
def right_player_down():
    game.right_paddle_down()


screen.listen()
screen.onkeypress(left_player_up, "w")
screen.onkeypress(left_player_down, "s")
screen.onkeypress(right_player_up, "Up")
screen.onkeypress(right_player_down, "Down")

sleep = 0.001

prev_points_left = None
prev_points_right = None
while True:
    game.tick()
    ball.goto(game.ball_pos())
    if prev_points_left != game.left_player_points or \
            prev_points_right != game.right_player_points:
        text.clear()
        text.write(
            f"Player One: {game.left_player_points} | Player Two: {game.right_player_points}",
            align="center",
            font=("Courier", 20, "bold"))
        prev_points_left = game.left_player_points
        prev_points_right = game.right_player_points
    paddle_left.goto(game.paddle_left_position)
    paddle_right.goto(game.paddle_right_position)
    screen.update()
    time.sleep(sleep)
Beispiel #10
0
import game

game = game.Game()
game.add_player(1)
game.add_player(2)
game.add_player(3)
game.add_player(4)
game.add_player(5)
game.add_player(6)
game.add_player(7)
game.start()
game.input(1, "u", "pressed")
for _ in range(50):
    print(game.tick())
game.input(1, "u", "r")
game.input(1, "d", "pressed")
game.input(1, "", KeyAction.PRESSED)
for i in range(25):
    print(game.tick())
y_pos = 0
while y_pos > -2490:
    x = game.tick()
    y_pos = x[0][1]['position'][1]
    print(y_pos)
    print(x)
Beispiel #11
0
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEMOTION:
            mouseX, mouseY = event.pos
        elif event.type == MOUSEBUTTONUP:
            paused = not paused
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()

    if not paused:
        mouseX, mouseY = pygame.mouse.get_pos()
        game.tick(VOLATILITY * (1-2*float(mouseY)/HEIGHT), TIMESTEP)
        #cDerivatives[ORDER] = VOLATILITY * (1 - 2*float(mouseY)/HEIGHT)
        #for i in range(ORDER,0,-1):
            #cDerivatives[i-1] = pDerivatives[i-1] + 0.5*TIMESTEP*(pDerivatives[i] + cDerivatives[i])
        #pDerivatives = cDerivatives
        #drawY.append(int(0.5*HEIGHT*(1-cDerivatives[0])))
        drawY.append(int(0.5*HEIGHT*(1-game.history[-1])))
        drawY.pop(0)

    window.fill(BLACK)

    if paused:
        text = font.render("Paused", True, WHITE)
        textpos = text.get_rect(centerx = WIDTH/2)
        textpos.top = 50
        window.blit(text, textpos)
Beispiel #12
0
    def serve(self):
        self.udp_socket.bind(('',self.port))
        self.tcp_socket.bind(('',self.port))
        self.tcp_socket.listen(self.tcp_backlog)

        while True:
            try:
                for game in self.games:
                    packets = game.tick()
                    self._send_packets(packets)

                for network_id in list(self.clients):
                    last_heard = self.clients[network_id]['last_heard']
                    last_sent = self.clients[network_id]['last_sent']

                    if last_heard.elapsed_seconds > self.timeout:
                        reason = constants.DISCONNECT_TIMEOUT
                        self._disconnect_client(network_id, reason)

                    elif last_sent.elapsed_seconds > constants.KEEPALIVE_TIME:
                        p = packet_pb2.Packet()
                        p.packet_id = get_id('packet')
                        p.payload_type = constants.KEEP_ALIVE
                        p.timestamp = int(time.time())

                        self._send_packets([(network_id, p)])
                        # Sending the packets resets the last_sent
                        # stopwatch

                rlist = [self.udp_socket, self.tcp_socket]
                rlist.extend(self.client_sockets)

                rlist, wlist, xlist = select.select(rlist,(),(),0.05)

                if self.display_stats:
                    display_stats(self.stats)


                for rs in rlist:
                    if rs == self.udp_socket:
                        data, addr = rs.recvfrom(4096)

                        key = ('UDP', addr)

                        if key not in self.network_id_bidict:
                            nid = get_id('network')
                            self.network_id_bidict[key] = nid
                            self.network_id_bidict[nid] = key

                        network_id = self.network_id_bidict[key]

                        if network_id not in self.clients:
                            self.clients[network_id] = {
                                'last_heard': utility.Stopwatch(start=True),
                                'last_sent': utility.Stopwatch(start=True),
                            }

                        self.clients[network_id]['last_heard'].restart()


                        packet = packet_pb2.Packet.FromString(data)
                        self.stats['packets_recieved'] += 1
                        self.stats['bytes_recieved'] += len(data)

                        self.handle(packet, network_id)

                    elif rs == self.tcp_socket:
                        conn, address = self.tcp_socket.accept()
                        key = ('TCP', conn)

                        self.network_id_bidict[key] = nid = get_id('network')
                        self.network_id_bidict[nid] = key

                        self.clients[nid] = {
                            'last_heard': utility.Stopwatch(start=True),
                            'last_sent': utility.Stopwatch(start=True),
                            'buffer': '',
                        }

                        self.client_sockets.append(conn)
                    else:
                        # Client socket.
                        key = ('TCP', rs)

                        network_id = self.network_id_bidict[key]
                        client = self.clients[network_id]

                        disconnect = False

                        try:
                            data = rs.recv(4096)
                        except socket.error as e:
                            disconnect = True
                            logger.error(e)

                        if not data:
                            disconnect = True

                        if not disconnect:
                            client['last_heard'].restart()

                            stream = client['buffer']
                            stream += data

                            chunks, remaining = utility.stream_unwrap(stream)
                            client['buffer'] = remaining

                            for chunk in chunks:
                                packet = packet_pb2.Packet.FromString(chunk)

                                self.handle(packet, network_id)

                                self.stats['packets_recieved'] += 1
                                self.stats['bytes_recieved'] += len(chunk)

                        else:
                            # Recieving the empty string means a disconnect
                            self._disconnect_client(network_id)


            except KeyboardInterrupt:
                if self.display_stats:
                    # Print an extra newline, because of the live statistics
                    print()
                # TODO Notify all connected clients of server shutdown
                break

            except Exception as e:
                if self.debug:
                    # If we're debugging, then the server can crash
                    # TODO Notify all connected clients of server crash
                    raise
                else:
                    traceback.print_exc()
Beispiel #13
0
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEMOTION:
            mouseX, mouseY = event.pos
        elif event.type == MOUSEBUTTONUP:
            paused = not paused
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()

    if not paused:
        mouseX, mouseY = pygame.mouse.get_pos()
        game.tick(VOLATILITY * (1 - 2 * float(mouseY) / HEIGHT), TIMESTEP)
        #cDerivatives[ORDER] = VOLATILITY * (1 - 2*float(mouseY)/HEIGHT)
        #for i in range(ORDER,0,-1):
        #cDerivatives[i-1] = pDerivatives[i-1] + 0.5*TIMESTEP*(pDerivatives[i] + cDerivatives[i])
        #pDerivatives = cDerivatives
        #drawY.append(int(0.5*HEIGHT*(1-cDerivatives[0])))
        drawY.append(int(0.5 * HEIGHT * (1 - game.history[-1])))
        drawY.pop(0)

    window.fill(BLACK)

    if paused:
        text = font.render("Paused", True, WHITE)
        textpos = text.get_rect(centerx=WIDTH / 2)
        textpos.top = 50
        window.blit(text, textpos)
Beispiel #14
0
 def tick():
    game.tick(level)
    drawLevel(canvas,level,w/2,h/2,TILE_SIZE,anchor = 'CENTER')
    master.after(10, tick)